mirror of
https://github.com/kennethreitz/pipenv.git
synced 2026-06-05 14:50:16 +00:00
Merge remote-tracking branch 'up/master' into patch-1
This commit is contained in:
Generated
+34
-34
@@ -77,11 +77,11 @@
|
||||
},
|
||||
"beautifulsoup4": {
|
||||
"hashes": [
|
||||
"sha256:05fd825eb01c290877657a56df4c6e4c311b3965bda790c613a3d6fb01a5462a",
|
||||
"sha256:9fbb4d6e48ecd30bcacc5b63b94088192dcda178513b2ae3c394229f8911b887",
|
||||
"sha256:e1505eeed31b0f4ce2dbb3bc8eb256c04cc2b3b72af7d551a4ab6efd5cbe5dae"
|
||||
"sha256:594ca51a10d2b3443cbac41214e12dbb2a1cd57e1a7344659849e2e20ba6a8d8",
|
||||
"sha256:a4bbe77fd30670455c5296242967a123ec28c37e9702a8a81bd2f20a4baf0368",
|
||||
"sha256:d4e96ac9b0c3a6d3f0caae2e4124e6055c5dcafde8e2f831ff194c104f0775a0"
|
||||
],
|
||||
"version": "==4.8.2"
|
||||
"version": "==4.9.0"
|
||||
},
|
||||
"black": {
|
||||
"hashes": [
|
||||
@@ -107,10 +107,10 @@
|
||||
},
|
||||
"certifi": {
|
||||
"hashes": [
|
||||
"sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3",
|
||||
"sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"
|
||||
"sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304",
|
||||
"sha256:51fcb31174be6e6664c5f69e3e1691a2d72a1a12e90f872cbdb1567eb47b6519"
|
||||
],
|
||||
"version": "==2019.11.28"
|
||||
"version": "==2020.4.5.1"
|
||||
},
|
||||
"cffi": {
|
||||
"hashes": [
|
||||
@@ -289,11 +289,11 @@
|
||||
},
|
||||
"flask": {
|
||||
"hashes": [
|
||||
"sha256:13f9f196f330c7c2c5d7a5cf91af894110ca0215ac051b5844701f2bfd934d52",
|
||||
"sha256:45eb5a6fd193d6cf7e0cf5d8a5b31f83d5faae0293695626f539a823e93b13f6"
|
||||
"sha256:4efa1ae2d7c9865af48986de8aeb8504bf32c7f3d6fdc9353d34b21f4b127060",
|
||||
"sha256:8a4fdd8936eba2512e9c85df320a37e694c93945b33ef33c89946a340a238557"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==1.1.1"
|
||||
"version": "==1.1.2"
|
||||
},
|
||||
"funcsigs": {
|
||||
"hashes": [
|
||||
@@ -391,19 +391,19 @@
|
||||
},
|
||||
"jedi": {
|
||||
"hashes": [
|
||||
"sha256:b4f4052551025c6b0b0b193b29a6ff7bdb74c52450631206c262aef9f7159ad2",
|
||||
"sha256:d5c871cb9360b414f981e7072c52c33258d598305280fef91c6cae34739d65d5"
|
||||
"sha256:cd60c93b71944d628ccac47df9a60fec53150de53d42dc10a7fc4b5ba6aae798",
|
||||
"sha256:df40c97641cb943661d2db4c33c2e1ff75d491189423249e989bcea4464f3030"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.16.0"
|
||||
"version": "==0.17.0"
|
||||
},
|
||||
"jinja2": {
|
||||
"hashes": [
|
||||
"sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250",
|
||||
"sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49"
|
||||
"sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0",
|
||||
"sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==2.11.1"
|
||||
"version": "==2.11.2"
|
||||
},
|
||||
"markupsafe": {
|
||||
"hashes": [
|
||||
@@ -484,11 +484,11 @@
|
||||
},
|
||||
"parso": {
|
||||
"hashes": [
|
||||
"sha256:0c5659e0c6eba20636f99a04f469798dca8da279645ce5c387315b2c23912157",
|
||||
"sha256:8515fc12cfca6ee3aa59138741fc5624d62340c97e401c74875769948d4f2995"
|
||||
"sha256:158c140fc04112dc45bca311633ae5033c2c2a7b732fa33d0955bad8152a8dd0",
|
||||
"sha256:908e9fae2144a076d72ae4e25539143d40b8e3eafbaeae03c1bfe226f4cdf12c"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==0.6.2"
|
||||
"version": "==0.7.0"
|
||||
},
|
||||
"parver": {
|
||||
"hashes": [
|
||||
@@ -520,10 +520,10 @@
|
||||
},
|
||||
"pbr": {
|
||||
"hashes": [
|
||||
"sha256:139d2625547dbfa5fb0b81daebb39601c478c21956dc57e2e07b74450a8c506b",
|
||||
"sha256:61aa52a0f18b71c5cc58232d2cf8f8d09cd67fcad60b742a60124cb8d6951488"
|
||||
"sha256:07f558fece33b05caf857474a366dfcc00562bca13dd8b47b2b3e22d9f9bf55c",
|
||||
"sha256:579170e23f8e0c2f24b0de612f71f648eccb79fb1322c814ae6b3c07b5ba23e8"
|
||||
],
|
||||
"version": "==5.4.4"
|
||||
"version": "==5.4.5"
|
||||
},
|
||||
"pipenv": {
|
||||
"editable": true,
|
||||
@@ -590,11 +590,11 @@
|
||||
},
|
||||
"pyparsing": {
|
||||
"hashes": [
|
||||
"sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f",
|
||||
"sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec"
|
||||
"sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b",
|
||||
"sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"
|
||||
],
|
||||
"markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==2.4.6"
|
||||
"version": "==2.4.7"
|
||||
},
|
||||
"pytest": {
|
||||
"hashes": [
|
||||
@@ -759,11 +759,11 @@
|
||||
},
|
||||
"sphinx-click": {
|
||||
"hashes": [
|
||||
"sha256:793c68b41c4a9435f953e2a27f9bf5883729037b7431f32b2776257c2966bd1b",
|
||||
"sha256:8c6274666730686a65efbae0b4465879b030372333de3114aeb63c44204da32e"
|
||||
"sha256:06952d5de6cbe2cb7d6dc656bc471652d2b484cf1e1b2d65edb7f4f2e867c7f6",
|
||||
"sha256:1b649ebe9f7a85b78ef6545d1dc258da5abca850ac6375be104d484a6334a728"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.3.1"
|
||||
"version": "==2.3.2"
|
||||
},
|
||||
"sphinxcontrib-websupport": {
|
||||
"hashes": [
|
||||
@@ -803,11 +803,11 @@
|
||||
},
|
||||
"tqdm": {
|
||||
"hashes": [
|
||||
"sha256:03d2366c64d44c7f61e74c700d9b202d57e9efe355ea5c28814c52bfe7a50b8c",
|
||||
"sha256:be5ddeec77d78ba781ea41eacb2358a77f74cc2407f54b82222d7ee7dc8c8ccf"
|
||||
"sha256:00339634a22c10a7a22476ee946bbde2dbe48d042ded784e4d88e0236eca5d81",
|
||||
"sha256:ea9e3fd6bd9a37e8783d75bfc4c1faf3c6813da6bd1c3e776488b41ec683af94"
|
||||
],
|
||||
"markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==4.44.1"
|
||||
"version": "==4.45.0"
|
||||
},
|
||||
"twine": {
|
||||
"hashes": [
|
||||
@@ -855,11 +855,11 @@
|
||||
},
|
||||
"urllib3": {
|
||||
"hashes": [
|
||||
"sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc",
|
||||
"sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"
|
||||
"sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115",
|
||||
"sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3' and python_version < '4'",
|
||||
"version": "==1.25.8"
|
||||
"version": "==1.25.9"
|
||||
},
|
||||
"virtualenv": {
|
||||
"hashes": [
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Fixed an issue with ``pipenv check`` failing due to an invalid API key from ``pyup.io``.
|
||||
@@ -0,0 +1,11 @@
|
||||
Add and update vendored dependencies to accommodate ``safety`` vendoring:
|
||||
- **safety** ``(none)`` => ``1.8.7``
|
||||
- **dparse** ``(none)`` => ``0.5.0``
|
||||
- **pyyaml** ``(none)`` => ``5.3.1``
|
||||
- **urllib3** ``1.25.8`` => ``1.25.9``
|
||||
- **certifi** ``2019.11.28`` => ``2020.4.5.1``
|
||||
- **pyparsing** ``2.4.6`` => ``2.4.7``
|
||||
- **resolvelib** ``0.2.2`` => ``0.3.0``
|
||||
- **importlib-metadata** ``1.5.1`` => ``1.6.0``
|
||||
- **pip-shims** ``0.5.1`` => ``0.5.2``
|
||||
- **requirementslib** ``1.5.5`` => ``1.5.6``
|
||||
+13
-7
@@ -125,8 +125,10 @@ def do_clear():
|
||||
from pip import locations
|
||||
|
||||
try:
|
||||
vistir.path.rmtree(PIPENV_CACHE_DIR)
|
||||
vistir.path.rmtree(locations.USER_CACHE_DIR)
|
||||
vistir.path.rmtree(PIPENV_CACHE_DIR, onerror=vistir.path.handle_remove_readonly)
|
||||
vistir.path.rmtree(
|
||||
locations.USER_CACHE_DIR, onerror=vistir.path.handle_remove_readonly
|
||||
)
|
||||
except OSError as e:
|
||||
# Ignore FileNotFoundError. This is needed for Python 2.7.
|
||||
import errno
|
||||
@@ -756,6 +758,9 @@ def batch_install(deps_list, procs, failed_deps_queue,
|
||||
del os.environ["PYTHONHOME"]
|
||||
if "GIT_CONFIG" in os.environ and dep.is_vcs:
|
||||
del os.environ["GIT_CONFIG"]
|
||||
use_pep517 = True
|
||||
if failed and not dep.is_vcs:
|
||||
use_pep517 = False
|
||||
|
||||
c = pip_install(
|
||||
dep,
|
||||
@@ -768,7 +773,7 @@ def batch_install(deps_list, procs, failed_deps_queue,
|
||||
pypi_mirror=pypi_mirror,
|
||||
trusted_hosts=trusted_hosts,
|
||||
extra_indexes=extra_indexes,
|
||||
use_pep517=not failed,
|
||||
use_pep517=use_pep517,
|
||||
)
|
||||
c.dep = dep
|
||||
# if dep.is_vcs or dep.editable:
|
||||
@@ -2585,7 +2590,7 @@ def do_check(
|
||||
click.echo(crayons.normal(decode_for_output("Checking PEP 508 requirements…"), bold=True))
|
||||
pep508checker_path = pep508checker.__file__.rstrip("cdo")
|
||||
safety_path = os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)), "patched", "safety.zip"
|
||||
os.path.dirname(os.path.abspath(__file__)), "patched", "safety"
|
||||
)
|
||||
if not system:
|
||||
python = which("python")
|
||||
@@ -2646,9 +2651,10 @@ def do_check(
|
||||
err=True,
|
||||
)
|
||||
else:
|
||||
ignored = ""
|
||||
key = "--key={0}".format(PIPENV_PYUP_API_KEY)
|
||||
cmd = _cmd + [safety_path, "check", "--json", key]
|
||||
ignored = []
|
||||
cmd = _cmd + [safety_path, "check", "--json"]
|
||||
if PIPENV_PYUP_API_KEY:
|
||||
cmd = cmd + ["--key={0}".format(PIPENV_PYUP_API_KEY)]
|
||||
if ignored:
|
||||
for cve in ignored:
|
||||
cmd += cve
|
||||
|
||||
@@ -90,7 +90,8 @@ class Environment(object):
|
||||
deps.add(dist)
|
||||
try:
|
||||
reqs = dist.requires()
|
||||
except (AttributeError, OSError, IOError): # The METADATA file can't be found
|
||||
# KeyError = limited metadata can be found
|
||||
except (KeyError, AttributeError, OSError, IOError): # The METADATA file can't be found
|
||||
return deps
|
||||
for req in reqs:
|
||||
dist = working_set.find(req)
|
||||
|
||||
@@ -256,7 +256,7 @@ approach, you may set this to '0', 'off', or 'false'.
|
||||
"""
|
||||
|
||||
PIPENV_PYUP_API_KEY = os.environ.get(
|
||||
"PIPENV_PYUP_API_KEY", "1ab8d58f-5122e025-83674263-bc1e79e0"
|
||||
"PIPENV_PYUP_API_KEY", None
|
||||
)
|
||||
|
||||
# Internal, support running in a different Python from sys.executable.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
safety
|
||||
safety==1.8.7
|
||||
crayons==0.1.2
|
||||
pipfile==0.0.2
|
||||
pip-tools==4.3.0
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2016, pyup.io
|
||||
Binary file not shown.
@@ -1,8 +1,51 @@
|
||||
"""Allow safety to be executable through `python -m safety`."""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .cli import cli
|
||||
import os
|
||||
import sys
|
||||
import sysconfig
|
||||
|
||||
|
||||
PATCHED_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
PIPENV_DIR = os.path.dirname(PATCHED_DIR)
|
||||
VENDORED_DIR = os.path.join("PIPENV_DIR", "vendor")
|
||||
|
||||
|
||||
def get_site_packages():
|
||||
prefixes = {sys.prefix, sysconfig.get_config_var('prefix')}
|
||||
try:
|
||||
prefixes.add(sys.real_prefix)
|
||||
except AttributeError:
|
||||
pass
|
||||
form = sysconfig.get_path('purelib', expand=False)
|
||||
py_version_short = '{0[0]}.{0[1]}'.format(sys.version_info)
|
||||
return {
|
||||
form.format(base=prefix, py_version_short=py_version_short)
|
||||
for prefix in prefixes
|
||||
}
|
||||
|
||||
|
||||
def insert_before_site_packages(*paths):
|
||||
site_packages = get_site_packages()
|
||||
index = None
|
||||
for i, path in enumerate(sys.path):
|
||||
if path in site_packages:
|
||||
index = i
|
||||
break
|
||||
if index is None:
|
||||
sys.path += list(paths)
|
||||
else:
|
||||
sys.path = sys.path[:index] + list(paths) + sys.path[index:]
|
||||
|
||||
|
||||
def insert_pipenv_dirs():
|
||||
insert_before_site_packages(os.path.dirname(PIPENV_DIR), PATCHED_DIR, VENDORED_DIR)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
insert_pipenv_dirs()
|
||||
yaml_lib = "pipenv.patched.yaml{0}".format(sys.version_info[0])
|
||||
locals()[yaml_lib] = __import__(yaml_lib)
|
||||
sys.modules["yaml"] = sys.modules[yaml_lib]
|
||||
from safety.cli import cli
|
||||
cli(prog_name="safety")
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2017-2020 Ingy döt Net
|
||||
Copyright (c) 2006-2016 Kirill Simonov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,431 @@
|
||||
|
||||
from error import *
|
||||
|
||||
from tokens import *
|
||||
from events import *
|
||||
from nodes import *
|
||||
|
||||
from loader import *
|
||||
from dumper import *
|
||||
|
||||
__version__ = '5.3.1'
|
||||
|
||||
try:
|
||||
from cyaml import *
|
||||
__with_libyaml__ = True
|
||||
except ImportError:
|
||||
__with_libyaml__ = False
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# Warnings control
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# 'Global' warnings state:
|
||||
_warnings_enabled = {
|
||||
'YAMLLoadWarning': True,
|
||||
}
|
||||
|
||||
# Get or set global warnings' state
|
||||
def warnings(settings=None):
|
||||
if settings is None:
|
||||
return _warnings_enabled
|
||||
|
||||
if type(settings) is dict:
|
||||
for key in settings:
|
||||
if key in _warnings_enabled:
|
||||
_warnings_enabled[key] = settings[key]
|
||||
|
||||
# Warn when load() is called without Loader=...
|
||||
class YAMLLoadWarning(RuntimeWarning):
|
||||
pass
|
||||
|
||||
def load_warning(method):
|
||||
if _warnings_enabled['YAMLLoadWarning'] is False:
|
||||
return
|
||||
|
||||
import warnings
|
||||
|
||||
message = (
|
||||
"calling yaml.%s() without Loader=... is deprecated, as the "
|
||||
"default Loader is unsafe. Please read "
|
||||
"https://msg.pyyaml.org/load for full details."
|
||||
) % method
|
||||
|
||||
warnings.warn(message, YAMLLoadWarning, stacklevel=3)
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
def scan(stream, Loader=Loader):
|
||||
"""
|
||||
Scan a YAML stream and produce scanning tokens.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_token():
|
||||
yield loader.get_token()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def parse(stream, Loader=Loader):
|
||||
"""
|
||||
Parse a YAML stream and produce parsing events.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_event():
|
||||
yield loader.get_event()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def compose(stream, Loader=Loader):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding representation tree.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
return loader.get_single_node()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def compose_all(stream, Loader=Loader):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding representation trees.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_node():
|
||||
yield loader.get_node()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def load(stream, Loader=None):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
load_warning('load')
|
||||
Loader = FullLoader
|
||||
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
return loader.get_single_data()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def load_all(stream, Loader=None):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
"""
|
||||
if Loader is None:
|
||||
load_warning('load_all')
|
||||
Loader = FullLoader
|
||||
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_data():
|
||||
yield loader.get_data()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def full_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve all tags except those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load(stream, FullLoader)
|
||||
|
||||
def full_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve all tags except those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load_all(stream, FullLoader)
|
||||
|
||||
def safe_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve only basic YAML tags. This is known
|
||||
to be safe for untrusted input.
|
||||
"""
|
||||
return load(stream, SafeLoader)
|
||||
|
||||
def safe_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve only basic YAML tags. This is known
|
||||
to be safe for untrusted input.
|
||||
"""
|
||||
return load_all(stream, SafeLoader)
|
||||
|
||||
def unsafe_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve all tags, even those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load(stream, UnsafeLoader)
|
||||
|
||||
def unsafe_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve all tags, even those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load_all(stream, UnsafeLoader)
|
||||
|
||||
def emit(events, stream=None, Dumper=Dumper,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None):
|
||||
"""
|
||||
Emit YAML parsing events into a stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
from StringIO import StringIO
|
||||
stream = StringIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
try:
|
||||
for event in events:
|
||||
dumper.emit(event)
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def serialize_all(nodes, stream=None, Dumper=Dumper,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding='utf-8', explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None):
|
||||
"""
|
||||
Serialize a sequence of representation trees into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
if encoding is None:
|
||||
from StringIO import StringIO
|
||||
else:
|
||||
from cStringIO import StringIO
|
||||
stream = StringIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
encoding=encoding, version=version, tags=tags,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end)
|
||||
try:
|
||||
dumper.open()
|
||||
for node in nodes:
|
||||
dumper.serialize(node)
|
||||
dumper.close()
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def serialize(node, stream=None, Dumper=Dumper, **kwds):
|
||||
"""
|
||||
Serialize a representation tree into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return serialize_all([node], stream, Dumper=Dumper, **kwds)
|
||||
|
||||
def dump_all(documents, stream=None, Dumper=Dumper,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding='utf-8', explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
"""
|
||||
Serialize a sequence of Python objects into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
if encoding is None:
|
||||
from StringIO import StringIO
|
||||
else:
|
||||
from cStringIO import StringIO
|
||||
stream = StringIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, default_style=default_style,
|
||||
default_flow_style=default_flow_style,
|
||||
canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
encoding=encoding, version=version, tags=tags,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end, sort_keys=sort_keys)
|
||||
try:
|
||||
dumper.open()
|
||||
for data in documents:
|
||||
dumper.represent(data)
|
||||
dumper.close()
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def dump(data, stream=None, Dumper=Dumper, **kwds):
|
||||
"""
|
||||
Serialize a Python object into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all([data], stream, Dumper=Dumper, **kwds)
|
||||
|
||||
def safe_dump_all(documents, stream=None, **kwds):
|
||||
"""
|
||||
Serialize a sequence of Python objects into a YAML stream.
|
||||
Produce only basic YAML tags.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
|
||||
|
||||
def safe_dump(data, stream=None, **kwds):
|
||||
"""
|
||||
Serialize a Python object into a YAML stream.
|
||||
Produce only basic YAML tags.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
|
||||
|
||||
def add_implicit_resolver(tag, regexp, first=None,
|
||||
Loader=None, Dumper=Dumper):
|
||||
"""
|
||||
Add an implicit scalar detector.
|
||||
If an implicit scalar value matches the given regexp,
|
||||
the corresponding tag is assigned to the scalar.
|
||||
first is a sequence of possible initial characters or None.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_implicit_resolver(tag, regexp, first)
|
||||
loader.FullLoader.add_implicit_resolver(tag, regexp, first)
|
||||
loader.UnsafeLoader.add_implicit_resolver(tag, regexp, first)
|
||||
else:
|
||||
Loader.add_implicit_resolver(tag, regexp, first)
|
||||
Dumper.add_implicit_resolver(tag, regexp, first)
|
||||
|
||||
def add_path_resolver(tag, path, kind=None, Loader=None, Dumper=Dumper):
|
||||
"""
|
||||
Add a path based resolver for the given tag.
|
||||
A path is a list of keys that forms a path
|
||||
to a node in the representation tree.
|
||||
Keys can be string values, integers, or None.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_path_resolver(tag, path, kind)
|
||||
loader.FullLoader.add_path_resolver(tag, path, kind)
|
||||
loader.UnsafeLoader.add_path_resolver(tag, path, kind)
|
||||
else:
|
||||
Loader.add_path_resolver(tag, path, kind)
|
||||
Dumper.add_path_resolver(tag, path, kind)
|
||||
|
||||
def add_constructor(tag, constructor, Loader=None):
|
||||
"""
|
||||
Add a constructor for the given tag.
|
||||
Constructor is a function that accepts a Loader instance
|
||||
and a node object and produces the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_constructor(tag, constructor)
|
||||
loader.FullLoader.add_constructor(tag, constructor)
|
||||
loader.UnsafeLoader.add_constructor(tag, constructor)
|
||||
else:
|
||||
Loader.add_constructor(tag, constructor)
|
||||
|
||||
def add_multi_constructor(tag_prefix, multi_constructor, Loader=None):
|
||||
"""
|
||||
Add a multi-constructor for the given tag prefix.
|
||||
Multi-constructor is called for a node if its tag starts with tag_prefix.
|
||||
Multi-constructor accepts a Loader instance, a tag suffix,
|
||||
and a node object and produces the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
loader.FullLoader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
loader.UnsafeLoader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
else:
|
||||
Loader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
|
||||
def add_representer(data_type, representer, Dumper=Dumper):
|
||||
"""
|
||||
Add a representer for the given type.
|
||||
Representer is a function accepting a Dumper instance
|
||||
and an instance of the given data type
|
||||
and producing the corresponding representation node.
|
||||
"""
|
||||
Dumper.add_representer(data_type, representer)
|
||||
|
||||
def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
|
||||
"""
|
||||
Add a representer for the given type.
|
||||
Multi-representer is a function accepting a Dumper instance
|
||||
and an instance of the given data type or subtype
|
||||
and producing the corresponding representation node.
|
||||
"""
|
||||
Dumper.add_multi_representer(data_type, multi_representer)
|
||||
|
||||
class YAMLObjectMetaclass(type):
|
||||
"""
|
||||
The metaclass for YAMLObject.
|
||||
"""
|
||||
def __init__(cls, name, bases, kwds):
|
||||
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
|
||||
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
|
||||
if isinstance(cls.yaml_loader, list):
|
||||
for loader in cls.yaml_loader:
|
||||
loader.add_constructor(cls.yaml_tag, cls.from_yaml)
|
||||
else:
|
||||
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
|
||||
|
||||
cls.yaml_dumper.add_representer(cls, cls.to_yaml)
|
||||
|
||||
class YAMLObject(object):
|
||||
"""
|
||||
An object that can dump itself to a YAML stream
|
||||
and load itself from a YAML stream.
|
||||
"""
|
||||
|
||||
__metaclass__ = YAMLObjectMetaclass
|
||||
__slots__ = () # no direct instantiation, so allow immutable subclasses
|
||||
|
||||
yaml_loader = [Loader, FullLoader, UnsafeLoader]
|
||||
yaml_dumper = Dumper
|
||||
|
||||
yaml_tag = None
|
||||
yaml_flow_style = None
|
||||
|
||||
def from_yaml(cls, loader, node):
|
||||
"""
|
||||
Convert a representation node to a Python object.
|
||||
"""
|
||||
return loader.construct_yaml_object(node, cls)
|
||||
from_yaml = classmethod(from_yaml)
|
||||
|
||||
def to_yaml(cls, dumper, data):
|
||||
"""
|
||||
Convert a Python object to a representation node.
|
||||
"""
|
||||
return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
|
||||
flow_style=cls.yaml_flow_style)
|
||||
to_yaml = classmethod(to_yaml)
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
|
||||
__all__ = ['Composer', 'ComposerError']
|
||||
|
||||
from error import MarkedYAMLError
|
||||
from events import *
|
||||
from nodes import *
|
||||
|
||||
class ComposerError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class Composer(object):
|
||||
|
||||
def __init__(self):
|
||||
self.anchors = {}
|
||||
|
||||
def check_node(self):
|
||||
# Drop the STREAM-START event.
|
||||
if self.check_event(StreamStartEvent):
|
||||
self.get_event()
|
||||
|
||||
# If there are more documents available?
|
||||
return not self.check_event(StreamEndEvent)
|
||||
|
||||
def get_node(self):
|
||||
# Get the root node of the next document.
|
||||
if not self.check_event(StreamEndEvent):
|
||||
return self.compose_document()
|
||||
|
||||
def get_single_node(self):
|
||||
# Drop the STREAM-START event.
|
||||
self.get_event()
|
||||
|
||||
# Compose a document if the stream is not empty.
|
||||
document = None
|
||||
if not self.check_event(StreamEndEvent):
|
||||
document = self.compose_document()
|
||||
|
||||
# Ensure that the stream contains no more documents.
|
||||
if not self.check_event(StreamEndEvent):
|
||||
event = self.get_event()
|
||||
raise ComposerError("expected a single document in the stream",
|
||||
document.start_mark, "but found another document",
|
||||
event.start_mark)
|
||||
|
||||
# Drop the STREAM-END event.
|
||||
self.get_event()
|
||||
|
||||
return document
|
||||
|
||||
def compose_document(self):
|
||||
# Drop the DOCUMENT-START event.
|
||||
self.get_event()
|
||||
|
||||
# Compose the root node.
|
||||
node = self.compose_node(None, None)
|
||||
|
||||
# Drop the DOCUMENT-END event.
|
||||
self.get_event()
|
||||
|
||||
self.anchors = {}
|
||||
return node
|
||||
|
||||
def compose_node(self, parent, index):
|
||||
if self.check_event(AliasEvent):
|
||||
event = self.get_event()
|
||||
anchor = event.anchor
|
||||
if anchor not in self.anchors:
|
||||
raise ComposerError(None, None, "found undefined alias %r"
|
||||
% anchor.encode('utf-8'), event.start_mark)
|
||||
return self.anchors[anchor]
|
||||
event = self.peek_event()
|
||||
anchor = event.anchor
|
||||
if anchor is not None:
|
||||
if anchor in self.anchors:
|
||||
raise ComposerError("found duplicate anchor %r; first occurrence"
|
||||
% anchor.encode('utf-8'), self.anchors[anchor].start_mark,
|
||||
"second occurrence", event.start_mark)
|
||||
self.descend_resolver(parent, index)
|
||||
if self.check_event(ScalarEvent):
|
||||
node = self.compose_scalar_node(anchor)
|
||||
elif self.check_event(SequenceStartEvent):
|
||||
node = self.compose_sequence_node(anchor)
|
||||
elif self.check_event(MappingStartEvent):
|
||||
node = self.compose_mapping_node(anchor)
|
||||
self.ascend_resolver()
|
||||
return node
|
||||
|
||||
def compose_scalar_node(self, anchor):
|
||||
event = self.get_event()
|
||||
tag = event.tag
|
||||
if tag is None or tag == u'!':
|
||||
tag = self.resolve(ScalarNode, event.value, event.implicit)
|
||||
node = ScalarNode(tag, event.value,
|
||||
event.start_mark, event.end_mark, style=event.style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
return node
|
||||
|
||||
def compose_sequence_node(self, anchor):
|
||||
start_event = self.get_event()
|
||||
tag = start_event.tag
|
||||
if tag is None or tag == u'!':
|
||||
tag = self.resolve(SequenceNode, None, start_event.implicit)
|
||||
node = SequenceNode(tag, [],
|
||||
start_event.start_mark, None,
|
||||
flow_style=start_event.flow_style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
index = 0
|
||||
while not self.check_event(SequenceEndEvent):
|
||||
node.value.append(self.compose_node(node, index))
|
||||
index += 1
|
||||
end_event = self.get_event()
|
||||
node.end_mark = end_event.end_mark
|
||||
return node
|
||||
|
||||
def compose_mapping_node(self, anchor):
|
||||
start_event = self.get_event()
|
||||
tag = start_event.tag
|
||||
if tag is None or tag == u'!':
|
||||
tag = self.resolve(MappingNode, None, start_event.implicit)
|
||||
node = MappingNode(tag, [],
|
||||
start_event.start_mark, None,
|
||||
flow_style=start_event.flow_style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
while not self.check_event(MappingEndEvent):
|
||||
#key_event = self.peek_event()
|
||||
item_key = self.compose_node(node, None)
|
||||
#if item_key in node.value:
|
||||
# raise ComposerError("while composing a mapping", start_event.start_mark,
|
||||
# "found duplicate key", key_event.start_mark)
|
||||
item_value = self.compose_node(node, item_key)
|
||||
#node.value[item_key] = item_value
|
||||
node.value.append((item_key, item_value))
|
||||
end_event = self.get_event()
|
||||
node.end_mark = end_event.end_mark
|
||||
return node
|
||||
|
||||
@@ -0,0 +1,760 @@
|
||||
|
||||
__all__ = [
|
||||
'BaseConstructor',
|
||||
'SafeConstructor',
|
||||
'FullConstructor',
|
||||
'UnsafeConstructor',
|
||||
'Constructor',
|
||||
'ConstructorError'
|
||||
]
|
||||
|
||||
from error import *
|
||||
from nodes import *
|
||||
|
||||
import datetime
|
||||
|
||||
import binascii, re, sys, types
|
||||
|
||||
class ConstructorError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
|
||||
class timezone(datetime.tzinfo):
|
||||
def __init__(self, offset):
|
||||
self._offset = offset
|
||||
seconds = abs(offset).total_seconds()
|
||||
self._name = 'UTC%s%02d:%02d' % (
|
||||
'-' if offset.days < 0 else '+',
|
||||
seconds // 3600,
|
||||
seconds % 3600 // 60
|
||||
)
|
||||
|
||||
def tzname(self, dt=None):
|
||||
return self._name
|
||||
|
||||
def utcoffset(self, dt=None):
|
||||
return self._offset
|
||||
|
||||
def dst(self, dt=None):
|
||||
return datetime.timedelta(0)
|
||||
|
||||
__repr__ = __str__ = tzname
|
||||
|
||||
|
||||
class BaseConstructor(object):
|
||||
|
||||
yaml_constructors = {}
|
||||
yaml_multi_constructors = {}
|
||||
|
||||
def __init__(self):
|
||||
self.constructed_objects = {}
|
||||
self.recursive_objects = {}
|
||||
self.state_generators = []
|
||||
self.deep_construct = False
|
||||
|
||||
def check_data(self):
|
||||
# If there are more documents available?
|
||||
return self.check_node()
|
||||
|
||||
def check_state_key(self, key):
|
||||
"""Block special attributes/methods from being set in a newly created
|
||||
object, to prevent user-controlled methods from being called during
|
||||
deserialization"""
|
||||
if self.get_state_keys_blacklist_regexp().match(key):
|
||||
raise ConstructorError(None, None,
|
||||
"blacklisted key '%s' in instance state found" % (key,), None)
|
||||
|
||||
def get_data(self):
|
||||
# Construct and return the next document.
|
||||
if self.check_node():
|
||||
return self.construct_document(self.get_node())
|
||||
|
||||
def get_single_data(self):
|
||||
# Ensure that the stream contains a single document and construct it.
|
||||
node = self.get_single_node()
|
||||
if node is not None:
|
||||
return self.construct_document(node)
|
||||
return None
|
||||
|
||||
def construct_document(self, node):
|
||||
data = self.construct_object(node)
|
||||
while self.state_generators:
|
||||
state_generators = self.state_generators
|
||||
self.state_generators = []
|
||||
for generator in state_generators:
|
||||
for dummy in generator:
|
||||
pass
|
||||
self.constructed_objects = {}
|
||||
self.recursive_objects = {}
|
||||
self.deep_construct = False
|
||||
return data
|
||||
|
||||
def construct_object(self, node, deep=False):
|
||||
if node in self.constructed_objects:
|
||||
return self.constructed_objects[node]
|
||||
if deep:
|
||||
old_deep = self.deep_construct
|
||||
self.deep_construct = True
|
||||
if node in self.recursive_objects:
|
||||
raise ConstructorError(None, None,
|
||||
"found unconstructable recursive node", node.start_mark)
|
||||
self.recursive_objects[node] = None
|
||||
constructor = None
|
||||
tag_suffix = None
|
||||
if node.tag in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[node.tag]
|
||||
else:
|
||||
for tag_prefix in self.yaml_multi_constructors:
|
||||
if tag_prefix is not None and node.tag.startswith(tag_prefix):
|
||||
tag_suffix = node.tag[len(tag_prefix):]
|
||||
constructor = self.yaml_multi_constructors[tag_prefix]
|
||||
break
|
||||
else:
|
||||
if None in self.yaml_multi_constructors:
|
||||
tag_suffix = node.tag
|
||||
constructor = self.yaml_multi_constructors[None]
|
||||
elif None in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[None]
|
||||
elif isinstance(node, ScalarNode):
|
||||
constructor = self.__class__.construct_scalar
|
||||
elif isinstance(node, SequenceNode):
|
||||
constructor = self.__class__.construct_sequence
|
||||
elif isinstance(node, MappingNode):
|
||||
constructor = self.__class__.construct_mapping
|
||||
if tag_suffix is None:
|
||||
data = constructor(self, node)
|
||||
else:
|
||||
data = constructor(self, tag_suffix, node)
|
||||
if isinstance(data, types.GeneratorType):
|
||||
generator = data
|
||||
data = generator.next()
|
||||
if self.deep_construct:
|
||||
for dummy in generator:
|
||||
pass
|
||||
else:
|
||||
self.state_generators.append(generator)
|
||||
self.constructed_objects[node] = data
|
||||
del self.recursive_objects[node]
|
||||
if deep:
|
||||
self.deep_construct = old_deep
|
||||
return data
|
||||
|
||||
def construct_scalar(self, node):
|
||||
if not isinstance(node, ScalarNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a scalar node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
return node.value
|
||||
|
||||
def construct_sequence(self, node, deep=False):
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a sequence node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
return [self.construct_object(child, deep=deep)
|
||||
for child in node.value]
|
||||
|
||||
def construct_mapping(self, node, deep=False):
|
||||
if not isinstance(node, MappingNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a mapping node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
mapping = {}
|
||||
for key_node, value_node in node.value:
|
||||
key = self.construct_object(key_node, deep=deep)
|
||||
try:
|
||||
hash(key)
|
||||
except TypeError, exc:
|
||||
raise ConstructorError("while constructing a mapping", node.start_mark,
|
||||
"found unacceptable key (%s)" % exc, key_node.start_mark)
|
||||
value = self.construct_object(value_node, deep=deep)
|
||||
mapping[key] = value
|
||||
return mapping
|
||||
|
||||
def construct_pairs(self, node, deep=False):
|
||||
if not isinstance(node, MappingNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a mapping node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
pairs = []
|
||||
for key_node, value_node in node.value:
|
||||
key = self.construct_object(key_node, deep=deep)
|
||||
value = self.construct_object(value_node, deep=deep)
|
||||
pairs.append((key, value))
|
||||
return pairs
|
||||
|
||||
def add_constructor(cls, tag, constructor):
|
||||
if not 'yaml_constructors' in cls.__dict__:
|
||||
cls.yaml_constructors = cls.yaml_constructors.copy()
|
||||
cls.yaml_constructors[tag] = constructor
|
||||
add_constructor = classmethod(add_constructor)
|
||||
|
||||
def add_multi_constructor(cls, tag_prefix, multi_constructor):
|
||||
if not 'yaml_multi_constructors' in cls.__dict__:
|
||||
cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy()
|
||||
cls.yaml_multi_constructors[tag_prefix] = multi_constructor
|
||||
add_multi_constructor = classmethod(add_multi_constructor)
|
||||
|
||||
class SafeConstructor(BaseConstructor):
|
||||
|
||||
def construct_scalar(self, node):
|
||||
if isinstance(node, MappingNode):
|
||||
for key_node, value_node in node.value:
|
||||
if key_node.tag == u'tag:yaml.org,2002:value':
|
||||
return self.construct_scalar(value_node)
|
||||
return BaseConstructor.construct_scalar(self, node)
|
||||
|
||||
def flatten_mapping(self, node):
|
||||
merge = []
|
||||
index = 0
|
||||
while index < len(node.value):
|
||||
key_node, value_node = node.value[index]
|
||||
if key_node.tag == u'tag:yaml.org,2002:merge':
|
||||
del node.value[index]
|
||||
if isinstance(value_node, MappingNode):
|
||||
self.flatten_mapping(value_node)
|
||||
merge.extend(value_node.value)
|
||||
elif isinstance(value_node, SequenceNode):
|
||||
submerge = []
|
||||
for subnode in value_node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing a mapping",
|
||||
node.start_mark,
|
||||
"expected a mapping for merging, but found %s"
|
||||
% subnode.id, subnode.start_mark)
|
||||
self.flatten_mapping(subnode)
|
||||
submerge.append(subnode.value)
|
||||
submerge.reverse()
|
||||
for value in submerge:
|
||||
merge.extend(value)
|
||||
else:
|
||||
raise ConstructorError("while constructing a mapping", node.start_mark,
|
||||
"expected a mapping or list of mappings for merging, but found %s"
|
||||
% value_node.id, value_node.start_mark)
|
||||
elif key_node.tag == u'tag:yaml.org,2002:value':
|
||||
key_node.tag = u'tag:yaml.org,2002:str'
|
||||
index += 1
|
||||
else:
|
||||
index += 1
|
||||
if merge:
|
||||
node.value = merge + node.value
|
||||
|
||||
def construct_mapping(self, node, deep=False):
|
||||
if isinstance(node, MappingNode):
|
||||
self.flatten_mapping(node)
|
||||
return BaseConstructor.construct_mapping(self, node, deep=deep)
|
||||
|
||||
def construct_yaml_null(self, node):
|
||||
self.construct_scalar(node)
|
||||
return None
|
||||
|
||||
bool_values = {
|
||||
u'yes': True,
|
||||
u'no': False,
|
||||
u'true': True,
|
||||
u'false': False,
|
||||
u'on': True,
|
||||
u'off': False,
|
||||
}
|
||||
|
||||
def construct_yaml_bool(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
return self.bool_values[value.lower()]
|
||||
|
||||
def construct_yaml_int(self, node):
|
||||
value = str(self.construct_scalar(node))
|
||||
value = value.replace('_', '')
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
if value[0] in '+-':
|
||||
value = value[1:]
|
||||
if value == '0':
|
||||
return 0
|
||||
elif value.startswith('0b'):
|
||||
return sign*int(value[2:], 2)
|
||||
elif value.startswith('0x'):
|
||||
return sign*int(value[2:], 16)
|
||||
elif value[0] == '0':
|
||||
return sign*int(value, 8)
|
||||
elif ':' in value:
|
||||
digits = [int(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*int(value)
|
||||
|
||||
inf_value = 1e300
|
||||
while inf_value != inf_value*inf_value:
|
||||
inf_value *= inf_value
|
||||
nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99).
|
||||
|
||||
def construct_yaml_float(self, node):
|
||||
value = str(self.construct_scalar(node))
|
||||
value = value.replace('_', '').lower()
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
if value[0] in '+-':
|
||||
value = value[1:]
|
||||
if value == '.inf':
|
||||
return sign*self.inf_value
|
||||
elif value == '.nan':
|
||||
return self.nan_value
|
||||
elif ':' in value:
|
||||
digits = [float(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0.0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*float(value)
|
||||
|
||||
def construct_yaml_binary(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
try:
|
||||
return str(value).decode('base64')
|
||||
except (binascii.Error, UnicodeEncodeError), exc:
|
||||
raise ConstructorError(None, None,
|
||||
"failed to decode base64 data: %s" % exc, node.start_mark)
|
||||
|
||||
timestamp_regexp = re.compile(
|
||||
ur'''^(?P<year>[0-9][0-9][0-9][0-9])
|
||||
-(?P<month>[0-9][0-9]?)
|
||||
-(?P<day>[0-9][0-9]?)
|
||||
(?:(?:[Tt]|[ \t]+)
|
||||
(?P<hour>[0-9][0-9]?)
|
||||
:(?P<minute>[0-9][0-9])
|
||||
:(?P<second>[0-9][0-9])
|
||||
(?:\.(?P<fraction>[0-9]*))?
|
||||
(?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
|
||||
(?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X)
|
||||
|
||||
def construct_yaml_timestamp(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
match = self.timestamp_regexp.match(node.value)
|
||||
values = match.groupdict()
|
||||
year = int(values['year'])
|
||||
month = int(values['month'])
|
||||
day = int(values['day'])
|
||||
if not values['hour']:
|
||||
return datetime.date(year, month, day)
|
||||
hour = int(values['hour'])
|
||||
minute = int(values['minute'])
|
||||
second = int(values['second'])
|
||||
fraction = 0
|
||||
tzinfo = None
|
||||
if values['fraction']:
|
||||
fraction = values['fraction'][:6]
|
||||
while len(fraction) < 6:
|
||||
fraction += '0'
|
||||
fraction = int(fraction)
|
||||
if values['tz_sign']:
|
||||
tz_hour = int(values['tz_hour'])
|
||||
tz_minute = int(values['tz_minute'] or 0)
|
||||
delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
|
||||
if values['tz_sign'] == '-':
|
||||
delta = -delta
|
||||
tzinfo = timezone(delta)
|
||||
elif values['tz']:
|
||||
tzinfo = timezone(datetime.timedelta(0))
|
||||
return datetime.datetime(year, month, day, hour, minute, second, fraction,
|
||||
tzinfo=tzinfo)
|
||||
|
||||
def construct_yaml_omap(self, node):
|
||||
# Note: we do not check for duplicate keys, because it's too
|
||||
# CPU-expensive.
|
||||
omap = []
|
||||
yield omap
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a sequence, but found %s" % node.id, node.start_mark)
|
||||
for subnode in node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a mapping of length 1, but found %s" % subnode.id,
|
||||
subnode.start_mark)
|
||||
if len(subnode.value) != 1:
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a single mapping item, but found %d items" % len(subnode.value),
|
||||
subnode.start_mark)
|
||||
key_node, value_node = subnode.value[0]
|
||||
key = self.construct_object(key_node)
|
||||
value = self.construct_object(value_node)
|
||||
omap.append((key, value))
|
||||
|
||||
def construct_yaml_pairs(self, node):
|
||||
# Note: the same code as `construct_yaml_omap`.
|
||||
pairs = []
|
||||
yield pairs
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a sequence, but found %s" % node.id, node.start_mark)
|
||||
for subnode in node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a mapping of length 1, but found %s" % subnode.id,
|
||||
subnode.start_mark)
|
||||
if len(subnode.value) != 1:
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a single mapping item, but found %d items" % len(subnode.value),
|
||||
subnode.start_mark)
|
||||
key_node, value_node = subnode.value[0]
|
||||
key = self.construct_object(key_node)
|
||||
value = self.construct_object(value_node)
|
||||
pairs.append((key, value))
|
||||
|
||||
def construct_yaml_set(self, node):
|
||||
data = set()
|
||||
yield data
|
||||
value = self.construct_mapping(node)
|
||||
data.update(value)
|
||||
|
||||
def construct_yaml_str(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
try:
|
||||
return value.encode('ascii')
|
||||
except UnicodeEncodeError:
|
||||
return value
|
||||
|
||||
def construct_yaml_seq(self, node):
|
||||
data = []
|
||||
yield data
|
||||
data.extend(self.construct_sequence(node))
|
||||
|
||||
def construct_yaml_map(self, node):
|
||||
data = {}
|
||||
yield data
|
||||
value = self.construct_mapping(node)
|
||||
data.update(value)
|
||||
|
||||
def construct_yaml_object(self, node, cls):
|
||||
data = cls.__new__(cls)
|
||||
yield data
|
||||
if hasattr(data, '__setstate__'):
|
||||
state = self.construct_mapping(node, deep=True)
|
||||
data.__setstate__(state)
|
||||
else:
|
||||
state = self.construct_mapping(node)
|
||||
data.__dict__.update(state)
|
||||
|
||||
def construct_undefined(self, node):
|
||||
raise ConstructorError(None, None,
|
||||
"could not determine a constructor for the tag %r" % node.tag.encode('utf-8'),
|
||||
node.start_mark)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:null',
|
||||
SafeConstructor.construct_yaml_null)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:bool',
|
||||
SafeConstructor.construct_yaml_bool)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:int',
|
||||
SafeConstructor.construct_yaml_int)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:float',
|
||||
SafeConstructor.construct_yaml_float)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:binary',
|
||||
SafeConstructor.construct_yaml_binary)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:timestamp',
|
||||
SafeConstructor.construct_yaml_timestamp)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:omap',
|
||||
SafeConstructor.construct_yaml_omap)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:pairs',
|
||||
SafeConstructor.construct_yaml_pairs)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:set',
|
||||
SafeConstructor.construct_yaml_set)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:str',
|
||||
SafeConstructor.construct_yaml_str)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:seq',
|
||||
SafeConstructor.construct_yaml_seq)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:map',
|
||||
SafeConstructor.construct_yaml_map)
|
||||
|
||||
SafeConstructor.add_constructor(None,
|
||||
SafeConstructor.construct_undefined)
|
||||
|
||||
class FullConstructor(SafeConstructor):
|
||||
# 'extend' is blacklisted because it is used by
|
||||
# construct_python_object_apply to add `listitems` to a newly generate
|
||||
# python instance
|
||||
def get_state_keys_blacklist(self):
|
||||
return ['^extend$', '^__.*__$']
|
||||
|
||||
def get_state_keys_blacklist_regexp(self):
|
||||
if not hasattr(self, 'state_keys_blacklist_regexp'):
|
||||
self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')')
|
||||
return self.state_keys_blacklist_regexp
|
||||
|
||||
def construct_python_str(self, node):
|
||||
return self.construct_scalar(node).encode('utf-8')
|
||||
|
||||
def construct_python_unicode(self, node):
|
||||
return self.construct_scalar(node)
|
||||
|
||||
def construct_python_long(self, node):
|
||||
return long(self.construct_yaml_int(node))
|
||||
|
||||
def construct_python_complex(self, node):
|
||||
return complex(self.construct_scalar(node))
|
||||
|
||||
def construct_python_tuple(self, node):
|
||||
return tuple(self.construct_sequence(node))
|
||||
|
||||
def find_python_module(self, name, mark, unsafe=False):
|
||||
if not name:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"expected non-empty name appended to the tag", mark)
|
||||
if unsafe:
|
||||
try:
|
||||
__import__(name)
|
||||
except ImportError, exc:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark)
|
||||
if name not in sys.modules:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"module %r is not imported" % name.encode('utf-8'), mark)
|
||||
return sys.modules[name]
|
||||
|
||||
def find_python_name(self, name, mark, unsafe=False):
|
||||
if not name:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"expected non-empty name appended to the tag", mark)
|
||||
if u'.' in name:
|
||||
module_name, object_name = name.rsplit('.', 1)
|
||||
else:
|
||||
module_name = '__builtin__'
|
||||
object_name = name
|
||||
if unsafe:
|
||||
try:
|
||||
__import__(module_name)
|
||||
except ImportError, exc:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark)
|
||||
if module_name not in sys.modules:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"module %r is not imported" % module_name.encode('utf-8'), mark)
|
||||
module = sys.modules[module_name]
|
||||
if not hasattr(module, object_name):
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"cannot find %r in the module %r" % (object_name.encode('utf-8'),
|
||||
module.__name__), mark)
|
||||
return getattr(module, object_name)
|
||||
|
||||
def construct_python_name(self, suffix, node):
|
||||
value = self.construct_scalar(node)
|
||||
if value:
|
||||
raise ConstructorError("while constructing a Python name", node.start_mark,
|
||||
"expected the empty value, but found %r" % value.encode('utf-8'),
|
||||
node.start_mark)
|
||||
return self.find_python_name(suffix, node.start_mark)
|
||||
|
||||
def construct_python_module(self, suffix, node):
|
||||
value = self.construct_scalar(node)
|
||||
if value:
|
||||
raise ConstructorError("while constructing a Python module", node.start_mark,
|
||||
"expected the empty value, but found %r" % value.encode('utf-8'),
|
||||
node.start_mark)
|
||||
return self.find_python_module(suffix, node.start_mark)
|
||||
|
||||
class classobj: pass
|
||||
|
||||
def make_python_instance(self, suffix, node,
|
||||
args=None, kwds=None, newobj=False, unsafe=False):
|
||||
if not args:
|
||||
args = []
|
||||
if not kwds:
|
||||
kwds = {}
|
||||
cls = self.find_python_name(suffix, node.start_mark)
|
||||
if not (unsafe or isinstance(cls, type) or isinstance(cls, type(self.classobj))):
|
||||
raise ConstructorError("while constructing a Python instance", node.start_mark,
|
||||
"expected a class, but found %r" % type(cls),
|
||||
node.start_mark)
|
||||
if newobj and isinstance(cls, type(self.classobj)) \
|
||||
and not args and not kwds:
|
||||
instance = self.classobj()
|
||||
instance.__class__ = cls
|
||||
return instance
|
||||
elif newobj and isinstance(cls, type):
|
||||
return cls.__new__(cls, *args, **kwds)
|
||||
else:
|
||||
return cls(*args, **kwds)
|
||||
|
||||
def set_python_instance_state(self, instance, state, unsafe=False):
|
||||
if hasattr(instance, '__setstate__'):
|
||||
instance.__setstate__(state)
|
||||
else:
|
||||
slotstate = {}
|
||||
if isinstance(state, tuple) and len(state) == 2:
|
||||
state, slotstate = state
|
||||
if hasattr(instance, '__dict__'):
|
||||
if not unsafe and state:
|
||||
for key in state.keys():
|
||||
self.check_state_key(key)
|
||||
instance.__dict__.update(state)
|
||||
elif state:
|
||||
slotstate.update(state)
|
||||
for key, value in slotstate.items():
|
||||
if not unsafe:
|
||||
self.check_state_key(key)
|
||||
setattr(instance, key, value)
|
||||
|
||||
def construct_python_object(self, suffix, node):
|
||||
# Format:
|
||||
# !!python/object:module.name { ... state ... }
|
||||
instance = self.make_python_instance(suffix, node, newobj=True)
|
||||
yield instance
|
||||
deep = hasattr(instance, '__setstate__')
|
||||
state = self.construct_mapping(node, deep=deep)
|
||||
self.set_python_instance_state(instance, state)
|
||||
|
||||
def construct_python_object_apply(self, suffix, node, newobj=False):
|
||||
# Format:
|
||||
# !!python/object/apply # (or !!python/object/new)
|
||||
# args: [ ... arguments ... ]
|
||||
# kwds: { ... keywords ... }
|
||||
# state: ... state ...
|
||||
# listitems: [ ... listitems ... ]
|
||||
# dictitems: { ... dictitems ... }
|
||||
# or short format:
|
||||
# !!python/object/apply [ ... arguments ... ]
|
||||
# The difference between !!python/object/apply and !!python/object/new
|
||||
# is how an object is created, check make_python_instance for details.
|
||||
if isinstance(node, SequenceNode):
|
||||
args = self.construct_sequence(node, deep=True)
|
||||
kwds = {}
|
||||
state = {}
|
||||
listitems = []
|
||||
dictitems = {}
|
||||
else:
|
||||
value = self.construct_mapping(node, deep=True)
|
||||
args = value.get('args', [])
|
||||
kwds = value.get('kwds', {})
|
||||
state = value.get('state', {})
|
||||
listitems = value.get('listitems', [])
|
||||
dictitems = value.get('dictitems', {})
|
||||
instance = self.make_python_instance(suffix, node, args, kwds, newobj)
|
||||
if state:
|
||||
self.set_python_instance_state(instance, state)
|
||||
if listitems:
|
||||
instance.extend(listitems)
|
||||
if dictitems:
|
||||
for key in dictitems:
|
||||
instance[key] = dictitems[key]
|
||||
return instance
|
||||
|
||||
def construct_python_object_new(self, suffix, node):
|
||||
return self.construct_python_object_apply(suffix, node, newobj=True)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/none',
|
||||
FullConstructor.construct_yaml_null)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/bool',
|
||||
FullConstructor.construct_yaml_bool)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/str',
|
||||
FullConstructor.construct_python_str)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/unicode',
|
||||
FullConstructor.construct_python_unicode)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/int',
|
||||
FullConstructor.construct_yaml_int)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/long',
|
||||
FullConstructor.construct_python_long)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/float',
|
||||
FullConstructor.construct_yaml_float)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/complex',
|
||||
FullConstructor.construct_python_complex)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/list',
|
||||
FullConstructor.construct_yaml_seq)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/tuple',
|
||||
FullConstructor.construct_python_tuple)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
u'tag:yaml.org,2002:python/dict',
|
||||
FullConstructor.construct_yaml_map)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
u'tag:yaml.org,2002:python/name:',
|
||||
FullConstructor.construct_python_name)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
u'tag:yaml.org,2002:python/module:',
|
||||
FullConstructor.construct_python_module)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
u'tag:yaml.org,2002:python/object:',
|
||||
FullConstructor.construct_python_object)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
u'tag:yaml.org,2002:python/object/new:',
|
||||
FullConstructor.construct_python_object_new)
|
||||
|
||||
class UnsafeConstructor(FullConstructor):
|
||||
|
||||
def find_python_module(self, name, mark):
|
||||
return super(UnsafeConstructor, self).find_python_module(name, mark, unsafe=True)
|
||||
|
||||
def find_python_name(self, name, mark):
|
||||
return super(UnsafeConstructor, self).find_python_name(name, mark, unsafe=True)
|
||||
|
||||
def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False):
|
||||
return super(UnsafeConstructor, self).make_python_instance(
|
||||
suffix, node, args, kwds, newobj, unsafe=True)
|
||||
|
||||
def set_python_instance_state(self, instance, state):
|
||||
return super(UnsafeConstructor, self).set_python_instance_state(
|
||||
instance, state, unsafe=True)
|
||||
|
||||
UnsafeConstructor.add_multi_constructor(
|
||||
u'tag:yaml.org,2002:python/object/apply:',
|
||||
UnsafeConstructor.construct_python_object_apply)
|
||||
|
||||
# Constructor is same as UnsafeConstructor. Need to leave this in place in case
|
||||
# people have extended it directly.
|
||||
class Constructor(UnsafeConstructor):
|
||||
pass
|
||||
@@ -0,0 +1,101 @@
|
||||
|
||||
__all__ = [
|
||||
'CBaseLoader', 'CSafeLoader', 'CFullLoader', 'CUnsafeLoader', 'CLoader',
|
||||
'CBaseDumper', 'CSafeDumper', 'CDumper'
|
||||
]
|
||||
|
||||
from _yaml import CParser, CEmitter
|
||||
|
||||
from constructor import *
|
||||
|
||||
from serializer import *
|
||||
from representer import *
|
||||
|
||||
from resolver import *
|
||||
|
||||
class CBaseLoader(CParser, BaseConstructor, BaseResolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
BaseConstructor.__init__(self)
|
||||
BaseResolver.__init__(self)
|
||||
|
||||
class CSafeLoader(CParser, SafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
SafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CFullLoader(CParser, FullConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
FullConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CUnsafeLoader(CParser, UnsafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
UnsafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CLoader(CParser, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CSafeDumper(CEmitter, SafeRepresenter, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
SafeRepresenter.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CDumper(CEmitter, Serializer, Representer, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
|
||||
__all__ = ['BaseDumper', 'SafeDumper', 'Dumper']
|
||||
|
||||
from emitter import *
|
||||
from serializer import *
|
||||
from representer import *
|
||||
from resolver import *
|
||||
|
||||
class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
SafeRepresenter.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class Dumper(Emitter, Serializer, Representer, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,75 @@
|
||||
|
||||
__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError']
|
||||
|
||||
class Mark(object):
|
||||
|
||||
def __init__(self, name, index, line, column, buffer, pointer):
|
||||
self.name = name
|
||||
self.index = index
|
||||
self.line = line
|
||||
self.column = column
|
||||
self.buffer = buffer
|
||||
self.pointer = pointer
|
||||
|
||||
def get_snippet(self, indent=4, max_length=75):
|
||||
if self.buffer is None:
|
||||
return None
|
||||
head = ''
|
||||
start = self.pointer
|
||||
while start > 0 and self.buffer[start-1] not in u'\0\r\n\x85\u2028\u2029':
|
||||
start -= 1
|
||||
if self.pointer-start > max_length/2-1:
|
||||
head = ' ... '
|
||||
start += 5
|
||||
break
|
||||
tail = ''
|
||||
end = self.pointer
|
||||
while end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029':
|
||||
end += 1
|
||||
if end-self.pointer > max_length/2-1:
|
||||
tail = ' ... '
|
||||
end -= 5
|
||||
break
|
||||
snippet = self.buffer[start:end].encode('utf-8')
|
||||
return ' '*indent + head + snippet + tail + '\n' \
|
||||
+ ' '*(indent+self.pointer-start+len(head)) + '^'
|
||||
|
||||
def __str__(self):
|
||||
snippet = self.get_snippet()
|
||||
where = " in \"%s\", line %d, column %d" \
|
||||
% (self.name, self.line+1, self.column+1)
|
||||
if snippet is not None:
|
||||
where += ":\n"+snippet
|
||||
return where
|
||||
|
||||
class YAMLError(Exception):
|
||||
pass
|
||||
|
||||
class MarkedYAMLError(YAMLError):
|
||||
|
||||
def __init__(self, context=None, context_mark=None,
|
||||
problem=None, problem_mark=None, note=None):
|
||||
self.context = context
|
||||
self.context_mark = context_mark
|
||||
self.problem = problem
|
||||
self.problem_mark = problem_mark
|
||||
self.note = note
|
||||
|
||||
def __str__(self):
|
||||
lines = []
|
||||
if self.context is not None:
|
||||
lines.append(self.context)
|
||||
if self.context_mark is not None \
|
||||
and (self.problem is None or self.problem_mark is None
|
||||
or self.context_mark.name != self.problem_mark.name
|
||||
or self.context_mark.line != self.problem_mark.line
|
||||
or self.context_mark.column != self.problem_mark.column):
|
||||
lines.append(str(self.context_mark))
|
||||
if self.problem is not None:
|
||||
lines.append(self.problem)
|
||||
if self.problem_mark is not None:
|
||||
lines.append(str(self.problem_mark))
|
||||
if self.note is not None:
|
||||
lines.append(self.note)
|
||||
return '\n'.join(lines)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
# Abstract classes.
|
||||
|
||||
class Event(object):
|
||||
def __init__(self, start_mark=None, end_mark=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
attributes = [key for key in ['anchor', 'tag', 'implicit', 'value']
|
||||
if hasattr(self, key)]
|
||||
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
class NodeEvent(Event):
|
||||
def __init__(self, anchor, start_mark=None, end_mark=None):
|
||||
self.anchor = anchor
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class CollectionStartEvent(NodeEvent):
|
||||
def __init__(self, anchor, tag, implicit, start_mark=None, end_mark=None,
|
||||
flow_style=None):
|
||||
self.anchor = anchor
|
||||
self.tag = tag
|
||||
self.implicit = implicit
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
class CollectionEndEvent(Event):
|
||||
pass
|
||||
|
||||
# Implementations.
|
||||
|
||||
class StreamStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None, encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
class StreamEndEvent(Event):
|
||||
pass
|
||||
|
||||
class DocumentStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None, version=None, tags=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.explicit = explicit
|
||||
self.version = version
|
||||
self.tags = tags
|
||||
|
||||
class DocumentEndEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.explicit = explicit
|
||||
|
||||
class AliasEvent(NodeEvent):
|
||||
pass
|
||||
|
||||
class ScalarEvent(NodeEvent):
|
||||
def __init__(self, anchor, tag, implicit, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
self.anchor = anchor
|
||||
self.tag = tag
|
||||
self.implicit = implicit
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
class SequenceStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
class SequenceEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
class MappingStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
class MappingEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
__all__ = ['BaseLoader', 'FullLoader', 'SafeLoader', 'Loader', 'UnsafeLoader']
|
||||
|
||||
from reader import *
|
||||
from scanner import *
|
||||
from parser import *
|
||||
from composer import *
|
||||
from constructor import *
|
||||
from resolver import *
|
||||
|
||||
class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
BaseConstructor.__init__(self)
|
||||
BaseResolver.__init__(self)
|
||||
|
||||
class FullLoader(Reader, Scanner, Parser, Composer, FullConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
FullConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class SafeLoader(Reader, Scanner, Parser, Composer, SafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
SafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
# UnsafeLoader is the same as Loader (which is and was always unsafe on
|
||||
# untrusted input). Use of either Loader or UnsafeLoader should be rare, since
|
||||
# FullLoad should be able to load almost all YAML safely. Loader is left intact
|
||||
# to ensure backwards compatibility.
|
||||
class UnsafeLoader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
@@ -0,0 +1,49 @@
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, tag, value, start_mark, end_mark):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
value = self.value
|
||||
#if isinstance(value, list):
|
||||
# if len(value) == 0:
|
||||
# value = '<empty>'
|
||||
# elif len(value) == 1:
|
||||
# value = '<1 item>'
|
||||
# else:
|
||||
# value = '<%d items>' % len(value)
|
||||
#else:
|
||||
# if len(value) > 75:
|
||||
# value = repr(value[:70]+u' ... ')
|
||||
# else:
|
||||
# value = repr(value)
|
||||
value = repr(value)
|
||||
return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
|
||||
|
||||
class ScalarNode(Node):
|
||||
id = 'scalar'
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
class CollectionNode(Node):
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, flow_style=None):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
class SequenceNode(CollectionNode):
|
||||
id = 'sequence'
|
||||
|
||||
class MappingNode(CollectionNode):
|
||||
id = 'mapping'
|
||||
|
||||
@@ -0,0 +1,589 @@
|
||||
|
||||
# The following YAML grammar is LL(1) and is parsed by a recursive descent
|
||||
# parser.
|
||||
#
|
||||
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
|
||||
# implicit_document ::= block_node DOCUMENT-END*
|
||||
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
|
||||
# block_node_or_indentless_sequence ::=
|
||||
# ALIAS
|
||||
# | properties (block_content | indentless_block_sequence)?
|
||||
# | block_content
|
||||
# | indentless_block_sequence
|
||||
# block_node ::= ALIAS
|
||||
# | properties block_content?
|
||||
# | block_content
|
||||
# flow_node ::= ALIAS
|
||||
# | properties flow_content?
|
||||
# | flow_content
|
||||
# properties ::= TAG ANCHOR? | ANCHOR TAG?
|
||||
# block_content ::= block_collection | flow_collection | SCALAR
|
||||
# flow_content ::= flow_collection | SCALAR
|
||||
# block_collection ::= block_sequence | block_mapping
|
||||
# flow_collection ::= flow_sequence | flow_mapping
|
||||
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
|
||||
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
|
||||
# block_mapping ::= BLOCK-MAPPING_START
|
||||
# ((KEY block_node_or_indentless_sequence?)?
|
||||
# (VALUE block_node_or_indentless_sequence?)?)*
|
||||
# BLOCK-END
|
||||
# flow_sequence ::= FLOW-SEQUENCE-START
|
||||
# (flow_sequence_entry FLOW-ENTRY)*
|
||||
# flow_sequence_entry?
|
||||
# FLOW-SEQUENCE-END
|
||||
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
# flow_mapping ::= FLOW-MAPPING-START
|
||||
# (flow_mapping_entry FLOW-ENTRY)*
|
||||
# flow_mapping_entry?
|
||||
# FLOW-MAPPING-END
|
||||
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
#
|
||||
# FIRST sets:
|
||||
#
|
||||
# stream: { STREAM-START }
|
||||
# explicit_document: { DIRECTIVE DOCUMENT-START }
|
||||
# implicit_document: FIRST(block_node)
|
||||
# block_node: { ALIAS TAG ANCHOR SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# flow_node: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# block_content: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
|
||||
# flow_content: { FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
|
||||
# block_collection: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START }
|
||||
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# block_sequence: { BLOCK-SEQUENCE-START }
|
||||
# block_mapping: { BLOCK-MAPPING-START }
|
||||
# block_node_or_indentless_sequence: { ALIAS ANCHOR TAG SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START BLOCK-ENTRY }
|
||||
# indentless_sequence: { ENTRY }
|
||||
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# flow_sequence: { FLOW-SEQUENCE-START }
|
||||
# flow_mapping: { FLOW-MAPPING-START }
|
||||
# flow_sequence_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
|
||||
# flow_mapping_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
|
||||
|
||||
__all__ = ['Parser', 'ParserError']
|
||||
|
||||
from error import MarkedYAMLError
|
||||
from tokens import *
|
||||
from events import *
|
||||
from scanner import *
|
||||
|
||||
class ParserError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class Parser(object):
|
||||
# Since writing a recursive-descendant parser is a straightforward task, we
|
||||
# do not give many comments here.
|
||||
|
||||
DEFAULT_TAGS = {
|
||||
u'!': u'!',
|
||||
u'!!': u'tag:yaml.org,2002:',
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.current_event = None
|
||||
self.yaml_version = None
|
||||
self.tag_handles = {}
|
||||
self.states = []
|
||||
self.marks = []
|
||||
self.state = self.parse_stream_start
|
||||
|
||||
def dispose(self):
|
||||
# Reset the state attributes (to clear self-references)
|
||||
self.states = []
|
||||
self.state = None
|
||||
|
||||
def check_event(self, *choices):
|
||||
# Check the type of the next event.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
if self.current_event is not None:
|
||||
if not choices:
|
||||
return True
|
||||
for choice in choices:
|
||||
if isinstance(self.current_event, choice):
|
||||
return True
|
||||
return False
|
||||
|
||||
def peek_event(self):
|
||||
# Get the next event.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
return self.current_event
|
||||
|
||||
def get_event(self):
|
||||
# Get the next event and proceed further.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
value = self.current_event
|
||||
self.current_event = None
|
||||
return value
|
||||
|
||||
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
|
||||
# implicit_document ::= block_node DOCUMENT-END*
|
||||
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
|
||||
|
||||
def parse_stream_start(self):
|
||||
|
||||
# Parse the stream start.
|
||||
token = self.get_token()
|
||||
event = StreamStartEvent(token.start_mark, token.end_mark,
|
||||
encoding=token.encoding)
|
||||
|
||||
# Prepare the next state.
|
||||
self.state = self.parse_implicit_document_start
|
||||
|
||||
return event
|
||||
|
||||
def parse_implicit_document_start(self):
|
||||
|
||||
# Parse an implicit document.
|
||||
if not self.check_token(DirectiveToken, DocumentStartToken,
|
||||
StreamEndToken):
|
||||
self.tag_handles = self.DEFAULT_TAGS
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
event = DocumentStartEvent(start_mark, end_mark,
|
||||
explicit=False)
|
||||
|
||||
# Prepare the next state.
|
||||
self.states.append(self.parse_document_end)
|
||||
self.state = self.parse_block_node
|
||||
|
||||
return event
|
||||
|
||||
else:
|
||||
return self.parse_document_start()
|
||||
|
||||
def parse_document_start(self):
|
||||
|
||||
# Parse any extra document end indicators.
|
||||
while self.check_token(DocumentEndToken):
|
||||
self.get_token()
|
||||
|
||||
# Parse an explicit document.
|
||||
if not self.check_token(StreamEndToken):
|
||||
token = self.peek_token()
|
||||
start_mark = token.start_mark
|
||||
version, tags = self.process_directives()
|
||||
if not self.check_token(DocumentStartToken):
|
||||
raise ParserError(None, None,
|
||||
"expected '<document start>', but found %r"
|
||||
% self.peek_token().id,
|
||||
self.peek_token().start_mark)
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
event = DocumentStartEvent(start_mark, end_mark,
|
||||
explicit=True, version=version, tags=tags)
|
||||
self.states.append(self.parse_document_end)
|
||||
self.state = self.parse_document_content
|
||||
else:
|
||||
# Parse the end of the stream.
|
||||
token = self.get_token()
|
||||
event = StreamEndEvent(token.start_mark, token.end_mark)
|
||||
assert not self.states
|
||||
assert not self.marks
|
||||
self.state = None
|
||||
return event
|
||||
|
||||
def parse_document_end(self):
|
||||
|
||||
# Parse the document end.
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
explicit = False
|
||||
if self.check_token(DocumentEndToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
explicit = True
|
||||
event = DocumentEndEvent(start_mark, end_mark,
|
||||
explicit=explicit)
|
||||
|
||||
# Prepare the next state.
|
||||
self.state = self.parse_document_start
|
||||
|
||||
return event
|
||||
|
||||
def parse_document_content(self):
|
||||
if self.check_token(DirectiveToken,
|
||||
DocumentStartToken, DocumentEndToken, StreamEndToken):
|
||||
event = self.process_empty_scalar(self.peek_token().start_mark)
|
||||
self.state = self.states.pop()
|
||||
return event
|
||||
else:
|
||||
return self.parse_block_node()
|
||||
|
||||
def process_directives(self):
|
||||
self.yaml_version = None
|
||||
self.tag_handles = {}
|
||||
while self.check_token(DirectiveToken):
|
||||
token = self.get_token()
|
||||
if token.name == u'YAML':
|
||||
if self.yaml_version is not None:
|
||||
raise ParserError(None, None,
|
||||
"found duplicate YAML directive", token.start_mark)
|
||||
major, minor = token.value
|
||||
if major != 1:
|
||||
raise ParserError(None, None,
|
||||
"found incompatible YAML document (version 1.* is required)",
|
||||
token.start_mark)
|
||||
self.yaml_version = token.value
|
||||
elif token.name == u'TAG':
|
||||
handle, prefix = token.value
|
||||
if handle in self.tag_handles:
|
||||
raise ParserError(None, None,
|
||||
"duplicate tag handle %r" % handle.encode('utf-8'),
|
||||
token.start_mark)
|
||||
self.tag_handles[handle] = prefix
|
||||
if self.tag_handles:
|
||||
value = self.yaml_version, self.tag_handles.copy()
|
||||
else:
|
||||
value = self.yaml_version, None
|
||||
for key in self.DEFAULT_TAGS:
|
||||
if key not in self.tag_handles:
|
||||
self.tag_handles[key] = self.DEFAULT_TAGS[key]
|
||||
return value
|
||||
|
||||
# block_node_or_indentless_sequence ::= ALIAS
|
||||
# | properties (block_content | indentless_block_sequence)?
|
||||
# | block_content
|
||||
# | indentless_block_sequence
|
||||
# block_node ::= ALIAS
|
||||
# | properties block_content?
|
||||
# | block_content
|
||||
# flow_node ::= ALIAS
|
||||
# | properties flow_content?
|
||||
# | flow_content
|
||||
# properties ::= TAG ANCHOR? | ANCHOR TAG?
|
||||
# block_content ::= block_collection | flow_collection | SCALAR
|
||||
# flow_content ::= flow_collection | SCALAR
|
||||
# block_collection ::= block_sequence | block_mapping
|
||||
# flow_collection ::= flow_sequence | flow_mapping
|
||||
|
||||
def parse_block_node(self):
|
||||
return self.parse_node(block=True)
|
||||
|
||||
def parse_flow_node(self):
|
||||
return self.parse_node()
|
||||
|
||||
def parse_block_node_or_indentless_sequence(self):
|
||||
return self.parse_node(block=True, indentless_sequence=True)
|
||||
|
||||
def parse_node(self, block=False, indentless_sequence=False):
|
||||
if self.check_token(AliasToken):
|
||||
token = self.get_token()
|
||||
event = AliasEvent(token.value, token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
else:
|
||||
anchor = None
|
||||
tag = None
|
||||
start_mark = end_mark = tag_mark = None
|
||||
if self.check_token(AnchorToken):
|
||||
token = self.get_token()
|
||||
start_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
anchor = token.value
|
||||
if self.check_token(TagToken):
|
||||
token = self.get_token()
|
||||
tag_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
tag = token.value
|
||||
elif self.check_token(TagToken):
|
||||
token = self.get_token()
|
||||
start_mark = tag_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
tag = token.value
|
||||
if self.check_token(AnchorToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
anchor = token.value
|
||||
if tag is not None:
|
||||
handle, suffix = tag
|
||||
if handle is not None:
|
||||
if handle not in self.tag_handles:
|
||||
raise ParserError("while parsing a node", start_mark,
|
||||
"found undefined tag handle %r" % handle.encode('utf-8'),
|
||||
tag_mark)
|
||||
tag = self.tag_handles[handle]+suffix
|
||||
else:
|
||||
tag = suffix
|
||||
#if tag == u'!':
|
||||
# raise ParserError("while parsing a node", start_mark,
|
||||
# "found non-specific tag '!'", tag_mark,
|
||||
# "Please check 'http://pyyaml.org/wiki/YAMLNonSpecificTag' and share your opinion.")
|
||||
if start_mark is None:
|
||||
start_mark = end_mark = self.peek_token().start_mark
|
||||
event = None
|
||||
implicit = (tag is None or tag == u'!')
|
||||
if indentless_sequence and self.check_token(BlockEntryToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark)
|
||||
self.state = self.parse_indentless_sequence_entry
|
||||
else:
|
||||
if self.check_token(ScalarToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
if (token.plain and tag is None) or tag == u'!':
|
||||
implicit = (True, False)
|
||||
elif tag is None:
|
||||
implicit = (False, True)
|
||||
else:
|
||||
implicit = (False, False)
|
||||
event = ScalarEvent(anchor, tag, implicit, token.value,
|
||||
start_mark, end_mark, style=token.style)
|
||||
self.state = self.states.pop()
|
||||
elif self.check_token(FlowSequenceStartToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=True)
|
||||
self.state = self.parse_flow_sequence_first_entry
|
||||
elif self.check_token(FlowMappingStartToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = MappingStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=True)
|
||||
self.state = self.parse_flow_mapping_first_key
|
||||
elif block and self.check_token(BlockSequenceStartToken):
|
||||
end_mark = self.peek_token().start_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=False)
|
||||
self.state = self.parse_block_sequence_first_entry
|
||||
elif block and self.check_token(BlockMappingStartToken):
|
||||
end_mark = self.peek_token().start_mark
|
||||
event = MappingStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=False)
|
||||
self.state = self.parse_block_mapping_first_key
|
||||
elif anchor is not None or tag is not None:
|
||||
# Empty scalars are allowed even if a tag or an anchor is
|
||||
# specified.
|
||||
event = ScalarEvent(anchor, tag, (implicit, False), u'',
|
||||
start_mark, end_mark)
|
||||
self.state = self.states.pop()
|
||||
else:
|
||||
if block:
|
||||
node = 'block'
|
||||
else:
|
||||
node = 'flow'
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a %s node" % node, start_mark,
|
||||
"expected the node content, but found %r" % token.id,
|
||||
token.start_mark)
|
||||
return event
|
||||
|
||||
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
|
||||
|
||||
def parse_block_sequence_first_entry(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_block_sequence_entry()
|
||||
|
||||
def parse_block_sequence_entry(self):
|
||||
if self.check_token(BlockEntryToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(BlockEntryToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_sequence_entry)
|
||||
return self.parse_block_node()
|
||||
else:
|
||||
self.state = self.parse_block_sequence_entry
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
if not self.check_token(BlockEndToken):
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a block collection", self.marks[-1],
|
||||
"expected <block end>, but found %r" % token.id, token.start_mark)
|
||||
token = self.get_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
|
||||
|
||||
def parse_indentless_sequence_entry(self):
|
||||
if self.check_token(BlockEntryToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(BlockEntryToken,
|
||||
KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_indentless_sequence_entry)
|
||||
return self.parse_block_node()
|
||||
else:
|
||||
self.state = self.parse_indentless_sequence_entry
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
token = self.peek_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.start_mark)
|
||||
self.state = self.states.pop()
|
||||
return event
|
||||
|
||||
# block_mapping ::= BLOCK-MAPPING_START
|
||||
# ((KEY block_node_or_indentless_sequence?)?
|
||||
# (VALUE block_node_or_indentless_sequence?)?)*
|
||||
# BLOCK-END
|
||||
|
||||
def parse_block_mapping_first_key(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_block_mapping_key()
|
||||
|
||||
def parse_block_mapping_key(self):
|
||||
if self.check_token(KeyToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_mapping_value)
|
||||
return self.parse_block_node_or_indentless_sequence()
|
||||
else:
|
||||
self.state = self.parse_block_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
if not self.check_token(BlockEndToken):
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a block mapping", self.marks[-1],
|
||||
"expected <block end>, but found %r" % token.id, token.start_mark)
|
||||
token = self.get_token()
|
||||
event = MappingEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_block_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_mapping_key)
|
||||
return self.parse_block_node_or_indentless_sequence()
|
||||
else:
|
||||
self.state = self.parse_block_mapping_key
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_block_mapping_key
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
# flow_sequence ::= FLOW-SEQUENCE-START
|
||||
# (flow_sequence_entry FLOW-ENTRY)*
|
||||
# flow_sequence_entry?
|
||||
# FLOW-SEQUENCE-END
|
||||
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
#
|
||||
# Note that while production rules for both flow_sequence_entry and
|
||||
# flow_mapping_entry are equal, their interpretations are different.
|
||||
# For `flow_sequence_entry`, the part `KEY flow_node? (VALUE flow_node?)?`
|
||||
# generate an inline mapping (set syntax).
|
||||
|
||||
def parse_flow_sequence_first_entry(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_flow_sequence_entry(first=True)
|
||||
|
||||
def parse_flow_sequence_entry(self, first=False):
|
||||
if not self.check_token(FlowSequenceEndToken):
|
||||
if not first:
|
||||
if self.check_token(FlowEntryToken):
|
||||
self.get_token()
|
||||
else:
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a flow sequence", self.marks[-1],
|
||||
"expected ',' or ']', but got %r" % token.id, token.start_mark)
|
||||
|
||||
if self.check_token(KeyToken):
|
||||
token = self.peek_token()
|
||||
event = MappingStartEvent(None, None, True,
|
||||
token.start_mark, token.end_mark,
|
||||
flow_style=True)
|
||||
self.state = self.parse_flow_sequence_entry_mapping_key
|
||||
return event
|
||||
elif not self.check_token(FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry)
|
||||
return self.parse_flow_node()
|
||||
token = self.get_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_flow_sequence_entry_mapping_key(self):
|
||||
token = self.get_token()
|
||||
if not self.check_token(ValueToken,
|
||||
FlowEntryToken, FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry_mapping_value)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
|
||||
def parse_flow_sequence_entry_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(FlowEntryToken, FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry_mapping_end)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_end
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_end
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
def parse_flow_sequence_entry_mapping_end(self):
|
||||
self.state = self.parse_flow_sequence_entry
|
||||
token = self.peek_token()
|
||||
return MappingEndEvent(token.start_mark, token.start_mark)
|
||||
|
||||
# flow_mapping ::= FLOW-MAPPING-START
|
||||
# (flow_mapping_entry FLOW-ENTRY)*
|
||||
# flow_mapping_entry?
|
||||
# FLOW-MAPPING-END
|
||||
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
|
||||
def parse_flow_mapping_first_key(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_flow_mapping_key(first=True)
|
||||
|
||||
def parse_flow_mapping_key(self, first=False):
|
||||
if not self.check_token(FlowMappingEndToken):
|
||||
if not first:
|
||||
if self.check_token(FlowEntryToken):
|
||||
self.get_token()
|
||||
else:
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a flow mapping", self.marks[-1],
|
||||
"expected ',' or '}', but got %r" % token.id, token.start_mark)
|
||||
if self.check_token(KeyToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(ValueToken,
|
||||
FlowEntryToken, FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_value)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
elif not self.check_token(FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_empty_value)
|
||||
return self.parse_flow_node()
|
||||
token = self.get_token()
|
||||
event = MappingEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_flow_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(FlowEntryToken, FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_key)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_key
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_key
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
def parse_flow_mapping_empty_value(self):
|
||||
self.state = self.parse_flow_mapping_key
|
||||
return self.process_empty_scalar(self.peek_token().start_mark)
|
||||
|
||||
def process_empty_scalar(self, mark):
|
||||
return ScalarEvent(None, None, (True, False), u'', mark, mark)
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
# This module contains abstractions for the input stream. You don't have to
|
||||
# looks further, there are no pretty code.
|
||||
#
|
||||
# We define two classes here.
|
||||
#
|
||||
# Mark(source, line, column)
|
||||
# It's just a record and its only use is producing nice error messages.
|
||||
# Parser does not use it for any other purposes.
|
||||
#
|
||||
# Reader(source, data)
|
||||
# Reader determines the encoding of `data` and converts it to unicode.
|
||||
# Reader provides the following methods and attributes:
|
||||
# reader.peek(length=1) - return the next `length` characters
|
||||
# reader.forward(length=1) - move the current position to `length` characters.
|
||||
# reader.index - the number of the current character.
|
||||
# reader.line, stream.column - the line and the column of the current character.
|
||||
|
||||
__all__ = ['Reader', 'ReaderError']
|
||||
|
||||
from error import YAMLError, Mark
|
||||
|
||||
import codecs, re, sys
|
||||
|
||||
has_ucs4 = sys.maxunicode > 0xffff
|
||||
|
||||
class ReaderError(YAMLError):
|
||||
|
||||
def __init__(self, name, position, character, encoding, reason):
|
||||
self.name = name
|
||||
self.character = character
|
||||
self.position = position
|
||||
self.encoding = encoding
|
||||
self.reason = reason
|
||||
|
||||
def __str__(self):
|
||||
if isinstance(self.character, str):
|
||||
return "'%s' codec can't decode byte #x%02x: %s\n" \
|
||||
" in \"%s\", position %d" \
|
||||
% (self.encoding, ord(self.character), self.reason,
|
||||
self.name, self.position)
|
||||
else:
|
||||
return "unacceptable character #x%04x: %s\n" \
|
||||
" in \"%s\", position %d" \
|
||||
% (self.character, self.reason,
|
||||
self.name, self.position)
|
||||
|
||||
class Reader(object):
|
||||
# Reader:
|
||||
# - determines the data encoding and converts it to unicode,
|
||||
# - checks if characters are in allowed range,
|
||||
# - adds '\0' to the end.
|
||||
|
||||
# Reader accepts
|
||||
# - a `str` object,
|
||||
# - a `unicode` object,
|
||||
# - a file-like object with its `read` method returning `str`,
|
||||
# - a file-like object with its `read` method returning `unicode`.
|
||||
|
||||
# Yeah, it's ugly and slow.
|
||||
|
||||
def __init__(self, stream):
|
||||
self.name = None
|
||||
self.stream = None
|
||||
self.stream_pointer = 0
|
||||
self.eof = True
|
||||
self.buffer = u''
|
||||
self.pointer = 0
|
||||
self.raw_buffer = None
|
||||
self.raw_decode = None
|
||||
self.encoding = None
|
||||
self.index = 0
|
||||
self.line = 0
|
||||
self.column = 0
|
||||
if isinstance(stream, unicode):
|
||||
self.name = "<unicode string>"
|
||||
self.check_printable(stream)
|
||||
self.buffer = stream+u'\0'
|
||||
elif isinstance(stream, str):
|
||||
self.name = "<string>"
|
||||
self.raw_buffer = stream
|
||||
self.determine_encoding()
|
||||
else:
|
||||
self.stream = stream
|
||||
self.name = getattr(stream, 'name', "<file>")
|
||||
self.eof = False
|
||||
self.raw_buffer = ''
|
||||
self.determine_encoding()
|
||||
|
||||
def peek(self, index=0):
|
||||
try:
|
||||
return self.buffer[self.pointer+index]
|
||||
except IndexError:
|
||||
self.update(index+1)
|
||||
return self.buffer[self.pointer+index]
|
||||
|
||||
def prefix(self, length=1):
|
||||
if self.pointer+length >= len(self.buffer):
|
||||
self.update(length)
|
||||
return self.buffer[self.pointer:self.pointer+length]
|
||||
|
||||
def forward(self, length=1):
|
||||
if self.pointer+length+1 >= len(self.buffer):
|
||||
self.update(length+1)
|
||||
while length:
|
||||
ch = self.buffer[self.pointer]
|
||||
self.pointer += 1
|
||||
self.index += 1
|
||||
if ch in u'\n\x85\u2028\u2029' \
|
||||
or (ch == u'\r' and self.buffer[self.pointer] != u'\n'):
|
||||
self.line += 1
|
||||
self.column = 0
|
||||
elif ch != u'\uFEFF':
|
||||
self.column += 1
|
||||
length -= 1
|
||||
|
||||
def get_mark(self):
|
||||
if self.stream is None:
|
||||
return Mark(self.name, self.index, self.line, self.column,
|
||||
self.buffer, self.pointer)
|
||||
else:
|
||||
return Mark(self.name, self.index, self.line, self.column,
|
||||
None, None)
|
||||
|
||||
def determine_encoding(self):
|
||||
while not self.eof and len(self.raw_buffer) < 2:
|
||||
self.update_raw()
|
||||
if not isinstance(self.raw_buffer, unicode):
|
||||
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
|
||||
self.raw_decode = codecs.utf_16_le_decode
|
||||
self.encoding = 'utf-16-le'
|
||||
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
|
||||
self.raw_decode = codecs.utf_16_be_decode
|
||||
self.encoding = 'utf-16-be'
|
||||
else:
|
||||
self.raw_decode = codecs.utf_8_decode
|
||||
self.encoding = 'utf-8'
|
||||
self.update(1)
|
||||
|
||||
if has_ucs4:
|
||||
NON_PRINTABLE = re.compile(u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010ffff]')
|
||||
else:
|
||||
NON_PRINTABLE = re.compile(u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uFFFD]|(?:^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?:[^\uDC00-\uDFFF]|$)')
|
||||
def check_printable(self, data):
|
||||
match = self.NON_PRINTABLE.search(data)
|
||||
if match:
|
||||
character = match.group()
|
||||
position = self.index+(len(self.buffer)-self.pointer)+match.start()
|
||||
raise ReaderError(self.name, position, ord(character),
|
||||
'unicode', "special characters are not allowed")
|
||||
|
||||
def update(self, length):
|
||||
if self.raw_buffer is None:
|
||||
return
|
||||
self.buffer = self.buffer[self.pointer:]
|
||||
self.pointer = 0
|
||||
while len(self.buffer) < length:
|
||||
if not self.eof:
|
||||
self.update_raw()
|
||||
if self.raw_decode is not None:
|
||||
try:
|
||||
data, converted = self.raw_decode(self.raw_buffer,
|
||||
'strict', self.eof)
|
||||
except UnicodeDecodeError, exc:
|
||||
character = exc.object[exc.start]
|
||||
if self.stream is not None:
|
||||
position = self.stream_pointer-len(self.raw_buffer)+exc.start
|
||||
else:
|
||||
position = exc.start
|
||||
raise ReaderError(self.name, position, character,
|
||||
exc.encoding, exc.reason)
|
||||
else:
|
||||
data = self.raw_buffer
|
||||
converted = len(data)
|
||||
self.check_printable(data)
|
||||
self.buffer += data
|
||||
self.raw_buffer = self.raw_buffer[converted:]
|
||||
if self.eof:
|
||||
self.buffer += u'\0'
|
||||
self.raw_buffer = None
|
||||
break
|
||||
|
||||
def update_raw(self, size=1024):
|
||||
data = self.stream.read(size)
|
||||
if data:
|
||||
self.raw_buffer += data
|
||||
self.stream_pointer += len(data)
|
||||
else:
|
||||
self.eof = True
|
||||
@@ -0,0 +1,489 @@
|
||||
|
||||
__all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
|
||||
'RepresenterError']
|
||||
|
||||
from error import *
|
||||
|
||||
from nodes import *
|
||||
|
||||
import datetime
|
||||
|
||||
import copy_reg, types
|
||||
|
||||
class RepresenterError(YAMLError):
|
||||
pass
|
||||
|
||||
class BaseRepresenter(object):
|
||||
|
||||
yaml_representers = {}
|
||||
yaml_multi_representers = {}
|
||||
|
||||
def __init__(self, default_style=None, default_flow_style=False, sort_keys=True):
|
||||
self.default_style = default_style
|
||||
self.default_flow_style = default_flow_style
|
||||
self.sort_keys = sort_keys
|
||||
self.represented_objects = {}
|
||||
self.object_keeper = []
|
||||
self.alias_key = None
|
||||
|
||||
def represent(self, data):
|
||||
node = self.represent_data(data)
|
||||
self.serialize(node)
|
||||
self.represented_objects = {}
|
||||
self.object_keeper = []
|
||||
self.alias_key = None
|
||||
|
||||
def get_classobj_bases(self, cls):
|
||||
bases = [cls]
|
||||
for base in cls.__bases__:
|
||||
bases.extend(self.get_classobj_bases(base))
|
||||
return bases
|
||||
|
||||
def represent_data(self, data):
|
||||
if self.ignore_aliases(data):
|
||||
self.alias_key = None
|
||||
else:
|
||||
self.alias_key = id(data)
|
||||
if self.alias_key is not None:
|
||||
if self.alias_key in self.represented_objects:
|
||||
node = self.represented_objects[self.alias_key]
|
||||
#if node is None:
|
||||
# raise RepresenterError("recursive objects are not allowed: %r" % data)
|
||||
return node
|
||||
#self.represented_objects[alias_key] = None
|
||||
self.object_keeper.append(data)
|
||||
data_types = type(data).__mro__
|
||||
if type(data) is types.InstanceType:
|
||||
data_types = self.get_classobj_bases(data.__class__)+list(data_types)
|
||||
if data_types[0] in self.yaml_representers:
|
||||
node = self.yaml_representers[data_types[0]](self, data)
|
||||
else:
|
||||
for data_type in data_types:
|
||||
if data_type in self.yaml_multi_representers:
|
||||
node = self.yaml_multi_representers[data_type](self, data)
|
||||
break
|
||||
else:
|
||||
if None in self.yaml_multi_representers:
|
||||
node = self.yaml_multi_representers[None](self, data)
|
||||
elif None in self.yaml_representers:
|
||||
node = self.yaml_representers[None](self, data)
|
||||
else:
|
||||
node = ScalarNode(None, unicode(data))
|
||||
#if alias_key is not None:
|
||||
# self.represented_objects[alias_key] = node
|
||||
return node
|
||||
|
||||
def add_representer(cls, data_type, representer):
|
||||
if not 'yaml_representers' in cls.__dict__:
|
||||
cls.yaml_representers = cls.yaml_representers.copy()
|
||||
cls.yaml_representers[data_type] = representer
|
||||
add_representer = classmethod(add_representer)
|
||||
|
||||
def add_multi_representer(cls, data_type, representer):
|
||||
if not 'yaml_multi_representers' in cls.__dict__:
|
||||
cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
|
||||
cls.yaml_multi_representers[data_type] = representer
|
||||
add_multi_representer = classmethod(add_multi_representer)
|
||||
|
||||
def represent_scalar(self, tag, value, style=None):
|
||||
if style is None:
|
||||
style = self.default_style
|
||||
node = ScalarNode(tag, value, style=style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
return node
|
||||
|
||||
def represent_sequence(self, tag, sequence, flow_style=None):
|
||||
value = []
|
||||
node = SequenceNode(tag, value, flow_style=flow_style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
best_style = True
|
||||
for item in sequence:
|
||||
node_item = self.represent_data(item)
|
||||
if not (isinstance(node_item, ScalarNode) and not node_item.style):
|
||||
best_style = False
|
||||
value.append(node_item)
|
||||
if flow_style is None:
|
||||
if self.default_flow_style is not None:
|
||||
node.flow_style = self.default_flow_style
|
||||
else:
|
||||
node.flow_style = best_style
|
||||
return node
|
||||
|
||||
def represent_mapping(self, tag, mapping, flow_style=None):
|
||||
value = []
|
||||
node = MappingNode(tag, value, flow_style=flow_style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
best_style = True
|
||||
if hasattr(mapping, 'items'):
|
||||
mapping = mapping.items()
|
||||
if self.sort_keys:
|
||||
mapping.sort()
|
||||
for item_key, item_value in mapping:
|
||||
node_key = self.represent_data(item_key)
|
||||
node_value = self.represent_data(item_value)
|
||||
if not (isinstance(node_key, ScalarNode) and not node_key.style):
|
||||
best_style = False
|
||||
if not (isinstance(node_value, ScalarNode) and not node_value.style):
|
||||
best_style = False
|
||||
value.append((node_key, node_value))
|
||||
if flow_style is None:
|
||||
if self.default_flow_style is not None:
|
||||
node.flow_style = self.default_flow_style
|
||||
else:
|
||||
node.flow_style = best_style
|
||||
return node
|
||||
|
||||
def ignore_aliases(self, data):
|
||||
return False
|
||||
|
||||
class SafeRepresenter(BaseRepresenter):
|
||||
|
||||
def ignore_aliases(self, data):
|
||||
if data is None:
|
||||
return True
|
||||
if isinstance(data, tuple) and data == ():
|
||||
return True
|
||||
if isinstance(data, (str, unicode, bool, int, float)):
|
||||
return True
|
||||
|
||||
def represent_none(self, data):
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:null',
|
||||
u'null')
|
||||
|
||||
def represent_str(self, data):
|
||||
tag = None
|
||||
style = None
|
||||
try:
|
||||
data = unicode(data, 'ascii')
|
||||
tag = u'tag:yaml.org,2002:str'
|
||||
except UnicodeDecodeError:
|
||||
try:
|
||||
data = unicode(data, 'utf-8')
|
||||
tag = u'tag:yaml.org,2002:str'
|
||||
except UnicodeDecodeError:
|
||||
data = data.encode('base64')
|
||||
tag = u'tag:yaml.org,2002:binary'
|
||||
style = '|'
|
||||
return self.represent_scalar(tag, data, style=style)
|
||||
|
||||
def represent_unicode(self, data):
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:str', data)
|
||||
|
||||
def represent_bool(self, data):
|
||||
if data:
|
||||
value = u'true'
|
||||
else:
|
||||
value = u'false'
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
|
||||
|
||||
def represent_int(self, data):
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
|
||||
|
||||
def represent_long(self, data):
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
|
||||
|
||||
inf_value = 1e300
|
||||
while repr(inf_value) != repr(inf_value*inf_value):
|
||||
inf_value *= inf_value
|
||||
|
||||
def represent_float(self, data):
|
||||
if data != data or (data == 0.0 and data == 1.0):
|
||||
value = u'.nan'
|
||||
elif data == self.inf_value:
|
||||
value = u'.inf'
|
||||
elif data == -self.inf_value:
|
||||
value = u'-.inf'
|
||||
else:
|
||||
value = unicode(repr(data)).lower()
|
||||
# Note that in some cases `repr(data)` represents a float number
|
||||
# without the decimal parts. For instance:
|
||||
# >>> repr(1e17)
|
||||
# '1e17'
|
||||
# Unfortunately, this is not a valid float representation according
|
||||
# to the definition of the `!!float` tag. We fix this by adding
|
||||
# '.0' before the 'e' symbol.
|
||||
if u'.' not in value and u'e' in value:
|
||||
value = value.replace(u'e', u'.0e', 1)
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:float', value)
|
||||
|
||||
def represent_list(self, data):
|
||||
#pairs = (len(data) > 0 and isinstance(data, list))
|
||||
#if pairs:
|
||||
# for item in data:
|
||||
# if not isinstance(item, tuple) or len(item) != 2:
|
||||
# pairs = False
|
||||
# break
|
||||
#if not pairs:
|
||||
return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
|
||||
#value = []
|
||||
#for item_key, item_value in data:
|
||||
# value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
|
||||
# [(item_key, item_value)]))
|
||||
#return SequenceNode(u'tag:yaml.org,2002:pairs', value)
|
||||
|
||||
def represent_dict(self, data):
|
||||
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
|
||||
|
||||
def represent_set(self, data):
|
||||
value = {}
|
||||
for key in data:
|
||||
value[key] = None
|
||||
return self.represent_mapping(u'tag:yaml.org,2002:set', value)
|
||||
|
||||
def represent_date(self, data):
|
||||
value = unicode(data.isoformat())
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
|
||||
|
||||
def represent_datetime(self, data):
|
||||
value = unicode(data.isoformat(' '))
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
|
||||
|
||||
def represent_yaml_object(self, tag, data, cls, flow_style=None):
|
||||
if hasattr(data, '__getstate__'):
|
||||
state = data.__getstate__()
|
||||
else:
|
||||
state = data.__dict__.copy()
|
||||
return self.represent_mapping(tag, state, flow_style=flow_style)
|
||||
|
||||
def represent_undefined(self, data):
|
||||
raise RepresenterError("cannot represent an object", data)
|
||||
|
||||
SafeRepresenter.add_representer(type(None),
|
||||
SafeRepresenter.represent_none)
|
||||
|
||||
SafeRepresenter.add_representer(str,
|
||||
SafeRepresenter.represent_str)
|
||||
|
||||
SafeRepresenter.add_representer(unicode,
|
||||
SafeRepresenter.represent_unicode)
|
||||
|
||||
SafeRepresenter.add_representer(bool,
|
||||
SafeRepresenter.represent_bool)
|
||||
|
||||
SafeRepresenter.add_representer(int,
|
||||
SafeRepresenter.represent_int)
|
||||
|
||||
SafeRepresenter.add_representer(long,
|
||||
SafeRepresenter.represent_long)
|
||||
|
||||
SafeRepresenter.add_representer(float,
|
||||
SafeRepresenter.represent_float)
|
||||
|
||||
SafeRepresenter.add_representer(list,
|
||||
SafeRepresenter.represent_list)
|
||||
|
||||
SafeRepresenter.add_representer(tuple,
|
||||
SafeRepresenter.represent_list)
|
||||
|
||||
SafeRepresenter.add_representer(dict,
|
||||
SafeRepresenter.represent_dict)
|
||||
|
||||
SafeRepresenter.add_representer(set,
|
||||
SafeRepresenter.represent_set)
|
||||
|
||||
SafeRepresenter.add_representer(datetime.date,
|
||||
SafeRepresenter.represent_date)
|
||||
|
||||
SafeRepresenter.add_representer(datetime.datetime,
|
||||
SafeRepresenter.represent_datetime)
|
||||
|
||||
SafeRepresenter.add_representer(None,
|
||||
SafeRepresenter.represent_undefined)
|
||||
|
||||
class Representer(SafeRepresenter):
|
||||
|
||||
def represent_str(self, data):
|
||||
tag = None
|
||||
style = None
|
||||
try:
|
||||
data = unicode(data, 'ascii')
|
||||
tag = u'tag:yaml.org,2002:str'
|
||||
except UnicodeDecodeError:
|
||||
try:
|
||||
data = unicode(data, 'utf-8')
|
||||
tag = u'tag:yaml.org,2002:python/str'
|
||||
except UnicodeDecodeError:
|
||||
data = data.encode('base64')
|
||||
tag = u'tag:yaml.org,2002:binary'
|
||||
style = '|'
|
||||
return self.represent_scalar(tag, data, style=style)
|
||||
|
||||
def represent_unicode(self, data):
|
||||
tag = None
|
||||
try:
|
||||
data.encode('ascii')
|
||||
tag = u'tag:yaml.org,2002:python/unicode'
|
||||
except UnicodeEncodeError:
|
||||
tag = u'tag:yaml.org,2002:str'
|
||||
return self.represent_scalar(tag, data)
|
||||
|
||||
def represent_long(self, data):
|
||||
tag = u'tag:yaml.org,2002:int'
|
||||
if int(data) is not data:
|
||||
tag = u'tag:yaml.org,2002:python/long'
|
||||
return self.represent_scalar(tag, unicode(data))
|
||||
|
||||
def represent_complex(self, data):
|
||||
if data.imag == 0.0:
|
||||
data = u'%r' % data.real
|
||||
elif data.real == 0.0:
|
||||
data = u'%rj' % data.imag
|
||||
elif data.imag > 0:
|
||||
data = u'%r+%rj' % (data.real, data.imag)
|
||||
else:
|
||||
data = u'%r%rj' % (data.real, data.imag)
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
|
||||
|
||||
def represent_tuple(self, data):
|
||||
return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
|
||||
|
||||
def represent_name(self, data):
|
||||
name = u'%s.%s' % (data.__module__, data.__name__)
|
||||
return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'')
|
||||
|
||||
def represent_module(self, data):
|
||||
return self.represent_scalar(
|
||||
u'tag:yaml.org,2002:python/module:'+data.__name__, u'')
|
||||
|
||||
def represent_instance(self, data):
|
||||
# For instances of classic classes, we use __getinitargs__ and
|
||||
# __getstate__ to serialize the data.
|
||||
|
||||
# If data.__getinitargs__ exists, the object must be reconstructed by
|
||||
# calling cls(**args), where args is a tuple returned by
|
||||
# __getinitargs__. Otherwise, the cls.__init__ method should never be
|
||||
# called and the class instance is created by instantiating a trivial
|
||||
# class and assigning to the instance's __class__ variable.
|
||||
|
||||
# If data.__getstate__ exists, it returns the state of the object.
|
||||
# Otherwise, the state of the object is data.__dict__.
|
||||
|
||||
# We produce either a !!python/object or !!python/object/new node.
|
||||
# If data.__getinitargs__ does not exist and state is a dictionary, we
|
||||
# produce a !!python/object node . Otherwise we produce a
|
||||
# !!python/object/new node.
|
||||
|
||||
cls = data.__class__
|
||||
class_name = u'%s.%s' % (cls.__module__, cls.__name__)
|
||||
args = None
|
||||
state = None
|
||||
if hasattr(data, '__getinitargs__'):
|
||||
args = list(data.__getinitargs__())
|
||||
if hasattr(data, '__getstate__'):
|
||||
state = data.__getstate__()
|
||||
else:
|
||||
state = data.__dict__
|
||||
if args is None and isinstance(state, dict):
|
||||
return self.represent_mapping(
|
||||
u'tag:yaml.org,2002:python/object:'+class_name, state)
|
||||
if isinstance(state, dict) and not state:
|
||||
return self.represent_sequence(
|
||||
u'tag:yaml.org,2002:python/object/new:'+class_name, args)
|
||||
value = {}
|
||||
if args:
|
||||
value['args'] = args
|
||||
value['state'] = state
|
||||
return self.represent_mapping(
|
||||
u'tag:yaml.org,2002:python/object/new:'+class_name, value)
|
||||
|
||||
def represent_object(self, data):
|
||||
# We use __reduce__ API to save the data. data.__reduce__ returns
|
||||
# a tuple of length 2-5:
|
||||
# (function, args, state, listitems, dictitems)
|
||||
|
||||
# For reconstructing, we calls function(*args), then set its state,
|
||||
# listitems, and dictitems if they are not None.
|
||||
|
||||
# A special case is when function.__name__ == '__newobj__'. In this
|
||||
# case we create the object with args[0].__new__(*args).
|
||||
|
||||
# Another special case is when __reduce__ returns a string - we don't
|
||||
# support it.
|
||||
|
||||
# We produce a !!python/object, !!python/object/new or
|
||||
# !!python/object/apply node.
|
||||
|
||||
cls = type(data)
|
||||
if cls in copy_reg.dispatch_table:
|
||||
reduce = copy_reg.dispatch_table[cls](data)
|
||||
elif hasattr(data, '__reduce_ex__'):
|
||||
reduce = data.__reduce_ex__(2)
|
||||
elif hasattr(data, '__reduce__'):
|
||||
reduce = data.__reduce__()
|
||||
else:
|
||||
raise RepresenterError("cannot represent an object", data)
|
||||
reduce = (list(reduce)+[None]*5)[:5]
|
||||
function, args, state, listitems, dictitems = reduce
|
||||
args = list(args)
|
||||
if state is None:
|
||||
state = {}
|
||||
if listitems is not None:
|
||||
listitems = list(listitems)
|
||||
if dictitems is not None:
|
||||
dictitems = dict(dictitems)
|
||||
if function.__name__ == '__newobj__':
|
||||
function = args[0]
|
||||
args = args[1:]
|
||||
tag = u'tag:yaml.org,2002:python/object/new:'
|
||||
newobj = True
|
||||
else:
|
||||
tag = u'tag:yaml.org,2002:python/object/apply:'
|
||||
newobj = False
|
||||
function_name = u'%s.%s' % (function.__module__, function.__name__)
|
||||
if not args and not listitems and not dictitems \
|
||||
and isinstance(state, dict) and newobj:
|
||||
return self.represent_mapping(
|
||||
u'tag:yaml.org,2002:python/object:'+function_name, state)
|
||||
if not listitems and not dictitems \
|
||||
and isinstance(state, dict) and not state:
|
||||
return self.represent_sequence(tag+function_name, args)
|
||||
value = {}
|
||||
if args:
|
||||
value['args'] = args
|
||||
if state or not isinstance(state, dict):
|
||||
value['state'] = state
|
||||
if listitems:
|
||||
value['listitems'] = listitems
|
||||
if dictitems:
|
||||
value['dictitems'] = dictitems
|
||||
return self.represent_mapping(tag+function_name, value)
|
||||
|
||||
Representer.add_representer(str,
|
||||
Representer.represent_str)
|
||||
|
||||
Representer.add_representer(unicode,
|
||||
Representer.represent_unicode)
|
||||
|
||||
Representer.add_representer(long,
|
||||
Representer.represent_long)
|
||||
|
||||
Representer.add_representer(complex,
|
||||
Representer.represent_complex)
|
||||
|
||||
Representer.add_representer(tuple,
|
||||
Representer.represent_tuple)
|
||||
|
||||
Representer.add_representer(type,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.ClassType,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.FunctionType,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.BuiltinFunctionType,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.ModuleType,
|
||||
Representer.represent_module)
|
||||
|
||||
Representer.add_multi_representer(types.InstanceType,
|
||||
Representer.represent_instance)
|
||||
|
||||
Representer.add_multi_representer(object,
|
||||
Representer.represent_object)
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
|
||||
__all__ = ['BaseResolver', 'Resolver']
|
||||
|
||||
from error import *
|
||||
from nodes import *
|
||||
|
||||
import re
|
||||
|
||||
class ResolverError(YAMLError):
|
||||
pass
|
||||
|
||||
class BaseResolver(object):
|
||||
|
||||
DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
|
||||
DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'
|
||||
DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
|
||||
|
||||
yaml_implicit_resolvers = {}
|
||||
yaml_path_resolvers = {}
|
||||
|
||||
def __init__(self):
|
||||
self.resolver_exact_paths = []
|
||||
self.resolver_prefix_paths = []
|
||||
|
||||
def add_implicit_resolver(cls, tag, regexp, first):
|
||||
if not 'yaml_implicit_resolvers' in cls.__dict__:
|
||||
implicit_resolvers = {}
|
||||
for key in cls.yaml_implicit_resolvers:
|
||||
implicit_resolvers[key] = cls.yaml_implicit_resolvers[key][:]
|
||||
cls.yaml_implicit_resolvers = implicit_resolvers
|
||||
if first is None:
|
||||
first = [None]
|
||||
for ch in first:
|
||||
cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
|
||||
add_implicit_resolver = classmethod(add_implicit_resolver)
|
||||
|
||||
def add_path_resolver(cls, tag, path, kind=None):
|
||||
# Note: `add_path_resolver` is experimental. The API could be changed.
|
||||
# `new_path` is a pattern that is matched against the path from the
|
||||
# root to the node that is being considered. `node_path` elements are
|
||||
# tuples `(node_check, index_check)`. `node_check` is a node class:
|
||||
# `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
|
||||
# matches any kind of a node. `index_check` could be `None`, a boolean
|
||||
# value, a string value, or a number. `None` and `False` match against
|
||||
# any _value_ of sequence and mapping nodes. `True` matches against
|
||||
# any _key_ of a mapping node. A string `index_check` matches against
|
||||
# a mapping value that corresponds to a scalar key which content is
|
||||
# equal to the `index_check` value. An integer `index_check` matches
|
||||
# against a sequence value with the index equal to `index_check`.
|
||||
if not 'yaml_path_resolvers' in cls.__dict__:
|
||||
cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
|
||||
new_path = []
|
||||
for element in path:
|
||||
if isinstance(element, (list, tuple)):
|
||||
if len(element) == 2:
|
||||
node_check, index_check = element
|
||||
elif len(element) == 1:
|
||||
node_check = element[0]
|
||||
index_check = True
|
||||
else:
|
||||
raise ResolverError("Invalid path element: %s" % element)
|
||||
else:
|
||||
node_check = None
|
||||
index_check = element
|
||||
if node_check is str:
|
||||
node_check = ScalarNode
|
||||
elif node_check is list:
|
||||
node_check = SequenceNode
|
||||
elif node_check is dict:
|
||||
node_check = MappingNode
|
||||
elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
|
||||
and not isinstance(node_check, basestring) \
|
||||
and node_check is not None:
|
||||
raise ResolverError("Invalid node checker: %s" % node_check)
|
||||
if not isinstance(index_check, (basestring, int)) \
|
||||
and index_check is not None:
|
||||
raise ResolverError("Invalid index checker: %s" % index_check)
|
||||
new_path.append((node_check, index_check))
|
||||
if kind is str:
|
||||
kind = ScalarNode
|
||||
elif kind is list:
|
||||
kind = SequenceNode
|
||||
elif kind is dict:
|
||||
kind = MappingNode
|
||||
elif kind not in [ScalarNode, SequenceNode, MappingNode] \
|
||||
and kind is not None:
|
||||
raise ResolverError("Invalid node kind: %s" % kind)
|
||||
cls.yaml_path_resolvers[tuple(new_path), kind] = tag
|
||||
add_path_resolver = classmethod(add_path_resolver)
|
||||
|
||||
def descend_resolver(self, current_node, current_index):
|
||||
if not self.yaml_path_resolvers:
|
||||
return
|
||||
exact_paths = {}
|
||||
prefix_paths = []
|
||||
if current_node:
|
||||
depth = len(self.resolver_prefix_paths)
|
||||
for path, kind in self.resolver_prefix_paths[-1]:
|
||||
if self.check_resolver_prefix(depth, path, kind,
|
||||
current_node, current_index):
|
||||
if len(path) > depth:
|
||||
prefix_paths.append((path, kind))
|
||||
else:
|
||||
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
|
||||
else:
|
||||
for path, kind in self.yaml_path_resolvers:
|
||||
if not path:
|
||||
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
|
||||
else:
|
||||
prefix_paths.append((path, kind))
|
||||
self.resolver_exact_paths.append(exact_paths)
|
||||
self.resolver_prefix_paths.append(prefix_paths)
|
||||
|
||||
def ascend_resolver(self):
|
||||
if not self.yaml_path_resolvers:
|
||||
return
|
||||
self.resolver_exact_paths.pop()
|
||||
self.resolver_prefix_paths.pop()
|
||||
|
||||
def check_resolver_prefix(self, depth, path, kind,
|
||||
current_node, current_index):
|
||||
node_check, index_check = path[depth-1]
|
||||
if isinstance(node_check, basestring):
|
||||
if current_node.tag != node_check:
|
||||
return
|
||||
elif node_check is not None:
|
||||
if not isinstance(current_node, node_check):
|
||||
return
|
||||
if index_check is True and current_index is not None:
|
||||
return
|
||||
if (index_check is False or index_check is None) \
|
||||
and current_index is None:
|
||||
return
|
||||
if isinstance(index_check, basestring):
|
||||
if not (isinstance(current_index, ScalarNode)
|
||||
and index_check == current_index.value):
|
||||
return
|
||||
elif isinstance(index_check, int) and not isinstance(index_check, bool):
|
||||
if index_check != current_index:
|
||||
return
|
||||
return True
|
||||
|
||||
def resolve(self, kind, value, implicit):
|
||||
if kind is ScalarNode and implicit[0]:
|
||||
if value == u'':
|
||||
resolvers = self.yaml_implicit_resolvers.get(u'', [])
|
||||
else:
|
||||
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
|
||||
resolvers += self.yaml_implicit_resolvers.get(None, [])
|
||||
for tag, regexp in resolvers:
|
||||
if regexp.match(value):
|
||||
return tag
|
||||
implicit = implicit[1]
|
||||
if self.yaml_path_resolvers:
|
||||
exact_paths = self.resolver_exact_paths[-1]
|
||||
if kind in exact_paths:
|
||||
return exact_paths[kind]
|
||||
if None in exact_paths:
|
||||
return exact_paths[None]
|
||||
if kind is ScalarNode:
|
||||
return self.DEFAULT_SCALAR_TAG
|
||||
elif kind is SequenceNode:
|
||||
return self.DEFAULT_SEQUENCE_TAG
|
||||
elif kind is MappingNode:
|
||||
return self.DEFAULT_MAPPING_TAG
|
||||
|
||||
class Resolver(BaseResolver):
|
||||
pass
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:bool',
|
||||
re.compile(ur'''^(?:yes|Yes|YES|no|No|NO
|
||||
|true|True|TRUE|false|False|FALSE
|
||||
|on|On|ON|off|Off|OFF)$''', re.X),
|
||||
list(u'yYnNtTfFoO'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:float',
|
||||
re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?
|
||||
|\.[0-9_]+(?:[eE][-+][0-9]+)?
|
||||
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
|
||||
|[-+]?\.(?:inf|Inf|INF)
|
||||
|\.(?:nan|NaN|NAN))$''', re.X),
|
||||
list(u'-+0123456789.'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:int',
|
||||
re.compile(ur'''^(?:[-+]?0b[0-1_]+
|
||||
|[-+]?0[0-7_]+
|
||||
|[-+]?(?:0|[1-9][0-9_]*)
|
||||
|[-+]?0x[0-9a-fA-F_]+
|
||||
|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
|
||||
list(u'-+0123456789'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:merge',
|
||||
re.compile(ur'^(?:<<)$'),
|
||||
[u'<'])
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:null',
|
||||
re.compile(ur'''^(?: ~
|
||||
|null|Null|NULL
|
||||
| )$''', re.X),
|
||||
[u'~', u'n', u'N', u''])
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:timestamp',
|
||||
re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
|
||||
|[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
|
||||
(?:[Tt]|[ \t]+)[0-9][0-9]?
|
||||
:[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
|
||||
(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
|
||||
list(u'0123456789'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:value',
|
||||
re.compile(ur'^(?:=)$'),
|
||||
[u'='])
|
||||
|
||||
# The following resolver is only for documentation purposes. It cannot work
|
||||
# because plain scalars cannot start with '!', '&', or '*'.
|
||||
Resolver.add_implicit_resolver(
|
||||
u'tag:yaml.org,2002:yaml',
|
||||
re.compile(ur'^(?:!|&|\*)$'),
|
||||
list(u'!&*'))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,111 @@
|
||||
|
||||
__all__ = ['Serializer', 'SerializerError']
|
||||
|
||||
from error import YAMLError
|
||||
from events import *
|
||||
from nodes import *
|
||||
|
||||
class SerializerError(YAMLError):
|
||||
pass
|
||||
|
||||
class Serializer(object):
|
||||
|
||||
ANCHOR_TEMPLATE = u'id%03d'
|
||||
|
||||
def __init__(self, encoding=None,
|
||||
explicit_start=None, explicit_end=None, version=None, tags=None):
|
||||
self.use_encoding = encoding
|
||||
self.use_explicit_start = explicit_start
|
||||
self.use_explicit_end = explicit_end
|
||||
self.use_version = version
|
||||
self.use_tags = tags
|
||||
self.serialized_nodes = {}
|
||||
self.anchors = {}
|
||||
self.last_anchor_id = 0
|
||||
self.closed = None
|
||||
|
||||
def open(self):
|
||||
if self.closed is None:
|
||||
self.emit(StreamStartEvent(encoding=self.use_encoding))
|
||||
self.closed = False
|
||||
elif self.closed:
|
||||
raise SerializerError("serializer is closed")
|
||||
else:
|
||||
raise SerializerError("serializer is already opened")
|
||||
|
||||
def close(self):
|
||||
if self.closed is None:
|
||||
raise SerializerError("serializer is not opened")
|
||||
elif not self.closed:
|
||||
self.emit(StreamEndEvent())
|
||||
self.closed = True
|
||||
|
||||
#def __del__(self):
|
||||
# self.close()
|
||||
|
||||
def serialize(self, node):
|
||||
if self.closed is None:
|
||||
raise SerializerError("serializer is not opened")
|
||||
elif self.closed:
|
||||
raise SerializerError("serializer is closed")
|
||||
self.emit(DocumentStartEvent(explicit=self.use_explicit_start,
|
||||
version=self.use_version, tags=self.use_tags))
|
||||
self.anchor_node(node)
|
||||
self.serialize_node(node, None, None)
|
||||
self.emit(DocumentEndEvent(explicit=self.use_explicit_end))
|
||||
self.serialized_nodes = {}
|
||||
self.anchors = {}
|
||||
self.last_anchor_id = 0
|
||||
|
||||
def anchor_node(self, node):
|
||||
if node in self.anchors:
|
||||
if self.anchors[node] is None:
|
||||
self.anchors[node] = self.generate_anchor(node)
|
||||
else:
|
||||
self.anchors[node] = None
|
||||
if isinstance(node, SequenceNode):
|
||||
for item in node.value:
|
||||
self.anchor_node(item)
|
||||
elif isinstance(node, MappingNode):
|
||||
for key, value in node.value:
|
||||
self.anchor_node(key)
|
||||
self.anchor_node(value)
|
||||
|
||||
def generate_anchor(self, node):
|
||||
self.last_anchor_id += 1
|
||||
return self.ANCHOR_TEMPLATE % self.last_anchor_id
|
||||
|
||||
def serialize_node(self, node, parent, index):
|
||||
alias = self.anchors[node]
|
||||
if node in self.serialized_nodes:
|
||||
self.emit(AliasEvent(alias))
|
||||
else:
|
||||
self.serialized_nodes[node] = True
|
||||
self.descend_resolver(parent, index)
|
||||
if isinstance(node, ScalarNode):
|
||||
detected_tag = self.resolve(ScalarNode, node.value, (True, False))
|
||||
default_tag = self.resolve(ScalarNode, node.value, (False, True))
|
||||
implicit = (node.tag == detected_tag), (node.tag == default_tag)
|
||||
self.emit(ScalarEvent(alias, node.tag, implicit, node.value,
|
||||
style=node.style))
|
||||
elif isinstance(node, SequenceNode):
|
||||
implicit = (node.tag
|
||||
== self.resolve(SequenceNode, node.value, True))
|
||||
self.emit(SequenceStartEvent(alias, node.tag, implicit,
|
||||
flow_style=node.flow_style))
|
||||
index = 0
|
||||
for item in node.value:
|
||||
self.serialize_node(item, node, index)
|
||||
index += 1
|
||||
self.emit(SequenceEndEvent())
|
||||
elif isinstance(node, MappingNode):
|
||||
implicit = (node.tag
|
||||
== self.resolve(MappingNode, node.value, True))
|
||||
self.emit(MappingStartEvent(alias, node.tag, implicit,
|
||||
flow_style=node.flow_style))
|
||||
for key, value in node.value:
|
||||
self.serialize_node(key, node, None)
|
||||
self.serialize_node(value, node, key)
|
||||
self.emit(MappingEndEvent())
|
||||
self.ascend_resolver()
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
|
||||
class Token(object):
|
||||
def __init__(self, start_mark, end_mark):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
attributes = [key for key in self.__dict__
|
||||
if not key.endswith('_mark')]
|
||||
attributes.sort()
|
||||
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
#class BOMToken(Token):
|
||||
# id = '<byte order mark>'
|
||||
|
||||
class DirectiveToken(Token):
|
||||
id = '<directive>'
|
||||
def __init__(self, name, value, start_mark, end_mark):
|
||||
self.name = name
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class DocumentStartToken(Token):
|
||||
id = '<document start>'
|
||||
|
||||
class DocumentEndToken(Token):
|
||||
id = '<document end>'
|
||||
|
||||
class StreamStartToken(Token):
|
||||
id = '<stream start>'
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
class StreamEndToken(Token):
|
||||
id = '<stream end>'
|
||||
|
||||
class BlockSequenceStartToken(Token):
|
||||
id = '<block sequence start>'
|
||||
|
||||
class BlockMappingStartToken(Token):
|
||||
id = '<block mapping start>'
|
||||
|
||||
class BlockEndToken(Token):
|
||||
id = '<block end>'
|
||||
|
||||
class FlowSequenceStartToken(Token):
|
||||
id = '['
|
||||
|
||||
class FlowMappingStartToken(Token):
|
||||
id = '{'
|
||||
|
||||
class FlowSequenceEndToken(Token):
|
||||
id = ']'
|
||||
|
||||
class FlowMappingEndToken(Token):
|
||||
id = '}'
|
||||
|
||||
class KeyToken(Token):
|
||||
id = '?'
|
||||
|
||||
class ValueToken(Token):
|
||||
id = ':'
|
||||
|
||||
class BlockEntryToken(Token):
|
||||
id = '-'
|
||||
|
||||
class FlowEntryToken(Token):
|
||||
id = ','
|
||||
|
||||
class AliasToken(Token):
|
||||
id = '<alias>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class AnchorToken(Token):
|
||||
id = '<anchor>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class TagToken(Token):
|
||||
id = '<tag>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class ScalarToken(Token):
|
||||
id = '<scalar>'
|
||||
def __init__(self, value, plain, start_mark, end_mark, style=None):
|
||||
self.value = value
|
||||
self.plain = plain
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2017-2020 Ingy döt Net
|
||||
Copyright (c) 2006-2016 Kirill Simonov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,427 @@
|
||||
|
||||
from .error import *
|
||||
|
||||
from .tokens import *
|
||||
from .events import *
|
||||
from .nodes import *
|
||||
|
||||
from .loader import *
|
||||
from .dumper import *
|
||||
|
||||
__version__ = '5.3.1'
|
||||
try:
|
||||
from .cyaml import *
|
||||
__with_libyaml__ = True
|
||||
except ImportError:
|
||||
__with_libyaml__ = False
|
||||
|
||||
import io
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# Warnings control
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
# 'Global' warnings state:
|
||||
_warnings_enabled = {
|
||||
'YAMLLoadWarning': True,
|
||||
}
|
||||
|
||||
# Get or set global warnings' state
|
||||
def warnings(settings=None):
|
||||
if settings is None:
|
||||
return _warnings_enabled
|
||||
|
||||
if type(settings) is dict:
|
||||
for key in settings:
|
||||
if key in _warnings_enabled:
|
||||
_warnings_enabled[key] = settings[key]
|
||||
|
||||
# Warn when load() is called without Loader=...
|
||||
class YAMLLoadWarning(RuntimeWarning):
|
||||
pass
|
||||
|
||||
def load_warning(method):
|
||||
if _warnings_enabled['YAMLLoadWarning'] is False:
|
||||
return
|
||||
|
||||
import warnings
|
||||
|
||||
message = (
|
||||
"calling yaml.%s() without Loader=... is deprecated, as the "
|
||||
"default Loader is unsafe. Please read "
|
||||
"https://msg.pyyaml.org/load for full details."
|
||||
) % method
|
||||
|
||||
warnings.warn(message, YAMLLoadWarning, stacklevel=3)
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
def scan(stream, Loader=Loader):
|
||||
"""
|
||||
Scan a YAML stream and produce scanning tokens.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_token():
|
||||
yield loader.get_token()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def parse(stream, Loader=Loader):
|
||||
"""
|
||||
Parse a YAML stream and produce parsing events.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_event():
|
||||
yield loader.get_event()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def compose(stream, Loader=Loader):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding representation tree.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
return loader.get_single_node()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def compose_all(stream, Loader=Loader):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding representation trees.
|
||||
"""
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_node():
|
||||
yield loader.get_node()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def load(stream, Loader=None):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
load_warning('load')
|
||||
Loader = FullLoader
|
||||
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
return loader.get_single_data()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def load_all(stream, Loader=None):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
"""
|
||||
if Loader is None:
|
||||
load_warning('load_all')
|
||||
Loader = FullLoader
|
||||
|
||||
loader = Loader(stream)
|
||||
try:
|
||||
while loader.check_data():
|
||||
yield loader.get_data()
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
def full_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve all tags except those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load(stream, FullLoader)
|
||||
|
||||
def full_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve all tags except those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load_all(stream, FullLoader)
|
||||
|
||||
def safe_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve only basic YAML tags. This is known
|
||||
to be safe for untrusted input.
|
||||
"""
|
||||
return load(stream, SafeLoader)
|
||||
|
||||
def safe_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve only basic YAML tags. This is known
|
||||
to be safe for untrusted input.
|
||||
"""
|
||||
return load_all(stream, SafeLoader)
|
||||
|
||||
def unsafe_load(stream):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
and produce the corresponding Python object.
|
||||
|
||||
Resolve all tags, even those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load(stream, UnsafeLoader)
|
||||
|
||||
def unsafe_load_all(stream):
|
||||
"""
|
||||
Parse all YAML documents in a stream
|
||||
and produce corresponding Python objects.
|
||||
|
||||
Resolve all tags, even those known to be
|
||||
unsafe on untrusted input.
|
||||
"""
|
||||
return load_all(stream, UnsafeLoader)
|
||||
|
||||
def emit(events, stream=None, Dumper=Dumper,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None):
|
||||
"""
|
||||
Emit YAML parsing events into a stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
stream = io.StringIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
try:
|
||||
for event in events:
|
||||
dumper.emit(event)
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def serialize_all(nodes, stream=None, Dumper=Dumper,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None):
|
||||
"""
|
||||
Serialize a sequence of representation trees into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
if encoding is None:
|
||||
stream = io.StringIO()
|
||||
else:
|
||||
stream = io.BytesIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
encoding=encoding, version=version, tags=tags,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end)
|
||||
try:
|
||||
dumper.open()
|
||||
for node in nodes:
|
||||
dumper.serialize(node)
|
||||
dumper.close()
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def serialize(node, stream=None, Dumper=Dumper, **kwds):
|
||||
"""
|
||||
Serialize a representation tree into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return serialize_all([node], stream, Dumper=Dumper, **kwds)
|
||||
|
||||
def dump_all(documents, stream=None, Dumper=Dumper,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
"""
|
||||
Serialize a sequence of Python objects into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
getvalue = None
|
||||
if stream is None:
|
||||
if encoding is None:
|
||||
stream = io.StringIO()
|
||||
else:
|
||||
stream = io.BytesIO()
|
||||
getvalue = stream.getvalue
|
||||
dumper = Dumper(stream, default_style=default_style,
|
||||
default_flow_style=default_flow_style,
|
||||
canonical=canonical, indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
encoding=encoding, version=version, tags=tags,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end, sort_keys=sort_keys)
|
||||
try:
|
||||
dumper.open()
|
||||
for data in documents:
|
||||
dumper.represent(data)
|
||||
dumper.close()
|
||||
finally:
|
||||
dumper.dispose()
|
||||
if getvalue:
|
||||
return getvalue()
|
||||
|
||||
def dump(data, stream=None, Dumper=Dumper, **kwds):
|
||||
"""
|
||||
Serialize a Python object into a YAML stream.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all([data], stream, Dumper=Dumper, **kwds)
|
||||
|
||||
def safe_dump_all(documents, stream=None, **kwds):
|
||||
"""
|
||||
Serialize a sequence of Python objects into a YAML stream.
|
||||
Produce only basic YAML tags.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
|
||||
|
||||
def safe_dump(data, stream=None, **kwds):
|
||||
"""
|
||||
Serialize a Python object into a YAML stream.
|
||||
Produce only basic YAML tags.
|
||||
If stream is None, return the produced string instead.
|
||||
"""
|
||||
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
|
||||
|
||||
def add_implicit_resolver(tag, regexp, first=None,
|
||||
Loader=None, Dumper=Dumper):
|
||||
"""
|
||||
Add an implicit scalar detector.
|
||||
If an implicit scalar value matches the given regexp,
|
||||
the corresponding tag is assigned to the scalar.
|
||||
first is a sequence of possible initial characters or None.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_implicit_resolver(tag, regexp, first)
|
||||
loader.FullLoader.add_implicit_resolver(tag, regexp, first)
|
||||
loader.UnsafeLoader.add_implicit_resolver(tag, regexp, first)
|
||||
else:
|
||||
Loader.add_implicit_resolver(tag, regexp, first)
|
||||
Dumper.add_implicit_resolver(tag, regexp, first)
|
||||
|
||||
def add_path_resolver(tag, path, kind=None, Loader=None, Dumper=Dumper):
|
||||
"""
|
||||
Add a path based resolver for the given tag.
|
||||
A path is a list of keys that forms a path
|
||||
to a node in the representation tree.
|
||||
Keys can be string values, integers, or None.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_path_resolver(tag, path, kind)
|
||||
loader.FullLoader.add_path_resolver(tag, path, kind)
|
||||
loader.UnsafeLoader.add_path_resolver(tag, path, kind)
|
||||
else:
|
||||
Loader.add_path_resolver(tag, path, kind)
|
||||
Dumper.add_path_resolver(tag, path, kind)
|
||||
|
||||
def add_constructor(tag, constructor, Loader=None):
|
||||
"""
|
||||
Add a constructor for the given tag.
|
||||
Constructor is a function that accepts a Loader instance
|
||||
and a node object and produces the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_constructor(tag, constructor)
|
||||
loader.FullLoader.add_constructor(tag, constructor)
|
||||
loader.UnsafeLoader.add_constructor(tag, constructor)
|
||||
else:
|
||||
Loader.add_constructor(tag, constructor)
|
||||
|
||||
def add_multi_constructor(tag_prefix, multi_constructor, Loader=None):
|
||||
"""
|
||||
Add a multi-constructor for the given tag prefix.
|
||||
Multi-constructor is called for a node if its tag starts with tag_prefix.
|
||||
Multi-constructor accepts a Loader instance, a tag suffix,
|
||||
and a node object and produces the corresponding Python object.
|
||||
"""
|
||||
if Loader is None:
|
||||
loader.Loader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
loader.FullLoader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
loader.UnsafeLoader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
else:
|
||||
Loader.add_multi_constructor(tag_prefix, multi_constructor)
|
||||
|
||||
def add_representer(data_type, representer, Dumper=Dumper):
|
||||
"""
|
||||
Add a representer for the given type.
|
||||
Representer is a function accepting a Dumper instance
|
||||
and an instance of the given data type
|
||||
and producing the corresponding representation node.
|
||||
"""
|
||||
Dumper.add_representer(data_type, representer)
|
||||
|
||||
def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
|
||||
"""
|
||||
Add a representer for the given type.
|
||||
Multi-representer is a function accepting a Dumper instance
|
||||
and an instance of the given data type or subtype
|
||||
and producing the corresponding representation node.
|
||||
"""
|
||||
Dumper.add_multi_representer(data_type, multi_representer)
|
||||
|
||||
class YAMLObjectMetaclass(type):
|
||||
"""
|
||||
The metaclass for YAMLObject.
|
||||
"""
|
||||
def __init__(cls, name, bases, kwds):
|
||||
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
|
||||
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
|
||||
if isinstance(cls.yaml_loader, list):
|
||||
for loader in cls.yaml_loader:
|
||||
loader.add_constructor(cls.yaml_tag, cls.from_yaml)
|
||||
else:
|
||||
cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
|
||||
|
||||
cls.yaml_dumper.add_representer(cls, cls.to_yaml)
|
||||
|
||||
class YAMLObject(metaclass=YAMLObjectMetaclass):
|
||||
"""
|
||||
An object that can dump itself to a YAML stream
|
||||
and load itself from a YAML stream.
|
||||
"""
|
||||
|
||||
__slots__ = () # no direct instantiation, so allow immutable subclasses
|
||||
|
||||
yaml_loader = [Loader, FullLoader, UnsafeLoader]
|
||||
yaml_dumper = Dumper
|
||||
|
||||
yaml_tag = None
|
||||
yaml_flow_style = None
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, loader, node):
|
||||
"""
|
||||
Convert a representation node to a Python object.
|
||||
"""
|
||||
return loader.construct_yaml_object(node, cls)
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, dumper, data):
|
||||
"""
|
||||
Convert a Python object to a representation node.
|
||||
"""
|
||||
return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
|
||||
flow_style=cls.yaml_flow_style)
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
|
||||
__all__ = ['Composer', 'ComposerError']
|
||||
|
||||
from .error import MarkedYAMLError
|
||||
from .events import *
|
||||
from .nodes import *
|
||||
|
||||
class ComposerError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class Composer:
|
||||
|
||||
def __init__(self):
|
||||
self.anchors = {}
|
||||
|
||||
def check_node(self):
|
||||
# Drop the STREAM-START event.
|
||||
if self.check_event(StreamStartEvent):
|
||||
self.get_event()
|
||||
|
||||
# If there are more documents available?
|
||||
return not self.check_event(StreamEndEvent)
|
||||
|
||||
def get_node(self):
|
||||
# Get the root node of the next document.
|
||||
if not self.check_event(StreamEndEvent):
|
||||
return self.compose_document()
|
||||
|
||||
def get_single_node(self):
|
||||
# Drop the STREAM-START event.
|
||||
self.get_event()
|
||||
|
||||
# Compose a document if the stream is not empty.
|
||||
document = None
|
||||
if not self.check_event(StreamEndEvent):
|
||||
document = self.compose_document()
|
||||
|
||||
# Ensure that the stream contains no more documents.
|
||||
if not self.check_event(StreamEndEvent):
|
||||
event = self.get_event()
|
||||
raise ComposerError("expected a single document in the stream",
|
||||
document.start_mark, "but found another document",
|
||||
event.start_mark)
|
||||
|
||||
# Drop the STREAM-END event.
|
||||
self.get_event()
|
||||
|
||||
return document
|
||||
|
||||
def compose_document(self):
|
||||
# Drop the DOCUMENT-START event.
|
||||
self.get_event()
|
||||
|
||||
# Compose the root node.
|
||||
node = self.compose_node(None, None)
|
||||
|
||||
# Drop the DOCUMENT-END event.
|
||||
self.get_event()
|
||||
|
||||
self.anchors = {}
|
||||
return node
|
||||
|
||||
def compose_node(self, parent, index):
|
||||
if self.check_event(AliasEvent):
|
||||
event = self.get_event()
|
||||
anchor = event.anchor
|
||||
if anchor not in self.anchors:
|
||||
raise ComposerError(None, None, "found undefined alias %r"
|
||||
% anchor, event.start_mark)
|
||||
return self.anchors[anchor]
|
||||
event = self.peek_event()
|
||||
anchor = event.anchor
|
||||
if anchor is not None:
|
||||
if anchor in self.anchors:
|
||||
raise ComposerError("found duplicate anchor %r; first occurrence"
|
||||
% anchor, self.anchors[anchor].start_mark,
|
||||
"second occurrence", event.start_mark)
|
||||
self.descend_resolver(parent, index)
|
||||
if self.check_event(ScalarEvent):
|
||||
node = self.compose_scalar_node(anchor)
|
||||
elif self.check_event(SequenceStartEvent):
|
||||
node = self.compose_sequence_node(anchor)
|
||||
elif self.check_event(MappingStartEvent):
|
||||
node = self.compose_mapping_node(anchor)
|
||||
self.ascend_resolver()
|
||||
return node
|
||||
|
||||
def compose_scalar_node(self, anchor):
|
||||
event = self.get_event()
|
||||
tag = event.tag
|
||||
if tag is None or tag == '!':
|
||||
tag = self.resolve(ScalarNode, event.value, event.implicit)
|
||||
node = ScalarNode(tag, event.value,
|
||||
event.start_mark, event.end_mark, style=event.style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
return node
|
||||
|
||||
def compose_sequence_node(self, anchor):
|
||||
start_event = self.get_event()
|
||||
tag = start_event.tag
|
||||
if tag is None or tag == '!':
|
||||
tag = self.resolve(SequenceNode, None, start_event.implicit)
|
||||
node = SequenceNode(tag, [],
|
||||
start_event.start_mark, None,
|
||||
flow_style=start_event.flow_style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
index = 0
|
||||
while not self.check_event(SequenceEndEvent):
|
||||
node.value.append(self.compose_node(node, index))
|
||||
index += 1
|
||||
end_event = self.get_event()
|
||||
node.end_mark = end_event.end_mark
|
||||
return node
|
||||
|
||||
def compose_mapping_node(self, anchor):
|
||||
start_event = self.get_event()
|
||||
tag = start_event.tag
|
||||
if tag is None or tag == '!':
|
||||
tag = self.resolve(MappingNode, None, start_event.implicit)
|
||||
node = MappingNode(tag, [],
|
||||
start_event.start_mark, None,
|
||||
flow_style=start_event.flow_style)
|
||||
if anchor is not None:
|
||||
self.anchors[anchor] = node
|
||||
while not self.check_event(MappingEndEvent):
|
||||
#key_event = self.peek_event()
|
||||
item_key = self.compose_node(node, None)
|
||||
#if item_key in node.value:
|
||||
# raise ComposerError("while composing a mapping", start_event.start_mark,
|
||||
# "found duplicate key", key_event.start_mark)
|
||||
item_value = self.compose_node(node, item_key)
|
||||
#node.value[item_key] = item_value
|
||||
node.value.append((item_key, item_value))
|
||||
end_event = self.get_event()
|
||||
node.end_mark = end_event.end_mark
|
||||
return node
|
||||
|
||||
@@ -0,0 +1,748 @@
|
||||
|
||||
__all__ = [
|
||||
'BaseConstructor',
|
||||
'SafeConstructor',
|
||||
'FullConstructor',
|
||||
'UnsafeConstructor',
|
||||
'Constructor',
|
||||
'ConstructorError'
|
||||
]
|
||||
|
||||
from .error import *
|
||||
from .nodes import *
|
||||
|
||||
import collections.abc, datetime, base64, binascii, re, sys, types
|
||||
|
||||
class ConstructorError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class BaseConstructor:
|
||||
|
||||
yaml_constructors = {}
|
||||
yaml_multi_constructors = {}
|
||||
|
||||
def __init__(self):
|
||||
self.constructed_objects = {}
|
||||
self.recursive_objects = {}
|
||||
self.state_generators = []
|
||||
self.deep_construct = False
|
||||
|
||||
def check_data(self):
|
||||
# If there are more documents available?
|
||||
return self.check_node()
|
||||
|
||||
def check_state_key(self, key):
|
||||
"""Block special attributes/methods from being set in a newly created
|
||||
object, to prevent user-controlled methods from being called during
|
||||
deserialization"""
|
||||
if self.get_state_keys_blacklist_regexp().match(key):
|
||||
raise ConstructorError(None, None,
|
||||
"blacklisted key '%s' in instance state found" % (key,), None)
|
||||
|
||||
def get_data(self):
|
||||
# Construct and return the next document.
|
||||
if self.check_node():
|
||||
return self.construct_document(self.get_node())
|
||||
|
||||
def get_single_data(self):
|
||||
# Ensure that the stream contains a single document and construct it.
|
||||
node = self.get_single_node()
|
||||
if node is not None:
|
||||
return self.construct_document(node)
|
||||
return None
|
||||
|
||||
def construct_document(self, node):
|
||||
data = self.construct_object(node)
|
||||
while self.state_generators:
|
||||
state_generators = self.state_generators
|
||||
self.state_generators = []
|
||||
for generator in state_generators:
|
||||
for dummy in generator:
|
||||
pass
|
||||
self.constructed_objects = {}
|
||||
self.recursive_objects = {}
|
||||
self.deep_construct = False
|
||||
return data
|
||||
|
||||
def construct_object(self, node, deep=False):
|
||||
if node in self.constructed_objects:
|
||||
return self.constructed_objects[node]
|
||||
if deep:
|
||||
old_deep = self.deep_construct
|
||||
self.deep_construct = True
|
||||
if node in self.recursive_objects:
|
||||
raise ConstructorError(None, None,
|
||||
"found unconstructable recursive node", node.start_mark)
|
||||
self.recursive_objects[node] = None
|
||||
constructor = None
|
||||
tag_suffix = None
|
||||
if node.tag in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[node.tag]
|
||||
else:
|
||||
for tag_prefix in self.yaml_multi_constructors:
|
||||
if tag_prefix is not None and node.tag.startswith(tag_prefix):
|
||||
tag_suffix = node.tag[len(tag_prefix):]
|
||||
constructor = self.yaml_multi_constructors[tag_prefix]
|
||||
break
|
||||
else:
|
||||
if None in self.yaml_multi_constructors:
|
||||
tag_suffix = node.tag
|
||||
constructor = self.yaml_multi_constructors[None]
|
||||
elif None in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[None]
|
||||
elif isinstance(node, ScalarNode):
|
||||
constructor = self.__class__.construct_scalar
|
||||
elif isinstance(node, SequenceNode):
|
||||
constructor = self.__class__.construct_sequence
|
||||
elif isinstance(node, MappingNode):
|
||||
constructor = self.__class__.construct_mapping
|
||||
if tag_suffix is None:
|
||||
data = constructor(self, node)
|
||||
else:
|
||||
data = constructor(self, tag_suffix, node)
|
||||
if isinstance(data, types.GeneratorType):
|
||||
generator = data
|
||||
data = next(generator)
|
||||
if self.deep_construct:
|
||||
for dummy in generator:
|
||||
pass
|
||||
else:
|
||||
self.state_generators.append(generator)
|
||||
self.constructed_objects[node] = data
|
||||
del self.recursive_objects[node]
|
||||
if deep:
|
||||
self.deep_construct = old_deep
|
||||
return data
|
||||
|
||||
def construct_scalar(self, node):
|
||||
if not isinstance(node, ScalarNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a scalar node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
return node.value
|
||||
|
||||
def construct_sequence(self, node, deep=False):
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a sequence node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
return [self.construct_object(child, deep=deep)
|
||||
for child in node.value]
|
||||
|
||||
def construct_mapping(self, node, deep=False):
|
||||
if not isinstance(node, MappingNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a mapping node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
mapping = {}
|
||||
for key_node, value_node in node.value:
|
||||
key = self.construct_object(key_node, deep=deep)
|
||||
if not isinstance(key, collections.abc.Hashable):
|
||||
raise ConstructorError("while constructing a mapping", node.start_mark,
|
||||
"found unhashable key", key_node.start_mark)
|
||||
value = self.construct_object(value_node, deep=deep)
|
||||
mapping[key] = value
|
||||
return mapping
|
||||
|
||||
def construct_pairs(self, node, deep=False):
|
||||
if not isinstance(node, MappingNode):
|
||||
raise ConstructorError(None, None,
|
||||
"expected a mapping node, but found %s" % node.id,
|
||||
node.start_mark)
|
||||
pairs = []
|
||||
for key_node, value_node in node.value:
|
||||
key = self.construct_object(key_node, deep=deep)
|
||||
value = self.construct_object(value_node, deep=deep)
|
||||
pairs.append((key, value))
|
||||
return pairs
|
||||
|
||||
@classmethod
|
||||
def add_constructor(cls, tag, constructor):
|
||||
if not 'yaml_constructors' in cls.__dict__:
|
||||
cls.yaml_constructors = cls.yaml_constructors.copy()
|
||||
cls.yaml_constructors[tag] = constructor
|
||||
|
||||
@classmethod
|
||||
def add_multi_constructor(cls, tag_prefix, multi_constructor):
|
||||
if not 'yaml_multi_constructors' in cls.__dict__:
|
||||
cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy()
|
||||
cls.yaml_multi_constructors[tag_prefix] = multi_constructor
|
||||
|
||||
class SafeConstructor(BaseConstructor):
|
||||
|
||||
def construct_scalar(self, node):
|
||||
if isinstance(node, MappingNode):
|
||||
for key_node, value_node in node.value:
|
||||
if key_node.tag == 'tag:yaml.org,2002:value':
|
||||
return self.construct_scalar(value_node)
|
||||
return super().construct_scalar(node)
|
||||
|
||||
def flatten_mapping(self, node):
|
||||
merge = []
|
||||
index = 0
|
||||
while index < len(node.value):
|
||||
key_node, value_node = node.value[index]
|
||||
if key_node.tag == 'tag:yaml.org,2002:merge':
|
||||
del node.value[index]
|
||||
if isinstance(value_node, MappingNode):
|
||||
self.flatten_mapping(value_node)
|
||||
merge.extend(value_node.value)
|
||||
elif isinstance(value_node, SequenceNode):
|
||||
submerge = []
|
||||
for subnode in value_node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing a mapping",
|
||||
node.start_mark,
|
||||
"expected a mapping for merging, but found %s"
|
||||
% subnode.id, subnode.start_mark)
|
||||
self.flatten_mapping(subnode)
|
||||
submerge.append(subnode.value)
|
||||
submerge.reverse()
|
||||
for value in submerge:
|
||||
merge.extend(value)
|
||||
else:
|
||||
raise ConstructorError("while constructing a mapping", node.start_mark,
|
||||
"expected a mapping or list of mappings for merging, but found %s"
|
||||
% value_node.id, value_node.start_mark)
|
||||
elif key_node.tag == 'tag:yaml.org,2002:value':
|
||||
key_node.tag = 'tag:yaml.org,2002:str'
|
||||
index += 1
|
||||
else:
|
||||
index += 1
|
||||
if merge:
|
||||
node.value = merge + node.value
|
||||
|
||||
def construct_mapping(self, node, deep=False):
|
||||
if isinstance(node, MappingNode):
|
||||
self.flatten_mapping(node)
|
||||
return super().construct_mapping(node, deep=deep)
|
||||
|
||||
def construct_yaml_null(self, node):
|
||||
self.construct_scalar(node)
|
||||
return None
|
||||
|
||||
bool_values = {
|
||||
'yes': True,
|
||||
'no': False,
|
||||
'true': True,
|
||||
'false': False,
|
||||
'on': True,
|
||||
'off': False,
|
||||
}
|
||||
|
||||
def construct_yaml_bool(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
return self.bool_values[value.lower()]
|
||||
|
||||
def construct_yaml_int(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
value = value.replace('_', '')
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
if value[0] in '+-':
|
||||
value = value[1:]
|
||||
if value == '0':
|
||||
return 0
|
||||
elif value.startswith('0b'):
|
||||
return sign*int(value[2:], 2)
|
||||
elif value.startswith('0x'):
|
||||
return sign*int(value[2:], 16)
|
||||
elif value[0] == '0':
|
||||
return sign*int(value, 8)
|
||||
elif ':' in value:
|
||||
digits = [int(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*int(value)
|
||||
|
||||
inf_value = 1e300
|
||||
while inf_value != inf_value*inf_value:
|
||||
inf_value *= inf_value
|
||||
nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99).
|
||||
|
||||
def construct_yaml_float(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
value = value.replace('_', '').lower()
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
if value[0] in '+-':
|
||||
value = value[1:]
|
||||
if value == '.inf':
|
||||
return sign*self.inf_value
|
||||
elif value == '.nan':
|
||||
return self.nan_value
|
||||
elif ':' in value:
|
||||
digits = [float(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0.0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*float(value)
|
||||
|
||||
def construct_yaml_binary(self, node):
|
||||
try:
|
||||
value = self.construct_scalar(node).encode('ascii')
|
||||
except UnicodeEncodeError as exc:
|
||||
raise ConstructorError(None, None,
|
||||
"failed to convert base64 data into ascii: %s" % exc,
|
||||
node.start_mark)
|
||||
try:
|
||||
if hasattr(base64, 'decodebytes'):
|
||||
return base64.decodebytes(value)
|
||||
else:
|
||||
return base64.decodestring(value)
|
||||
except binascii.Error as exc:
|
||||
raise ConstructorError(None, None,
|
||||
"failed to decode base64 data: %s" % exc, node.start_mark)
|
||||
|
||||
timestamp_regexp = re.compile(
|
||||
r'''^(?P<year>[0-9][0-9][0-9][0-9])
|
||||
-(?P<month>[0-9][0-9]?)
|
||||
-(?P<day>[0-9][0-9]?)
|
||||
(?:(?:[Tt]|[ \t]+)
|
||||
(?P<hour>[0-9][0-9]?)
|
||||
:(?P<minute>[0-9][0-9])
|
||||
:(?P<second>[0-9][0-9])
|
||||
(?:\.(?P<fraction>[0-9]*))?
|
||||
(?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
|
||||
(?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X)
|
||||
|
||||
def construct_yaml_timestamp(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
match = self.timestamp_regexp.match(node.value)
|
||||
values = match.groupdict()
|
||||
year = int(values['year'])
|
||||
month = int(values['month'])
|
||||
day = int(values['day'])
|
||||
if not values['hour']:
|
||||
return datetime.date(year, month, day)
|
||||
hour = int(values['hour'])
|
||||
minute = int(values['minute'])
|
||||
second = int(values['second'])
|
||||
fraction = 0
|
||||
tzinfo = None
|
||||
if values['fraction']:
|
||||
fraction = values['fraction'][:6]
|
||||
while len(fraction) < 6:
|
||||
fraction += '0'
|
||||
fraction = int(fraction)
|
||||
if values['tz_sign']:
|
||||
tz_hour = int(values['tz_hour'])
|
||||
tz_minute = int(values['tz_minute'] or 0)
|
||||
delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
|
||||
if values['tz_sign'] == '-':
|
||||
delta = -delta
|
||||
tzinfo = datetime.timezone(delta)
|
||||
elif values['tz']:
|
||||
tzinfo = datetime.timezone.utc
|
||||
return datetime.datetime(year, month, day, hour, minute, second, fraction,
|
||||
tzinfo=tzinfo)
|
||||
|
||||
def construct_yaml_omap(self, node):
|
||||
# Note: we do not check for duplicate keys, because it's too
|
||||
# CPU-expensive.
|
||||
omap = []
|
||||
yield omap
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a sequence, but found %s" % node.id, node.start_mark)
|
||||
for subnode in node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a mapping of length 1, but found %s" % subnode.id,
|
||||
subnode.start_mark)
|
||||
if len(subnode.value) != 1:
|
||||
raise ConstructorError("while constructing an ordered map", node.start_mark,
|
||||
"expected a single mapping item, but found %d items" % len(subnode.value),
|
||||
subnode.start_mark)
|
||||
key_node, value_node = subnode.value[0]
|
||||
key = self.construct_object(key_node)
|
||||
value = self.construct_object(value_node)
|
||||
omap.append((key, value))
|
||||
|
||||
def construct_yaml_pairs(self, node):
|
||||
# Note: the same code as `construct_yaml_omap`.
|
||||
pairs = []
|
||||
yield pairs
|
||||
if not isinstance(node, SequenceNode):
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a sequence, but found %s" % node.id, node.start_mark)
|
||||
for subnode in node.value:
|
||||
if not isinstance(subnode, MappingNode):
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a mapping of length 1, but found %s" % subnode.id,
|
||||
subnode.start_mark)
|
||||
if len(subnode.value) != 1:
|
||||
raise ConstructorError("while constructing pairs", node.start_mark,
|
||||
"expected a single mapping item, but found %d items" % len(subnode.value),
|
||||
subnode.start_mark)
|
||||
key_node, value_node = subnode.value[0]
|
||||
key = self.construct_object(key_node)
|
||||
value = self.construct_object(value_node)
|
||||
pairs.append((key, value))
|
||||
|
||||
def construct_yaml_set(self, node):
|
||||
data = set()
|
||||
yield data
|
||||
value = self.construct_mapping(node)
|
||||
data.update(value)
|
||||
|
||||
def construct_yaml_str(self, node):
|
||||
return self.construct_scalar(node)
|
||||
|
||||
def construct_yaml_seq(self, node):
|
||||
data = []
|
||||
yield data
|
||||
data.extend(self.construct_sequence(node))
|
||||
|
||||
def construct_yaml_map(self, node):
|
||||
data = {}
|
||||
yield data
|
||||
value = self.construct_mapping(node)
|
||||
data.update(value)
|
||||
|
||||
def construct_yaml_object(self, node, cls):
|
||||
data = cls.__new__(cls)
|
||||
yield data
|
||||
if hasattr(data, '__setstate__'):
|
||||
state = self.construct_mapping(node, deep=True)
|
||||
data.__setstate__(state)
|
||||
else:
|
||||
state = self.construct_mapping(node)
|
||||
data.__dict__.update(state)
|
||||
|
||||
def construct_undefined(self, node):
|
||||
raise ConstructorError(None, None,
|
||||
"could not determine a constructor for the tag %r" % node.tag,
|
||||
node.start_mark)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:null',
|
||||
SafeConstructor.construct_yaml_null)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:bool',
|
||||
SafeConstructor.construct_yaml_bool)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:int',
|
||||
SafeConstructor.construct_yaml_int)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:float',
|
||||
SafeConstructor.construct_yaml_float)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:binary',
|
||||
SafeConstructor.construct_yaml_binary)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:timestamp',
|
||||
SafeConstructor.construct_yaml_timestamp)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:omap',
|
||||
SafeConstructor.construct_yaml_omap)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:pairs',
|
||||
SafeConstructor.construct_yaml_pairs)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:set',
|
||||
SafeConstructor.construct_yaml_set)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:str',
|
||||
SafeConstructor.construct_yaml_str)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:seq',
|
||||
SafeConstructor.construct_yaml_seq)
|
||||
|
||||
SafeConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:map',
|
||||
SafeConstructor.construct_yaml_map)
|
||||
|
||||
SafeConstructor.add_constructor(None,
|
||||
SafeConstructor.construct_undefined)
|
||||
|
||||
class FullConstructor(SafeConstructor):
|
||||
# 'extend' is blacklisted because it is used by
|
||||
# construct_python_object_apply to add `listitems` to a newly generate
|
||||
# python instance
|
||||
def get_state_keys_blacklist(self):
|
||||
return ['^extend$', '^__.*__$']
|
||||
|
||||
def get_state_keys_blacklist_regexp(self):
|
||||
if not hasattr(self, 'state_keys_blacklist_regexp'):
|
||||
self.state_keys_blacklist_regexp = re.compile('(' + '|'.join(self.get_state_keys_blacklist()) + ')')
|
||||
return self.state_keys_blacklist_regexp
|
||||
|
||||
def construct_python_str(self, node):
|
||||
return self.construct_scalar(node)
|
||||
|
||||
def construct_python_unicode(self, node):
|
||||
return self.construct_scalar(node)
|
||||
|
||||
def construct_python_bytes(self, node):
|
||||
try:
|
||||
value = self.construct_scalar(node).encode('ascii')
|
||||
except UnicodeEncodeError as exc:
|
||||
raise ConstructorError(None, None,
|
||||
"failed to convert base64 data into ascii: %s" % exc,
|
||||
node.start_mark)
|
||||
try:
|
||||
if hasattr(base64, 'decodebytes'):
|
||||
return base64.decodebytes(value)
|
||||
else:
|
||||
return base64.decodestring(value)
|
||||
except binascii.Error as exc:
|
||||
raise ConstructorError(None, None,
|
||||
"failed to decode base64 data: %s" % exc, node.start_mark)
|
||||
|
||||
def construct_python_long(self, node):
|
||||
return self.construct_yaml_int(node)
|
||||
|
||||
def construct_python_complex(self, node):
|
||||
return complex(self.construct_scalar(node))
|
||||
|
||||
def construct_python_tuple(self, node):
|
||||
return tuple(self.construct_sequence(node))
|
||||
|
||||
def find_python_module(self, name, mark, unsafe=False):
|
||||
if not name:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"expected non-empty name appended to the tag", mark)
|
||||
if unsafe:
|
||||
try:
|
||||
__import__(name)
|
||||
except ImportError as exc:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"cannot find module %r (%s)" % (name, exc), mark)
|
||||
if name not in sys.modules:
|
||||
raise ConstructorError("while constructing a Python module", mark,
|
||||
"module %r is not imported" % name, mark)
|
||||
return sys.modules[name]
|
||||
|
||||
def find_python_name(self, name, mark, unsafe=False):
|
||||
if not name:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"expected non-empty name appended to the tag", mark)
|
||||
if '.' in name:
|
||||
module_name, object_name = name.rsplit('.', 1)
|
||||
else:
|
||||
module_name = 'builtins'
|
||||
object_name = name
|
||||
if unsafe:
|
||||
try:
|
||||
__import__(module_name)
|
||||
except ImportError as exc:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"cannot find module %r (%s)" % (module_name, exc), mark)
|
||||
if module_name not in sys.modules:
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"module %r is not imported" % module_name, mark)
|
||||
module = sys.modules[module_name]
|
||||
if not hasattr(module, object_name):
|
||||
raise ConstructorError("while constructing a Python object", mark,
|
||||
"cannot find %r in the module %r"
|
||||
% (object_name, module.__name__), mark)
|
||||
return getattr(module, object_name)
|
||||
|
||||
def construct_python_name(self, suffix, node):
|
||||
value = self.construct_scalar(node)
|
||||
if value:
|
||||
raise ConstructorError("while constructing a Python name", node.start_mark,
|
||||
"expected the empty value, but found %r" % value, node.start_mark)
|
||||
return self.find_python_name(suffix, node.start_mark)
|
||||
|
||||
def construct_python_module(self, suffix, node):
|
||||
value = self.construct_scalar(node)
|
||||
if value:
|
||||
raise ConstructorError("while constructing a Python module", node.start_mark,
|
||||
"expected the empty value, but found %r" % value, node.start_mark)
|
||||
return self.find_python_module(suffix, node.start_mark)
|
||||
|
||||
def make_python_instance(self, suffix, node,
|
||||
args=None, kwds=None, newobj=False, unsafe=False):
|
||||
if not args:
|
||||
args = []
|
||||
if not kwds:
|
||||
kwds = {}
|
||||
cls = self.find_python_name(suffix, node.start_mark)
|
||||
if not (unsafe or isinstance(cls, type)):
|
||||
raise ConstructorError("while constructing a Python instance", node.start_mark,
|
||||
"expected a class, but found %r" % type(cls),
|
||||
node.start_mark)
|
||||
if newobj and isinstance(cls, type):
|
||||
return cls.__new__(cls, *args, **kwds)
|
||||
else:
|
||||
return cls(*args, **kwds)
|
||||
|
||||
def set_python_instance_state(self, instance, state, unsafe=False):
|
||||
if hasattr(instance, '__setstate__'):
|
||||
instance.__setstate__(state)
|
||||
else:
|
||||
slotstate = {}
|
||||
if isinstance(state, tuple) and len(state) == 2:
|
||||
state, slotstate = state
|
||||
if hasattr(instance, '__dict__'):
|
||||
if not unsafe and state:
|
||||
for key in state.keys():
|
||||
self.check_state_key(key)
|
||||
instance.__dict__.update(state)
|
||||
elif state:
|
||||
slotstate.update(state)
|
||||
for key, value in slotstate.items():
|
||||
if not unsafe:
|
||||
self.check_state_key(key)
|
||||
setattr(instance, key, value)
|
||||
|
||||
def construct_python_object(self, suffix, node):
|
||||
# Format:
|
||||
# !!python/object:module.name { ... state ... }
|
||||
instance = self.make_python_instance(suffix, node, newobj=True)
|
||||
yield instance
|
||||
deep = hasattr(instance, '__setstate__')
|
||||
state = self.construct_mapping(node, deep=deep)
|
||||
self.set_python_instance_state(instance, state)
|
||||
|
||||
def construct_python_object_apply(self, suffix, node, newobj=False):
|
||||
# Format:
|
||||
# !!python/object/apply # (or !!python/object/new)
|
||||
# args: [ ... arguments ... ]
|
||||
# kwds: { ... keywords ... }
|
||||
# state: ... state ...
|
||||
# listitems: [ ... listitems ... ]
|
||||
# dictitems: { ... dictitems ... }
|
||||
# or short format:
|
||||
# !!python/object/apply [ ... arguments ... ]
|
||||
# The difference between !!python/object/apply and !!python/object/new
|
||||
# is how an object is created, check make_python_instance for details.
|
||||
if isinstance(node, SequenceNode):
|
||||
args = self.construct_sequence(node, deep=True)
|
||||
kwds = {}
|
||||
state = {}
|
||||
listitems = []
|
||||
dictitems = {}
|
||||
else:
|
||||
value = self.construct_mapping(node, deep=True)
|
||||
args = value.get('args', [])
|
||||
kwds = value.get('kwds', {})
|
||||
state = value.get('state', {})
|
||||
listitems = value.get('listitems', [])
|
||||
dictitems = value.get('dictitems', {})
|
||||
instance = self.make_python_instance(suffix, node, args, kwds, newobj)
|
||||
if state:
|
||||
self.set_python_instance_state(instance, state)
|
||||
if listitems:
|
||||
instance.extend(listitems)
|
||||
if dictitems:
|
||||
for key in dictitems:
|
||||
instance[key] = dictitems[key]
|
||||
return instance
|
||||
|
||||
def construct_python_object_new(self, suffix, node):
|
||||
return self.construct_python_object_apply(suffix, node, newobj=True)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/none',
|
||||
FullConstructor.construct_yaml_null)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/bool',
|
||||
FullConstructor.construct_yaml_bool)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/str',
|
||||
FullConstructor.construct_python_str)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/unicode',
|
||||
FullConstructor.construct_python_unicode)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/bytes',
|
||||
FullConstructor.construct_python_bytes)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/int',
|
||||
FullConstructor.construct_yaml_int)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/long',
|
||||
FullConstructor.construct_python_long)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/float',
|
||||
FullConstructor.construct_yaml_float)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/complex',
|
||||
FullConstructor.construct_python_complex)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/list',
|
||||
FullConstructor.construct_yaml_seq)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/tuple',
|
||||
FullConstructor.construct_python_tuple)
|
||||
|
||||
FullConstructor.add_constructor(
|
||||
'tag:yaml.org,2002:python/dict',
|
||||
FullConstructor.construct_yaml_map)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
'tag:yaml.org,2002:python/name:',
|
||||
FullConstructor.construct_python_name)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
'tag:yaml.org,2002:python/module:',
|
||||
FullConstructor.construct_python_module)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
'tag:yaml.org,2002:python/object:',
|
||||
FullConstructor.construct_python_object)
|
||||
|
||||
FullConstructor.add_multi_constructor(
|
||||
'tag:yaml.org,2002:python/object/new:',
|
||||
FullConstructor.construct_python_object_new)
|
||||
|
||||
class UnsafeConstructor(FullConstructor):
|
||||
|
||||
def find_python_module(self, name, mark):
|
||||
return super(UnsafeConstructor, self).find_python_module(name, mark, unsafe=True)
|
||||
|
||||
def find_python_name(self, name, mark):
|
||||
return super(UnsafeConstructor, self).find_python_name(name, mark, unsafe=True)
|
||||
|
||||
def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False):
|
||||
return super(UnsafeConstructor, self).make_python_instance(
|
||||
suffix, node, args, kwds, newobj, unsafe=True)
|
||||
|
||||
def set_python_instance_state(self, instance, state):
|
||||
return super(UnsafeConstructor, self).set_python_instance_state(
|
||||
instance, state, unsafe=True)
|
||||
|
||||
UnsafeConstructor.add_multi_constructor(
|
||||
'tag:yaml.org,2002:python/object/apply:',
|
||||
UnsafeConstructor.construct_python_object_apply)
|
||||
|
||||
# Constructor is same as UnsafeConstructor. Need to leave this in place in case
|
||||
# people have extended it directly.
|
||||
class Constructor(UnsafeConstructor):
|
||||
pass
|
||||
@@ -0,0 +1,101 @@
|
||||
|
||||
__all__ = [
|
||||
'CBaseLoader', 'CSafeLoader', 'CFullLoader', 'CUnsafeLoader', 'CLoader',
|
||||
'CBaseDumper', 'CSafeDumper', 'CDumper'
|
||||
]
|
||||
|
||||
from _yaml import CParser, CEmitter
|
||||
|
||||
from .constructor import *
|
||||
|
||||
from .serializer import *
|
||||
from .representer import *
|
||||
|
||||
from .resolver import *
|
||||
|
||||
class CBaseLoader(CParser, BaseConstructor, BaseResolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
BaseConstructor.__init__(self)
|
||||
BaseResolver.__init__(self)
|
||||
|
||||
class CSafeLoader(CParser, SafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
SafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CFullLoader(CParser, FullConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
FullConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CUnsafeLoader(CParser, UnsafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
UnsafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CLoader(CParser, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
CParser.__init__(self, stream)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CSafeDumper(CEmitter, SafeRepresenter, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
SafeRepresenter.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class CDumper(CEmitter, Serializer, Representer, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
CEmitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width, encoding=encoding,
|
||||
allow_unicode=allow_unicode, line_break=line_break,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
|
||||
__all__ = ['BaseDumper', 'SafeDumper', 'Dumper']
|
||||
|
||||
from .emitter import *
|
||||
from .serializer import *
|
||||
from .representer import *
|
||||
from .resolver import *
|
||||
|
||||
class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
SafeRepresenter.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class Dumper(Emitter, Serializer, Representer, Resolver):
|
||||
|
||||
def __init__(self, stream,
|
||||
default_style=None, default_flow_style=False,
|
||||
canonical=None, indent=None, width=None,
|
||||
allow_unicode=None, line_break=None,
|
||||
encoding=None, explicit_start=None, explicit_end=None,
|
||||
version=None, tags=None, sort_keys=True):
|
||||
Emitter.__init__(self, stream, canonical=canonical,
|
||||
indent=indent, width=width,
|
||||
allow_unicode=allow_unicode, line_break=line_break)
|
||||
Serializer.__init__(self, encoding=encoding,
|
||||
explicit_start=explicit_start, explicit_end=explicit_end,
|
||||
version=version, tags=tags)
|
||||
Representer.__init__(self, default_style=default_style,
|
||||
default_flow_style=default_flow_style, sort_keys=sort_keys)
|
||||
Resolver.__init__(self)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,75 @@
|
||||
|
||||
__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError']
|
||||
|
||||
class Mark:
|
||||
|
||||
def __init__(self, name, index, line, column, buffer, pointer):
|
||||
self.name = name
|
||||
self.index = index
|
||||
self.line = line
|
||||
self.column = column
|
||||
self.buffer = buffer
|
||||
self.pointer = pointer
|
||||
|
||||
def get_snippet(self, indent=4, max_length=75):
|
||||
if self.buffer is None:
|
||||
return None
|
||||
head = ''
|
||||
start = self.pointer
|
||||
while start > 0 and self.buffer[start-1] not in '\0\r\n\x85\u2028\u2029':
|
||||
start -= 1
|
||||
if self.pointer-start > max_length/2-1:
|
||||
head = ' ... '
|
||||
start += 5
|
||||
break
|
||||
tail = ''
|
||||
end = self.pointer
|
||||
while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029':
|
||||
end += 1
|
||||
if end-self.pointer > max_length/2-1:
|
||||
tail = ' ... '
|
||||
end -= 5
|
||||
break
|
||||
snippet = self.buffer[start:end]
|
||||
return ' '*indent + head + snippet + tail + '\n' \
|
||||
+ ' '*(indent+self.pointer-start+len(head)) + '^'
|
||||
|
||||
def __str__(self):
|
||||
snippet = self.get_snippet()
|
||||
where = " in \"%s\", line %d, column %d" \
|
||||
% (self.name, self.line+1, self.column+1)
|
||||
if snippet is not None:
|
||||
where += ":\n"+snippet
|
||||
return where
|
||||
|
||||
class YAMLError(Exception):
|
||||
pass
|
||||
|
||||
class MarkedYAMLError(YAMLError):
|
||||
|
||||
def __init__(self, context=None, context_mark=None,
|
||||
problem=None, problem_mark=None, note=None):
|
||||
self.context = context
|
||||
self.context_mark = context_mark
|
||||
self.problem = problem
|
||||
self.problem_mark = problem_mark
|
||||
self.note = note
|
||||
|
||||
def __str__(self):
|
||||
lines = []
|
||||
if self.context is not None:
|
||||
lines.append(self.context)
|
||||
if self.context_mark is not None \
|
||||
and (self.problem is None or self.problem_mark is None
|
||||
or self.context_mark.name != self.problem_mark.name
|
||||
or self.context_mark.line != self.problem_mark.line
|
||||
or self.context_mark.column != self.problem_mark.column):
|
||||
lines.append(str(self.context_mark))
|
||||
if self.problem is not None:
|
||||
lines.append(self.problem)
|
||||
if self.problem_mark is not None:
|
||||
lines.append(str(self.problem_mark))
|
||||
if self.note is not None:
|
||||
lines.append(self.note)
|
||||
return '\n'.join(lines)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
# Abstract classes.
|
||||
|
||||
class Event(object):
|
||||
def __init__(self, start_mark=None, end_mark=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
attributes = [key for key in ['anchor', 'tag', 'implicit', 'value']
|
||||
if hasattr(self, key)]
|
||||
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
class NodeEvent(Event):
|
||||
def __init__(self, anchor, start_mark=None, end_mark=None):
|
||||
self.anchor = anchor
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class CollectionStartEvent(NodeEvent):
|
||||
def __init__(self, anchor, tag, implicit, start_mark=None, end_mark=None,
|
||||
flow_style=None):
|
||||
self.anchor = anchor
|
||||
self.tag = tag
|
||||
self.implicit = implicit
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
class CollectionEndEvent(Event):
|
||||
pass
|
||||
|
||||
# Implementations.
|
||||
|
||||
class StreamStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None, encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
class StreamEndEvent(Event):
|
||||
pass
|
||||
|
||||
class DocumentStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None, version=None, tags=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.explicit = explicit
|
||||
self.version = version
|
||||
self.tags = tags
|
||||
|
||||
class DocumentEndEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.explicit = explicit
|
||||
|
||||
class AliasEvent(NodeEvent):
|
||||
pass
|
||||
|
||||
class ScalarEvent(NodeEvent):
|
||||
def __init__(self, anchor, tag, implicit, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
self.anchor = anchor
|
||||
self.tag = tag
|
||||
self.implicit = implicit
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
class SequenceStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
class SequenceEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
class MappingStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
class MappingEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
__all__ = ['BaseLoader', 'FullLoader', 'SafeLoader', 'Loader', 'UnsafeLoader']
|
||||
|
||||
from .reader import *
|
||||
from .scanner import *
|
||||
from .parser import *
|
||||
from .composer import *
|
||||
from .constructor import *
|
||||
from .resolver import *
|
||||
|
||||
class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
BaseConstructor.__init__(self)
|
||||
BaseResolver.__init__(self)
|
||||
|
||||
class FullLoader(Reader, Scanner, Parser, Composer, FullConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
FullConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class SafeLoader(Reader, Scanner, Parser, Composer, SafeConstructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
SafeConstructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
|
||||
# UnsafeLoader is the same as Loader (which is and was always unsafe on
|
||||
# untrusted input). Use of either Loader or UnsafeLoader should be rare, since
|
||||
# FullLoad should be able to load almost all YAML safely. Loader is left intact
|
||||
# to ensure backwards compatibility.
|
||||
class UnsafeLoader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
Parser.__init__(self)
|
||||
Composer.__init__(self)
|
||||
Constructor.__init__(self)
|
||||
Resolver.__init__(self)
|
||||
@@ -0,0 +1,49 @@
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, tag, value, start_mark, end_mark):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
value = self.value
|
||||
#if isinstance(value, list):
|
||||
# if len(value) == 0:
|
||||
# value = '<empty>'
|
||||
# elif len(value) == 1:
|
||||
# value = '<1 item>'
|
||||
# else:
|
||||
# value = '<%d items>' % len(value)
|
||||
#else:
|
||||
# if len(value) > 75:
|
||||
# value = repr(value[:70]+u' ... ')
|
||||
# else:
|
||||
# value = repr(value)
|
||||
value = repr(value)
|
||||
return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
|
||||
|
||||
class ScalarNode(Node):
|
||||
id = 'scalar'
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
class CollectionNode(Node):
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, flow_style=None):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
class SequenceNode(CollectionNode):
|
||||
id = 'sequence'
|
||||
|
||||
class MappingNode(CollectionNode):
|
||||
id = 'mapping'
|
||||
|
||||
@@ -0,0 +1,589 @@
|
||||
|
||||
# The following YAML grammar is LL(1) and is parsed by a recursive descent
|
||||
# parser.
|
||||
#
|
||||
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
|
||||
# implicit_document ::= block_node DOCUMENT-END*
|
||||
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
|
||||
# block_node_or_indentless_sequence ::=
|
||||
# ALIAS
|
||||
# | properties (block_content | indentless_block_sequence)?
|
||||
# | block_content
|
||||
# | indentless_block_sequence
|
||||
# block_node ::= ALIAS
|
||||
# | properties block_content?
|
||||
# | block_content
|
||||
# flow_node ::= ALIAS
|
||||
# | properties flow_content?
|
||||
# | flow_content
|
||||
# properties ::= TAG ANCHOR? | ANCHOR TAG?
|
||||
# block_content ::= block_collection | flow_collection | SCALAR
|
||||
# flow_content ::= flow_collection | SCALAR
|
||||
# block_collection ::= block_sequence | block_mapping
|
||||
# flow_collection ::= flow_sequence | flow_mapping
|
||||
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
|
||||
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
|
||||
# block_mapping ::= BLOCK-MAPPING_START
|
||||
# ((KEY block_node_or_indentless_sequence?)?
|
||||
# (VALUE block_node_or_indentless_sequence?)?)*
|
||||
# BLOCK-END
|
||||
# flow_sequence ::= FLOW-SEQUENCE-START
|
||||
# (flow_sequence_entry FLOW-ENTRY)*
|
||||
# flow_sequence_entry?
|
||||
# FLOW-SEQUENCE-END
|
||||
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
# flow_mapping ::= FLOW-MAPPING-START
|
||||
# (flow_mapping_entry FLOW-ENTRY)*
|
||||
# flow_mapping_entry?
|
||||
# FLOW-MAPPING-END
|
||||
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
#
|
||||
# FIRST sets:
|
||||
#
|
||||
# stream: { STREAM-START }
|
||||
# explicit_document: { DIRECTIVE DOCUMENT-START }
|
||||
# implicit_document: FIRST(block_node)
|
||||
# block_node: { ALIAS TAG ANCHOR SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# flow_node: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# block_content: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
|
||||
# flow_content: { FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
|
||||
# block_collection: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START }
|
||||
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# block_sequence: { BLOCK-SEQUENCE-START }
|
||||
# block_mapping: { BLOCK-MAPPING-START }
|
||||
# block_node_or_indentless_sequence: { ALIAS ANCHOR TAG SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START BLOCK-ENTRY }
|
||||
# indentless_sequence: { ENTRY }
|
||||
# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
|
||||
# flow_sequence: { FLOW-SEQUENCE-START }
|
||||
# flow_mapping: { FLOW-MAPPING-START }
|
||||
# flow_sequence_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
|
||||
# flow_mapping_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
|
||||
|
||||
__all__ = ['Parser', 'ParserError']
|
||||
|
||||
from .error import MarkedYAMLError
|
||||
from .tokens import *
|
||||
from .events import *
|
||||
from .scanner import *
|
||||
|
||||
class ParserError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class Parser:
|
||||
# Since writing a recursive-descendant parser is a straightforward task, we
|
||||
# do not give many comments here.
|
||||
|
||||
DEFAULT_TAGS = {
|
||||
'!': '!',
|
||||
'!!': 'tag:yaml.org,2002:',
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.current_event = None
|
||||
self.yaml_version = None
|
||||
self.tag_handles = {}
|
||||
self.states = []
|
||||
self.marks = []
|
||||
self.state = self.parse_stream_start
|
||||
|
||||
def dispose(self):
|
||||
# Reset the state attributes (to clear self-references)
|
||||
self.states = []
|
||||
self.state = None
|
||||
|
||||
def check_event(self, *choices):
|
||||
# Check the type of the next event.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
if self.current_event is not None:
|
||||
if not choices:
|
||||
return True
|
||||
for choice in choices:
|
||||
if isinstance(self.current_event, choice):
|
||||
return True
|
||||
return False
|
||||
|
||||
def peek_event(self):
|
||||
# Get the next event.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
return self.current_event
|
||||
|
||||
def get_event(self):
|
||||
# Get the next event and proceed further.
|
||||
if self.current_event is None:
|
||||
if self.state:
|
||||
self.current_event = self.state()
|
||||
value = self.current_event
|
||||
self.current_event = None
|
||||
return value
|
||||
|
||||
# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
|
||||
# implicit_document ::= block_node DOCUMENT-END*
|
||||
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
|
||||
|
||||
def parse_stream_start(self):
|
||||
|
||||
# Parse the stream start.
|
||||
token = self.get_token()
|
||||
event = StreamStartEvent(token.start_mark, token.end_mark,
|
||||
encoding=token.encoding)
|
||||
|
||||
# Prepare the next state.
|
||||
self.state = self.parse_implicit_document_start
|
||||
|
||||
return event
|
||||
|
||||
def parse_implicit_document_start(self):
|
||||
|
||||
# Parse an implicit document.
|
||||
if not self.check_token(DirectiveToken, DocumentStartToken,
|
||||
StreamEndToken):
|
||||
self.tag_handles = self.DEFAULT_TAGS
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
event = DocumentStartEvent(start_mark, end_mark,
|
||||
explicit=False)
|
||||
|
||||
# Prepare the next state.
|
||||
self.states.append(self.parse_document_end)
|
||||
self.state = self.parse_block_node
|
||||
|
||||
return event
|
||||
|
||||
else:
|
||||
return self.parse_document_start()
|
||||
|
||||
def parse_document_start(self):
|
||||
|
||||
# Parse any extra document end indicators.
|
||||
while self.check_token(DocumentEndToken):
|
||||
self.get_token()
|
||||
|
||||
# Parse an explicit document.
|
||||
if not self.check_token(StreamEndToken):
|
||||
token = self.peek_token()
|
||||
start_mark = token.start_mark
|
||||
version, tags = self.process_directives()
|
||||
if not self.check_token(DocumentStartToken):
|
||||
raise ParserError(None, None,
|
||||
"expected '<document start>', but found %r"
|
||||
% self.peek_token().id,
|
||||
self.peek_token().start_mark)
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
event = DocumentStartEvent(start_mark, end_mark,
|
||||
explicit=True, version=version, tags=tags)
|
||||
self.states.append(self.parse_document_end)
|
||||
self.state = self.parse_document_content
|
||||
else:
|
||||
# Parse the end of the stream.
|
||||
token = self.get_token()
|
||||
event = StreamEndEvent(token.start_mark, token.end_mark)
|
||||
assert not self.states
|
||||
assert not self.marks
|
||||
self.state = None
|
||||
return event
|
||||
|
||||
def parse_document_end(self):
|
||||
|
||||
# Parse the document end.
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
explicit = False
|
||||
if self.check_token(DocumentEndToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
explicit = True
|
||||
event = DocumentEndEvent(start_mark, end_mark,
|
||||
explicit=explicit)
|
||||
|
||||
# Prepare the next state.
|
||||
self.state = self.parse_document_start
|
||||
|
||||
return event
|
||||
|
||||
def parse_document_content(self):
|
||||
if self.check_token(DirectiveToken,
|
||||
DocumentStartToken, DocumentEndToken, StreamEndToken):
|
||||
event = self.process_empty_scalar(self.peek_token().start_mark)
|
||||
self.state = self.states.pop()
|
||||
return event
|
||||
else:
|
||||
return self.parse_block_node()
|
||||
|
||||
def process_directives(self):
|
||||
self.yaml_version = None
|
||||
self.tag_handles = {}
|
||||
while self.check_token(DirectiveToken):
|
||||
token = self.get_token()
|
||||
if token.name == 'YAML':
|
||||
if self.yaml_version is not None:
|
||||
raise ParserError(None, None,
|
||||
"found duplicate YAML directive", token.start_mark)
|
||||
major, minor = token.value
|
||||
if major != 1:
|
||||
raise ParserError(None, None,
|
||||
"found incompatible YAML document (version 1.* is required)",
|
||||
token.start_mark)
|
||||
self.yaml_version = token.value
|
||||
elif token.name == 'TAG':
|
||||
handle, prefix = token.value
|
||||
if handle in self.tag_handles:
|
||||
raise ParserError(None, None,
|
||||
"duplicate tag handle %r" % handle,
|
||||
token.start_mark)
|
||||
self.tag_handles[handle] = prefix
|
||||
if self.tag_handles:
|
||||
value = self.yaml_version, self.tag_handles.copy()
|
||||
else:
|
||||
value = self.yaml_version, None
|
||||
for key in self.DEFAULT_TAGS:
|
||||
if key not in self.tag_handles:
|
||||
self.tag_handles[key] = self.DEFAULT_TAGS[key]
|
||||
return value
|
||||
|
||||
# block_node_or_indentless_sequence ::= ALIAS
|
||||
# | properties (block_content | indentless_block_sequence)?
|
||||
# | block_content
|
||||
# | indentless_block_sequence
|
||||
# block_node ::= ALIAS
|
||||
# | properties block_content?
|
||||
# | block_content
|
||||
# flow_node ::= ALIAS
|
||||
# | properties flow_content?
|
||||
# | flow_content
|
||||
# properties ::= TAG ANCHOR? | ANCHOR TAG?
|
||||
# block_content ::= block_collection | flow_collection | SCALAR
|
||||
# flow_content ::= flow_collection | SCALAR
|
||||
# block_collection ::= block_sequence | block_mapping
|
||||
# flow_collection ::= flow_sequence | flow_mapping
|
||||
|
||||
def parse_block_node(self):
|
||||
return self.parse_node(block=True)
|
||||
|
||||
def parse_flow_node(self):
|
||||
return self.parse_node()
|
||||
|
||||
def parse_block_node_or_indentless_sequence(self):
|
||||
return self.parse_node(block=True, indentless_sequence=True)
|
||||
|
||||
def parse_node(self, block=False, indentless_sequence=False):
|
||||
if self.check_token(AliasToken):
|
||||
token = self.get_token()
|
||||
event = AliasEvent(token.value, token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
else:
|
||||
anchor = None
|
||||
tag = None
|
||||
start_mark = end_mark = tag_mark = None
|
||||
if self.check_token(AnchorToken):
|
||||
token = self.get_token()
|
||||
start_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
anchor = token.value
|
||||
if self.check_token(TagToken):
|
||||
token = self.get_token()
|
||||
tag_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
tag = token.value
|
||||
elif self.check_token(TagToken):
|
||||
token = self.get_token()
|
||||
start_mark = tag_mark = token.start_mark
|
||||
end_mark = token.end_mark
|
||||
tag = token.value
|
||||
if self.check_token(AnchorToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
anchor = token.value
|
||||
if tag is not None:
|
||||
handle, suffix = tag
|
||||
if handle is not None:
|
||||
if handle not in self.tag_handles:
|
||||
raise ParserError("while parsing a node", start_mark,
|
||||
"found undefined tag handle %r" % handle,
|
||||
tag_mark)
|
||||
tag = self.tag_handles[handle]+suffix
|
||||
else:
|
||||
tag = suffix
|
||||
#if tag == '!':
|
||||
# raise ParserError("while parsing a node", start_mark,
|
||||
# "found non-specific tag '!'", tag_mark,
|
||||
# "Please check 'http://pyyaml.org/wiki/YAMLNonSpecificTag' and share your opinion.")
|
||||
if start_mark is None:
|
||||
start_mark = end_mark = self.peek_token().start_mark
|
||||
event = None
|
||||
implicit = (tag is None or tag == '!')
|
||||
if indentless_sequence and self.check_token(BlockEntryToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark)
|
||||
self.state = self.parse_indentless_sequence_entry
|
||||
else:
|
||||
if self.check_token(ScalarToken):
|
||||
token = self.get_token()
|
||||
end_mark = token.end_mark
|
||||
if (token.plain and tag is None) or tag == '!':
|
||||
implicit = (True, False)
|
||||
elif tag is None:
|
||||
implicit = (False, True)
|
||||
else:
|
||||
implicit = (False, False)
|
||||
event = ScalarEvent(anchor, tag, implicit, token.value,
|
||||
start_mark, end_mark, style=token.style)
|
||||
self.state = self.states.pop()
|
||||
elif self.check_token(FlowSequenceStartToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=True)
|
||||
self.state = self.parse_flow_sequence_first_entry
|
||||
elif self.check_token(FlowMappingStartToken):
|
||||
end_mark = self.peek_token().end_mark
|
||||
event = MappingStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=True)
|
||||
self.state = self.parse_flow_mapping_first_key
|
||||
elif block and self.check_token(BlockSequenceStartToken):
|
||||
end_mark = self.peek_token().start_mark
|
||||
event = SequenceStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=False)
|
||||
self.state = self.parse_block_sequence_first_entry
|
||||
elif block and self.check_token(BlockMappingStartToken):
|
||||
end_mark = self.peek_token().start_mark
|
||||
event = MappingStartEvent(anchor, tag, implicit,
|
||||
start_mark, end_mark, flow_style=False)
|
||||
self.state = self.parse_block_mapping_first_key
|
||||
elif anchor is not None or tag is not None:
|
||||
# Empty scalars are allowed even if a tag or an anchor is
|
||||
# specified.
|
||||
event = ScalarEvent(anchor, tag, (implicit, False), '',
|
||||
start_mark, end_mark)
|
||||
self.state = self.states.pop()
|
||||
else:
|
||||
if block:
|
||||
node = 'block'
|
||||
else:
|
||||
node = 'flow'
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a %s node" % node, start_mark,
|
||||
"expected the node content, but found %r" % token.id,
|
||||
token.start_mark)
|
||||
return event
|
||||
|
||||
# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
|
||||
|
||||
def parse_block_sequence_first_entry(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_block_sequence_entry()
|
||||
|
||||
def parse_block_sequence_entry(self):
|
||||
if self.check_token(BlockEntryToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(BlockEntryToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_sequence_entry)
|
||||
return self.parse_block_node()
|
||||
else:
|
||||
self.state = self.parse_block_sequence_entry
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
if not self.check_token(BlockEndToken):
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a block collection", self.marks[-1],
|
||||
"expected <block end>, but found %r" % token.id, token.start_mark)
|
||||
token = self.get_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
|
||||
|
||||
def parse_indentless_sequence_entry(self):
|
||||
if self.check_token(BlockEntryToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(BlockEntryToken,
|
||||
KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_indentless_sequence_entry)
|
||||
return self.parse_block_node()
|
||||
else:
|
||||
self.state = self.parse_indentless_sequence_entry
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
token = self.peek_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.start_mark)
|
||||
self.state = self.states.pop()
|
||||
return event
|
||||
|
||||
# block_mapping ::= BLOCK-MAPPING_START
|
||||
# ((KEY block_node_or_indentless_sequence?)?
|
||||
# (VALUE block_node_or_indentless_sequence?)?)*
|
||||
# BLOCK-END
|
||||
|
||||
def parse_block_mapping_first_key(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_block_mapping_key()
|
||||
|
||||
def parse_block_mapping_key(self):
|
||||
if self.check_token(KeyToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_mapping_value)
|
||||
return self.parse_block_node_or_indentless_sequence()
|
||||
else:
|
||||
self.state = self.parse_block_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
if not self.check_token(BlockEndToken):
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a block mapping", self.marks[-1],
|
||||
"expected <block end>, but found %r" % token.id, token.start_mark)
|
||||
token = self.get_token()
|
||||
event = MappingEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_block_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(KeyToken, ValueToken, BlockEndToken):
|
||||
self.states.append(self.parse_block_mapping_key)
|
||||
return self.parse_block_node_or_indentless_sequence()
|
||||
else:
|
||||
self.state = self.parse_block_mapping_key
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_block_mapping_key
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
# flow_sequence ::= FLOW-SEQUENCE-START
|
||||
# (flow_sequence_entry FLOW-ENTRY)*
|
||||
# flow_sequence_entry?
|
||||
# FLOW-SEQUENCE-END
|
||||
# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
#
|
||||
# Note that while production rules for both flow_sequence_entry and
|
||||
# flow_mapping_entry are equal, their interpretations are different.
|
||||
# For `flow_sequence_entry`, the part `KEY flow_node? (VALUE flow_node?)?`
|
||||
# generate an inline mapping (set syntax).
|
||||
|
||||
def parse_flow_sequence_first_entry(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_flow_sequence_entry(first=True)
|
||||
|
||||
def parse_flow_sequence_entry(self, first=False):
|
||||
if not self.check_token(FlowSequenceEndToken):
|
||||
if not first:
|
||||
if self.check_token(FlowEntryToken):
|
||||
self.get_token()
|
||||
else:
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a flow sequence", self.marks[-1],
|
||||
"expected ',' or ']', but got %r" % token.id, token.start_mark)
|
||||
|
||||
if self.check_token(KeyToken):
|
||||
token = self.peek_token()
|
||||
event = MappingStartEvent(None, None, True,
|
||||
token.start_mark, token.end_mark,
|
||||
flow_style=True)
|
||||
self.state = self.parse_flow_sequence_entry_mapping_key
|
||||
return event
|
||||
elif not self.check_token(FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry)
|
||||
return self.parse_flow_node()
|
||||
token = self.get_token()
|
||||
event = SequenceEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_flow_sequence_entry_mapping_key(self):
|
||||
token = self.get_token()
|
||||
if not self.check_token(ValueToken,
|
||||
FlowEntryToken, FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry_mapping_value)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
|
||||
def parse_flow_sequence_entry_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(FlowEntryToken, FlowSequenceEndToken):
|
||||
self.states.append(self.parse_flow_sequence_entry_mapping_end)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_end
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_flow_sequence_entry_mapping_end
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
def parse_flow_sequence_entry_mapping_end(self):
|
||||
self.state = self.parse_flow_sequence_entry
|
||||
token = self.peek_token()
|
||||
return MappingEndEvent(token.start_mark, token.start_mark)
|
||||
|
||||
# flow_mapping ::= FLOW-MAPPING-START
|
||||
# (flow_mapping_entry FLOW-ENTRY)*
|
||||
# flow_mapping_entry?
|
||||
# FLOW-MAPPING-END
|
||||
# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
|
||||
|
||||
def parse_flow_mapping_first_key(self):
|
||||
token = self.get_token()
|
||||
self.marks.append(token.start_mark)
|
||||
return self.parse_flow_mapping_key(first=True)
|
||||
|
||||
def parse_flow_mapping_key(self, first=False):
|
||||
if not self.check_token(FlowMappingEndToken):
|
||||
if not first:
|
||||
if self.check_token(FlowEntryToken):
|
||||
self.get_token()
|
||||
else:
|
||||
token = self.peek_token()
|
||||
raise ParserError("while parsing a flow mapping", self.marks[-1],
|
||||
"expected ',' or '}', but got %r" % token.id, token.start_mark)
|
||||
if self.check_token(KeyToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(ValueToken,
|
||||
FlowEntryToken, FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_value)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_value
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
elif not self.check_token(FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_empty_value)
|
||||
return self.parse_flow_node()
|
||||
token = self.get_token()
|
||||
event = MappingEndEvent(token.start_mark, token.end_mark)
|
||||
self.state = self.states.pop()
|
||||
self.marks.pop()
|
||||
return event
|
||||
|
||||
def parse_flow_mapping_value(self):
|
||||
if self.check_token(ValueToken):
|
||||
token = self.get_token()
|
||||
if not self.check_token(FlowEntryToken, FlowMappingEndToken):
|
||||
self.states.append(self.parse_flow_mapping_key)
|
||||
return self.parse_flow_node()
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_key
|
||||
return self.process_empty_scalar(token.end_mark)
|
||||
else:
|
||||
self.state = self.parse_flow_mapping_key
|
||||
token = self.peek_token()
|
||||
return self.process_empty_scalar(token.start_mark)
|
||||
|
||||
def parse_flow_mapping_empty_value(self):
|
||||
self.state = self.parse_flow_mapping_key
|
||||
return self.process_empty_scalar(self.peek_token().start_mark)
|
||||
|
||||
def process_empty_scalar(self, mark):
|
||||
return ScalarEvent(None, None, (True, False), '', mark, mark)
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
# This module contains abstractions for the input stream. You don't have to
|
||||
# looks further, there are no pretty code.
|
||||
#
|
||||
# We define two classes here.
|
||||
#
|
||||
# Mark(source, line, column)
|
||||
# It's just a record and its only use is producing nice error messages.
|
||||
# Parser does not use it for any other purposes.
|
||||
#
|
||||
# Reader(source, data)
|
||||
# Reader determines the encoding of `data` and converts it to unicode.
|
||||
# Reader provides the following methods and attributes:
|
||||
# reader.peek(length=1) - return the next `length` characters
|
||||
# reader.forward(length=1) - move the current position to `length` characters.
|
||||
# reader.index - the number of the current character.
|
||||
# reader.line, stream.column - the line and the column of the current character.
|
||||
|
||||
__all__ = ['Reader', 'ReaderError']
|
||||
|
||||
from .error import YAMLError, Mark
|
||||
|
||||
import codecs, re
|
||||
|
||||
class ReaderError(YAMLError):
|
||||
|
||||
def __init__(self, name, position, character, encoding, reason):
|
||||
self.name = name
|
||||
self.character = character
|
||||
self.position = position
|
||||
self.encoding = encoding
|
||||
self.reason = reason
|
||||
|
||||
def __str__(self):
|
||||
if isinstance(self.character, bytes):
|
||||
return "'%s' codec can't decode byte #x%02x: %s\n" \
|
||||
" in \"%s\", position %d" \
|
||||
% (self.encoding, ord(self.character), self.reason,
|
||||
self.name, self.position)
|
||||
else:
|
||||
return "unacceptable character #x%04x: %s\n" \
|
||||
" in \"%s\", position %d" \
|
||||
% (self.character, self.reason,
|
||||
self.name, self.position)
|
||||
|
||||
class Reader(object):
|
||||
# Reader:
|
||||
# - determines the data encoding and converts it to a unicode string,
|
||||
# - checks if characters are in allowed range,
|
||||
# - adds '\0' to the end.
|
||||
|
||||
# Reader accepts
|
||||
# - a `bytes` object,
|
||||
# - a `str` object,
|
||||
# - a file-like object with its `read` method returning `str`,
|
||||
# - a file-like object with its `read` method returning `unicode`.
|
||||
|
||||
# Yeah, it's ugly and slow.
|
||||
|
||||
def __init__(self, stream):
|
||||
self.name = None
|
||||
self.stream = None
|
||||
self.stream_pointer = 0
|
||||
self.eof = True
|
||||
self.buffer = ''
|
||||
self.pointer = 0
|
||||
self.raw_buffer = None
|
||||
self.raw_decode = None
|
||||
self.encoding = None
|
||||
self.index = 0
|
||||
self.line = 0
|
||||
self.column = 0
|
||||
if isinstance(stream, str):
|
||||
self.name = "<unicode string>"
|
||||
self.check_printable(stream)
|
||||
self.buffer = stream+'\0'
|
||||
elif isinstance(stream, bytes):
|
||||
self.name = "<byte string>"
|
||||
self.raw_buffer = stream
|
||||
self.determine_encoding()
|
||||
else:
|
||||
self.stream = stream
|
||||
self.name = getattr(stream, 'name', "<file>")
|
||||
self.eof = False
|
||||
self.raw_buffer = None
|
||||
self.determine_encoding()
|
||||
|
||||
def peek(self, index=0):
|
||||
try:
|
||||
return self.buffer[self.pointer+index]
|
||||
except IndexError:
|
||||
self.update(index+1)
|
||||
return self.buffer[self.pointer+index]
|
||||
|
||||
def prefix(self, length=1):
|
||||
if self.pointer+length >= len(self.buffer):
|
||||
self.update(length)
|
||||
return self.buffer[self.pointer:self.pointer+length]
|
||||
|
||||
def forward(self, length=1):
|
||||
if self.pointer+length+1 >= len(self.buffer):
|
||||
self.update(length+1)
|
||||
while length:
|
||||
ch = self.buffer[self.pointer]
|
||||
self.pointer += 1
|
||||
self.index += 1
|
||||
if ch in '\n\x85\u2028\u2029' \
|
||||
or (ch == '\r' and self.buffer[self.pointer] != '\n'):
|
||||
self.line += 1
|
||||
self.column = 0
|
||||
elif ch != '\uFEFF':
|
||||
self.column += 1
|
||||
length -= 1
|
||||
|
||||
def get_mark(self):
|
||||
if self.stream is None:
|
||||
return Mark(self.name, self.index, self.line, self.column,
|
||||
self.buffer, self.pointer)
|
||||
else:
|
||||
return Mark(self.name, self.index, self.line, self.column,
|
||||
None, None)
|
||||
|
||||
def determine_encoding(self):
|
||||
while not self.eof and (self.raw_buffer is None or len(self.raw_buffer) < 2):
|
||||
self.update_raw()
|
||||
if isinstance(self.raw_buffer, bytes):
|
||||
if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
|
||||
self.raw_decode = codecs.utf_16_le_decode
|
||||
self.encoding = 'utf-16-le'
|
||||
elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
|
||||
self.raw_decode = codecs.utf_16_be_decode
|
||||
self.encoding = 'utf-16-be'
|
||||
else:
|
||||
self.raw_decode = codecs.utf_8_decode
|
||||
self.encoding = 'utf-8'
|
||||
self.update(1)
|
||||
|
||||
NON_PRINTABLE = re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010ffff]')
|
||||
def check_printable(self, data):
|
||||
match = self.NON_PRINTABLE.search(data)
|
||||
if match:
|
||||
character = match.group()
|
||||
position = self.index+(len(self.buffer)-self.pointer)+match.start()
|
||||
raise ReaderError(self.name, position, ord(character),
|
||||
'unicode', "special characters are not allowed")
|
||||
|
||||
def update(self, length):
|
||||
if self.raw_buffer is None:
|
||||
return
|
||||
self.buffer = self.buffer[self.pointer:]
|
||||
self.pointer = 0
|
||||
while len(self.buffer) < length:
|
||||
if not self.eof:
|
||||
self.update_raw()
|
||||
if self.raw_decode is not None:
|
||||
try:
|
||||
data, converted = self.raw_decode(self.raw_buffer,
|
||||
'strict', self.eof)
|
||||
except UnicodeDecodeError as exc:
|
||||
character = self.raw_buffer[exc.start]
|
||||
if self.stream is not None:
|
||||
position = self.stream_pointer-len(self.raw_buffer)+exc.start
|
||||
else:
|
||||
position = exc.start
|
||||
raise ReaderError(self.name, position, character,
|
||||
exc.encoding, exc.reason)
|
||||
else:
|
||||
data = self.raw_buffer
|
||||
converted = len(data)
|
||||
self.check_printable(data)
|
||||
self.buffer += data
|
||||
self.raw_buffer = self.raw_buffer[converted:]
|
||||
if self.eof:
|
||||
self.buffer += '\0'
|
||||
self.raw_buffer = None
|
||||
break
|
||||
|
||||
def update_raw(self, size=4096):
|
||||
data = self.stream.read(size)
|
||||
if self.raw_buffer is None:
|
||||
self.raw_buffer = data
|
||||
else:
|
||||
self.raw_buffer += data
|
||||
self.stream_pointer += len(data)
|
||||
if not data:
|
||||
self.eof = True
|
||||
@@ -0,0 +1,389 @@
|
||||
|
||||
__all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
|
||||
'RepresenterError']
|
||||
|
||||
from .error import *
|
||||
from .nodes import *
|
||||
|
||||
import datetime, copyreg, types, base64, collections
|
||||
|
||||
class RepresenterError(YAMLError):
|
||||
pass
|
||||
|
||||
class BaseRepresenter:
|
||||
|
||||
yaml_representers = {}
|
||||
yaml_multi_representers = {}
|
||||
|
||||
def __init__(self, default_style=None, default_flow_style=False, sort_keys=True):
|
||||
self.default_style = default_style
|
||||
self.sort_keys = sort_keys
|
||||
self.default_flow_style = default_flow_style
|
||||
self.represented_objects = {}
|
||||
self.object_keeper = []
|
||||
self.alias_key = None
|
||||
|
||||
def represent(self, data):
|
||||
node = self.represent_data(data)
|
||||
self.serialize(node)
|
||||
self.represented_objects = {}
|
||||
self.object_keeper = []
|
||||
self.alias_key = None
|
||||
|
||||
def represent_data(self, data):
|
||||
if self.ignore_aliases(data):
|
||||
self.alias_key = None
|
||||
else:
|
||||
self.alias_key = id(data)
|
||||
if self.alias_key is not None:
|
||||
if self.alias_key in self.represented_objects:
|
||||
node = self.represented_objects[self.alias_key]
|
||||
#if node is None:
|
||||
# raise RepresenterError("recursive objects are not allowed: %r" % data)
|
||||
return node
|
||||
#self.represented_objects[alias_key] = None
|
||||
self.object_keeper.append(data)
|
||||
data_types = type(data).__mro__
|
||||
if data_types[0] in self.yaml_representers:
|
||||
node = self.yaml_representers[data_types[0]](self, data)
|
||||
else:
|
||||
for data_type in data_types:
|
||||
if data_type in self.yaml_multi_representers:
|
||||
node = self.yaml_multi_representers[data_type](self, data)
|
||||
break
|
||||
else:
|
||||
if None in self.yaml_multi_representers:
|
||||
node = self.yaml_multi_representers[None](self, data)
|
||||
elif None in self.yaml_representers:
|
||||
node = self.yaml_representers[None](self, data)
|
||||
else:
|
||||
node = ScalarNode(None, str(data))
|
||||
#if alias_key is not None:
|
||||
# self.represented_objects[alias_key] = node
|
||||
return node
|
||||
|
||||
@classmethod
|
||||
def add_representer(cls, data_type, representer):
|
||||
if not 'yaml_representers' in cls.__dict__:
|
||||
cls.yaml_representers = cls.yaml_representers.copy()
|
||||
cls.yaml_representers[data_type] = representer
|
||||
|
||||
@classmethod
|
||||
def add_multi_representer(cls, data_type, representer):
|
||||
if not 'yaml_multi_representers' in cls.__dict__:
|
||||
cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
|
||||
cls.yaml_multi_representers[data_type] = representer
|
||||
|
||||
def represent_scalar(self, tag, value, style=None):
|
||||
if style is None:
|
||||
style = self.default_style
|
||||
node = ScalarNode(tag, value, style=style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
return node
|
||||
|
||||
def represent_sequence(self, tag, sequence, flow_style=None):
|
||||
value = []
|
||||
node = SequenceNode(tag, value, flow_style=flow_style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
best_style = True
|
||||
for item in sequence:
|
||||
node_item = self.represent_data(item)
|
||||
if not (isinstance(node_item, ScalarNode) and not node_item.style):
|
||||
best_style = False
|
||||
value.append(node_item)
|
||||
if flow_style is None:
|
||||
if self.default_flow_style is not None:
|
||||
node.flow_style = self.default_flow_style
|
||||
else:
|
||||
node.flow_style = best_style
|
||||
return node
|
||||
|
||||
def represent_mapping(self, tag, mapping, flow_style=None):
|
||||
value = []
|
||||
node = MappingNode(tag, value, flow_style=flow_style)
|
||||
if self.alias_key is not None:
|
||||
self.represented_objects[self.alias_key] = node
|
||||
best_style = True
|
||||
if hasattr(mapping, 'items'):
|
||||
mapping = list(mapping.items())
|
||||
if self.sort_keys:
|
||||
try:
|
||||
mapping = sorted(mapping)
|
||||
except TypeError:
|
||||
pass
|
||||
for item_key, item_value in mapping:
|
||||
node_key = self.represent_data(item_key)
|
||||
node_value = self.represent_data(item_value)
|
||||
if not (isinstance(node_key, ScalarNode) and not node_key.style):
|
||||
best_style = False
|
||||
if not (isinstance(node_value, ScalarNode) and not node_value.style):
|
||||
best_style = False
|
||||
value.append((node_key, node_value))
|
||||
if flow_style is None:
|
||||
if self.default_flow_style is not None:
|
||||
node.flow_style = self.default_flow_style
|
||||
else:
|
||||
node.flow_style = best_style
|
||||
return node
|
||||
|
||||
def ignore_aliases(self, data):
|
||||
return False
|
||||
|
||||
class SafeRepresenter(BaseRepresenter):
|
||||
|
||||
def ignore_aliases(self, data):
|
||||
if data is None:
|
||||
return True
|
||||
if isinstance(data, tuple) and data == ():
|
||||
return True
|
||||
if isinstance(data, (str, bytes, bool, int, float)):
|
||||
return True
|
||||
|
||||
def represent_none(self, data):
|
||||
return self.represent_scalar('tag:yaml.org,2002:null', 'null')
|
||||
|
||||
def represent_str(self, data):
|
||||
return self.represent_scalar('tag:yaml.org,2002:str', data)
|
||||
|
||||
def represent_binary(self, data):
|
||||
if hasattr(base64, 'encodebytes'):
|
||||
data = base64.encodebytes(data).decode('ascii')
|
||||
else:
|
||||
data = base64.encodestring(data).decode('ascii')
|
||||
return self.represent_scalar('tag:yaml.org,2002:binary', data, style='|')
|
||||
|
||||
def represent_bool(self, data):
|
||||
if data:
|
||||
value = 'true'
|
||||
else:
|
||||
value = 'false'
|
||||
return self.represent_scalar('tag:yaml.org,2002:bool', value)
|
||||
|
||||
def represent_int(self, data):
|
||||
return self.represent_scalar('tag:yaml.org,2002:int', str(data))
|
||||
|
||||
inf_value = 1e300
|
||||
while repr(inf_value) != repr(inf_value*inf_value):
|
||||
inf_value *= inf_value
|
||||
|
||||
def represent_float(self, data):
|
||||
if data != data or (data == 0.0 and data == 1.0):
|
||||
value = '.nan'
|
||||
elif data == self.inf_value:
|
||||
value = '.inf'
|
||||
elif data == -self.inf_value:
|
||||
value = '-.inf'
|
||||
else:
|
||||
value = repr(data).lower()
|
||||
# Note that in some cases `repr(data)` represents a float number
|
||||
# without the decimal parts. For instance:
|
||||
# >>> repr(1e17)
|
||||
# '1e17'
|
||||
# Unfortunately, this is not a valid float representation according
|
||||
# to the definition of the `!!float` tag. We fix this by adding
|
||||
# '.0' before the 'e' symbol.
|
||||
if '.' not in value and 'e' in value:
|
||||
value = value.replace('e', '.0e', 1)
|
||||
return self.represent_scalar('tag:yaml.org,2002:float', value)
|
||||
|
||||
def represent_list(self, data):
|
||||
#pairs = (len(data) > 0 and isinstance(data, list))
|
||||
#if pairs:
|
||||
# for item in data:
|
||||
# if not isinstance(item, tuple) or len(item) != 2:
|
||||
# pairs = False
|
||||
# break
|
||||
#if not pairs:
|
||||
return self.represent_sequence('tag:yaml.org,2002:seq', data)
|
||||
#value = []
|
||||
#for item_key, item_value in data:
|
||||
# value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
|
||||
# [(item_key, item_value)]))
|
||||
#return SequenceNode(u'tag:yaml.org,2002:pairs', value)
|
||||
|
||||
def represent_dict(self, data):
|
||||
return self.represent_mapping('tag:yaml.org,2002:map', data)
|
||||
|
||||
def represent_set(self, data):
|
||||
value = {}
|
||||
for key in data:
|
||||
value[key] = None
|
||||
return self.represent_mapping('tag:yaml.org,2002:set', value)
|
||||
|
||||
def represent_date(self, data):
|
||||
value = data.isoformat()
|
||||
return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
|
||||
|
||||
def represent_datetime(self, data):
|
||||
value = data.isoformat(' ')
|
||||
return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
|
||||
|
||||
def represent_yaml_object(self, tag, data, cls, flow_style=None):
|
||||
if hasattr(data, '__getstate__'):
|
||||
state = data.__getstate__()
|
||||
else:
|
||||
state = data.__dict__.copy()
|
||||
return self.represent_mapping(tag, state, flow_style=flow_style)
|
||||
|
||||
def represent_undefined(self, data):
|
||||
raise RepresenterError("cannot represent an object", data)
|
||||
|
||||
SafeRepresenter.add_representer(type(None),
|
||||
SafeRepresenter.represent_none)
|
||||
|
||||
SafeRepresenter.add_representer(str,
|
||||
SafeRepresenter.represent_str)
|
||||
|
||||
SafeRepresenter.add_representer(bytes,
|
||||
SafeRepresenter.represent_binary)
|
||||
|
||||
SafeRepresenter.add_representer(bool,
|
||||
SafeRepresenter.represent_bool)
|
||||
|
||||
SafeRepresenter.add_representer(int,
|
||||
SafeRepresenter.represent_int)
|
||||
|
||||
SafeRepresenter.add_representer(float,
|
||||
SafeRepresenter.represent_float)
|
||||
|
||||
SafeRepresenter.add_representer(list,
|
||||
SafeRepresenter.represent_list)
|
||||
|
||||
SafeRepresenter.add_representer(tuple,
|
||||
SafeRepresenter.represent_list)
|
||||
|
||||
SafeRepresenter.add_representer(dict,
|
||||
SafeRepresenter.represent_dict)
|
||||
|
||||
SafeRepresenter.add_representer(set,
|
||||
SafeRepresenter.represent_set)
|
||||
|
||||
SafeRepresenter.add_representer(datetime.date,
|
||||
SafeRepresenter.represent_date)
|
||||
|
||||
SafeRepresenter.add_representer(datetime.datetime,
|
||||
SafeRepresenter.represent_datetime)
|
||||
|
||||
SafeRepresenter.add_representer(None,
|
||||
SafeRepresenter.represent_undefined)
|
||||
|
||||
class Representer(SafeRepresenter):
|
||||
|
||||
def represent_complex(self, data):
|
||||
if data.imag == 0.0:
|
||||
data = '%r' % data.real
|
||||
elif data.real == 0.0:
|
||||
data = '%rj' % data.imag
|
||||
elif data.imag > 0:
|
||||
data = '%r+%rj' % (data.real, data.imag)
|
||||
else:
|
||||
data = '%r%rj' % (data.real, data.imag)
|
||||
return self.represent_scalar('tag:yaml.org,2002:python/complex', data)
|
||||
|
||||
def represent_tuple(self, data):
|
||||
return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)
|
||||
|
||||
def represent_name(self, data):
|
||||
name = '%s.%s' % (data.__module__, data.__name__)
|
||||
return self.represent_scalar('tag:yaml.org,2002:python/name:'+name, '')
|
||||
|
||||
def represent_module(self, data):
|
||||
return self.represent_scalar(
|
||||
'tag:yaml.org,2002:python/module:'+data.__name__, '')
|
||||
|
||||
def represent_object(self, data):
|
||||
# We use __reduce__ API to save the data. data.__reduce__ returns
|
||||
# a tuple of length 2-5:
|
||||
# (function, args, state, listitems, dictitems)
|
||||
|
||||
# For reconstructing, we calls function(*args), then set its state,
|
||||
# listitems, and dictitems if they are not None.
|
||||
|
||||
# A special case is when function.__name__ == '__newobj__'. In this
|
||||
# case we create the object with args[0].__new__(*args).
|
||||
|
||||
# Another special case is when __reduce__ returns a string - we don't
|
||||
# support it.
|
||||
|
||||
# We produce a !!python/object, !!python/object/new or
|
||||
# !!python/object/apply node.
|
||||
|
||||
cls = type(data)
|
||||
if cls in copyreg.dispatch_table:
|
||||
reduce = copyreg.dispatch_table[cls](data)
|
||||
elif hasattr(data, '__reduce_ex__'):
|
||||
reduce = data.__reduce_ex__(2)
|
||||
elif hasattr(data, '__reduce__'):
|
||||
reduce = data.__reduce__()
|
||||
else:
|
||||
raise RepresenterError("cannot represent an object", data)
|
||||
reduce = (list(reduce)+[None]*5)[:5]
|
||||
function, args, state, listitems, dictitems = reduce
|
||||
args = list(args)
|
||||
if state is None:
|
||||
state = {}
|
||||
if listitems is not None:
|
||||
listitems = list(listitems)
|
||||
if dictitems is not None:
|
||||
dictitems = dict(dictitems)
|
||||
if function.__name__ == '__newobj__':
|
||||
function = args[0]
|
||||
args = args[1:]
|
||||
tag = 'tag:yaml.org,2002:python/object/new:'
|
||||
newobj = True
|
||||
else:
|
||||
tag = 'tag:yaml.org,2002:python/object/apply:'
|
||||
newobj = False
|
||||
function_name = '%s.%s' % (function.__module__, function.__name__)
|
||||
if not args and not listitems and not dictitems \
|
||||
and isinstance(state, dict) and newobj:
|
||||
return self.represent_mapping(
|
||||
'tag:yaml.org,2002:python/object:'+function_name, state)
|
||||
if not listitems and not dictitems \
|
||||
and isinstance(state, dict) and not state:
|
||||
return self.represent_sequence(tag+function_name, args)
|
||||
value = {}
|
||||
if args:
|
||||
value['args'] = args
|
||||
if state or not isinstance(state, dict):
|
||||
value['state'] = state
|
||||
if listitems:
|
||||
value['listitems'] = listitems
|
||||
if dictitems:
|
||||
value['dictitems'] = dictitems
|
||||
return self.represent_mapping(tag+function_name, value)
|
||||
|
||||
def represent_ordered_dict(self, data):
|
||||
# Provide uniform representation across different Python versions.
|
||||
data_type = type(data)
|
||||
tag = 'tag:yaml.org,2002:python/object/apply:%s.%s' \
|
||||
% (data_type.__module__, data_type.__name__)
|
||||
items = [[key, value] for key, value in data.items()]
|
||||
return self.represent_sequence(tag, [items])
|
||||
|
||||
Representer.add_representer(complex,
|
||||
Representer.represent_complex)
|
||||
|
||||
Representer.add_representer(tuple,
|
||||
Representer.represent_tuple)
|
||||
|
||||
Representer.add_representer(type,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(collections.OrderedDict,
|
||||
Representer.represent_ordered_dict)
|
||||
|
||||
Representer.add_representer(types.FunctionType,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.BuiltinFunctionType,
|
||||
Representer.represent_name)
|
||||
|
||||
Representer.add_representer(types.ModuleType,
|
||||
Representer.represent_module)
|
||||
|
||||
Representer.add_multi_representer(object,
|
||||
Representer.represent_object)
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
|
||||
__all__ = ['BaseResolver', 'Resolver']
|
||||
|
||||
from .error import *
|
||||
from .nodes import *
|
||||
|
||||
import re
|
||||
|
||||
class ResolverError(YAMLError):
|
||||
pass
|
||||
|
||||
class BaseResolver:
|
||||
|
||||
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
||||
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
|
||||
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
||||
|
||||
yaml_implicit_resolvers = {}
|
||||
yaml_path_resolvers = {}
|
||||
|
||||
def __init__(self):
|
||||
self.resolver_exact_paths = []
|
||||
self.resolver_prefix_paths = []
|
||||
|
||||
@classmethod
|
||||
def add_implicit_resolver(cls, tag, regexp, first):
|
||||
if not 'yaml_implicit_resolvers' in cls.__dict__:
|
||||
implicit_resolvers = {}
|
||||
for key in cls.yaml_implicit_resolvers:
|
||||
implicit_resolvers[key] = cls.yaml_implicit_resolvers[key][:]
|
||||
cls.yaml_implicit_resolvers = implicit_resolvers
|
||||
if first is None:
|
||||
first = [None]
|
||||
for ch in first:
|
||||
cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
|
||||
|
||||
@classmethod
|
||||
def add_path_resolver(cls, tag, path, kind=None):
|
||||
# Note: `add_path_resolver` is experimental. The API could be changed.
|
||||
# `new_path` is a pattern that is matched against the path from the
|
||||
# root to the node that is being considered. `node_path` elements are
|
||||
# tuples `(node_check, index_check)`. `node_check` is a node class:
|
||||
# `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
|
||||
# matches any kind of a node. `index_check` could be `None`, a boolean
|
||||
# value, a string value, or a number. `None` and `False` match against
|
||||
# any _value_ of sequence and mapping nodes. `True` matches against
|
||||
# any _key_ of a mapping node. A string `index_check` matches against
|
||||
# a mapping value that corresponds to a scalar key which content is
|
||||
# equal to the `index_check` value. An integer `index_check` matches
|
||||
# against a sequence value with the index equal to `index_check`.
|
||||
if not 'yaml_path_resolvers' in cls.__dict__:
|
||||
cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
|
||||
new_path = []
|
||||
for element in path:
|
||||
if isinstance(element, (list, tuple)):
|
||||
if len(element) == 2:
|
||||
node_check, index_check = element
|
||||
elif len(element) == 1:
|
||||
node_check = element[0]
|
||||
index_check = True
|
||||
else:
|
||||
raise ResolverError("Invalid path element: %s" % element)
|
||||
else:
|
||||
node_check = None
|
||||
index_check = element
|
||||
if node_check is str:
|
||||
node_check = ScalarNode
|
||||
elif node_check is list:
|
||||
node_check = SequenceNode
|
||||
elif node_check is dict:
|
||||
node_check = MappingNode
|
||||
elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
|
||||
and not isinstance(node_check, str) \
|
||||
and node_check is not None:
|
||||
raise ResolverError("Invalid node checker: %s" % node_check)
|
||||
if not isinstance(index_check, (str, int)) \
|
||||
and index_check is not None:
|
||||
raise ResolverError("Invalid index checker: %s" % index_check)
|
||||
new_path.append((node_check, index_check))
|
||||
if kind is str:
|
||||
kind = ScalarNode
|
||||
elif kind is list:
|
||||
kind = SequenceNode
|
||||
elif kind is dict:
|
||||
kind = MappingNode
|
||||
elif kind not in [ScalarNode, SequenceNode, MappingNode] \
|
||||
and kind is not None:
|
||||
raise ResolverError("Invalid node kind: %s" % kind)
|
||||
cls.yaml_path_resolvers[tuple(new_path), kind] = tag
|
||||
|
||||
def descend_resolver(self, current_node, current_index):
|
||||
if not self.yaml_path_resolvers:
|
||||
return
|
||||
exact_paths = {}
|
||||
prefix_paths = []
|
||||
if current_node:
|
||||
depth = len(self.resolver_prefix_paths)
|
||||
for path, kind in self.resolver_prefix_paths[-1]:
|
||||
if self.check_resolver_prefix(depth, path, kind,
|
||||
current_node, current_index):
|
||||
if len(path) > depth:
|
||||
prefix_paths.append((path, kind))
|
||||
else:
|
||||
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
|
||||
else:
|
||||
for path, kind in self.yaml_path_resolvers:
|
||||
if not path:
|
||||
exact_paths[kind] = self.yaml_path_resolvers[path, kind]
|
||||
else:
|
||||
prefix_paths.append((path, kind))
|
||||
self.resolver_exact_paths.append(exact_paths)
|
||||
self.resolver_prefix_paths.append(prefix_paths)
|
||||
|
||||
def ascend_resolver(self):
|
||||
if not self.yaml_path_resolvers:
|
||||
return
|
||||
self.resolver_exact_paths.pop()
|
||||
self.resolver_prefix_paths.pop()
|
||||
|
||||
def check_resolver_prefix(self, depth, path, kind,
|
||||
current_node, current_index):
|
||||
node_check, index_check = path[depth-1]
|
||||
if isinstance(node_check, str):
|
||||
if current_node.tag != node_check:
|
||||
return
|
||||
elif node_check is not None:
|
||||
if not isinstance(current_node, node_check):
|
||||
return
|
||||
if index_check is True and current_index is not None:
|
||||
return
|
||||
if (index_check is False or index_check is None) \
|
||||
and current_index is None:
|
||||
return
|
||||
if isinstance(index_check, str):
|
||||
if not (isinstance(current_index, ScalarNode)
|
||||
and index_check == current_index.value):
|
||||
return
|
||||
elif isinstance(index_check, int) and not isinstance(index_check, bool):
|
||||
if index_check != current_index:
|
||||
return
|
||||
return True
|
||||
|
||||
def resolve(self, kind, value, implicit):
|
||||
if kind is ScalarNode and implicit[0]:
|
||||
if value == '':
|
||||
resolvers = self.yaml_implicit_resolvers.get('', [])
|
||||
else:
|
||||
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
|
||||
resolvers += self.yaml_implicit_resolvers.get(None, [])
|
||||
for tag, regexp in resolvers:
|
||||
if regexp.match(value):
|
||||
return tag
|
||||
implicit = implicit[1]
|
||||
if self.yaml_path_resolvers:
|
||||
exact_paths = self.resolver_exact_paths[-1]
|
||||
if kind in exact_paths:
|
||||
return exact_paths[kind]
|
||||
if None in exact_paths:
|
||||
return exact_paths[None]
|
||||
if kind is ScalarNode:
|
||||
return self.DEFAULT_SCALAR_TAG
|
||||
elif kind is SequenceNode:
|
||||
return self.DEFAULT_SEQUENCE_TAG
|
||||
elif kind is MappingNode:
|
||||
return self.DEFAULT_MAPPING_TAG
|
||||
|
||||
class Resolver(BaseResolver):
|
||||
pass
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:bool',
|
||||
re.compile(r'''^(?:yes|Yes|YES|no|No|NO
|
||||
|true|True|TRUE|false|False|FALSE
|
||||
|on|On|ON|off|Off|OFF)$''', re.X),
|
||||
list('yYnNtTfFoO'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:float',
|
||||
re.compile(r'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?
|
||||
|\.[0-9_]+(?:[eE][-+][0-9]+)?
|
||||
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
|
||||
|[-+]?\.(?:inf|Inf|INF)
|
||||
|\.(?:nan|NaN|NAN))$''', re.X),
|
||||
list('-+0123456789.'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:int',
|
||||
re.compile(r'''^(?:[-+]?0b[0-1_]+
|
||||
|[-+]?0[0-7_]+
|
||||
|[-+]?(?:0|[1-9][0-9_]*)
|
||||
|[-+]?0x[0-9a-fA-F_]+
|
||||
|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
|
||||
list('-+0123456789'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:merge',
|
||||
re.compile(r'^(?:<<)$'),
|
||||
['<'])
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:null',
|
||||
re.compile(r'''^(?: ~
|
||||
|null|Null|NULL
|
||||
| )$''', re.X),
|
||||
['~', 'n', 'N', ''])
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:timestamp',
|
||||
re.compile(r'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
|
||||
|[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
|
||||
(?:[Tt]|[ \t]+)[0-9][0-9]?
|
||||
:[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
|
||||
(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
|
||||
list('0123456789'))
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:value',
|
||||
re.compile(r'^(?:=)$'),
|
||||
['='])
|
||||
|
||||
# The following resolver is only for documentation purposes. It cannot work
|
||||
# because plain scalars cannot start with '!', '&', or '*'.
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:yaml',
|
||||
re.compile(r'^(?:!|&|\*)$'),
|
||||
list('!&*'))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,111 @@
|
||||
|
||||
__all__ = ['Serializer', 'SerializerError']
|
||||
|
||||
from .error import YAMLError
|
||||
from .events import *
|
||||
from .nodes import *
|
||||
|
||||
class SerializerError(YAMLError):
|
||||
pass
|
||||
|
||||
class Serializer:
|
||||
|
||||
ANCHOR_TEMPLATE = 'id%03d'
|
||||
|
||||
def __init__(self, encoding=None,
|
||||
explicit_start=None, explicit_end=None, version=None, tags=None):
|
||||
self.use_encoding = encoding
|
||||
self.use_explicit_start = explicit_start
|
||||
self.use_explicit_end = explicit_end
|
||||
self.use_version = version
|
||||
self.use_tags = tags
|
||||
self.serialized_nodes = {}
|
||||
self.anchors = {}
|
||||
self.last_anchor_id = 0
|
||||
self.closed = None
|
||||
|
||||
def open(self):
|
||||
if self.closed is None:
|
||||
self.emit(StreamStartEvent(encoding=self.use_encoding))
|
||||
self.closed = False
|
||||
elif self.closed:
|
||||
raise SerializerError("serializer is closed")
|
||||
else:
|
||||
raise SerializerError("serializer is already opened")
|
||||
|
||||
def close(self):
|
||||
if self.closed is None:
|
||||
raise SerializerError("serializer is not opened")
|
||||
elif not self.closed:
|
||||
self.emit(StreamEndEvent())
|
||||
self.closed = True
|
||||
|
||||
#def __del__(self):
|
||||
# self.close()
|
||||
|
||||
def serialize(self, node):
|
||||
if self.closed is None:
|
||||
raise SerializerError("serializer is not opened")
|
||||
elif self.closed:
|
||||
raise SerializerError("serializer is closed")
|
||||
self.emit(DocumentStartEvent(explicit=self.use_explicit_start,
|
||||
version=self.use_version, tags=self.use_tags))
|
||||
self.anchor_node(node)
|
||||
self.serialize_node(node, None, None)
|
||||
self.emit(DocumentEndEvent(explicit=self.use_explicit_end))
|
||||
self.serialized_nodes = {}
|
||||
self.anchors = {}
|
||||
self.last_anchor_id = 0
|
||||
|
||||
def anchor_node(self, node):
|
||||
if node in self.anchors:
|
||||
if self.anchors[node] is None:
|
||||
self.anchors[node] = self.generate_anchor(node)
|
||||
else:
|
||||
self.anchors[node] = None
|
||||
if isinstance(node, SequenceNode):
|
||||
for item in node.value:
|
||||
self.anchor_node(item)
|
||||
elif isinstance(node, MappingNode):
|
||||
for key, value in node.value:
|
||||
self.anchor_node(key)
|
||||
self.anchor_node(value)
|
||||
|
||||
def generate_anchor(self, node):
|
||||
self.last_anchor_id += 1
|
||||
return self.ANCHOR_TEMPLATE % self.last_anchor_id
|
||||
|
||||
def serialize_node(self, node, parent, index):
|
||||
alias = self.anchors[node]
|
||||
if node in self.serialized_nodes:
|
||||
self.emit(AliasEvent(alias))
|
||||
else:
|
||||
self.serialized_nodes[node] = True
|
||||
self.descend_resolver(parent, index)
|
||||
if isinstance(node, ScalarNode):
|
||||
detected_tag = self.resolve(ScalarNode, node.value, (True, False))
|
||||
default_tag = self.resolve(ScalarNode, node.value, (False, True))
|
||||
implicit = (node.tag == detected_tag), (node.tag == default_tag)
|
||||
self.emit(ScalarEvent(alias, node.tag, implicit, node.value,
|
||||
style=node.style))
|
||||
elif isinstance(node, SequenceNode):
|
||||
implicit = (node.tag
|
||||
== self.resolve(SequenceNode, node.value, True))
|
||||
self.emit(SequenceStartEvent(alias, node.tag, implicit,
|
||||
flow_style=node.flow_style))
|
||||
index = 0
|
||||
for item in node.value:
|
||||
self.serialize_node(item, node, index)
|
||||
index += 1
|
||||
self.emit(SequenceEndEvent())
|
||||
elif isinstance(node, MappingNode):
|
||||
implicit = (node.tag
|
||||
== self.resolve(MappingNode, node.value, True))
|
||||
self.emit(MappingStartEvent(alias, node.tag, implicit,
|
||||
flow_style=node.flow_style))
|
||||
for key, value in node.value:
|
||||
self.serialize_node(key, node, None)
|
||||
self.serialize_node(value, node, key)
|
||||
self.emit(MappingEndEvent())
|
||||
self.ascend_resolver()
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
|
||||
class Token(object):
|
||||
def __init__(self, start_mark, end_mark):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
def __repr__(self):
|
||||
attributes = [key for key in self.__dict__
|
||||
if not key.endswith('_mark')]
|
||||
attributes.sort()
|
||||
arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
#class BOMToken(Token):
|
||||
# id = '<byte order mark>'
|
||||
|
||||
class DirectiveToken(Token):
|
||||
id = '<directive>'
|
||||
def __init__(self, name, value, start_mark, end_mark):
|
||||
self.name = name
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class DocumentStartToken(Token):
|
||||
id = '<document start>'
|
||||
|
||||
class DocumentEndToken(Token):
|
||||
id = '<document end>'
|
||||
|
||||
class StreamStartToken(Token):
|
||||
id = '<stream start>'
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
class StreamEndToken(Token):
|
||||
id = '<stream end>'
|
||||
|
||||
class BlockSequenceStartToken(Token):
|
||||
id = '<block sequence start>'
|
||||
|
||||
class BlockMappingStartToken(Token):
|
||||
id = '<block mapping start>'
|
||||
|
||||
class BlockEndToken(Token):
|
||||
id = '<block end>'
|
||||
|
||||
class FlowSequenceStartToken(Token):
|
||||
id = '['
|
||||
|
||||
class FlowMappingStartToken(Token):
|
||||
id = '{'
|
||||
|
||||
class FlowSequenceEndToken(Token):
|
||||
id = ']'
|
||||
|
||||
class FlowMappingEndToken(Token):
|
||||
id = '}'
|
||||
|
||||
class KeyToken(Token):
|
||||
id = '?'
|
||||
|
||||
class ValueToken(Token):
|
||||
id = ':'
|
||||
|
||||
class BlockEntryToken(Token):
|
||||
id = '-'
|
||||
|
||||
class FlowEntryToken(Token):
|
||||
id = ','
|
||||
|
||||
class AliasToken(Token):
|
||||
id = '<alias>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class AnchorToken(Token):
|
||||
id = '<anchor>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class TagToken(Token):
|
||||
id = '<tag>'
|
||||
def __init__(self, value, start_mark, end_mark):
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
class ScalarToken(Token):
|
||||
id = '<scalar>'
|
||||
def __init__(self, value, plain, start_mark, end_mark, style=None):
|
||||
self.value = value
|
||||
self.plain = plain
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
Vendored
+2
-2
@@ -1,3 +1,3 @@
|
||||
from .core import where
|
||||
from .core import contents, where
|
||||
|
||||
__version__ = "2019.11.28"
|
||||
__version__ = "2020.04.05.1"
|
||||
|
||||
Vendored
+12
-2
@@ -1,2 +1,12 @@
|
||||
from certifi import where
|
||||
print(where())
|
||||
import argparse
|
||||
|
||||
from certifi import contents, where
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-c", "--contents", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.contents:
|
||||
print(contents())
|
||||
else:
|
||||
print(where())
|
||||
|
||||
Vendored
+39
@@ -2140,6 +2140,45 @@ t/2jioSgrGK+KwmHNPBqAbubKVY8/gA3zyNs8U6qtnRGEmyR7jTV7JqR50S+kDFy
|
||||
SjnRBUkLp7Y3gaVdjKozXoEofKd9J+sAro03
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
# Issuer: CN=EC-ACC O=Agencia Catalana de Certificacio (NIF Q-0801176-I) OU=Serveis Publics de Certificacio/Vegeu https://www.catcert.net/verarrel (c)03/Jerarquia Entitats de Certificacio Catalanes
|
||||
# Subject: CN=EC-ACC O=Agencia Catalana de Certificacio (NIF Q-0801176-I) OU=Serveis Publics de Certificacio/Vegeu https://www.catcert.net/verarrel (c)03/Jerarquia Entitats de Certificacio Catalanes
|
||||
# Label: "EC-ACC"
|
||||
# Serial: -23701579247955709139626555126524820479
|
||||
# MD5 Fingerprint: eb:f5:9d:29:0d:61:f9:42:1f:7c:c2:ba:6d:e3:15:09
|
||||
# SHA1 Fingerprint: 28:90:3a:63:5b:52:80:fa:e6:77:4c:0b:6d:a7:d6:ba:a6:4a:f2:e8
|
||||
# SHA256 Fingerprint: 88:49:7f:01:60:2f:31:54:24:6a:e2:8c:4d:5a:ef:10:f1:d8:7e:bb:76:62:6f:4a:e0:b7:f9:5b:a7:96:87:99
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIFVjCCBD6gAwIBAgIQ7is969Qh3hSoYqwE893EATANBgkqhkiG9w0BAQUFADCB
|
||||
8zELMAkGA1UEBhMCRVMxOzA5BgNVBAoTMkFnZW5jaWEgQ2F0YWxhbmEgZGUgQ2Vy
|
||||
dGlmaWNhY2lvIChOSUYgUS0wODAxMTc2LUkpMSgwJgYDVQQLEx9TZXJ2ZWlzIFB1
|
||||
YmxpY3MgZGUgQ2VydGlmaWNhY2lvMTUwMwYDVQQLEyxWZWdldSBodHRwczovL3d3
|
||||
dy5jYXRjZXJ0Lm5ldC92ZXJhcnJlbCAoYykwMzE1MDMGA1UECxMsSmVyYXJxdWlh
|
||||
IEVudGl0YXRzIGRlIENlcnRpZmljYWNpbyBDYXRhbGFuZXMxDzANBgNVBAMTBkVD
|
||||
LUFDQzAeFw0wMzAxMDcyMzAwMDBaFw0zMTAxMDcyMjU5NTlaMIHzMQswCQYDVQQG
|
||||
EwJFUzE7MDkGA1UEChMyQWdlbmNpYSBDYXRhbGFuYSBkZSBDZXJ0aWZpY2FjaW8g
|
||||
KE5JRiBRLTA4MDExNzYtSSkxKDAmBgNVBAsTH1NlcnZlaXMgUHVibGljcyBkZSBD
|
||||
ZXJ0aWZpY2FjaW8xNTAzBgNVBAsTLFZlZ2V1IGh0dHBzOi8vd3d3LmNhdGNlcnQu
|
||||
bmV0L3ZlcmFycmVsIChjKTAzMTUwMwYDVQQLEyxKZXJhcnF1aWEgRW50aXRhdHMg
|
||||
ZGUgQ2VydGlmaWNhY2lvIENhdGFsYW5lczEPMA0GA1UEAxMGRUMtQUNDMIIBIjAN
|
||||
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsyLHT+KXQpWIR4NA9h0X84NzJB5R
|
||||
85iKw5K4/0CQBXCHYMkAqbWUZRkiFRfCQ2xmRJoNBD45b6VLeqpjt4pEndljkYRm
|
||||
4CgPukLjbo73FCeTae6RDqNfDrHrZqJyTxIThmV6PttPB/SnCWDaOkKZx7J/sxaV
|
||||
HMf5NLWUhdWZXqBIoH7nF2W4onW4HvPlQn2v7fOKSGRdghST2MDk/7NQcvJ29rNd
|
||||
QlB50JQ+awwAvthrDk4q7D7SzIKiGGUzE3eeml0aE9jD2z3Il3rucO2n5nzbcc8t
|
||||
lGLfbdb1OL4/pYUKGbio2Al1QnDE6u/LDsg0qBIimAy4E5S2S+zw0JDnJwIDAQAB
|
||||
o4HjMIHgMB0GA1UdEQQWMBSBEmVjX2FjY0BjYXRjZXJ0Lm5ldDAPBgNVHRMBAf8E
|
||||
BTADAQH/MA4GA1UdDwEB/wQEAwIBBjAdBgNVHQ4EFgQUoMOLRKo3pUW/l4Ba0fF4
|
||||
opvpXY0wfwYDVR0gBHgwdjB0BgsrBgEEAfV4AQMBCjBlMCwGCCsGAQUFBwIBFiBo
|
||||
dHRwczovL3d3dy5jYXRjZXJ0Lm5ldC92ZXJhcnJlbDA1BggrBgEFBQcCAjApGidW
|
||||
ZWdldSBodHRwczovL3d3dy5jYXRjZXJ0Lm5ldC92ZXJhcnJlbCAwDQYJKoZIhvcN
|
||||
AQEFBQADggEBAKBIW4IB9k1IuDlVNZyAelOZ1Vr/sXE7zDkJlF7W2u++AVtd0x7Y
|
||||
/X1PzaBB4DSTv8vihpw3kpBWHNzrKQXlxJ7HNd+KDM3FIUPpqojlNcAZQmNaAl6k
|
||||
SBg6hW/cnbw/nZzBh7h6YQjpdwt/cKt63dmXLGQehb+8dJahw3oS7AwaboMMPOhy
|
||||
Rp/7SNVel+axofjk70YllJyJ22k4vuxcDlbHZVHlUIiIv0LVKz3l+bqeLrPK9HOS
|
||||
Agu+TGbrIP65y7WZf+a2E/rKS03Z7lNGBjvGTq2TWoF+bCpLagVFjPIhpDGQh2xl
|
||||
nJ2lYJU6Un/10asIbvPuW/mIPX64b24D5EI=
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
# Issuer: CN=Hellenic Academic and Research Institutions RootCA 2011 O=Hellenic Academic and Research Institutions Cert. Authority
|
||||
# Subject: CN=Hellenic Academic and Research Institutions RootCA 2011 O=Hellenic Academic and Research Institutions Cert. Authority
|
||||
# Label: "Hellenic Academic and Research Institutions RootCA 2011"
|
||||
|
||||
Vendored
+17
-2
@@ -4,12 +4,27 @@
|
||||
certifi.py
|
||||
~~~~~~~~~~
|
||||
|
||||
This module returns the installation location of cacert.pem.
|
||||
This module returns the installation location of cacert.pem or its contents.
|
||||
"""
|
||||
import os
|
||||
|
||||
try:
|
||||
from importlib.resources import read_text
|
||||
except ImportError:
|
||||
# This fallback will work for Python versions prior to 3.7 that lack the
|
||||
# importlib.resources module but relies on the existing `where` function
|
||||
# so won't address issues with environments like PyOxidizer that don't set
|
||||
# __file__ on modules.
|
||||
def read_text(_module, _path, encoding="ascii"):
|
||||
with open(where(), "r", encoding=encoding) as data:
|
||||
return data.read()
|
||||
|
||||
|
||||
def where():
|
||||
f = os.path.dirname(__file__)
|
||||
|
||||
return os.path.join(f, 'cacert.pem')
|
||||
return os.path.join(f, "cacert.pem")
|
||||
|
||||
|
||||
def contents():
|
||||
return read_text("certifi", "cacert.pem", encoding="ascii")
|
||||
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017, Jannis Gebauer
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
"""Top-level package for Dependency Parser."""
|
||||
|
||||
__author__ = """Jannis Gebauer"""
|
||||
__email__ = 'jay@pyup.io'
|
||||
__version__ = '0.5.0'
|
||||
|
||||
from .parser import parse
|
||||
Vendored
+196
@@ -0,0 +1,196 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import json
|
||||
|
||||
from . import filetypes, errors
|
||||
|
||||
|
||||
class Dependency(object):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, name, specs, line, source="pypi", meta={}, extras=[], line_numbers=None, index_server=None, hashes=(), dependency_type=None, section=None):
|
||||
"""
|
||||
|
||||
:param name:
|
||||
:param specs:
|
||||
:param line:
|
||||
:param source:
|
||||
:param extras:
|
||||
:param line_numbers:
|
||||
:param index_server:
|
||||
:param hashes:
|
||||
:param dependency_type:
|
||||
"""
|
||||
self.name = name
|
||||
self.key = name.lower().replace("_", "-")
|
||||
self.specs = specs
|
||||
self.line = line
|
||||
self.source = source
|
||||
self.meta = meta
|
||||
self.line_numbers = line_numbers
|
||||
self.index_server = index_server
|
||||
self.hashes = hashes
|
||||
self.dependency_type = dependency_type
|
||||
self.extras = extras
|
||||
self.section = section
|
||||
|
||||
def __str__(self): # pragma: no cover
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
return "Dependency({name}, {specs}, {line})".format(
|
||||
name=self.name,
|
||||
specs=self.specs,
|
||||
line=self.line
|
||||
)
|
||||
|
||||
def serialize(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
return {
|
||||
"name": self.name,
|
||||
"specs": self.specs,
|
||||
"line": self.line,
|
||||
"source": self.source,
|
||||
"meta": self.meta,
|
||||
"line_numbers": self.line_numbers,
|
||||
"index_server": self.index_server,
|
||||
"hashes": self.hashes,
|
||||
"dependency_type": self.dependency_type,
|
||||
"extras": self.extras,
|
||||
"section": self.section
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, d):
|
||||
"""
|
||||
|
||||
:param d:
|
||||
:return:
|
||||
"""
|
||||
return cls(**d)
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
if self.extras:
|
||||
return "{}[{}]".format(self.name, ",".join(self.extras))
|
||||
return self.name
|
||||
|
||||
|
||||
class DependencyFile(object):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, content, path=None, sha=None, file_type=None, marker=((), ()), parser=None):
|
||||
"""
|
||||
|
||||
:param content:
|
||||
:param path:
|
||||
:param sha:
|
||||
:param marker:
|
||||
:param file_type:
|
||||
:param parser:
|
||||
"""
|
||||
self.content = content
|
||||
self.file_type = file_type
|
||||
self.path = path
|
||||
self.sha = sha
|
||||
self.marker = marker
|
||||
|
||||
self.dependencies = []
|
||||
self.resolved_files = []
|
||||
self.is_valid = False
|
||||
self.file_marker, self.line_marker = marker
|
||||
|
||||
if parser:
|
||||
self.parser = parser
|
||||
else:
|
||||
from . import parser as parser_class
|
||||
if file_type is not None:
|
||||
if file_type == filetypes.requirements_txt:
|
||||
self.parser = parser_class.RequirementsTXTParser
|
||||
elif file_type == filetypes.tox_ini:
|
||||
self.parser = parser_class.ToxINIParser
|
||||
elif file_type == filetypes.conda_yml:
|
||||
self.parser = parser_class.CondaYMLParser
|
||||
elif file_type == filetypes.pipfile:
|
||||
self.parser = parser_class.PipfileParser
|
||||
elif file_type == filetypes.pipfile_lock:
|
||||
self.parser = parser_class.PipfileLockParser
|
||||
elif file_type == filetypes.setup_cfg:
|
||||
self.parser = parser_class.SetupCfgParser
|
||||
|
||||
elif path is not None:
|
||||
if path.endswith(".txt"):
|
||||
self.parser = parser_class.RequirementsTXTParser
|
||||
elif path.endswith(".yml"):
|
||||
self.parser = parser_class.CondaYMLParser
|
||||
elif path.endswith(".ini"):
|
||||
self.parser = parser_class.ToxINIParser
|
||||
elif path.endswith("Pipfile"):
|
||||
self.parser = parser_class.PipfileParser
|
||||
elif path.endswith("Pipfile.lock"):
|
||||
self.parser = parser_class.PipfileLockParser
|
||||
elif path.endswith("setup.cfg"):
|
||||
self.parser = parser_class.SetupCfgParser
|
||||
|
||||
if not hasattr(self, "parser"):
|
||||
raise errors.UnknownDependencyFileError
|
||||
|
||||
self.parser = self.parser(self)
|
||||
|
||||
def serialize(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
return {
|
||||
"file_type": self.file_type,
|
||||
"content": self.content,
|
||||
"path": self.path,
|
||||
"sha": self.sha,
|
||||
"dependencies": [dep.serialize() for dep in self.dependencies]
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, d):
|
||||
"""
|
||||
|
||||
:param d:
|
||||
:return:
|
||||
"""
|
||||
dependencies = [Dependency.deserialize(dep) for dep in d.pop("dependencies", [])]
|
||||
instance = cls(**d)
|
||||
instance.dependencies = dependencies
|
||||
return instance
|
||||
|
||||
def json(self): # pragma: no cover
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
return json.dumps(self.serialize(), indent=2)
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
if self.parser.is_marked_file:
|
||||
self.is_valid = False
|
||||
return self
|
||||
self.parser.parse()
|
||||
|
||||
self.is_valid = len(self.dependencies) > 0 or len(self.resolved_files) > 0
|
||||
return self
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
class UnknownDependencyFileError(Exception):
|
||||
"""
|
||||
|
||||
"""
|
||||
pass
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
requirements_txt = "requirements.txt"
|
||||
conda_yml = "conda.yml"
|
||||
setup_cfg = "setup.cfg"
|
||||
tox_ini = "tox.ini"
|
||||
pipfile = "Pipfile"
|
||||
pipfile_lock = "Pipfile.lock"
|
||||
Vendored
+427
@@ -0,0 +1,427 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
from collections import OrderedDict
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from io import StringIO
|
||||
|
||||
from six.moves.configparser import SafeConfigParser, NoOptionError
|
||||
|
||||
|
||||
from .regex import URL_REGEX, HASH_REGEX
|
||||
|
||||
from .dependencies import DependencyFile, Dependency
|
||||
from packaging.requirements import Requirement as PackagingRequirement, InvalidRequirement
|
||||
from . import filetypes
|
||||
import toml
|
||||
from packaging.specifiers import SpecifierSet
|
||||
import json
|
||||
|
||||
|
||||
# this is a backport from setuptools 26.1
|
||||
def setuptools_parse_requirements_backport(strs): # pragma: no cover
|
||||
# Copyright (C) 2016 Jason R Coombs <jaraco@jaraco.com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
# this software and associated documentation files (the "Software"), to deal in
|
||||
# the Software without restriction, including without limitation the rights to
|
||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
# of the Software, and to permit persons to whom the Software is furnished to do
|
||||
# so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
"""Yield ``Requirement`` objects for each specification in `strs`
|
||||
|
||||
`strs` must be a string, or a (possibly-nested) iterable thereof.
|
||||
"""
|
||||
# create a steppable iterator, so we can handle \-continuations
|
||||
def yield_lines(strs):
|
||||
"""Yield non-empty/non-comment lines of a string or sequence"""
|
||||
if isinstance(strs, str):
|
||||
for s in strs.splitlines():
|
||||
s = s.strip()
|
||||
# skip blank lines/comments
|
||||
if s and not s.startswith('#'):
|
||||
yield s
|
||||
else:
|
||||
for ss in strs:
|
||||
for s in yield_lines(ss):
|
||||
yield s
|
||||
lines = iter(yield_lines(strs))
|
||||
|
||||
for line in lines:
|
||||
# Drop comments -- a hash without a space may be in a URL.
|
||||
if ' #' in line:
|
||||
line = line[:line.find(' #')]
|
||||
# If there is a line continuation, drop it, and append the next line.
|
||||
if line.endswith('\\'):
|
||||
line = line[:-2].strip()
|
||||
line += next(lines)
|
||||
yield PackagingRequirement(line)
|
||||
|
||||
|
||||
class RequirementsTXTLineParser(object):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def parse(cls, line):
|
||||
"""
|
||||
|
||||
:param line:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
# setuptools requires a space before the comment. If this isn't the case, add it.
|
||||
if "\t#" in line:
|
||||
parsed, = setuptools_parse_requirements_backport(line.replace("\t#", "\t #"))
|
||||
else:
|
||||
parsed, = setuptools_parse_requirements_backport(line)
|
||||
except InvalidRequirement:
|
||||
return None
|
||||
dep = Dependency(
|
||||
name=parsed.name,
|
||||
specs=parsed.specifier,
|
||||
line=line,
|
||||
extras=parsed.extras,
|
||||
dependency_type=filetypes.requirements_txt
|
||||
)
|
||||
return dep
|
||||
|
||||
|
||||
class Parser(object):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, obj):
|
||||
"""
|
||||
|
||||
:param obj:
|
||||
"""
|
||||
self.obj = obj
|
||||
self._lines = None
|
||||
|
||||
def iter_lines(self, lineno=0):
|
||||
"""
|
||||
|
||||
:param lineno:
|
||||
:return:
|
||||
"""
|
||||
for line in self.lines[lineno:]:
|
||||
yield line
|
||||
|
||||
@property
|
||||
def lines(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
if self._lines is None:
|
||||
self._lines = self.obj.content.splitlines()
|
||||
return self._lines
|
||||
|
||||
@property
|
||||
def is_marked_file(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
for n, line in enumerate(self.iter_lines()):
|
||||
for marker in self.obj.file_marker:
|
||||
if marker in line:
|
||||
return True
|
||||
if n >= 2:
|
||||
break
|
||||
return False
|
||||
|
||||
def is_marked_line(self, line):
|
||||
"""
|
||||
|
||||
:param line:
|
||||
:return:
|
||||
"""
|
||||
for marker in self.obj.line_marker:
|
||||
if marker in line:
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def parse_hashes(cls, line):
|
||||
"""
|
||||
|
||||
:param line:
|
||||
:return:
|
||||
"""
|
||||
hashes = []
|
||||
for match in re.finditer(HASH_REGEX, line):
|
||||
hashes.append(line[match.start():match.end()])
|
||||
return re.sub(HASH_REGEX, "", line).strip(), hashes
|
||||
|
||||
@classmethod
|
||||
def parse_index_server(cls, line):
|
||||
"""
|
||||
|
||||
:param line:
|
||||
:return:
|
||||
"""
|
||||
matches = URL_REGEX.findall(line)
|
||||
if matches:
|
||||
url = matches[0]
|
||||
return url if url.endswith("/") else url + "/"
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def resolve_file(cls, file_path, line):
|
||||
"""
|
||||
|
||||
:param file_path:
|
||||
:param line:
|
||||
:return:
|
||||
"""
|
||||
line = line.replace("-r ", "").replace("--requirement ", "")
|
||||
parts = file_path.split("/")
|
||||
if " #" in line:
|
||||
line = line.split("#")[0].strip()
|
||||
if len(parts) == 1:
|
||||
return line
|
||||
return "/".join(parts[:-1]) + "/" + line
|
||||
|
||||
|
||||
class RequirementsTXTParser(Parser):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Parses a requirements.txt-like file
|
||||
"""
|
||||
index_server = None
|
||||
for num, line in enumerate(self.iter_lines()):
|
||||
line = line.rstrip()
|
||||
if not line:
|
||||
continue
|
||||
if line.startswith('#'):
|
||||
# comments are lines that start with # only
|
||||
continue
|
||||
if line.startswith('-i') or \
|
||||
line.startswith('--index-url') or \
|
||||
line.startswith('--extra-index-url'):
|
||||
# this file is using a private index server, try to parse it
|
||||
index_server = self.parse_index_server(line)
|
||||
continue
|
||||
elif self.obj.path and (line.startswith('-r') or line.startswith('--requirement')):
|
||||
self.obj.resolved_files.append(self.resolve_file(self.obj.path, line))
|
||||
elif line.startswith('-f') or line.startswith('--find-links') or \
|
||||
line.startswith('--no-index') or line.startswith('--allow-external') or \
|
||||
line.startswith('--allow-unverified') or line.startswith('-Z') or \
|
||||
line.startswith('--always-unzip'):
|
||||
continue
|
||||
elif self.is_marked_line(line):
|
||||
continue
|
||||
else:
|
||||
try:
|
||||
|
||||
parseable_line = line
|
||||
|
||||
# multiline requirements are not parseable
|
||||
if "\\" in line:
|
||||
parseable_line = line.replace("\\", "")
|
||||
for next_line in self.iter_lines(num + 1):
|
||||
parseable_line += next_line.strip().replace("\\", "")
|
||||
line += "\n" + next_line
|
||||
if "\\" in next_line:
|
||||
continue
|
||||
break
|
||||
# ignore multiline requirements if they are marked
|
||||
if self.is_marked_line(parseable_line):
|
||||
continue
|
||||
|
||||
hashes = []
|
||||
if "--hash" in parseable_line:
|
||||
parseable_line, hashes = Parser.parse_hashes(parseable_line)
|
||||
|
||||
req = RequirementsTXTLineParser.parse(parseable_line)
|
||||
if req:
|
||||
req.hashes = hashes
|
||||
req.index_server = index_server
|
||||
# replace the requirements line with the 'real' line
|
||||
req.line = line
|
||||
self.obj.dependencies.append(req)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
|
||||
class ToxINIParser(Parser):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
parser = SafeConfigParser()
|
||||
parser.readfp(StringIO(self.obj.content))
|
||||
for section in parser.sections():
|
||||
try:
|
||||
content = parser.get(section=section, option="deps")
|
||||
for n, line in enumerate(content.splitlines()):
|
||||
if self.is_marked_line(line):
|
||||
continue
|
||||
if line:
|
||||
req = RequirementsTXTLineParser.parse(line)
|
||||
if req:
|
||||
req.dependency_type = self.obj.file_type
|
||||
self.obj.dependencies.append(req)
|
||||
except NoOptionError:
|
||||
pass
|
||||
|
||||
|
||||
class CondaYMLParser(Parser):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
data = yaml.safe_load(self.obj.content)
|
||||
if data and 'dependencies' in data and isinstance(data['dependencies'], list):
|
||||
for dep in data['dependencies']:
|
||||
if isinstance(dep, dict) and 'pip' in dep:
|
||||
for n, line in enumerate(dep['pip']):
|
||||
if self.is_marked_line(line):
|
||||
continue
|
||||
req = RequirementsTXTLineParser.parse(line)
|
||||
if req:
|
||||
req.dependency_type = self.obj.file_type
|
||||
self.obj.dependencies.append(req)
|
||||
except yaml.YAMLError:
|
||||
pass
|
||||
|
||||
|
||||
class PipfileParser(Parser):
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Parse a Pipfile (as seen in pipenv)
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
data = toml.loads(self.obj.content, _dict=OrderedDict)
|
||||
if data:
|
||||
for package_type in ['packages', 'dev-packages']:
|
||||
if package_type in data:
|
||||
for name, specs in data[package_type].items():
|
||||
# skip on VCS dependencies
|
||||
if not isinstance(specs, str):
|
||||
continue
|
||||
if specs == '*':
|
||||
specs = ''
|
||||
self.obj.dependencies.append(
|
||||
Dependency(
|
||||
name=name, specs=SpecifierSet(specs),
|
||||
dependency_type=filetypes.pipfile,
|
||||
line=''.join([name, specs]),
|
||||
section=package_type
|
||||
)
|
||||
)
|
||||
except (toml.TomlDecodeError, IndexError) as e:
|
||||
pass
|
||||
|
||||
class PipfileLockParser(Parser):
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Parse a Pipfile.lock (as seen in pipenv)
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
data = json.loads(self.obj.content, object_pairs_hook=OrderedDict)
|
||||
if data:
|
||||
for package_type in ['default', 'develop']:
|
||||
if package_type in data:
|
||||
for name, meta in data[package_type].items():
|
||||
# skip VCS dependencies
|
||||
if 'version' not in meta:
|
||||
continue
|
||||
specs = meta['version']
|
||||
hashes = meta['hashes']
|
||||
self.obj.dependencies.append(
|
||||
Dependency(
|
||||
name=name, specs=SpecifierSet(specs),
|
||||
dependency_type=filetypes.pipfile_lock,
|
||||
hashes=hashes,
|
||||
line=''.join([name, specs]),
|
||||
section=package_type
|
||||
)
|
||||
)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
class SetupCfgParser(Parser):
|
||||
def parse(self):
|
||||
parser = SafeConfigParser()
|
||||
parser.readfp(StringIO(self.obj.content))
|
||||
for section in parser.values():
|
||||
if section.name == 'options':
|
||||
options = 'install_requires', 'setup_requires', 'test_require'
|
||||
for name in options:
|
||||
content = section.get(name)
|
||||
if not content:
|
||||
continue
|
||||
self._parse_content(content)
|
||||
elif section.name == 'options.extras_require':
|
||||
for content in section.values():
|
||||
self._parse_content(content)
|
||||
|
||||
def _parse_content(self, content):
|
||||
for n, line in enumerate(content.splitlines()):
|
||||
if self.is_marked_line(line):
|
||||
continue
|
||||
if line:
|
||||
req = RequirementsTXTLineParser.parse(line)
|
||||
if req:
|
||||
req.dependency_type = self.obj.file_type
|
||||
self.obj.dependencies.append(req)
|
||||
|
||||
|
||||
def parse(content, file_type=None, path=None, sha=None, marker=((), ()), parser=None):
|
||||
"""
|
||||
|
||||
:param content:
|
||||
:param file_type:
|
||||
:param path:
|
||||
:param sha:
|
||||
:param marker:
|
||||
:param parser:
|
||||
:return:
|
||||
"""
|
||||
dep_file = DependencyFile(
|
||||
content=content,
|
||||
path=path,
|
||||
sha=sha,
|
||||
marker=marker,
|
||||
file_type=file_type,
|
||||
parser=parser
|
||||
)
|
||||
|
||||
return dep_file.parse()
|
||||
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import re
|
||||
# see https://gist.github.com/dperini/729294
|
||||
URL_REGEX = re.compile(
|
||||
# protocol identifier
|
||||
"(?:(?:https?|ftp)://)"
|
||||
# user:pass authentication
|
||||
"(?:\S+(?::\S*)?@)?"
|
||||
"(?:"
|
||||
# IP address exclusion
|
||||
# private & local networks
|
||||
"(?!(?:10|127)(?:\.\d{1,3}){3})"
|
||||
"(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})"
|
||||
"(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})"
|
||||
# IP address dotted notation octets
|
||||
# excludes loopback network 0.0.0.0
|
||||
# excludes reserved space >= 224.0.0.0
|
||||
# excludes network & broadcast addresses
|
||||
# (first & last IP address of each class)
|
||||
"(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])"
|
||||
"(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}"
|
||||
"(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))"
|
||||
"|"
|
||||
# host name
|
||||
"(?:(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)"
|
||||
# domain name
|
||||
"(?:\.(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)*"
|
||||
# TLD identifier
|
||||
"(?:\.(?:[a-z\u00a1-\uffff]{2,}))"
|
||||
")"
|
||||
# port number
|
||||
"(?::\d{2,5})?"
|
||||
# resource path
|
||||
"(?:/\S*)?",
|
||||
re.UNICODE)
|
||||
|
||||
HASH_REGEX = r"--hash[=| ][\w]+:[\w]+"
|
||||
Vendored
+119
@@ -0,0 +1,119 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import re
|
||||
import json
|
||||
import tempfile
|
||||
import toml
|
||||
import os
|
||||
|
||||
|
||||
class RequirementsTXTUpdater(object):
|
||||
|
||||
SUB_REGEX = r"^{}(?=\s*\r?\n?$)"
|
||||
|
||||
@classmethod
|
||||
def update(cls, content, dependency, version, spec="==", hashes=()):
|
||||
"""
|
||||
Updates the requirement to the latest version for the given content and adds hashes
|
||||
if neccessary.
|
||||
:param content: str, content
|
||||
:return: str, updated content
|
||||
"""
|
||||
new_line = "{name}{spec}{version}".format(name=dependency.full_name, spec=spec, version=version)
|
||||
appendix = ''
|
||||
# leave environment markers intact
|
||||
if ";" in dependency.line:
|
||||
# condense multiline, split out the env marker, strip comments and --hashes
|
||||
new_line += ";" + dependency.line.splitlines()[0].split(";", 1)[1] \
|
||||
.split("#")[0].split("--hash")[0].rstrip()
|
||||
# add the comment
|
||||
if "#" in dependency.line:
|
||||
# split the line into parts: requirement and comment
|
||||
parts = dependency.line.split("#")
|
||||
requirement, comment = parts[0], "#".join(parts[1:])
|
||||
# find all whitespaces between the requirement and the comment
|
||||
whitespaces = (hex(ord('\t')), hex(ord(' ')))
|
||||
trailing_whitespace = ''
|
||||
for c in requirement[::-1]:
|
||||
if hex(ord(c)) in whitespaces:
|
||||
trailing_whitespace += c
|
||||
else:
|
||||
break
|
||||
appendix += trailing_whitespace + "#" + comment
|
||||
# if this is a hashed requirement, add a multiline break before the comment
|
||||
if dependency.hashes and not new_line.endswith("\\"):
|
||||
new_line += " \\"
|
||||
# if this is a hashed requirement, add the hashes
|
||||
if hashes:
|
||||
for n, new_hash in enumerate(hashes):
|
||||
new_line += "\n --hash={method}:{hash}".format(
|
||||
method=new_hash['method'],
|
||||
hash=new_hash['hash']
|
||||
)
|
||||
# append a new multiline break if this is not the last line
|
||||
if len(hashes) > n + 1:
|
||||
new_line += " \\"
|
||||
new_line += appendix
|
||||
|
||||
regex = cls.SUB_REGEX.format(re.escape(dependency.line))
|
||||
|
||||
return re.sub(regex, new_line, content, flags=re.MULTILINE)
|
||||
|
||||
|
||||
class CondaYMLUpdater(RequirementsTXTUpdater):
|
||||
|
||||
SUB_REGEX = r"{}(?=\s*\r?\n?$)"
|
||||
|
||||
|
||||
class ToxINIUpdater(CondaYMLUpdater):
|
||||
pass
|
||||
|
||||
|
||||
class SetupCFGUpdater(CondaYMLUpdater):
|
||||
pass
|
||||
|
||||
|
||||
class PipfileUpdater(object):
|
||||
@classmethod
|
||||
def update(cls, content, dependency, version, spec="==", hashes=()):
|
||||
data = toml.loads(content)
|
||||
if data:
|
||||
for package_type in ['packages', 'dev-packages']:
|
||||
if package_type in data:
|
||||
if dependency.full_name in data[package_type]:
|
||||
data[package_type][dependency.full_name] = "{spec}{version}".format(
|
||||
spec=spec, version=version
|
||||
)
|
||||
try:
|
||||
from pipenv.project import Project
|
||||
except ImportError:
|
||||
raise ImportError("Updating a Pipfile requires the pipenv extra to be installed. Install it with "
|
||||
"pip install dparse[pipenv]")
|
||||
pipfile = tempfile.NamedTemporaryFile(delete=False)
|
||||
p = Project(chdir=False)
|
||||
p.write_toml(data=data, path=pipfile.name)
|
||||
data = open(pipfile.name).read()
|
||||
os.remove(pipfile.name)
|
||||
return data
|
||||
|
||||
|
||||
class PipfileLockUpdater(object):
|
||||
@classmethod
|
||||
def update(cls, content, dependency, version, spec="==", hashes=()):
|
||||
data = json.loads(content)
|
||||
if data:
|
||||
for package_type in ['default', 'develop']:
|
||||
if package_type in data:
|
||||
if dependency.full_name in data[package_type]:
|
||||
data[package_type][dependency.full_name] = {
|
||||
'hashes': [
|
||||
"{method}:{hash}".format(
|
||||
hash=h['hash'],
|
||||
method=h['method']
|
||||
) for h in hashes
|
||||
],
|
||||
'version': "{spec}{version}".format(
|
||||
spec=spec, version=version
|
||||
)
|
||||
}
|
||||
return json.dumps(data, indent=4, separators=(',', ': ')) + "\n"
|
||||
+13
-2
@@ -28,6 +28,7 @@ from ._compat import (
|
||||
MetaPathFinder,
|
||||
email_message_from_string,
|
||||
PyPy_repr,
|
||||
unique_ordered,
|
||||
)
|
||||
from importlib import import_module
|
||||
from itertools import starmap
|
||||
@@ -95,6 +96,16 @@ class EntryPoint(
|
||||
attrs = filter(None, (match.group('attr') or '').split('.'))
|
||||
return functools.reduce(getattr, attrs, module)
|
||||
|
||||
@property
|
||||
def module(self):
|
||||
match = self.pattern.match(self.value)
|
||||
return match.group('module')
|
||||
|
||||
@property
|
||||
def attr(self):
|
||||
match = self.pattern.match(self.value)
|
||||
return match.group('attr')
|
||||
|
||||
@property
|
||||
def extras(self):
|
||||
match = self.pattern.match(self.value)
|
||||
@@ -425,8 +436,8 @@ class FastPath:
|
||||
names = zip_path.root.namelist()
|
||||
self.joinpath = zip_path.joinpath
|
||||
|
||||
return (
|
||||
posixpath.split(child)[0]
|
||||
return unique_ordered(
|
||||
child.split(posixpath.sep, 1)[0]
|
||||
for child in names
|
||||
)
|
||||
|
||||
|
||||
+17
@@ -15,9 +15,11 @@ if sys.version_info > (3,): # pragma: nocover
|
||||
NotADirectoryError = builtins.NotADirectoryError
|
||||
PermissionError = builtins.PermissionError
|
||||
map = builtins.map
|
||||
from itertools import filterfalse
|
||||
else: # pragma: nocover
|
||||
from backports.configparser import ConfigParser
|
||||
from itertools import imap as map # type: ignore
|
||||
from itertools import ifilterfalse as filterfalse
|
||||
import contextlib2 as contextlib
|
||||
FileNotFoundError = IOError, OSError
|
||||
IsADirectoryError = IOError, OSError
|
||||
@@ -131,3 +133,18 @@ class PyPy_repr:
|
||||
if affected: # pragma: nocover
|
||||
__repr__ = __compat_repr__
|
||||
del affected
|
||||
|
||||
|
||||
# from itertools recipes
|
||||
def unique_everseen(iterable): # pragma: nocover
|
||||
"List unique elements, preserving order. Remember all elements ever seen."
|
||||
seen = set()
|
||||
seen_add = seen.add
|
||||
|
||||
for element in filterfalse(seen.__contains__, iterable):
|
||||
seen_add(element)
|
||||
yield element
|
||||
|
||||
|
||||
unique_ordered = (
|
||||
unique_everseen if sys.version_info < (3, 7) else dict.fromkeys)
|
||||
|
||||
@@ -2,6 +2,17 @@
|
||||
importlib_metadata NEWS
|
||||
=========================
|
||||
|
||||
v1.6.0
|
||||
======
|
||||
|
||||
* Added ``module`` and ``attr`` attributes to ``EntryPoint``
|
||||
|
||||
v1.5.2
|
||||
======
|
||||
|
||||
* Fix redundant entries from ``FastPath.zip_children``.
|
||||
Closes #117.
|
||||
|
||||
v1.5.1
|
||||
======
|
||||
|
||||
|
||||
+9
-1
@@ -70,7 +70,9 @@ Entry points
|
||||
The ``entry_points()`` function returns a dictionary of all entry points,
|
||||
keyed by group. Entry points are represented by ``EntryPoint`` instances;
|
||||
each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
|
||||
a ``.load()`` method to resolve the value::
|
||||
a ``.load()`` method to resolve the value. There are also ``.module``,
|
||||
``.attr``, and ``.extras`` attributes for getting the components of the
|
||||
``.value`` attribute::
|
||||
|
||||
>>> eps = entry_points()
|
||||
>>> list(eps)
|
||||
@@ -79,6 +81,12 @@ a ``.load()`` method to resolve the value::
|
||||
>>> wheel = [ep for ep in scripts if ep.name == 'wheel'][0]
|
||||
>>> wheel
|
||||
EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')
|
||||
>>> wheel.module
|
||||
'wheel.cli'
|
||||
>>> wheel.attr
|
||||
'main'
|
||||
>>> wheel.extras
|
||||
[]
|
||||
>>> main = wheel.load()
|
||||
>>> main
|
||||
<function main at 0x103528488>
|
||||
|
||||
@@ -250,3 +250,9 @@ class TestEntryPoints(unittest.TestCase):
|
||||
"""
|
||||
with self.assertRaises(Exception):
|
||||
json.dumps(self.ep)
|
||||
|
||||
def test_module(self):
|
||||
assert self.ep.module == 'value'
|
||||
|
||||
def test_attr(self):
|
||||
assert self.ep.attr is None
|
||||
|
||||
+8
-1
@@ -1,7 +1,10 @@
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from .. import distribution, entry_points, files, PackageNotFoundError, version
|
||||
from .. import (
|
||||
distribution, entry_points, files, PackageNotFoundError,
|
||||
version, distributions,
|
||||
)
|
||||
|
||||
try:
|
||||
from importlib.resources import path
|
||||
@@ -52,6 +55,10 @@ class TestZip(unittest.TestCase):
|
||||
path = str(file.dist.locate_file(file))
|
||||
assert '.whl/' in path, path
|
||||
|
||||
def test_one_distribution(self):
|
||||
dists = list(distributions(path=sys.path[:1]))
|
||||
assert len(dists) == 1
|
||||
|
||||
|
||||
class TestEgg(TestZip):
|
||||
def setUp(self):
|
||||
|
||||
Vendored
+1
-1
@@ -1,4 +1,4 @@
|
||||
"""Wrappers to build Python packages using PEP 517 hooks
|
||||
"""
|
||||
|
||||
__version__ = '0.8.1'
|
||||
__version__ = '0.8.2'
|
||||
|
||||
Vendored
+27
-4
@@ -14,6 +14,7 @@ Results:
|
||||
"""
|
||||
from glob import glob
|
||||
from importlib import import_module
|
||||
import json
|
||||
import os
|
||||
import os.path
|
||||
from os.path import join as pjoin
|
||||
@@ -22,8 +23,30 @@ import shutil
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
# This is run as a script, not a module, so it can't do a relative import
|
||||
import compat
|
||||
# This file is run as a script, and `import compat` is not zip-safe, so we
|
||||
# include write_json() and read_json() from compat.py.
|
||||
#
|
||||
# Handle reading and writing JSON in UTF-8, on Python 3 and 2.
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
# Python 3
|
||||
def write_json(obj, path, **kwargs):
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(obj, f, **kwargs)
|
||||
|
||||
def read_json(path):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
else:
|
||||
# Python 2
|
||||
def write_json(obj, path, **kwargs):
|
||||
with open(path, 'wb') as f:
|
||||
json.dump(obj, f, encoding='utf-8', **kwargs)
|
||||
|
||||
def read_json(path):
|
||||
with open(path, 'rb') as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
class BackendUnavailable(Exception):
|
||||
@@ -233,7 +256,7 @@ def main():
|
||||
sys.exit("Unknown hook: %s" % hook_name)
|
||||
hook = globals()[hook_name]
|
||||
|
||||
hook_input = compat.read_json(pjoin(control_dir, 'input.json'))
|
||||
hook_input = read_json(pjoin(control_dir, 'input.json'))
|
||||
|
||||
json_out = {'unsupported': False, 'return_val': None}
|
||||
try:
|
||||
@@ -250,7 +273,7 @@ def main():
|
||||
except HookMissing:
|
||||
json_out['hook_missing'] = True
|
||||
|
||||
compat.write_json(json_out, pjoin(control_dir, 'output.json'), indent=2)
|
||||
write_json(json_out, pjoin(control_dir, 'output.json'), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Vendored
+1
-1
@@ -5,7 +5,7 @@ import sys
|
||||
|
||||
from . import shims
|
||||
|
||||
__version__ = "0.5.1"
|
||||
__version__ = "0.5.2"
|
||||
|
||||
|
||||
if "pip_shims" in sys.modules:
|
||||
|
||||
Vendored
+146
-78
@@ -19,6 +19,8 @@ from packaging import specifiers
|
||||
from .environment import MYPY_RUNNING
|
||||
from .utils import (
|
||||
call_function_with_correct_args,
|
||||
filter_allowed_args,
|
||||
get_allowed_args,
|
||||
get_method_args,
|
||||
nullcontext,
|
||||
suppress_setattr,
|
||||
@@ -65,6 +67,7 @@ if MYPY_RUNNING:
|
||||
TCommandInstance = TypeVar("TCommandInstance")
|
||||
TCmdDict = Dict[str, Union[Tuple[str, str, str], TCommandInstance]]
|
||||
TInstallRequirement = TypeVar("TInstallRequirement")
|
||||
TFormatControl = TypeVar("TFormatControl")
|
||||
TShimmedCmdDict = Union[TShim, TCmdDict]
|
||||
TWheelCache = TypeVar("TWheelCache")
|
||||
TPreparer = TypeVar("TPreparer")
|
||||
@@ -352,11 +355,11 @@ def ensure_resolution_dirs(**kwargs):
|
||||
|
||||
@contextlib.contextmanager
|
||||
def wheel_cache(
|
||||
wheel_cache_provider, # type: TShimmedFunc
|
||||
tempdir_manager_provider, # type: TShimmedFunc
|
||||
cache_dir, # type: str
|
||||
cache_dir=None, # type: str
|
||||
format_control=None, # type: Any
|
||||
wheel_cache_provider=None, # type: TShimmedFunc
|
||||
format_control_provider=None, # type: Optional[TShimmedFunc]
|
||||
tempdir_manager_provider=None, # type: TShimmedFunc
|
||||
):
|
||||
tempdir_manager_provider = resolve_possible_shim(tempdir_manager_provider)
|
||||
wheel_cache_provider = resolve_possible_shim(wheel_cache_provider)
|
||||
@@ -490,6 +493,7 @@ def get_requirement_set(
|
||||
cache_dir=None, # type: Optional[str]
|
||||
options=None, # type: Optional[Values]
|
||||
install_cmd_provider=None, # type: Optional[TShimmedFunc]
|
||||
wheel_cache_provider=None, # type: Optional[TShimmedFunc]
|
||||
):
|
||||
# (...) -> TRequirementSet
|
||||
"""
|
||||
@@ -499,6 +503,8 @@ def get_requirement_set(
|
||||
invalid parameters will be ignored if they are not needed to generate a
|
||||
requirement set on the current pip version.
|
||||
|
||||
:param :class:`~pip_shims.models.ShimmedPathCollection` wheel_cache_provider: A
|
||||
context manager provider which resolves to a `WheelCache` instance
|
||||
:param install_command: A :class:`~pip._internal.commands.install.InstallCommand`
|
||||
instance which is used to generate the finder.
|
||||
:param :class:`~pip_shims.models.ShimmedPathCollection` req_set_provider: A provider
|
||||
@@ -538,6 +544,7 @@ def get_requirement_set(
|
||||
:return: A new requirement set instance
|
||||
:rtype: :class:`~pip._internal.req.req_set.RequirementSet`
|
||||
"""
|
||||
wheel_cache_provider = resolve_possible_shim(wheel_cache_provider)
|
||||
req_set_provider = resolve_possible_shim(req_set_provider)
|
||||
if install_command is None:
|
||||
install_cmd_provider = resolve_possible_shim(install_cmd_provider)
|
||||
@@ -565,10 +572,13 @@ def get_requirement_set(
|
||||
)
|
||||
if session is None and "session" in required_args:
|
||||
session = get_session(install_cmd=install_command, options=options)
|
||||
results["wheel_cache"] = wheel_cache
|
||||
results["session"] = session
|
||||
results["wheel_download_dir"] = wheel_download_dir
|
||||
return call_function_with_correct_args(req_set_provider, **results)
|
||||
with ExitStack() as stack:
|
||||
if wheel_cache is None:
|
||||
wheel_cache = stack.enter_context(wheel_cache_provider(cache_dir=cache_dir))
|
||||
results["wheel_cache"] = wheel_cache
|
||||
results["session"] = session
|
||||
results["wheel_download_dir"] = wheel_download_dir
|
||||
return call_function_with_correct_args(req_set_provider, **results)
|
||||
|
||||
|
||||
def get_package_finder(
|
||||
@@ -665,11 +675,11 @@ def get_package_finder(
|
||||
):
|
||||
if target_python and not received_python:
|
||||
tags = target_python.get_tags()
|
||||
version_impl = set([t[0] for t in tags])
|
||||
version_impl = {t[0] for t in tags}
|
||||
# impls = set([v[:2] for v in version_impl])
|
||||
# impls.remove("py")
|
||||
# impl = next(iter(impls), "py") if not target_python
|
||||
versions = set([v[2:] for v in version_impl])
|
||||
versions = {v[2:] for v in version_impl}
|
||||
build_kwargs.update(
|
||||
{
|
||||
"platform": target_python.platform,
|
||||
@@ -689,6 +699,7 @@ def get_package_finder(
|
||||
def shim_unpack(
|
||||
unpack_fn, # type: TShimmedFunc
|
||||
download_dir, # type str
|
||||
tempdir_manager_provider, # type: TShimmedFunc
|
||||
ireq=None, # type: Optional[Any]
|
||||
link=None, # type: Optional[Any]
|
||||
location=None, # type Optional[str],
|
||||
@@ -708,6 +719,8 @@ def shim_unpack(
|
||||
:param unpack_fn: A callable or shim referring to the pip implementation
|
||||
:type unpack_fn: Callable
|
||||
:param str download_dir: The directory to download the file to
|
||||
:param TShimmedFunc tempdir_manager_provider: A callable or shim referring to
|
||||
`global_tempdir_manager` function from pip or a shimmed no-op context manager
|
||||
:param Optional[:class:`~pip._internal.req.req_install.InstallRequirement`] ireq:
|
||||
an Install Requirement instance, defaults to None
|
||||
:param Optional[:class:`~pip._internal.models.link.Link`] link: A Link instance,
|
||||
@@ -727,31 +740,33 @@ def shim_unpack(
|
||||
"""
|
||||
unpack_fn = resolve_possible_shim(unpack_fn)
|
||||
downloader_provider = resolve_possible_shim(downloader_provider)
|
||||
tempdir_manager_provider = resolve_possible_shim(tempdir_manager_provider)
|
||||
required_args = inspect.getargs(unpack_fn.__code__).args # type: ignore
|
||||
unpack_kwargs = {"download_dir": download_dir}
|
||||
if ireq:
|
||||
if not link and ireq.link:
|
||||
link = ireq.link
|
||||
if only_download is None:
|
||||
only_download = ireq.is_wheel
|
||||
if hashes is None:
|
||||
hashes = ireq.hashes(True)
|
||||
if location is None and getattr(ireq, "source_dir", None):
|
||||
location = ireq.source_dir
|
||||
unpack_kwargs.update({"link": link, "location": location})
|
||||
if hashes is not None and "hashes" in required_args:
|
||||
unpack_kwargs["hashes"] = hashes
|
||||
if "progress_bar" in required_args:
|
||||
unpack_kwargs["progress_bar"] = progress_bar
|
||||
if only_download is not None and "only_download" in required_args:
|
||||
unpack_kwargs["only_download"] = only_download
|
||||
if session is not None and "session" in required_args:
|
||||
unpack_kwargs["session"] = session
|
||||
if "downloader" in required_args and downloader_provider is not None:
|
||||
assert session is not None
|
||||
assert progress_bar is not None
|
||||
unpack_kwargs["downloader"] = downloader_provider(session, progress_bar)
|
||||
return unpack_fn(**unpack_kwargs) # type: ignore
|
||||
with tempdir_manager_provider():
|
||||
if ireq:
|
||||
if not link and ireq.link:
|
||||
link = ireq.link
|
||||
if only_download is None:
|
||||
only_download = ireq.is_wheel
|
||||
if hashes is None:
|
||||
hashes = ireq.hashes(True)
|
||||
if location is None and getattr(ireq, "source_dir", None):
|
||||
location = ireq.source_dir
|
||||
unpack_kwargs.update({"link": link, "location": location})
|
||||
if hashes is not None and "hashes" in required_args:
|
||||
unpack_kwargs["hashes"] = hashes
|
||||
if "progress_bar" in required_args:
|
||||
unpack_kwargs["progress_bar"] = progress_bar
|
||||
if only_download is not None and "only_download" in required_args:
|
||||
unpack_kwargs["only_download"] = only_download
|
||||
if session is not None and "session" in required_args:
|
||||
unpack_kwargs["session"] = session
|
||||
if "downloader" in required_args and downloader_provider is not None:
|
||||
assert session is not None
|
||||
assert progress_bar is not None
|
||||
unpack_kwargs["downloader"] = downloader_provider(session, progress_bar)
|
||||
return unpack_fn(**unpack_kwargs) # type: ignore
|
||||
|
||||
|
||||
def _ensure_finder(
|
||||
@@ -860,7 +875,7 @@ def make_preparer(
|
||||
preparer_fn = resolve_possible_shim(preparer_fn)
|
||||
downloader_provider = resolve_possible_shim(downloader_provider)
|
||||
finder_provider = resolve_possible_shim(finder_provider)
|
||||
required_args = inspect.getargs(preparer_fn.__init__.__code__).args # type: ignore
|
||||
required_args, required_kwargs = get_allowed_args(preparer_fn) # type: ignore
|
||||
if not req_tracker and not req_tracker_fn and "req_tracker" in required_args:
|
||||
raise TypeError("No requirement tracker and no req tracker generator found!")
|
||||
if "downloader" in required_args and not downloader_provider:
|
||||
@@ -918,6 +933,38 @@ def make_preparer(
|
||||
yield result
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def _ensure_wheel_cache(
|
||||
wheel_cache=None, # type: Optional[Type[TWheelCache]]
|
||||
wheel_cache_provider=None, # type: Optional[Callable]
|
||||
format_control=None, # type: Optional[TFormatControl]
|
||||
format_control_provider=None, # type: Optional[Type[TShimmedFunc]]
|
||||
options=None, # type: Optional[Values]
|
||||
cache_dir=None, # type: Optional[str]
|
||||
):
|
||||
if wheel_cache is not None:
|
||||
yield wheel_cache
|
||||
elif wheel_cache_provider is not None:
|
||||
with ExitStack() as stack:
|
||||
cache_dir = getattr(options, "cache_dir", cache_dir)
|
||||
format_control = getattr(
|
||||
options,
|
||||
"format_control",
|
||||
format_control_provider(None, None), # TFormatControl
|
||||
)
|
||||
wheel_cache = stack.enter_context(
|
||||
wheel_cache_provider(cache_dir, format_control)
|
||||
)
|
||||
yield wheel_cache
|
||||
|
||||
|
||||
def get_ireq_output_path(wheel_cache, ireq):
|
||||
if getattr(wheel_cache, "get_path_for_link", None):
|
||||
return wheel_cache.get_path_for_link(ireq.link)
|
||||
elif getattr(wheel_cache, "cached_wheel", None):
|
||||
return wheel_cache.cached_wheel(ireq.link, ireq.name).url_without_fragment
|
||||
|
||||
|
||||
def get_resolver(
|
||||
resolver_fn, # type: TShimmedFunc
|
||||
install_req_provider=None, # type: Optional[TShimmedFunc]
|
||||
@@ -938,6 +985,7 @@ def get_resolver(
|
||||
make_install_req=None, # type: Optional[Callable]
|
||||
install_cmd_provider=None, # type: Optional[TShimmedFunc]
|
||||
install_cmd=None, # type: Optional[TCommandInstance]
|
||||
use_pep517=True, # type: bool
|
||||
):
|
||||
# (...) -> TResolver
|
||||
"""
|
||||
@@ -985,6 +1033,7 @@ def get_resolver(
|
||||
to the resolver for actually generating install requirements, if necessary
|
||||
:param Optional[TCommandInstance] install_cmd: The install command used to create
|
||||
the finder, session, and options if needed, defaults to None.
|
||||
:param bool use_pep517: Whether to use the pep517 build process.
|
||||
:return: A new resolver instance.
|
||||
:rtype: :class:`~pip._internal.legacy_resolve.Resolver`
|
||||
|
||||
@@ -1049,7 +1098,7 @@ def get_resolver(
|
||||
continue
|
||||
elif val is None and install_cmd is None:
|
||||
raise TypeError(
|
||||
"Preparer requires a {0} but did not receive one "
|
||||
"Preparer requires a {} but did not receive one "
|
||||
"and cannot generate one".format(arg)
|
||||
)
|
||||
elif arg == "session" and val is None:
|
||||
@@ -1059,26 +1108,21 @@ def get_resolver(
|
||||
resolver_kwargs[arg] = val
|
||||
if "make_install_req" in required_args:
|
||||
if make_install_req is None and install_req_provider is not None:
|
||||
make_install_req_kwargs = {
|
||||
"isolated": isolated,
|
||||
"wheel_cache": wheel_cache,
|
||||
"use_pep517": use_pep517,
|
||||
}
|
||||
factory_args, factory_kwargs = filter_allowed_args(
|
||||
install_req_provider, **make_install_req_kwargs
|
||||
)
|
||||
make_install_req = functools.partial(
|
||||
install_req_provider,
|
||||
isolated=isolated,
|
||||
wheel_cache=wheel_cache,
|
||||
# use_pep517=use_pep517,
|
||||
install_req_provider, *factory_args, **factory_kwargs
|
||||
)
|
||||
assert make_install_req is not None
|
||||
resolver_kwargs["make_install_req"] = make_install_req
|
||||
if "isolated" in required_args:
|
||||
resolver_kwargs["isolated"] = isolated
|
||||
if "wheel_cache" in required_args:
|
||||
if wheel_cache is None and wheel_cache_provider is not None:
|
||||
cache_dir = getattr(options, "cache_dir", None)
|
||||
format_control = getattr(
|
||||
options,
|
||||
"format_control",
|
||||
format_control_provider(None, None), # type: ignore
|
||||
)
|
||||
wheel_cache = wheel_cache_provider(cache_dir, format_control)
|
||||
resolver_kwargs["wheel_cache"] = wheel_cache
|
||||
resolver_kwargs.update(
|
||||
{
|
||||
"upgrade_strategy": upgrade_strategy,
|
||||
@@ -1090,6 +1134,15 @@ def get_resolver(
|
||||
"preparer": preparer,
|
||||
}
|
||||
)
|
||||
if "wheel_cache" in required_args:
|
||||
with _ensure_wheel_cache(
|
||||
wheel_cache=wheel_cache,
|
||||
wheel_cache_provider=wheel_cache_provider,
|
||||
format_control_provider=format_control_provider,
|
||||
options=options,
|
||||
) as wheel_cache:
|
||||
resolver_kwargs["wheel_cache"] = wheel_cache
|
||||
return resolver_fn(**resolver_kwargs) # type: ignore
|
||||
return resolver_fn(**resolver_kwargs) # type: ignore
|
||||
|
||||
|
||||
@@ -1255,22 +1308,29 @@ def resolve( # noqa:C901
|
||||
if session is None:
|
||||
session = get_session(install_cmd=install_command, options=options)
|
||||
if finder is None:
|
||||
finder = finder_provider(install_command, options=options, session=session) # type: ignore
|
||||
finder = finder_provider(
|
||||
install_command, options=options, session=session
|
||||
) # type: ignore
|
||||
format_control = getattr(options, "format_control", None)
|
||||
if not format_control:
|
||||
format_control = format_control_provider(None, None) # type: ignore
|
||||
wheel_cache = wheel_cache_provider(
|
||||
kwargs["cache_dir"], format_control
|
||||
wheel_cache = ctx.enter_context(
|
||||
wheel_cache_provider(kwargs["cache_dir"], format_control)
|
||||
) # type: ignore
|
||||
ireq.is_direct = True # type: ignore
|
||||
build_location_kwargs = {"build_dir": kwargs["build_dir"], "autodelete": True}
|
||||
call_function_with_correct_args(ireq.build_location, **build_location_kwargs)
|
||||
# ireq.build_location(kwargs["build_dir"]) # type: ignore
|
||||
if reqset_provider is None:
|
||||
raise TypeError(
|
||||
"cannot resolve without a requirement set provider... failed!"
|
||||
)
|
||||
reqset = reqset_provider(install_command, options=options, session=session, wheel_download_dir=wheel_download_dir, **kwargs) # type: ignore
|
||||
reqset = reqset_provider(
|
||||
install_command,
|
||||
options=options,
|
||||
session=session,
|
||||
wheel_download_dir=wheel_download_dir,
|
||||
**kwargs
|
||||
) # type: ignore
|
||||
if getattr(reqset, "prepare_files", None):
|
||||
reqset.add_requirement(ireq)
|
||||
results = reqset.prepare_files(finder)
|
||||
@@ -1293,7 +1353,6 @@ def resolve( # noqa:C901
|
||||
"use_user_site": use_user_site,
|
||||
"require_hashes": require_hashes,
|
||||
}
|
||||
# with req_tracker_provider() as req_tracker:
|
||||
if isinstance(req_tracker_provider, (types.FunctionType, functools.partial)):
|
||||
preparer_args["req_tracker"] = ctx.enter_context(req_tracker_provider())
|
||||
resolver_keys = [
|
||||
@@ -1338,7 +1397,7 @@ def resolve( # noqa:C901
|
||||
return results
|
||||
|
||||
|
||||
def build_wheel(
|
||||
def build_wheel( # noqa:C901
|
||||
req=None, # type: Optional[TInstallRequirement]
|
||||
reqset=None, # type: Optional[Union[TReqSet, Iterable[TInstallRequirement]]]
|
||||
output_dir=None, # type: Optional[str]
|
||||
@@ -1368,8 +1427,9 @@ def build_wheel(
|
||||
build_many_provider=None, # type: Optional[TShimmedFunc]
|
||||
install_command_provider=None, # type: Optional[TShimmedFunc]
|
||||
finder_provider=None, # type: Optional[TShimmedFunc]
|
||||
reqset_provider=None, # type: Optional[TShimmedFunc]
|
||||
):
|
||||
# type: (...) -> Optional[Union[str, Tuple[List[TInstallRequirement], List[TInstallRequirement]]]]
|
||||
# type: (...) -> Generator[Union[str, Tuple[List[TInstallRequirement], ...]], None, None]
|
||||
"""
|
||||
Build a wheel or a set of wheels
|
||||
|
||||
@@ -1421,19 +1481,21 @@ def build_wheel(
|
||||
:param Optional[TShimmedFunc] install_command_provider: A shim for providing new
|
||||
install command instances
|
||||
:param TShimmedFunc finder_provider: A provider to package finder instances
|
||||
:param TShimmedFunc reqset_provider: A provider for requirement set generation
|
||||
:return: A tuple of successful and failed install requirements or else a path to
|
||||
a wheel
|
||||
:rtype: Optional[Union[str, Tuple[List[TInstallRequirement], List[TInstallRequirement]]]]
|
||||
"""
|
||||
wheel_cache_provider = resolve_possible_shim(wheel_cache_provider)
|
||||
preparer = resolve_possible_shim(preparer)
|
||||
preparer_provider = resolve_possible_shim(preparer_provider)
|
||||
wheel_builder_provider = resolve_possible_shim(wheel_builder_provider)
|
||||
build_one_provider = resolve_possible_shim(build_one_provider)
|
||||
build_one_inside_env_provider = resolve_possible_shim(build_one_inside_env_provider)
|
||||
build_many_provider = resolve_possible_shim(build_many_provider)
|
||||
install_cmd_provider = resolve_possible_shim(install_command_provider)
|
||||
format_control_provider = resolve_possible_shim(format_control_provider)
|
||||
finder_provider = resolve_possible_shim(finder_provider)
|
||||
finder_provider = resolve_possible_shim(finder_provider) or get_package_finder
|
||||
reqset_provider = resolve_possible_shim(reqset_provider)
|
||||
global_options = [] if global_options is None else global_options
|
||||
build_options = [] if build_options is None else build_options
|
||||
options = None
|
||||
@@ -1447,25 +1509,31 @@ def build_wheel(
|
||||
}
|
||||
if not req and not reqset:
|
||||
raise TypeError("Must provide either a requirement or requirement set to build")
|
||||
if wheel_cache is None and (reqset is not None or output_dir is None):
|
||||
if install_command is None:
|
||||
assert isinstance(install_cmd_provider, (type, functools.partial))
|
||||
install_command = install_cmd_provider()
|
||||
kwargs, options = populate_options(install_command, options, **kwarg_map)
|
||||
format_control = getattr(options, "format_control", None)
|
||||
if not format_control:
|
||||
format_control = format_control_provider(None, None) # type: ignore
|
||||
wheel_cache = wheel_cache_provider(options.cache_dir, format_control)
|
||||
if req and not reqset and not output_dir:
|
||||
output_dir = wheel_cache.get_path_for_link(req.link)
|
||||
if not reqset and build_one_provider:
|
||||
yield build_one_provider(req, output_dir, build_options, global_options)
|
||||
elif build_many_provider:
|
||||
yield build_many_provider(
|
||||
reqset, wheel_cache, build_options, global_options, check_binary_allowed
|
||||
)
|
||||
else:
|
||||
with ExitStack() as ctx:
|
||||
with ExitStack() as ctx:
|
||||
kwargs = kwarg_map.copy()
|
||||
if wheel_cache is None and (reqset is not None or output_dir is None):
|
||||
if install_command is None:
|
||||
assert isinstance(install_cmd_provider, (type, functools.partial))
|
||||
install_command = install_cmd_provider()
|
||||
kwargs, options = populate_options(install_command, options, **kwarg_map)
|
||||
format_control = getattr(options, "format_control", None)
|
||||
if not format_control:
|
||||
format_control = format_control_provider(None, None) # type: ignore
|
||||
wheel_cache = ctx.enter_context(
|
||||
wheel_cache_provider(options.cache_dir, format_control)
|
||||
)
|
||||
if req and not reqset and not output_dir:
|
||||
output_dir = get_ireq_output_path(wheel_cache, req)
|
||||
if not reqset and build_one_provider:
|
||||
yield build_one_provider(req, output_dir, build_options, global_options)
|
||||
elif build_many_provider:
|
||||
yield build_many_provider(
|
||||
reqset, wheel_cache, build_options, global_options, check_binary_allowed
|
||||
)
|
||||
else:
|
||||
builder_args, builder_kwargs = get_allowed_args(wheel_builder_provider)
|
||||
if "requirement_set" in builder_args and not reqset:
|
||||
reqset = reqset_provider()
|
||||
if session is None and finder is None:
|
||||
session = get_session(install_cmd=install_command, options=options)
|
||||
finder = finder_provider(
|
||||
@@ -1503,7 +1571,7 @@ def build_wheel(
|
||||
)
|
||||
if req and not reqset:
|
||||
if not output_dir:
|
||||
output_dir = wheel_cache.get_path_for_link(req.link)
|
||||
output_dir = get_ireq_output_path(wheel_cache, req)
|
||||
if use_pep517 is not None:
|
||||
req.use_pep517 = use_pep517
|
||||
yield builder._build_one(req, output_dir)
|
||||
|
||||
Vendored
+38
-22
@@ -104,6 +104,9 @@ PIP_VERSION_SET = {
|
||||
"19.2.3",
|
||||
"19.3",
|
||||
"19.3.1",
|
||||
"20.0",
|
||||
"20.0.1",
|
||||
"20.0.2",
|
||||
}
|
||||
|
||||
|
||||
@@ -140,9 +143,9 @@ class PipVersion(Sequence):
|
||||
parsed_version = self._parse()
|
||||
if base_import_path is None:
|
||||
if parsed_version >= parse_version("10.0.0"):
|
||||
base_import_path = "{0}._internal".format(BASE_IMPORT_PATH)
|
||||
base_import_path = "{}._internal".format(BASE_IMPORT_PATH)
|
||||
else:
|
||||
base_import_path = "{0}".format(BASE_IMPORT_PATH)
|
||||
base_import_path = "{}".format(BASE_IMPORT_PATH)
|
||||
self.base_import_path = base_import_path
|
||||
self.parsed_version = parsed_version
|
||||
|
||||
@@ -175,13 +178,12 @@ class PipVersion(Sequence):
|
||||
|
||||
def __str__(self):
|
||||
# type: () -> str
|
||||
return "{0!s}".format(self.parsed_version)
|
||||
return "{!s}".format(self.parsed_version)
|
||||
|
||||
def __repr__(self):
|
||||
# type: () -> str
|
||||
return (
|
||||
"<PipVersion {0!r}, Path: {1!r}, Vendor Path: {2!r}, "
|
||||
"Parsed Version: {3!r}>"
|
||||
"<PipVersion {!r}, Path: {!r}, Vendor Path: {!r}, " "Parsed Version: {!r}>"
|
||||
).format(
|
||||
self.version,
|
||||
self.base_import_path,
|
||||
@@ -253,17 +255,17 @@ class PipVersionRange(Sequence):
|
||||
|
||||
def __str__(self):
|
||||
# type: () -> str
|
||||
return "{0!s} -> {1!s}".format(self._versions[0], self._versions[-1])
|
||||
return "{!s} -> {!s}".format(self._versions[0], self._versions[-1])
|
||||
|
||||
@property
|
||||
def base_import_paths(self):
|
||||
# type: () -> Set[str]
|
||||
return set([version.base_import_path for version in self._versions])
|
||||
return {version.base_import_path for version in self._versions}
|
||||
|
||||
@property
|
||||
def vendor_import_paths(self):
|
||||
# type: () -> Set[str]
|
||||
return set([version.vendor_import_path for version in self._versions])
|
||||
return {version.vendor_import_path for version in self._versions}
|
||||
|
||||
def is_valid(self):
|
||||
# type: () -> bool
|
||||
@@ -430,7 +432,7 @@ class ShimmedPath(object):
|
||||
if not self.is_class:
|
||||
return provided
|
||||
if not inspect.isclass(provided):
|
||||
raise TypeError("Provided argument is not a class: {0!r}".format(provided))
|
||||
raise TypeError("Provided argument is not a class: {!r}".format(provided))
|
||||
methods = self._parse_provides_dict(
|
||||
self.provided_methods, prepend_arg_to_callables="self"
|
||||
)
|
||||
@@ -554,9 +556,7 @@ class ShimmedPath(object):
|
||||
imported, result = self._shim_parent(imported, attribute_name)
|
||||
if result is not None:
|
||||
result = self._ensure_functions(result)
|
||||
full_import_path = "{0}.{1}".format(
|
||||
self.calculated_module_path, attribute_name
|
||||
)
|
||||
full_import_path = "{}.{}".format(self.calculated_module_path, attribute_name)
|
||||
self._imported = imported
|
||||
assert isinstance(result, types.ModuleType)
|
||||
self._provided = result
|
||||
@@ -921,13 +921,6 @@ unpack_url = ShimmedPathCollection("unpack_url", ImportTypes.FUNCTION)
|
||||
unpack_url.create_path("download.unpack_url", "7.0.0", "19.3.9")
|
||||
unpack_url.create_path("operations.prepare.unpack_url", "20.0", "9999")
|
||||
|
||||
shim_unpack = ShimmedPathCollection("shim_unpack", ImportTypes.FUNCTION)
|
||||
shim_unpack.set_default(
|
||||
functools.partial(
|
||||
compat.shim_unpack, unpack_fn=unpack_url, downloader_provider=Downloader
|
||||
)
|
||||
)
|
||||
|
||||
is_installable_dir = ShimmedPathCollection("is_installable_dir", ImportTypes.FUNCTION)
|
||||
is_installable_dir.create_path("utils.misc.is_installable_dir", "10.0.0", "9999")
|
||||
is_installable_dir.create_path("utils.is_installable_dir", "7.0.0", "9.0.3")
|
||||
@@ -1036,6 +1029,16 @@ global_tempdir_manager.create_path(
|
||||
"utils.temp_dir.global_tempdir_manager", "7.0.0", "9999"
|
||||
)
|
||||
|
||||
shim_unpack = ShimmedPathCollection("shim_unpack", ImportTypes.FUNCTION)
|
||||
shim_unpack.set_default(
|
||||
functools.partial(
|
||||
compat.shim_unpack,
|
||||
unpack_fn=unpack_url,
|
||||
downloader_provider=Downloader,
|
||||
tempdir_manager_provider=global_tempdir_manager,
|
||||
)
|
||||
)
|
||||
|
||||
get_requirement_tracker = ShimmedPathCollection(
|
||||
"get_requirement_tracker", ImportTypes.CONTEXTMANAGER
|
||||
)
|
||||
@@ -1128,6 +1131,17 @@ DEV_PKGS.create_path("commands.freeze.DEV_PKGS", "9.0.0", "9999")
|
||||
DEV_PKGS.set_default({"setuptools", "pip", "distribute", "wheel"})
|
||||
|
||||
|
||||
wheel_cache = ShimmedPathCollection("wheel_cache", ImportTypes.FUNCTION)
|
||||
wheel_cache.set_default(
|
||||
functools.partial(
|
||||
compat.wheel_cache,
|
||||
wheel_cache_provider=WheelCache,
|
||||
tempdir_manager_provider=global_tempdir_manager,
|
||||
format_control_provider=FormatControl,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
get_package_finder = ShimmedPathCollection("get_package_finder", ImportTypes.FUNCTION)
|
||||
get_package_finder.set_default(
|
||||
functools.partial(
|
||||
@@ -1158,7 +1172,7 @@ get_resolver.set_default(
|
||||
install_cmd_provider=InstallCommand,
|
||||
resolver_fn=Resolver,
|
||||
install_req_provider=install_req_from_req_string,
|
||||
wheel_cache_provider=WheelCache,
|
||||
wheel_cache_provider=wheel_cache,
|
||||
format_control_provider=FormatControl,
|
||||
)
|
||||
)
|
||||
@@ -1170,6 +1184,7 @@ get_requirement_set.set_default(
|
||||
compat.get_requirement_set,
|
||||
install_cmd_provider=InstallCommand,
|
||||
req_set_provider=RequirementSet,
|
||||
wheel_cache_provider=wheel_cache,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1182,7 +1197,7 @@ resolve.set_default(
|
||||
reqset_provider=get_requirement_set,
|
||||
finder_provider=get_package_finder,
|
||||
resolver_provider=get_resolver,
|
||||
wheel_cache_provider=WheelCache,
|
||||
wheel_cache_provider=wheel_cache,
|
||||
format_control_provider=FormatControl,
|
||||
make_preparer_provider=make_preparer,
|
||||
req_tracker_provider=get_requirement_tracker,
|
||||
@@ -1196,12 +1211,13 @@ build_wheel.set_default(
|
||||
functools.partial(
|
||||
compat.build_wheel,
|
||||
install_command_provider=InstallCommand,
|
||||
wheel_cache_provider=WheelCache,
|
||||
wheel_cache_provider=wheel_cache,
|
||||
wheel_builder_provider=WheelBuilder,
|
||||
build_one_provider=build_one,
|
||||
build_one_inside_env_provider=build_one_inside_env,
|
||||
build_many_provider=build,
|
||||
preparer_provider=make_preparer,
|
||||
format_control_provider=FormatControl,
|
||||
reqset_provider=get_requirement_set,
|
||||
)
|
||||
)
|
||||
|
||||
Vendored
+23
@@ -440,3 +440,26 @@ def call_function_with_correct_args(fn, **provided_kwargs):
|
||||
continue
|
||||
kwargs[arg] = provided_kwargs[arg]
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
|
||||
def filter_allowed_args(fn, **provided_kwargs):
|
||||
# type: (Callable, Dict[str, Any]) -> Tuple[List[Any], Dict[str, Any]]
|
||||
"""
|
||||
Given a function and a kwarg mapping, return only those kwargs used in the function.
|
||||
|
||||
:param Callable fn: A function to inspect
|
||||
:param Dict[str, Any] kwargs: A mapping of kwargs to filter
|
||||
:return: A new, filtered kwarg mapping
|
||||
:rtype: Tuple[List[Any], Dict[str, Any]]
|
||||
"""
|
||||
args = []
|
||||
kwargs = {}
|
||||
func_args, func_kwargs = get_allowed_args(fn)
|
||||
for arg in func_args:
|
||||
if arg in provided_kwargs:
|
||||
args.append(provided_kwargs[arg])
|
||||
for arg in func_kwargs:
|
||||
if arg not in provided_kwargs:
|
||||
continue
|
||||
kwargs[arg] = provided_kwargs[arg]
|
||||
return args, kwargs
|
||||
|
||||
Vendored
+31
-14
@@ -95,8 +95,8 @@ classes inherit from. Use the docstrings for examples of how to:
|
||||
namespace class
|
||||
"""
|
||||
|
||||
__version__ = "2.4.6"
|
||||
__versionTime__ = "24 Dec 2019 04:27 UTC"
|
||||
__version__ = "2.4.7"
|
||||
__versionTime__ = "30 Mar 2020 00:43 UTC"
|
||||
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
|
||||
|
||||
import string
|
||||
@@ -1391,6 +1391,12 @@ class ParserElement(object):
|
||||
"""
|
||||
ParserElement._literalStringClass = cls
|
||||
|
||||
@classmethod
|
||||
def _trim_traceback(cls, tb):
|
||||
while tb.tb_next:
|
||||
tb = tb.tb_next
|
||||
return tb
|
||||
|
||||
def __init__(self, savelist=False):
|
||||
self.parseAction = list()
|
||||
self.failAction = None
|
||||
@@ -1943,7 +1949,9 @@ class ParserElement(object):
|
||||
if ParserElement.verbose_stacktrace:
|
||||
raise
|
||||
else:
|
||||
# catch and re-raise exception from here, clears out pyparsing internal stack trace
|
||||
# catch and re-raise exception from here, clearing out pyparsing internal stack trace
|
||||
if getattr(exc, '__traceback__', None) is not None:
|
||||
exc.__traceback__ = self._trim_traceback(exc.__traceback__)
|
||||
raise exc
|
||||
else:
|
||||
return tokens
|
||||
@@ -2017,7 +2025,9 @@ class ParserElement(object):
|
||||
if ParserElement.verbose_stacktrace:
|
||||
raise
|
||||
else:
|
||||
# catch and re-raise exception from here, clears out pyparsing internal stack trace
|
||||
# catch and re-raise exception from here, clearing out pyparsing internal stack trace
|
||||
if getattr(exc, '__traceback__', None) is not None:
|
||||
exc.__traceback__ = self._trim_traceback(exc.__traceback__)
|
||||
raise exc
|
||||
|
||||
def transformString(self, instring):
|
||||
@@ -2063,7 +2073,9 @@ class ParserElement(object):
|
||||
if ParserElement.verbose_stacktrace:
|
||||
raise
|
||||
else:
|
||||
# catch and re-raise exception from here, clears out pyparsing internal stack trace
|
||||
# catch and re-raise exception from here, clearing out pyparsing internal stack trace
|
||||
if getattr(exc, '__traceback__', None) is not None:
|
||||
exc.__traceback__ = self._trim_traceback(exc.__traceback__)
|
||||
raise exc
|
||||
|
||||
def searchString(self, instring, maxMatches=_MAX_INT):
|
||||
@@ -2093,7 +2105,9 @@ class ParserElement(object):
|
||||
if ParserElement.verbose_stacktrace:
|
||||
raise
|
||||
else:
|
||||
# catch and re-raise exception from here, clears out pyparsing internal stack trace
|
||||
# catch and re-raise exception from here, clearing out pyparsing internal stack trace
|
||||
if getattr(exc, '__traceback__', None) is not None:
|
||||
exc.__traceback__ = self._trim_traceback(exc.__traceback__)
|
||||
raise exc
|
||||
|
||||
def split(self, instring, maxsplit=_MAX_INT, includeSeparators=False):
|
||||
@@ -2565,7 +2579,9 @@ class ParserElement(object):
|
||||
if ParserElement.verbose_stacktrace:
|
||||
raise
|
||||
else:
|
||||
# catch and re-raise exception from here, clears out pyparsing internal stack trace
|
||||
# catch and re-raise exception from here, clearing out pyparsing internal stack trace
|
||||
if getattr(exc, '__traceback__', None) is not None:
|
||||
exc.__traceback__ = self._trim_traceback(exc.__traceback__)
|
||||
raise exc
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -2724,7 +2740,7 @@ class ParserElement(object):
|
||||
continue
|
||||
if not t:
|
||||
continue
|
||||
out = ['\n'.join(comments), t]
|
||||
out = ['\n' + '\n'.join(comments) if comments else '', t]
|
||||
comments = []
|
||||
try:
|
||||
# convert newline marks to actual newlines, and strip leading BOM if present
|
||||
@@ -3312,7 +3328,7 @@ class Regex(Token):
|
||||
self.name = _ustr(self)
|
||||
self.errmsg = "Expected " + self.name
|
||||
self.mayIndexError = False
|
||||
self.mayReturnEmpty = True
|
||||
self.mayReturnEmpty = self.re_match("") is not None
|
||||
self.asGroupList = asGroupList
|
||||
self.asMatch = asMatch
|
||||
if self.asGroupList:
|
||||
@@ -3993,6 +4009,7 @@ class And(ParseExpression):
|
||||
self.leaveWhitespace()
|
||||
|
||||
def __init__(self, exprs, savelist=True):
|
||||
exprs = list(exprs)
|
||||
if exprs and Ellipsis in exprs:
|
||||
tmp = []
|
||||
for i, expr in enumerate(exprs):
|
||||
@@ -4358,7 +4375,7 @@ class Each(ParseExpression):
|
||||
if self.initExprGroups:
|
||||
self.opt1map = dict((id(e.expr), e) for e in self.exprs if isinstance(e, Optional))
|
||||
opt1 = [e.expr for e in self.exprs if isinstance(e, Optional)]
|
||||
opt2 = [e for e in self.exprs if e.mayReturnEmpty and not isinstance(e, Optional)]
|
||||
opt2 = [e for e in self.exprs if e.mayReturnEmpty and not isinstance(e, (Optional, Regex))]
|
||||
self.optionals = opt1 + opt2
|
||||
self.multioptionals = [e.expr for e in self.exprs if isinstance(e, ZeroOrMore)]
|
||||
self.multirequired = [e.expr for e in self.exprs if isinstance(e, OneOrMore)]
|
||||
@@ -5435,8 +5452,8 @@ def matchPreviousExpr(expr):
|
||||
return rep
|
||||
|
||||
def _escapeRegexRangeChars(s):
|
||||
# ~ escape these chars: ^-]
|
||||
for c in r"\^-]":
|
||||
# ~ escape these chars: ^-[]
|
||||
for c in r"\^-[]":
|
||||
s = s.replace(c, _bslash + c)
|
||||
s = s.replace("\n", r"\n")
|
||||
s = s.replace("\t", r"\t")
|
||||
@@ -6550,10 +6567,10 @@ class pyparsing_common:
|
||||
"""mixed integer of the form 'integer - fraction', with optional leading integer, returns float"""
|
||||
mixed_integer.addParseAction(sum)
|
||||
|
||||
real = Regex(r'[+-]?(:?\d+\.\d*|\.\d+)').setName("real number").setParseAction(convertToFloat)
|
||||
real = Regex(r'[+-]?(?:\d+\.\d*|\.\d+)').setName("real number").setParseAction(convertToFloat)
|
||||
"""expression that parses a floating point number and returns a float"""
|
||||
|
||||
sci_real = Regex(r'[+-]?(:?\d+(:?[eE][+-]?\d+)|(:?\d+\.\d*|\.\d+)(:?[eE][+-]?\d+)?)').setName("real number with scientific notation").setParseAction(convertToFloat)
|
||||
sci_real = Regex(r'[+-]?(?:\d+(?:[eE][+-]?\d+)|(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?)').setName("real number with scientific notation").setParseAction(convertToFloat)
|
||||
"""expression that parses a floating point number with optional
|
||||
scientific notation and returns a float"""
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ from .models.lockfile import Lockfile
|
||||
from .models.pipfile import Pipfile
|
||||
from .models.requirements import Requirement
|
||||
|
||||
__version__ = "1.5.4"
|
||||
__version__ = "1.5.7"
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
+39
-5
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import itertools
|
||||
import operator
|
||||
import re
|
||||
|
||||
import attr
|
||||
import distlib.markers
|
||||
@@ -196,15 +197,19 @@ def _get_specs(specset):
|
||||
return sorted(result, key=operator.itemgetter(1))
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
def _group_by_op(specs):
|
||||
# type: (Union[Set[Specifier], SpecifierSet]) -> Iterator
|
||||
specs = [_get_specs(x) for x in list(specs)]
|
||||
flattened = [(op, version) for spec in specs for op, version in spec]
|
||||
flattened = [
|
||||
((op, len(version) > 2), version) for spec in specs for op, version in spec
|
||||
]
|
||||
specs = sorted(flattened)
|
||||
grouping = itertools.groupby(specs, key=operator.itemgetter(0))
|
||||
return grouping
|
||||
|
||||
|
||||
# TODO: rename this to something meaningful
|
||||
def normalize_specifier_set(specs):
|
||||
# type: (Union[str, SpecifierSet]) -> Optional[Set[Specifier]]
|
||||
"""Given a specifier set, a string, or an iterable, normalize the specifiers
|
||||
@@ -235,6 +240,8 @@ def normalize_specifier_set(specs):
|
||||
return normalize_specifier_set(SpecifierSet(",".join(spec_list)))
|
||||
|
||||
|
||||
# TODO: Check if this is used by anything public otherwise make it private
|
||||
# And rename it to something meaningful
|
||||
def get_sorted_version_string(version_set):
|
||||
# type: (Set[AnyStr]) -> AnyStr
|
||||
version_list = sorted(
|
||||
@@ -244,6 +251,9 @@ def get_sorted_version_string(version_set):
|
||||
return version
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
# TODO: Add a deprecation decorator and deprecate this -- i'm sure it's used
|
||||
# in other libraries
|
||||
@lru_cache(maxsize=1024)
|
||||
def cleanup_pyspecs(specs, joiner="or"):
|
||||
specs = normalize_specifier_set(specs)
|
||||
@@ -275,7 +285,8 @@ def cleanup_pyspecs(specs, joiner="or"):
|
||||
"==": lambda x: "in" if len(x) > 1 else "==",
|
||||
}
|
||||
translation_keys = list(translation_map.keys())
|
||||
for op, versions in _group_by_op(tuple(specs)):
|
||||
for op_and_version_type, versions in _group_by_op(tuple(specs)):
|
||||
op = op_and_version_type[0]
|
||||
versions = [version[1] for version in versions]
|
||||
versions = sorted(dedup(versions))
|
||||
op_key = next(iter(k for k in translation_keys if op in k), None)
|
||||
@@ -284,10 +295,11 @@ def cleanup_pyspecs(specs, joiner="or"):
|
||||
version_value = translation_map[op_key][joiner](versions)
|
||||
if op in op_translations:
|
||||
op = op_translations[op](versions)
|
||||
results[op] = version_value
|
||||
return sorted([(k, v) for k, v in results.items()], key=operator.itemgetter(1))
|
||||
results[(op, op_and_version_type[1])] = version_value
|
||||
return sorted([(k[0], v) for k, v in results.items()], key=operator.itemgetter(1))
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
@lru_cache(maxsize=1024)
|
||||
def fix_version_tuple(version_tuple):
|
||||
# type: (Tuple[AnyStr, AnyStr]) -> Tuple[AnyStr, AnyStr]
|
||||
@@ -302,6 +314,7 @@ def fix_version_tuple(version_tuple):
|
||||
return (op, version)
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful, deprecate it (See prior function)
|
||||
@lru_cache(maxsize=128)
|
||||
def get_versions(specset, group_by_operator=True):
|
||||
# type: (Union[Set[Specifier], SpecifierSet], bool) -> List[Tuple[STRING_TYPE, STRING_TYPE]]
|
||||
@@ -590,6 +603,7 @@ def get_specset(marker_list):
|
||||
return specifiers
|
||||
|
||||
|
||||
# TODO: Refactor this (reduce complexity)
|
||||
def parse_marker_dict(marker_dict):
|
||||
op = marker_dict["op"]
|
||||
lhs = marker_dict["lhs"]
|
||||
@@ -658,9 +672,16 @@ def parse_marker_dict(marker_dict):
|
||||
return specset, finalized_marker
|
||||
|
||||
|
||||
def _contains_micro_version(version_string):
|
||||
return re.search("\d+\.\d+\.\d+", version_string) is not None
|
||||
|
||||
|
||||
def format_pyversion(parts):
|
||||
op, val = parts
|
||||
return "python_version {0} '{1}'".format(op, val)
|
||||
version_marker = (
|
||||
"python_full_version" if _contains_micro_version(val) else "python_version"
|
||||
)
|
||||
return "{0} {1} '{2}'".format(version_marker, op, val)
|
||||
|
||||
|
||||
def normalize_marker_str(marker):
|
||||
@@ -699,3 +720,16 @@ def marker_from_specifier(spec):
|
||||
marker_segments.append(format_pyversion(marker_segment))
|
||||
marker_str = " and ".join(marker_segments).replace('"', "'")
|
||||
return Marker(marker_str)
|
||||
|
||||
|
||||
def merge_markers(m1, m2):
|
||||
# type: (Marker, Marker) -> Optional[Marker]
|
||||
if not all((m1, m2)):
|
||||
return next(iter(v for v in (m1, m2) if v), None)
|
||||
m1 = _ensure_marker(m1)
|
||||
m2 = _ensure_marker(m2)
|
||||
_markers = [] # type: List[Marker]
|
||||
for marker in (m1, m2):
|
||||
_markers.append(str(marker))
|
||||
marker_str = " and ".join([normalize_marker_str(m) for m in _markers if m])
|
||||
return _ensure_marker(normalize_marker_str(marker_str))
|
||||
|
||||
+1240
File diff suppressed because it is too large
Load Diff
@@ -164,6 +164,10 @@ class Pipfile(object):
|
||||
# type: () -> Union[plette.pipfiles.Pipfile, PipfileLoader]
|
||||
return self.projectfile.model
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
return self.path.parent
|
||||
|
||||
@property
|
||||
def extended_keys(self):
|
||||
return [
|
||||
|
||||
+55
-25
@@ -679,7 +679,7 @@ AST_COMPARATORS = dict(
|
||||
(ast.And, operator.and_),
|
||||
(ast.Or, operator.or_),
|
||||
(ast.Not, operator.not_),
|
||||
(ast.In, operator.contains),
|
||||
(ast.In, lambda a, b: operator.contains(b, a)),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -688,6 +688,8 @@ class Analyzer(ast.NodeVisitor):
|
||||
def __init__(self):
|
||||
self.name_types = []
|
||||
self.function_map = {} # type: Dict[Any, Any]
|
||||
self.function_names = {}
|
||||
self.resolved_function_names = {}
|
||||
self.functions = []
|
||||
self.strings = []
|
||||
self.assignments = {}
|
||||
@@ -725,6 +727,38 @@ class Analyzer(ast.NodeVisitor):
|
||||
iter(k for k in self.assignments if getattr(k, "id", "") == match.id), None
|
||||
)
|
||||
|
||||
def parse_function_names(self, should_retry=True, function_map=None):
|
||||
if function_map is None:
|
||||
function_map = {}
|
||||
retries = []
|
||||
for k, v in function_map.items():
|
||||
fn_name = ""
|
||||
if k in self.function_names:
|
||||
fn_name = self.function_names[k]
|
||||
elif isinstance(k, ast.Name):
|
||||
fn_name = k.id
|
||||
elif isinstance(k, ast.Attribute):
|
||||
try:
|
||||
fn = ast_unparse(k, analyzer=self)
|
||||
except Exception:
|
||||
if should_retry:
|
||||
retries.append((k, v))
|
||||
continue
|
||||
else:
|
||||
if isinstance(fn, six.string_types):
|
||||
_, _, fn_name = fn.rpartition(".")
|
||||
if fn_name:
|
||||
self.resolved_function_names[fn_name] = ast_unparse(v, analyzer=self)
|
||||
return retries
|
||||
|
||||
def parse_functions(self):
|
||||
retries = self.parse_function_names(function_map=self.function_map)
|
||||
if retries:
|
||||
failures = self.parse_function_names(
|
||||
should_retry=False, function_map=dict(retries)
|
||||
)
|
||||
return self.resolved_function_names
|
||||
|
||||
|
||||
def ast_unparse(item, initial_mapping=False, analyzer=None, recurse=True): # noqa:C901
|
||||
# type: (Any, bool, Optional[Analyzer], bool) -> Union[List[Any], Dict[Any, Any], Tuple[Any, ...], STRING_TYPE]
|
||||
@@ -746,6 +780,13 @@ def ast_unparse(item, initial_mapping=False, analyzer=None, recurse=True): # no
|
||||
unparsed = item.s
|
||||
elif isinstance(item, ast.Subscript):
|
||||
unparsed = unparse(item.value)
|
||||
if not initial_mapping:
|
||||
if isinstance(item.slice, ast.Index):
|
||||
try:
|
||||
unparsed = unparsed[unparse(item.slice.value)]
|
||||
except KeyError:
|
||||
# not everything can be looked up before runtime
|
||||
unparsed = item
|
||||
elif any(isinstance(item, k) for k in AST_BINOP_MAP.keys()):
|
||||
unparsed = AST_BINOP_MAP[type(item)]
|
||||
elif isinstance(item, ast.Num):
|
||||
@@ -789,7 +830,7 @@ def ast_unparse(item, initial_mapping=False, analyzer=None, recurse=True): # no
|
||||
elif isinstance(item, constant):
|
||||
unparsed = item.value
|
||||
elif isinstance(item, ast.Compare):
|
||||
if isinstance(item.left, ast.Attribute):
|
||||
if isinstance(item.left, ast.Attribute) or isinstance(item.left, ast.Str):
|
||||
import importlib
|
||||
|
||||
left = unparse(item.left)
|
||||
@@ -934,18 +975,10 @@ def ast_parse_setup_py(path):
|
||||
ast_analyzer = ast_parse_file(path)
|
||||
setup = {} # type: Dict[Any, Any]
|
||||
ast_analyzer.unmap_binops()
|
||||
for k, v in ast_analyzer.function_map.items():
|
||||
fn_name = ""
|
||||
if isinstance(k, ast.Name):
|
||||
fn_name = k.id
|
||||
elif isinstance(k, ast.Attribute):
|
||||
fn = ast_unparse(k)
|
||||
if isinstance(fn, six.string_types):
|
||||
_, _, fn_name = fn.rpartition(".")
|
||||
if fn_name == "setup":
|
||||
setup = v
|
||||
cleaned_setup = ast_unparse(setup, analyzer=ast_analyzer)
|
||||
return cleaned_setup
|
||||
function_names = ast_analyzer.parse_functions()
|
||||
if "setup" in function_names:
|
||||
setup = ast_unparse(function_names["setup"], analyzer=ast_analyzer)
|
||||
return setup
|
||||
|
||||
|
||||
def run_setup(script_path, egg_base=None):
|
||||
@@ -1557,7 +1590,7 @@ build-backend = "{1}"
|
||||
from .dependencies import get_finder
|
||||
|
||||
session, finder = get_finder()
|
||||
_, uri = split_vcs_method_from_uri(unquote(ireq.link.url_without_fragment))
|
||||
vcs, uri = split_vcs_method_from_uri(unquote(ireq.link.url_without_fragment))
|
||||
parsed = urlparse(uri)
|
||||
if "file" in parsed.scheme:
|
||||
url_path = parsed.path
|
||||
@@ -1571,14 +1604,10 @@ build-backend = "{1}"
|
||||
uri = uri.replace("file:/", "file:///")
|
||||
path = pip_shims.shims.url_to_path(uri)
|
||||
kwargs = _prepare_wheel_building_kwargs(ireq)
|
||||
ireq.source_dir = kwargs["src_dir"]
|
||||
try:
|
||||
is_vcs = ireq.link.is_vcs
|
||||
except AttributeError:
|
||||
try:
|
||||
is_vcs = not ireq.link.is_artifact
|
||||
except AttributeError:
|
||||
is_vcs = False
|
||||
is_artifact_or_vcs = getattr(
|
||||
ireq.link, "is_vcs", getattr(ireq.link, "is_artifact", False)
|
||||
)
|
||||
is_vcs = True if vcs else is_artifact_or_vcs
|
||||
if not (ireq.editable and pip_shims.shims.is_file_url(ireq.link) and is_vcs):
|
||||
if ireq.is_wheel:
|
||||
only_download = True
|
||||
@@ -1599,7 +1628,6 @@ build-backend = "{1}"
|
||||
ireq.ensure_has_source_dir(kwargs["src_dir"])
|
||||
src_dir = ireq.source_dir
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
ireq.populate_link(finder, False, False)
|
||||
pip_shims.shims.shim_unpack(
|
||||
link=ireq.link,
|
||||
location=kwargs["src_dir"],
|
||||
@@ -1609,7 +1637,9 @@ build-backend = "{1}"
|
||||
hashes=ireq.hashes(False),
|
||||
progress_bar="off",
|
||||
)
|
||||
created = cls.create(src_dir, subdirectory=subdir, ireq=ireq, kwargs=kwargs)
|
||||
created = cls.create(
|
||||
kwargs["src_dir"], subdirectory=subdir, ireq=ireq, kwargs=kwargs
|
||||
)
|
||||
return created
|
||||
|
||||
@classmethod
|
||||
|
||||
+1
-1
@@ -723,7 +723,7 @@ def get_pinned_version(ireq):
|
||||
except AttributeError:
|
||||
raise TypeError("Expected InstallRequirement, not {}".format(type(ireq).__name__))
|
||||
|
||||
if ireq.editable:
|
||||
if getattr(ireq, "editable", False):
|
||||
raise ValueError("InstallRequirement is editable")
|
||||
if not specifier:
|
||||
raise ValueError("InstallRequirement has no version specification")
|
||||
|
||||
Vendored
+18
-8
@@ -1,16 +1,26 @@
|
||||
__all__ = [
|
||||
'__version__',
|
||||
'AbstractProvider', 'BaseReporter', 'Resolver',
|
||||
'NoVersionsAvailable', 'RequirementsConflicted',
|
||||
'ResolutionError', 'ResolutionImpossible', 'ResolutionTooDeep',
|
||||
"__version__",
|
||||
"AbstractProvider",
|
||||
"AbstractResolver",
|
||||
"BaseReporter",
|
||||
"InconsistentCandidate",
|
||||
"Resolver",
|
||||
"RequirementsConflicted",
|
||||
"ResolutionError",
|
||||
"ResolutionImpossible",
|
||||
"ResolutionTooDeep",
|
||||
]
|
||||
|
||||
__version__ = '0.2.2'
|
||||
__version__ = "0.3.0"
|
||||
|
||||
|
||||
from .providers import AbstractProvider
|
||||
from .providers import AbstractProvider, AbstractResolver
|
||||
from .reporters import BaseReporter
|
||||
from .resolvers import (
|
||||
NoVersionsAvailable, RequirementsConflicted,
|
||||
Resolver, ResolutionError, ResolutionImpossible, ResolutionTooDeep,
|
||||
InconsistentCandidate,
|
||||
RequirementsConflicted,
|
||||
Resolver,
|
||||
ResolutionError,
|
||||
ResolutionImpossible,
|
||||
ResolutionTooDeep,
|
||||
)
|
||||
|
||||
+47
-4
@@ -1,6 +1,7 @@
|
||||
class AbstractProvider(object):
|
||||
"""Delegate class to provide requirment interface for the resolver.
|
||||
"""Delegate class to provide requirement interface for the resolver.
|
||||
"""
|
||||
|
||||
def identify(self, dependency):
|
||||
"""Given a dependency, return an identifier for it.
|
||||
|
||||
@@ -56,15 +57,15 @@ class AbstractProvider(object):
|
||||
consulted to find concrete candidates for this requirement.
|
||||
|
||||
The returned candidates should be sorted by reversed preference, e.g.
|
||||
the latest should be LAST. This is done so list-popping can be as
|
||||
efficient as possible.
|
||||
the most preferred should be LAST. This is done so list-popping can be
|
||||
as efficient as possible.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def is_satisfied_by(self, requirement, candidate):
|
||||
"""Whether the given requirement can be satisfied by a candidate.
|
||||
|
||||
A boolean should be retuened to indicate whether `candidate` is a
|
||||
A boolean should be returned to indicate whether `candidate` is a
|
||||
viable solution to the requirement.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
@@ -76,3 +77,45 @@ class AbstractProvider(object):
|
||||
specifies as its dependencies.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class AbstractResolver(object):
|
||||
"""The thing that performs the actual resolution work.
|
||||
"""
|
||||
|
||||
base_exception = Exception
|
||||
|
||||
def __init__(self, provider, reporter):
|
||||
self.provider = provider
|
||||
self.reporter = reporter
|
||||
|
||||
def resolve(self, requirements, **kwargs):
|
||||
"""Take a collection of constraints, spit out the resolution result.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
requirements : Collection
|
||||
A collection of constraints
|
||||
kwargs : optional
|
||||
Additional keyword arguments that subclasses may accept.
|
||||
|
||||
Raises
|
||||
------
|
||||
self.base_exception
|
||||
Any raised exception is guaranteed to be a subclass of
|
||||
self.base_exception. The string representation of an exception
|
||||
should be human readable and provide context for why it occurred.
|
||||
|
||||
Returns
|
||||
-------
|
||||
retval : object
|
||||
A representation of the final resolution state. It can be any object
|
||||
with a `mapping` attribute that is a Mapping. Other attributes can
|
||||
be used to provide resolver-specific information.
|
||||
|
||||
The `mapping` attribute MUST be key-value pair is an identifier of a
|
||||
requirement (as returned by the provider's `identify` method) mapped
|
||||
to the resolved candidate (chosen from the return value of the
|
||||
provider's `find_matches` method).
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
+13
@@ -1,6 +1,7 @@
|
||||
class BaseReporter(object):
|
||||
"""Delegate class to provider progress reporting for the resolver.
|
||||
"""
|
||||
|
||||
def starting(self):
|
||||
"""Called before the resolution actually starts.
|
||||
"""
|
||||
@@ -21,3 +22,15 @@ class BaseReporter(object):
|
||||
def ending(self, state):
|
||||
"""Called before the resolution ends successfully.
|
||||
"""
|
||||
|
||||
def adding_requirement(self, requirement):
|
||||
"""Called when the resolver adds a new requirement into the resolve criteria.
|
||||
"""
|
||||
|
||||
def backtracking(self, candidate):
|
||||
"""Called when the resolver rejects a candidate during backtracking.
|
||||
"""
|
||||
|
||||
def pinning(self, candidate):
|
||||
"""Called when adding a candidate to the potential solution.
|
||||
"""
|
||||
|
||||
+248
-121
@@ -1,52 +1,91 @@
|
||||
import collections
|
||||
|
||||
from .providers import AbstractResolver
|
||||
from .structs import DirectedGraph
|
||||
|
||||
|
||||
RequirementInformation = collections.namedtuple('RequirementInformation', [
|
||||
'requirement', 'parent',
|
||||
])
|
||||
RequirementInformation = collections.namedtuple(
|
||||
"RequirementInformation", ["requirement", "parent"]
|
||||
)
|
||||
|
||||
|
||||
class NoVersionsAvailable(Exception):
|
||||
def __init__(self, requirement, parent):
|
||||
super(NoVersionsAvailable, self).__init__()
|
||||
self.requirement = requirement
|
||||
self.parent = parent
|
||||
class ResolverException(Exception):
|
||||
"""A base class for all exceptions raised by this module.
|
||||
|
||||
Exceptions derived by this class should all be handled in this module. Any
|
||||
bubbling pass the resolver should be treated as a bug.
|
||||
"""
|
||||
|
||||
|
||||
class RequirementsConflicted(Exception):
|
||||
class RequirementsConflicted(ResolverException):
|
||||
def __init__(self, criterion):
|
||||
super(RequirementsConflicted, self).__init__()
|
||||
super(RequirementsConflicted, self).__init__(criterion)
|
||||
self.criterion = criterion
|
||||
|
||||
def __str__(self):
|
||||
return "Requirements conflict: {}".format(
|
||||
", ".join(repr(r) for r in self.criterion.iter_requirement()),
|
||||
)
|
||||
|
||||
|
||||
class InconsistentCandidate(ResolverException):
|
||||
def __init__(self, candidate, criterion):
|
||||
super(InconsistentCandidate, self).__init__(candidate, criterion)
|
||||
self.candidate = candidate
|
||||
self.criterion = criterion
|
||||
|
||||
def __str__(self):
|
||||
return "Provided candidate {!r} does not satisfy {}".format(
|
||||
self.candidate,
|
||||
", ".join(repr(r) for r in self.criterion.iter_requirement()),
|
||||
)
|
||||
|
||||
|
||||
class Criterion(object):
|
||||
"""Internal representation of possible resolution results of a package.
|
||||
"""Representation of possible resolution results of a package.
|
||||
|
||||
This holds two attributes:
|
||||
This holds three attributes:
|
||||
|
||||
* `information` is a collection of `RequirementInformation` pairs. Each
|
||||
pair is a requirement contributing to this criterion, and the candidate
|
||||
that provides the requirement.
|
||||
* `information` is a collection of `RequirementInformation` pairs.
|
||||
Each pair is a requirement contributing to this criterion, and the
|
||||
candidate that provides the requirement.
|
||||
* `incompatibilities` is a collection of all known not-to-work candidates
|
||||
to exclude from consideration.
|
||||
* `candidates` is a collection containing all possible candidates deducted
|
||||
from the union of contributing requirements. It should never be empty.
|
||||
from the union of contributing requirements and known incompatibilities.
|
||||
It should never be empty, except when the criterion is an attribute of a
|
||||
raised `RequirementsConflicted` (in which case it is always empty).
|
||||
|
||||
.. note::
|
||||
This class is intended to be externally immutable. **Do not** mutate
|
||||
any of its attribute containers.
|
||||
"""
|
||||
def __init__(self, candidates, information):
|
||||
|
||||
def __init__(self, candidates, information, incompatibilities):
|
||||
self.candidates = candidates
|
||||
self.information = information
|
||||
self.incompatibilities = incompatibilities
|
||||
|
||||
def __repr__(self):
|
||||
requirements = ", ".join(
|
||||
"{!r} from {!r}".format(req, parent)
|
||||
for req, parent in self.information
|
||||
)
|
||||
return "<Criterion {}>".format(requirements)
|
||||
|
||||
@classmethod
|
||||
def from_requirement(cls, provider, requirement, parent):
|
||||
"""Build an instance from a requirement.
|
||||
"""
|
||||
candidates = provider.find_matches(requirement)
|
||||
if not candidates:
|
||||
raise NoVersionsAvailable(requirement, parent)
|
||||
return cls(
|
||||
criterion = cls(
|
||||
candidates=candidates,
|
||||
information=[RequirementInformation(requirement, parent)],
|
||||
incompatibilities=[],
|
||||
)
|
||||
if not candidates:
|
||||
raise RequirementsConflicted(criterion)
|
||||
return criterion
|
||||
|
||||
def iter_requirement(self):
|
||||
return (i.requirement for i in self.information)
|
||||
@@ -60,22 +99,38 @@ class Criterion(object):
|
||||
infos = list(self.information)
|
||||
infos.append(RequirementInformation(requirement, parent))
|
||||
candidates = [
|
||||
c for c in self.candidates
|
||||
c
|
||||
for c in self.candidates
|
||||
if provider.is_satisfied_by(requirement, c)
|
||||
]
|
||||
criterion = type(self)(candidates, infos, list(self.incompatibilities))
|
||||
if not candidates:
|
||||
raise RequirementsConflicted(self)
|
||||
return type(self)(candidates, infos)
|
||||
raise RequirementsConflicted(criterion)
|
||||
return criterion
|
||||
|
||||
def excluded_of(self, candidate):
|
||||
"""Build a new instance from this, but excluding specified candidate.
|
||||
|
||||
Returns the new instance, or None if we still have no valid candidates.
|
||||
"""
|
||||
incompats = list(self.incompatibilities)
|
||||
incompats.append(candidate)
|
||||
candidates = [c for c in self.candidates if c != candidate]
|
||||
if not candidates:
|
||||
return None
|
||||
criterion = type(self)(candidates, list(self.information), incompats)
|
||||
return criterion
|
||||
|
||||
|
||||
class ResolutionError(Exception):
|
||||
class ResolutionError(ResolverException):
|
||||
pass
|
||||
|
||||
|
||||
class ResolutionImpossible(ResolutionError):
|
||||
def __init__(self, requirements):
|
||||
super(ResolutionImpossible, self).__init__()
|
||||
self.requirements = requirements
|
||||
def __init__(self, causes):
|
||||
super(ResolutionImpossible, self).__init__(causes)
|
||||
# causes is a list of RequirementInformation objects
|
||||
self.causes = causes
|
||||
|
||||
|
||||
class ResolutionTooDeep(ResolutionError):
|
||||
@@ -85,7 +140,7 @@ class ResolutionTooDeep(ResolutionError):
|
||||
|
||||
|
||||
# Resolution state in a round.
|
||||
State = collections.namedtuple('State', 'mapping graph')
|
||||
State = collections.namedtuple("State", "mapping criteria")
|
||||
|
||||
|
||||
class Resolution(object):
|
||||
@@ -94,10 +149,10 @@ class Resolution(object):
|
||||
This is designed as a one-off object that holds information to kick start
|
||||
the resolution process, and holds the results afterwards.
|
||||
"""
|
||||
|
||||
def __init__(self, provider, reporter):
|
||||
self._p = provider
|
||||
self._r = reporter
|
||||
self._criteria = {}
|
||||
self._states = []
|
||||
|
||||
@property
|
||||
@@ -105,7 +160,7 @@ class Resolution(object):
|
||||
try:
|
||||
return self._states[-1]
|
||||
except IndexError:
|
||||
raise AttributeError('state')
|
||||
raise AttributeError("state")
|
||||
|
||||
def _push_new_state(self):
|
||||
"""Push a new state into history.
|
||||
@@ -116,30 +171,29 @@ class Resolution(object):
|
||||
try:
|
||||
base = self._states[-1]
|
||||
except IndexError:
|
||||
graph = DirectedGraph()
|
||||
graph.add(None) # Sentinel as root dependencies' parent.
|
||||
state = State(mapping={}, graph=graph)
|
||||
state = State(mapping=collections.OrderedDict(), criteria={})
|
||||
else:
|
||||
state = State(
|
||||
mapping=base.mapping.copy(),
|
||||
graph=base.graph.copy(),
|
||||
mapping=base.mapping.copy(), criteria=base.criteria.copy(),
|
||||
)
|
||||
self._states.append(state)
|
||||
|
||||
def _contribute_to_criteria(self, name, requirement, parent):
|
||||
def _merge_into_criterion(self, requirement, parent):
|
||||
self._r.adding_requirement(requirement)
|
||||
name = self._p.identify(requirement)
|
||||
try:
|
||||
crit = self._criteria[name]
|
||||
crit = self.state.criteria[name]
|
||||
except KeyError:
|
||||
crit = Criterion.from_requirement(self._p, requirement, parent)
|
||||
else:
|
||||
crit = crit.merged_with(self._p, requirement, parent)
|
||||
self._criteria[name] = crit
|
||||
return name, crit
|
||||
|
||||
def _get_criterion_item_preference(self, item):
|
||||
name, criterion = item
|
||||
try:
|
||||
pinned = self.state.mapping[name]
|
||||
except (IndexError, KeyError):
|
||||
except KeyError:
|
||||
pinned = None
|
||||
return self._p.get_preference(
|
||||
pinned, criterion.candidates, criterion.information,
|
||||
@@ -155,114 +209,183 @@ class Resolution(object):
|
||||
for r in criterion.iter_requirement()
|
||||
)
|
||||
|
||||
def _check_pinnability(self, candidate, dependencies):
|
||||
backup = self._criteria.copy()
|
||||
contributed = set()
|
||||
try:
|
||||
for subdep in dependencies:
|
||||
key = self._p.identify(subdep)
|
||||
self._contribute_to_criteria(key, subdep, parent=candidate)
|
||||
contributed.add(key)
|
||||
except RequirementsConflicted:
|
||||
self._criteria = backup
|
||||
return None
|
||||
return contributed
|
||||
def _get_criteria_to_update(self, candidate):
|
||||
criteria = {}
|
||||
for r in self._p.get_dependencies(candidate):
|
||||
name, crit = self._merge_into_criterion(r, parent=candidate)
|
||||
criteria[name] = crit
|
||||
return criteria
|
||||
|
||||
def _pin_candidate(self, name, criterion, candidate, child_names):
|
||||
try:
|
||||
self.state.graph.remove(name)
|
||||
except KeyError:
|
||||
pass
|
||||
self.state.mapping[name] = candidate
|
||||
self.state.graph.add(name)
|
||||
for parent in criterion.iter_parent():
|
||||
parent_name = None if parent is None else self._p.identify(parent)
|
||||
def _attempt_to_pin_criterion(self, name, criterion):
|
||||
causes = []
|
||||
for candidate in reversed(criterion.candidates):
|
||||
try:
|
||||
self.state.graph.connect(parent_name, name)
|
||||
except KeyError:
|
||||
# Parent is not yet pinned. Skip now; this edge will be
|
||||
# connected when the parent is being pinned.
|
||||
pass
|
||||
for child_name in child_names:
|
||||
try:
|
||||
self.state.graph.connect(name, child_name)
|
||||
except KeyError:
|
||||
# Child is not yet pinned. Skip now; this edge will be
|
||||
# connected when the child is being pinned.
|
||||
pass
|
||||
|
||||
def _pin_criteria(self):
|
||||
criterion_names = [name for name, _ in sorted(
|
||||
self._criteria.items(),
|
||||
key=self._get_criterion_item_preference,
|
||||
)]
|
||||
for name in criterion_names:
|
||||
# Any pin may modify any criterion during the loop. Criteria are
|
||||
# replaced, not updated in-place, so we need to read this value
|
||||
# in the loop instead of outside. (sarugaku/resolvelib#5)
|
||||
criterion = self._criteria[name]
|
||||
|
||||
if self._is_current_pin_satisfying(name, criterion):
|
||||
# If the current pin already works, just use it.
|
||||
criteria = self._get_criteria_to_update(candidate)
|
||||
except RequirementsConflicted as e:
|
||||
causes.append(e.criterion)
|
||||
continue
|
||||
candidates = list(criterion.candidates)
|
||||
while candidates:
|
||||
candidate = candidates.pop()
|
||||
dependencies = self._p.get_dependencies(candidate)
|
||||
child_names = self._check_pinnability(candidate, dependencies)
|
||||
if child_names is None:
|
||||
continue
|
||||
self._pin_candidate(name, criterion, candidate, child_names)
|
||||
break
|
||||
else: # All candidates tried, nothing works. Give up. (?)
|
||||
raise ResolutionImpossible(list(criterion.iter_requirement()))
|
||||
|
||||
# Put newly-pinned candidate at the end. This is essential because
|
||||
# backtracking looks at this mapping to get the last pin.
|
||||
self._r.pinning(candidate)
|
||||
self.state.mapping.pop(name, None)
|
||||
self.state.mapping[name] = candidate
|
||||
self.state.criteria.update(criteria)
|
||||
|
||||
# Check the newly-pinned candidate actually works. This should
|
||||
# always pass under normal circumstances, but in the case of a
|
||||
# faulty provider, we will raise an error to notify the implementer
|
||||
# to fix find_matches() and/or is_satisfied_by().
|
||||
if not self._is_current_pin_satisfying(name, criterion):
|
||||
raise InconsistentCandidate(candidate, criterion)
|
||||
|
||||
return []
|
||||
|
||||
# All candidates tried, nothing works. This criterion is a dead
|
||||
# end, signal for backtracking.
|
||||
return causes
|
||||
|
||||
def _backtrack(self):
|
||||
# We need at least 3 states here:
|
||||
# (a) One known not working, to drop.
|
||||
# (b) One to backtrack to.
|
||||
# (c) One to restore state (b) to its state prior to candidate-pinning,
|
||||
# so we can pin another one instead.
|
||||
while len(self._states) >= 3:
|
||||
del self._states[-1]
|
||||
|
||||
# Retract the last candidate pin, and create a new (b).
|
||||
name, candidate = self._states.pop().mapping.popitem()
|
||||
self._r.backtracking(candidate)
|
||||
self._push_new_state()
|
||||
|
||||
# Mark the retracted candidate as incompatible.
|
||||
criterion = self.state.criteria[name].excluded_of(candidate)
|
||||
if criterion is None:
|
||||
# This state still does not work. Try the still previous state.
|
||||
continue
|
||||
self.state.criteria[name] = criterion
|
||||
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def resolve(self, requirements, max_rounds):
|
||||
if self._states:
|
||||
raise RuntimeError('already resolved')
|
||||
raise RuntimeError("already resolved")
|
||||
|
||||
for requirement in requirements:
|
||||
self._push_new_state()
|
||||
for r in requirements:
|
||||
try:
|
||||
name = self._p.identify(requirement)
|
||||
self._contribute_to_criteria(name, requirement, parent=None)
|
||||
name, crit = self._merge_into_criterion(r, parent=None)
|
||||
except RequirementsConflicted as e:
|
||||
# If initial requirements conflict, nothing would ever work.
|
||||
raise ResolutionImpossible(e.requirements + [requirement])
|
||||
raise ResolutionImpossible(e.criterion.information)
|
||||
self.state.criteria[name] = crit
|
||||
|
||||
last = None
|
||||
self._r.starting()
|
||||
|
||||
for round_index in range(max_rounds):
|
||||
self._r.starting_round(round_index)
|
||||
|
||||
self._push_new_state()
|
||||
self._pin_criteria()
|
||||
|
||||
curr = self.state
|
||||
if last is not None and len(curr.mapping) == len(last.mapping):
|
||||
# Nothing new added. Done! Remove the duplicated entry.
|
||||
|
||||
unsatisfied_criterion_items = [
|
||||
item
|
||||
for item in self.state.criteria.items()
|
||||
if not self._is_current_pin_satisfying(*item)
|
||||
]
|
||||
|
||||
# All criteria are accounted for. Nothing more to pin, we are done!
|
||||
if not unsatisfied_criterion_items:
|
||||
del self._states[-1]
|
||||
self._r.ending(last)
|
||||
return
|
||||
last = curr
|
||||
self._r.ending(curr)
|
||||
return self.state
|
||||
|
||||
# Choose the most preferred unpinned criterion to try.
|
||||
name, criterion = min(
|
||||
unsatisfied_criterion_items,
|
||||
key=self._get_criterion_item_preference,
|
||||
)
|
||||
failure_causes = self._attempt_to_pin_criterion(name, criterion)
|
||||
|
||||
# Backtrack if pinning fails.
|
||||
if failure_causes:
|
||||
result = self._backtrack()
|
||||
if not result:
|
||||
causes = [
|
||||
i for crit in failure_causes for i in crit.information
|
||||
]
|
||||
raise ResolutionImpossible(causes)
|
||||
|
||||
self._r.ending_round(round_index, curr)
|
||||
|
||||
raise ResolutionTooDeep(max_rounds)
|
||||
|
||||
|
||||
class Resolver(object):
|
||||
def _has_route_to_root(criteria, key, all_keys, connected):
|
||||
if key in connected:
|
||||
return True
|
||||
if key not in criteria:
|
||||
return False
|
||||
for p in criteria[key].iter_parent():
|
||||
try:
|
||||
pkey = all_keys[id(p)]
|
||||
except KeyError:
|
||||
continue
|
||||
if pkey in connected:
|
||||
connected.add(key)
|
||||
return True
|
||||
if _has_route_to_root(criteria, pkey, all_keys, connected):
|
||||
connected.add(key)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Result = collections.namedtuple("Result", "mapping graph criteria")
|
||||
|
||||
|
||||
def _build_result(state):
|
||||
mapping = state.mapping
|
||||
all_keys = {id(v): k for k, v in mapping.items()}
|
||||
all_keys[id(None)] = None
|
||||
|
||||
graph = DirectedGraph()
|
||||
graph.add(None) # Sentinel as root dependencies' parent.
|
||||
|
||||
connected = {None}
|
||||
for key, criterion in state.criteria.items():
|
||||
if not _has_route_to_root(state.criteria, key, all_keys, connected):
|
||||
continue
|
||||
if key not in graph:
|
||||
graph.add(key)
|
||||
for p in criterion.iter_parent():
|
||||
try:
|
||||
pkey = all_keys[id(p)]
|
||||
except KeyError:
|
||||
continue
|
||||
if pkey not in graph:
|
||||
graph.add(pkey)
|
||||
graph.connect(pkey, key)
|
||||
|
||||
return Result(
|
||||
mapping={k: v for k, v in mapping.items() if k in connected},
|
||||
graph=graph,
|
||||
criteria=state.criteria,
|
||||
)
|
||||
|
||||
|
||||
class Resolver(AbstractResolver):
|
||||
"""The thing that performs the actual resolution work.
|
||||
"""
|
||||
def __init__(self, provider, reporter):
|
||||
self.provider = provider
|
||||
self.reporter = reporter
|
||||
|
||||
def resolve(self, requirements, max_rounds=20):
|
||||
base_exception = ResolverException
|
||||
|
||||
def resolve(self, requirements, max_rounds=100):
|
||||
"""Take a collection of constraints, spit out the resolution result.
|
||||
|
||||
The return value is a representation to the final resolution result. It
|
||||
is a tuple subclass with two public members:
|
||||
is a tuple subclass with three public members:
|
||||
|
||||
* `mapping`: A dict of resolved candidates. Each key is an identifier
|
||||
of a requirement (as returned by the provider's `identify` method),
|
||||
@@ -271,17 +394,21 @@ class Resolver(object):
|
||||
The vertices are keys of `mapping`, and each edge represents *why*
|
||||
a particular package is included. A special vertex `None` is
|
||||
included to represent parents of user-supplied requirements.
|
||||
* `criteria`: A dict of "criteria" that hold detailed information on
|
||||
how edges in the graph are derived. Each key is an identifier of a
|
||||
requirement, and the value is a `Criterion` instance.
|
||||
|
||||
The following exceptions may be raised if a resolution cannot be found:
|
||||
|
||||
* `NoVersionsAvailable`: A requirement has no available candidates.
|
||||
* `ResolutionImpossible`: A resolution cannot be found for the given
|
||||
combination of requirements.
|
||||
combination of requirements. The `causes` attribute of the
|
||||
exception is a list of (requirement, parent), giving the
|
||||
requirements that could not be satisfied.
|
||||
* `ResolutionTooDeep`: The dependency tree is too deeply nested and
|
||||
the resolver gave up. This is usually caused by a circular
|
||||
dependency, but you can try to resolve this by increasing the
|
||||
`max_rounds` argument.
|
||||
"""
|
||||
resolution = Resolution(self.provider, self.reporter)
|
||||
resolution.resolve(requirements, max_rounds=max_rounds)
|
||||
return resolution.state
|
||||
state = resolution.resolve(requirements, max_rounds=max_rounds)
|
||||
return _build_result(state)
|
||||
|
||||
Vendored
+4
-3
@@ -1,10 +1,11 @@
|
||||
class DirectedGraph(object):
|
||||
"""A graph structure with directed edges.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._vertices = set()
|
||||
self._forwards = {} # <key> -> Set[<key>]
|
||||
self._backwards = {} # <key> -> Set[<key>]
|
||||
self._forwards = {} # <key> -> Set[<key>]
|
||||
self._backwards = {} # <key> -> Set[<key>]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._vertices)
|
||||
@@ -28,7 +29,7 @@ class DirectedGraph(object):
|
||||
"""Add a new vertex to the graph.
|
||||
"""
|
||||
if key in self._vertices:
|
||||
raise ValueError('vertex exists')
|
||||
raise ValueError("vertex exists")
|
||||
self._vertices.add(key)
|
||||
self._forwards[key] = set()
|
||||
self._backwards[key] = set()
|
||||
|
||||
Vendored
+1
-1
@@ -22,7 +22,7 @@ from logging import NullHandler
|
||||
|
||||
__author__ = "Andrey Petrov (andrey.petrov@shazow.net)"
|
||||
__license__ = "MIT"
|
||||
__version__ = "1.25.8"
|
||||
__version__ = "1.25.9"
|
||||
|
||||
__all__ = (
|
||||
"HTTPConnectionPool",
|
||||
|
||||
Vendored
+28
-19
@@ -1,4 +1,5 @@
|
||||
from __future__ import absolute_import
|
||||
import re
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
@@ -58,6 +59,8 @@ port_by_scheme = {"http": 80, "https": 443}
|
||||
# (ie test_recent_date is failing) update it to ~6 months before the current date.
|
||||
RECENT_DATE = datetime.date(2019, 1, 1)
|
||||
|
||||
_CONTAINS_CONTROL_CHAR_RE = re.compile(r"[^-!#$%&'*+.^_`|~0-9a-zA-Z]")
|
||||
|
||||
|
||||
class DummyConnection(object):
|
||||
"""Used to detect a failed ConnectionCls import."""
|
||||
@@ -184,6 +187,17 @@ class HTTPConnection(_HTTPConnection, object):
|
||||
conn = self._new_conn()
|
||||
self._prepare_conn(conn)
|
||||
|
||||
def putrequest(self, method, url, *args, **kwargs):
|
||||
"""Send a request to the server"""
|
||||
match = _CONTAINS_CONTROL_CHAR_RE.search(method)
|
||||
if match:
|
||||
raise ValueError(
|
||||
"Method cannot contain non-token characters %r (found at least %r)"
|
||||
% (method, match.group())
|
||||
)
|
||||
|
||||
return _HTTPConnection.putrequest(self, method, url, *args, **kwargs)
|
||||
|
||||
def request_chunked(self, method, url, body=None, headers=None):
|
||||
"""
|
||||
Alternative to the common request method, which sends the
|
||||
@@ -223,7 +237,12 @@ class HTTPConnection(_HTTPConnection, object):
|
||||
class HTTPSConnection(HTTPConnection):
|
||||
default_port = port_by_scheme["https"]
|
||||
|
||||
cert_reqs = None
|
||||
ca_certs = None
|
||||
ca_cert_dir = None
|
||||
ca_cert_data = None
|
||||
ssl_version = None
|
||||
assert_fingerprint = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -251,19 +270,6 @@ class HTTPSConnection(HTTPConnection):
|
||||
# HTTPS requests to go out as HTTP. (See Issue #356)
|
||||
self._protocol = "https"
|
||||
|
||||
|
||||
class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
"""
|
||||
Based on httplib.HTTPSConnection but wraps the socket with
|
||||
SSL certification.
|
||||
"""
|
||||
|
||||
cert_reqs = None
|
||||
ca_certs = None
|
||||
ca_cert_dir = None
|
||||
ssl_version = None
|
||||
assert_fingerprint = None
|
||||
|
||||
def set_cert(
|
||||
self,
|
||||
key_file=None,
|
||||
@@ -274,6 +280,7 @@ class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
assert_hostname=None,
|
||||
assert_fingerprint=None,
|
||||
ca_cert_dir=None,
|
||||
ca_cert_data=None,
|
||||
):
|
||||
"""
|
||||
This method should only be called once, before the connection is used.
|
||||
@@ -294,6 +301,7 @@ class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
self.assert_fingerprint = assert_fingerprint
|
||||
self.ca_certs = ca_certs and os.path.expanduser(ca_certs)
|
||||
self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir)
|
||||
self.ca_cert_data = ca_cert_data
|
||||
|
||||
def connect(self):
|
||||
# Add certificate verification
|
||||
@@ -344,6 +352,7 @@ class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
if (
|
||||
not self.ca_certs
|
||||
and not self.ca_cert_dir
|
||||
and not self.ca_cert_data
|
||||
and default_ssl_context
|
||||
and hasattr(context, "load_default_certs")
|
||||
):
|
||||
@@ -356,6 +365,7 @@ class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
key_password=self.key_password,
|
||||
ca_certs=self.ca_certs,
|
||||
ca_cert_dir=self.ca_cert_dir,
|
||||
ca_cert_data=self.ca_cert_data,
|
||||
server_hostname=server_hostname,
|
||||
ssl_context=context,
|
||||
)
|
||||
@@ -406,9 +416,8 @@ def _match_hostname(cert, asserted_hostname):
|
||||
raise
|
||||
|
||||
|
||||
if ssl:
|
||||
# Make a copy for testing.
|
||||
UnverifiedHTTPSConnection = HTTPSConnection
|
||||
HTTPSConnection = VerifiedHTTPSConnection
|
||||
else:
|
||||
HTTPSConnection = DummyConnection
|
||||
if not ssl:
|
||||
HTTPSConnection = DummyConnection # noqa: F811
|
||||
|
||||
|
||||
VerifiedHTTPSConnection = HTTPSConnection
|
||||
|
||||
+9
-27
@@ -65,6 +65,11 @@ class ConnectionPool(object):
|
||||
"""
|
||||
Base class for all connection pools, such as
|
||||
:class:`.HTTPConnectionPool` and :class:`.HTTPSConnectionPool`.
|
||||
|
||||
.. note::
|
||||
ConnectionPool.urlopen() does not normalize or percent-encode target URIs
|
||||
which is useful if your target server doesn't support percent-encoded
|
||||
target URIs.
|
||||
"""
|
||||
|
||||
scheme = None
|
||||
@@ -760,21 +765,6 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
|
||||
**response_kw
|
||||
)
|
||||
|
||||
def drain_and_release_conn(response):
|
||||
try:
|
||||
# discard any remaining response body, the connection will be
|
||||
# released back to the pool once the entire response is read
|
||||
response.read()
|
||||
except (
|
||||
TimeoutError,
|
||||
HTTPException,
|
||||
SocketError,
|
||||
ProtocolError,
|
||||
BaseSSLError,
|
||||
SSLError,
|
||||
):
|
||||
pass
|
||||
|
||||
# Handle redirect?
|
||||
redirect_location = redirect and response.get_redirect_location()
|
||||
if redirect_location:
|
||||
@@ -785,15 +775,11 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
|
||||
retries = retries.increment(method, url, response=response, _pool=self)
|
||||
except MaxRetryError:
|
||||
if retries.raise_on_redirect:
|
||||
# Drain and release the connection for this response, since
|
||||
# we're not returning it to be released manually.
|
||||
drain_and_release_conn(response)
|
||||
response.drain_conn()
|
||||
raise
|
||||
return response
|
||||
|
||||
# drain and return the connection to the pool before recursing
|
||||
drain_and_release_conn(response)
|
||||
|
||||
response.drain_conn()
|
||||
retries.sleep_for_retry(response)
|
||||
log.debug("Redirecting %s -> %s", url, redirect_location)
|
||||
return self.urlopen(
|
||||
@@ -819,15 +805,11 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
|
||||
retries = retries.increment(method, url, response=response, _pool=self)
|
||||
except MaxRetryError:
|
||||
if retries.raise_on_status:
|
||||
# Drain and release the connection for this response, since
|
||||
# we're not returning it to be released manually.
|
||||
drain_and_release_conn(response)
|
||||
response.drain_conn()
|
||||
raise
|
||||
return response
|
||||
|
||||
# drain and return the connection to the pool before recursing
|
||||
drain_and_release_conn(response)
|
||||
|
||||
response.drain_conn()
|
||||
retries.sleep(response)
|
||||
log.debug("Retry: %s", url)
|
||||
return self.urlopen(
|
||||
|
||||
+6
-3
@@ -450,9 +450,12 @@ class PyOpenSSLContext(object):
|
||||
cafile = cafile.encode("utf-8")
|
||||
if capath is not None:
|
||||
capath = capath.encode("utf-8")
|
||||
self._ctx.load_verify_locations(cafile, capath)
|
||||
if cadata is not None:
|
||||
self._ctx.load_verify_locations(BytesIO(cadata))
|
||||
try:
|
||||
self._ctx.load_verify_locations(cafile, capath)
|
||||
if cadata is not None:
|
||||
self._ctx.load_verify_locations(BytesIO(cadata))
|
||||
except OpenSSL.SSL.Error as e:
|
||||
raise ssl.SSLError("unable to load trusted certificates: %r" % e)
|
||||
|
||||
def load_cert_chain(self, certfile, keyfile=None, password=None):
|
||||
self._ctx.use_certificate_chain_file(certfile)
|
||||
|
||||
@@ -819,6 +819,11 @@ class SecureTransportContext(object):
|
||||
if capath is not None:
|
||||
raise ValueError("SecureTransport does not support cert directories")
|
||||
|
||||
# Raise if cafile does not exist.
|
||||
if cafile is not None:
|
||||
with open(cafile):
|
||||
pass
|
||||
|
||||
self._trust_bundle = cafile or cadata
|
||||
|
||||
def load_cert_chain(self, certfile, keyfile=None, password=None):
|
||||
|
||||
Vendored
+18
-1
@@ -45,7 +45,10 @@ class SSLError(HTTPError):
|
||||
|
||||
class ProxyError(HTTPError):
|
||||
"Raised when the connection to a proxy fails."
|
||||
pass
|
||||
|
||||
def __init__(self, message, error, *args):
|
||||
super(ProxyError, self).__init__(message, error, *args)
|
||||
self.original_error = error
|
||||
|
||||
|
||||
class DecodeError(HTTPError):
|
||||
@@ -195,6 +198,20 @@ class DependencyWarning(HTTPWarning):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidProxyConfigurationWarning(HTTPWarning):
|
||||
"""
|
||||
Warned when using an HTTPS proxy and an HTTPS URL. Currently
|
||||
urllib3 doesn't support HTTPS proxies and the proxy will be
|
||||
contacted via HTTP instead. This warning can be fixed by
|
||||
changing your HTTPS proxy URL into an HTTP proxy URL.
|
||||
|
||||
If you encounter this warning read this:
|
||||
https://github.com/urllib3/urllib3/issues/1850
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class ResponseNotChunked(ProtocolError, ValueError):
|
||||
"Response needs to be chunked in order to read it as chunks."
|
||||
pass
|
||||
|
||||
Vendored
+23
-1
@@ -2,11 +2,17 @@ from __future__ import absolute_import
|
||||
import collections
|
||||
import functools
|
||||
import logging
|
||||
import warnings
|
||||
|
||||
from ._collections import RecentlyUsedContainer
|
||||
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool
|
||||
from .connectionpool import port_by_scheme
|
||||
from .exceptions import LocationValueError, MaxRetryError, ProxySchemeUnknown
|
||||
from .exceptions import (
|
||||
LocationValueError,
|
||||
MaxRetryError,
|
||||
ProxySchemeUnknown,
|
||||
InvalidProxyConfigurationWarning,
|
||||
)
|
||||
from .packages import six
|
||||
from .packages.six.moves.urllib.parse import urljoin
|
||||
from .request import RequestMethods
|
||||
@@ -359,6 +365,7 @@ class PoolManager(RequestMethods):
|
||||
retries = retries.increment(method, url, response=response, _pool=conn)
|
||||
except MaxRetryError:
|
||||
if retries.raise_on_redirect:
|
||||
response.drain_conn()
|
||||
raise
|
||||
return response
|
||||
|
||||
@@ -366,6 +373,8 @@ class PoolManager(RequestMethods):
|
||||
kw["redirect"] = redirect
|
||||
|
||||
log.info("Redirecting %s -> %s", url, redirect_location)
|
||||
|
||||
response.drain_conn()
|
||||
return self.urlopen(method, redirect_location, **kw)
|
||||
|
||||
|
||||
@@ -452,9 +461,22 @@ class ProxyManager(PoolManager):
|
||||
headers_.update(headers)
|
||||
return headers_
|
||||
|
||||
def _validate_proxy_scheme_url_selection(self, url_scheme):
|
||||
if url_scheme == "https" and self.proxy.scheme == "https":
|
||||
warnings.warn(
|
||||
"Your proxy configuration specified an HTTPS scheme for the proxy. "
|
||||
"Are you sure you want to use HTTPS to contact the proxy? "
|
||||
"This most likely indicates an error in your configuration. "
|
||||
"Read this issue for more info: "
|
||||
"https://github.com/urllib3/urllib3/issues/1850",
|
||||
InvalidProxyConfigurationWarning,
|
||||
stacklevel=3,
|
||||
)
|
||||
|
||||
def urlopen(self, method, url, redirect=True, **kw):
|
||||
"Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
|
||||
u = parse_url(url)
|
||||
self._validate_proxy_scheme_url_selection(u.scheme)
|
||||
|
||||
if u.scheme == "http":
|
||||
# For proxied HTTPS requests, httplib sets the necessary headers
|
||||
|
||||
Vendored
+12
@@ -20,6 +20,7 @@ from .exceptions import (
|
||||
ResponseNotChunked,
|
||||
IncompleteRead,
|
||||
InvalidHeader,
|
||||
HTTPError,
|
||||
)
|
||||
from .packages.six import string_types as basestring, PY3
|
||||
from .packages.six.moves import http_client as httplib
|
||||
@@ -277,6 +278,17 @@ class HTTPResponse(io.IOBase):
|
||||
self._pool._put_conn(self._connection)
|
||||
self._connection = None
|
||||
|
||||
def drain_conn(self):
|
||||
"""
|
||||
Read and discard any remaining HTTP response data in the response connection.
|
||||
|
||||
Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.
|
||||
"""
|
||||
try:
|
||||
self.read()
|
||||
except (HTTPError, SocketError, BaseSSLError, HTTPException):
|
||||
pass
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
# For backwords-compat with earlier urllib3 0.4 and earlier.
|
||||
|
||||
Vendored
+3
@@ -13,6 +13,7 @@ from ..exceptions import (
|
||||
ReadTimeoutError,
|
||||
ResponseError,
|
||||
InvalidHeader,
|
||||
ProxyError,
|
||||
)
|
||||
from ..packages import six
|
||||
|
||||
@@ -306,6 +307,8 @@ class Retry(object):
|
||||
""" Errors when we're fairly sure that the server did not receive the
|
||||
request, so it should be safe to retry.
|
||||
"""
|
||||
if isinstance(err, ProxyError):
|
||||
err = err.original_error
|
||||
return isinstance(err, ConnectTimeoutError)
|
||||
|
||||
def _is_read_error(self, err):
|
||||
|
||||
Vendored
+10
-3
@@ -119,12 +119,15 @@ except ImportError:
|
||||
self.certfile = certfile
|
||||
self.keyfile = keyfile
|
||||
|
||||
def load_verify_locations(self, cafile=None, capath=None):
|
||||
def load_verify_locations(self, cafile=None, capath=None, cadata=None):
|
||||
self.ca_certs = cafile
|
||||
|
||||
if capath is not None:
|
||||
raise SSLError("CA directories not supported in older Pythons")
|
||||
|
||||
if cadata is not None:
|
||||
raise SSLError("CA data not supported in older Pythons")
|
||||
|
||||
def set_ciphers(self, cipher_suite):
|
||||
self.ciphers = cipher_suite
|
||||
|
||||
@@ -305,6 +308,7 @@ def ssl_wrap_socket(
|
||||
ssl_context=None,
|
||||
ca_cert_dir=None,
|
||||
key_password=None,
|
||||
ca_cert_data=None,
|
||||
):
|
||||
"""
|
||||
All arguments except for server_hostname, ssl_context, and ca_cert_dir have
|
||||
@@ -323,6 +327,9 @@ def ssl_wrap_socket(
|
||||
SSLContext.load_verify_locations().
|
||||
:param key_password:
|
||||
Optional password if the keyfile is encrypted.
|
||||
:param ca_cert_data:
|
||||
Optional string containing CA certificates in PEM format suitable for
|
||||
passing as the cadata parameter to SSLContext.load_verify_locations()
|
||||
"""
|
||||
context = ssl_context
|
||||
if context is None:
|
||||
@@ -331,9 +338,9 @@ def ssl_wrap_socket(
|
||||
# this code.
|
||||
context = create_urllib3_context(ssl_version, cert_reqs, ciphers=ciphers)
|
||||
|
||||
if ca_certs or ca_cert_dir:
|
||||
if ca_certs or ca_cert_dir or ca_cert_data:
|
||||
try:
|
||||
context.load_verify_locations(ca_certs, ca_cert_dir)
|
||||
context.load_verify_locations(ca_certs, ca_cert_dir, ca_cert_data)
|
||||
except IOError as e: # Platform-specific: Python 2.7
|
||||
raise SSLError(e)
|
||||
# Py33 raises FileNotFoundError which subclasses OSError
|
||||
|
||||
+4
-1
@@ -98,7 +98,7 @@ class Timeout(object):
|
||||
self.total = self._validate_timeout(total, "total")
|
||||
self._start_connect = None
|
||||
|
||||
def __str__(self):
|
||||
def __repr__(self):
|
||||
return "%s(connect=%r, read=%r, total=%r)" % (
|
||||
type(self).__name__,
|
||||
self._connect,
|
||||
@@ -106,6 +106,9 @@ class Timeout(object):
|
||||
self.total,
|
||||
)
|
||||
|
||||
# __str__ provided for backwards compatibility
|
||||
__str__ = __repr__
|
||||
|
||||
@classmethod
|
||||
def _validate_timeout(cls, value, name):
|
||||
""" Check that a timeout attribute is valid.
|
||||
|
||||
Vendored
+1
-1
@@ -18,7 +18,7 @@ PERCENT_RE = re.compile(r"%[a-fA-F0-9]{2}")
|
||||
SCHEME_RE = re.compile(r"^(?:[a-zA-Z][a-zA-Z0-9+-]*:|/)")
|
||||
URI_RE = re.compile(
|
||||
r"^(?:([a-zA-Z][a-zA-Z0-9+.-]*):)?"
|
||||
r"(?://([^/?#]*))?"
|
||||
r"(?://([^\\/?#]*))?"
|
||||
r"([^?#]*)"
|
||||
r"(?:\?([^#]*))?"
|
||||
r"(?:#(.*))?$",
|
||||
|
||||
Vendored
+9
-8
@@ -24,13 +24,13 @@ pythonfinder==1.2.2
|
||||
requests==2.23.0
|
||||
chardet==3.0.4
|
||||
idna==2.9
|
||||
urllib3==1.25.8
|
||||
certifi==2019.11.28
|
||||
requirementslib==1.5.5
|
||||
urllib3==1.25.9
|
||||
certifi==2020.4.5.1
|
||||
requirementslib==1.5.6
|
||||
attrs==19.3.0
|
||||
distlib==0.3.0
|
||||
packaging==20.3
|
||||
pyparsing==2.4.6
|
||||
pyparsing==2.4.7
|
||||
git+https://github.com/sarugaku/plette.git@master#egg=plette
|
||||
tomlkit==0.5.11
|
||||
shellingham==1.3.2
|
||||
@@ -39,19 +39,20 @@ semver==2.9.0
|
||||
toml==0.10.0
|
||||
cached-property==1.5.1
|
||||
vistir==0.5.0
|
||||
pip-shims==0.5.1
|
||||
pip-shims==0.5.2
|
||||
contextlib2==0.6.0.post1
|
||||
funcsigs==1.0.2
|
||||
enum34==1.1.6
|
||||
# yaspin==0.15.0
|
||||
yaspin==0.14.3
|
||||
cerberus==1.3.2
|
||||
resolvelib==0.2.2
|
||||
resolvelib==0.3.0
|
||||
backports.functools_lru_cache==1.5
|
||||
pep517==0.8.1
|
||||
pep517==0.8.2
|
||||
zipp==0.6.0
|
||||
importlib_metadata==1.5.1
|
||||
importlib_metadata==1.6.0
|
||||
importlib-resources==1.4.0
|
||||
more-itertools==5.0.0
|
||||
git+https://github.com/sarugaku/passa.git@master#egg=passa
|
||||
orderedmultidict==1.0.1
|
||||
dparse==0.5.0
|
||||
|
||||
+351
-231
@@ -5,9 +5,11 @@
|
||||
|
||||
import io
|
||||
import itertools
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
# from tempfile import TemporaryDirectory
|
||||
import tarfile
|
||||
import zipfile
|
||||
@@ -23,75 +25,72 @@ from urllib3.util import parse_url as urllib3_parse
|
||||
from pipenv.utils import mkdir_p
|
||||
from pipenv.vendor.vistir.compat import NamedTemporaryFile, TemporaryDirectory
|
||||
from pipenv.vendor.vistir.contextmanagers import open_file
|
||||
from pipenv.vendor.requirementslib.models.lockfile import Lockfile, merge_items
|
||||
import pipenv.vendor.parse as parse
|
||||
|
||||
|
||||
TASK_NAME = 'update'
|
||||
TASK_NAME = "update"
|
||||
|
||||
LIBRARY_DIRNAMES = {
|
||||
'requirements-parser': 'requirements',
|
||||
'backports.shutil_get_terminal_size': 'backports/shutil_get_terminal_size',
|
||||
'backports.weakref': 'backports/weakref',
|
||||
'backports.functools_lru_cache': 'backports/functools_lru_cache',
|
||||
'python-dotenv': 'dotenv',
|
||||
'pip-tools': 'piptools',
|
||||
'setuptools': 'pkg_resources',
|
||||
'msgpack-python': 'msgpack',
|
||||
'attrs': 'attr',
|
||||
'enum': 'backports/enum'
|
||||
"requirements-parser": "requirements",
|
||||
"backports.shutil_get_terminal_size": "backports/shutil_get_terminal_size",
|
||||
"backports.weakref": "backports/weakref",
|
||||
"backports.functools_lru_cache": "backports/functools_lru_cache",
|
||||
"python-dotenv": "dotenv",
|
||||
"pip-tools": "piptools",
|
||||
"setuptools": "pkg_resources",
|
||||
"msgpack-python": "msgpack",
|
||||
"attrs": "attr",
|
||||
"enum": "backports/enum",
|
||||
}
|
||||
|
||||
PY2_DOWNLOAD = ['enum34']
|
||||
PY2_DOWNLOAD = ["enum34"]
|
||||
|
||||
# from time to time, remove the no longer needed ones
|
||||
HARDCODED_LICENSE_URLS = {
|
||||
'pytoml': 'https://github.com/avakar/pytoml/raw/master/LICENSE',
|
||||
'cursor': 'https://raw.githubusercontent.com/GijsTimmers/cursor/master/LICENSE',
|
||||
'delegator.py': 'https://raw.githubusercontent.com/kennethreitz/delegator.py/master/LICENSE',
|
||||
'click-didyoumean': 'https://raw.githubusercontent.com/click-contrib/click-didyoumean/master/LICENSE',
|
||||
'click-completion': 'https://raw.githubusercontent.com/click-contrib/click-completion/master/LICENSE',
|
||||
'parse': 'https://raw.githubusercontent.com/techalchemy/parse/master/LICENSE',
|
||||
'semver': 'https://raw.githubusercontent.com/k-bx/python-semver/master/LICENSE.txt',
|
||||
'crayons': 'https://raw.githubusercontent.com/kennethreitz/crayons/master/LICENSE',
|
||||
'pip-tools': 'https://raw.githubusercontent.com/jazzband/pip-tools/master/LICENSE',
|
||||
'pytoml': 'https://github.com/avakar/pytoml/raw/master/LICENSE',
|
||||
'webencodings': 'https://github.com/SimonSapin/python-webencodings/raw/'
|
||||
'master/LICENSE',
|
||||
'requirementslib': 'https://github.com/techalchemy/requirementslib/raw/master/LICENSE',
|
||||
'distlib': 'https://github.com/vsajip/distlib/raw/master/LICENSE.txt',
|
||||
'pythonfinder': 'https://raw.githubusercontent.com/techalchemy/pythonfinder/master/LICENSE.txt',
|
||||
'pyparsing': 'https://raw.githubusercontent.com/pyparsing/pyparsing/master/LICENSE',
|
||||
'resolvelib': 'https://raw.githubusercontent.com/sarugaku/resolvelib/master/LICENSE',
|
||||
'funcsigs': 'https://raw.githubusercontent.com/aliles/funcsigs/master/LICENSE'
|
||||
"pytoml": "https://github.com/avakar/pytoml/raw/master/LICENSE",
|
||||
"cursor": "https://raw.githubusercontent.com/GijsTimmers/cursor/master/LICENSE",
|
||||
"delegator.py": "https://raw.githubusercontent.com/kennethreitz/delegator.py/master/LICENSE",
|
||||
"click-didyoumean": "https://raw.githubusercontent.com/click-contrib/click-didyoumean/master/LICENSE",
|
||||
"click-completion": "https://raw.githubusercontent.com/click-contrib/click-completion/master/LICENSE",
|
||||
"parse": "https://raw.githubusercontent.com/techalchemy/parse/master/LICENSE",
|
||||
"semver": "https://raw.githubusercontent.com/k-bx/python-semver/master/LICENSE.txt",
|
||||
"crayons": "https://raw.githubusercontent.com/kennethreitz/crayons/master/LICENSE",
|
||||
"pip-tools": "https://raw.githubusercontent.com/jazzband/pip-tools/master/LICENSE",
|
||||
"pytoml": "https://github.com/avakar/pytoml/raw/master/LICENSE",
|
||||
"webencodings": "https://github.com/SimonSapin/python-webencodings/raw/"
|
||||
"master/LICENSE",
|
||||
"requirementslib": "https://github.com/techalchemy/requirementslib/raw/master/LICENSE",
|
||||
"distlib": "https://github.com/vsajip/distlib/raw/master/LICENSE.txt",
|
||||
"pythonfinder": "https://raw.githubusercontent.com/techalchemy/pythonfinder/master/LICENSE.txt",
|
||||
"pyparsing": "https://raw.githubusercontent.com/pyparsing/pyparsing/master/LICENSE",
|
||||
"resolvelib": "https://raw.githubusercontent.com/sarugaku/resolvelib/master/LICENSE",
|
||||
"funcsigs": "https://raw.githubusercontent.com/aliles/funcsigs/master/LICENSE",
|
||||
}
|
||||
|
||||
FILE_WHITE_LIST = (
|
||||
'Makefile',
|
||||
'vendor.txt',
|
||||
'patched.txt',
|
||||
'__init__.py',
|
||||
'README.rst',
|
||||
'README.md',
|
||||
'appdirs.py',
|
||||
'safety.zip',
|
||||
'cacert.pem',
|
||||
'vendor_pip.txt',
|
||||
"Makefile",
|
||||
"vendor.txt",
|
||||
"patched.txt",
|
||||
"__init__.py",
|
||||
"README.rst",
|
||||
"README.md",
|
||||
"appdirs.py",
|
||||
"safety.zip",
|
||||
"cacert.pem",
|
||||
"vendor_pip.txt",
|
||||
)
|
||||
|
||||
PATCHED_RENAMES = {
|
||||
'pip': 'notpip'
|
||||
}
|
||||
PATCHED_RENAMES = {"pip": "notpip"}
|
||||
|
||||
LIBRARY_RENAMES = {
|
||||
'pip': 'pipenv.patched.notpip',
|
||||
"pip": "pipenv.patched.notpip",
|
||||
"functools32": "pipenv.vendor.backports.functools_lru_cache",
|
||||
'enum34': 'enum',
|
||||
"enum34": "enum",
|
||||
}
|
||||
|
||||
|
||||
LICENSE_RENAMES = {
|
||||
"pythonfinder/LICENSE": "pythonfinder/pep514tools.LICENSE"
|
||||
}
|
||||
LICENSE_RENAMES = {"pythonfinder/LICENSE": "pythonfinder/pep514tools.LICENSE"}
|
||||
|
||||
|
||||
def drop_dir(path):
|
||||
@@ -108,32 +107,32 @@ def remove_all(paths):
|
||||
|
||||
|
||||
def log(msg):
|
||||
print('[vendoring.%s] %s' % (TASK_NAME, msg))
|
||||
print("[vendoring.%s] %s" % (TASK_NAME, msg))
|
||||
|
||||
|
||||
def _get_git_root(ctx):
|
||||
return Path(ctx.run('git rev-parse --show-toplevel', hide=True).stdout.strip())
|
||||
return Path(ctx.run("git rev-parse --show-toplevel", hide=True).stdout.strip())
|
||||
|
||||
|
||||
def _get_vendor_dir(ctx):
|
||||
return _get_git_root(ctx) / 'pipenv' / 'vendor'
|
||||
return _get_git_root(ctx) / "pipenv" / "vendor"
|
||||
|
||||
|
||||
def _get_patched_dir(ctx):
|
||||
return _get_git_root(ctx) / 'pipenv' / 'patched'
|
||||
return _get_git_root(ctx) / "pipenv" / "patched"
|
||||
|
||||
|
||||
def clean_vendor(ctx, vendor_dir):
|
||||
# Old _vendor cleanup
|
||||
remove_all(vendor_dir.glob('*.pyc'))
|
||||
log('Cleaning %s' % vendor_dir)
|
||||
remove_all(vendor_dir.glob("*.pyc"))
|
||||
log("Cleaning %s" % vendor_dir)
|
||||
for item in vendor_dir.iterdir():
|
||||
if item.is_dir():
|
||||
shutil.rmtree(str(item))
|
||||
elif item.name not in FILE_WHITE_LIST:
|
||||
item.unlink()
|
||||
else:
|
||||
log('Skipping %s' % item)
|
||||
log("Skipping %s" % item)
|
||||
|
||||
|
||||
def detect_vendored_libs(vendor_dir):
|
||||
@@ -154,7 +153,7 @@ def rewrite_imports(package_dir, vendored_libs, vendor_dir):
|
||||
for item in package_dir.iterdir():
|
||||
if item.is_dir():
|
||||
rewrite_imports(item, vendored_libs, vendor_dir)
|
||||
elif item.name.endswith('.py'):
|
||||
elif item.name.endswith(".py"):
|
||||
rewrite_file_imports(item, vendored_libs, vendor_dir)
|
||||
|
||||
|
||||
@@ -162,9 +161,9 @@ def rewrite_file_imports(item, vendored_libs, vendor_dir):
|
||||
"""Rewrite 'import xxx' and 'from xxx import' for vendored_libs"""
|
||||
# log('Reading file: %s' % item)
|
||||
try:
|
||||
text = item.read_text(encoding='utf-8')
|
||||
text = item.read_text(encoding="utf-8")
|
||||
except UnicodeDecodeError:
|
||||
text = item.read_text(encoding='cp1252')
|
||||
text = item.read_text(encoding="cp1252")
|
||||
renames = LIBRARY_RENAMES
|
||||
for k in LIBRARY_RENAMES.keys():
|
||||
if k not in vendored_libs:
|
||||
@@ -174,111 +173,165 @@ def rewrite_file_imports(item, vendored_libs, vendor_dir):
|
||||
if lib in renames:
|
||||
to_lib = renames[lib]
|
||||
text = re.sub(
|
||||
r'([\n\s]*)import %s([\n\s\.]+)' % lib,
|
||||
r'\1import %s\2' % to_lib,
|
||||
text,
|
||||
)
|
||||
text = re.sub(
|
||||
r'([\n\s]*)from %s([\s\.])+' % lib,
|
||||
r'\1from %s\2' % to_lib,
|
||||
text,
|
||||
r"([\n\s]*)import %s([\n\s\.]+)" % lib, r"\1import %s\2" % to_lib, text,
|
||||
)
|
||||
text = re.sub(r"([\n\s]*)from %s([\s\.])+" % lib, r"\1from %s\2" % to_lib, text,)
|
||||
text = re.sub(
|
||||
r"(\n\s*)__import__\('%s([\s'\.])+" % lib,
|
||||
r"\1__import__('%s\2" % to_lib,
|
||||
text,
|
||||
)
|
||||
item.write_text(text, encoding='utf-8')
|
||||
item.write_text(text, encoding="utf-8")
|
||||
|
||||
|
||||
def apply_patch(ctx, patch_file_path):
|
||||
log('Applying patch %s' % patch_file_path.name)
|
||||
ctx.run('git apply --ignore-whitespace --verbose %s' % patch_file_path)
|
||||
log("Applying patch %s" % patch_file_path.name)
|
||||
ctx.run("git apply --ignore-whitespace --verbose %s" % patch_file_path)
|
||||
|
||||
|
||||
def _recursive_write_to_zip(zf, path, root=None):
|
||||
if path == Path(zf.filename):
|
||||
return
|
||||
if root is None:
|
||||
if not path.is_dir():
|
||||
raise ValueError('root is required for non-directory path')
|
||||
root = path
|
||||
if not path.is_dir():
|
||||
zf.write(str(path), str(path.relative_to(root)))
|
||||
return
|
||||
for c in path.iterdir():
|
||||
_recursive_write_to_zip(zf, c, root)
|
||||
|
||||
|
||||
@invoke.task
|
||||
def update_safety(ctx):
|
||||
ignore_subdeps = ['pip', 'pip-egg-info', 'bin']
|
||||
ignore_files = ['pip-delete-this-directory.txt', 'PKG-INFO']
|
||||
vendor_dir = _get_patched_dir(ctx)
|
||||
log('Using vendor dir: %s' % vendor_dir)
|
||||
log('Downloading safety package files...')
|
||||
build_dir = vendor_dir / 'build'
|
||||
download_dir = TemporaryDirectory(prefix='pipenv-', suffix='-safety')
|
||||
if build_dir.exists() and build_dir.is_dir():
|
||||
drop_dir(build_dir)
|
||||
|
||||
ctx.run(
|
||||
'pip download -b {0} --no-binary=:all: --no-clean -d {1} safety pyyaml'.format(
|
||||
str(build_dir), str(download_dir.name),
|
||||
ignore_subdeps = ["pip", "pip-egg-info", "bin", "pipenv", "virtualenv", "virtualenv-clone", "setuptools",]
|
||||
ignore_files = ["pip-delete-this-directory.txt", "PKG-INFO", "easy_install.py", "clonevirtualenv.py"]
|
||||
ignore_patterns = ["*.pyd", "*.so", "**/*.pyc", "*.pyc"]
|
||||
cmd_envvars = {
|
||||
"PIPENV_NO_INHERIT": "true",
|
||||
"PIPENV_IGNORE_VIRTUALENVS": "true",
|
||||
"PIPENV_VENV_IN_PROJECT": "true"
|
||||
}
|
||||
patched_dir = _get_patched_dir(ctx)
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
safety_dir = Path(__file__).absolute().parent.joinpath("safety")
|
||||
log("Using vendor dir: %s" % patched_dir)
|
||||
log("Downloading safety package files...")
|
||||
build_dir = patched_dir / "build"
|
||||
root = _get_git_root(ctx)
|
||||
with TemporaryDirectory(prefix="pipenv-", suffix="-safety") as download_dir:
|
||||
log("generating lockfile...")
|
||||
packages = "\n".join(["safety", "requests[security]"])
|
||||
env = {"PIPENV_PACKAGES": packages}
|
||||
resolve_cmd = "python {0}".format(root.joinpath("pipenv/resolver.py").as_posix())
|
||||
py27_resolve_cmd = "python2.7 {0}".format(root.joinpath("pipenv/resolver.py").as_posix())
|
||||
_, _, resolved = ctx.run(resolve_cmd, hide=True, env=env).stdout.partition("RESULTS:")
|
||||
_, _, resolved_py2 = ctx.run(py27_resolve_cmd, hide=True, env=env).stdout.partition("RESULTS:")
|
||||
resolved = json.loads(resolved.strip())
|
||||
resolved_py2 = json.loads(resolved_py2.strip())
|
||||
pkg_dict, pkg_dict_py2 = {}, {}
|
||||
for pkg in resolved:
|
||||
name = pkg.pop("name")
|
||||
pkg["version"] = "=={0}".format(pkg["version"])
|
||||
pkg_dict[name] = pkg
|
||||
for pkg in resolved_py2:
|
||||
name = pkg.pop("name")
|
||||
pkg["version"] = "=={0}".format(pkg["version"])
|
||||
pkg_dict_py2[name] = pkg
|
||||
merged = merge_items([pkg_dict, pkg_dict_py2])
|
||||
lf = Lockfile.create(safety_dir.as_posix())
|
||||
lf["default"] = merged
|
||||
lf.write()
|
||||
# envvars_no_deps = {"PIP_NO_DEPS": "true"}.update(cmd_envvars)
|
||||
# ctx.run("python -m pipenv run pip install safety", env=envvars_no_deps)
|
||||
# ctx.run("python -m pipenv run pip uninstall -y pipenv", env=cmd_envvars)
|
||||
# ctx.run("python -m pipenv install safety", env=cmd_envvars)
|
||||
# ctx.run("python -m pipenv run pip uninstall -y pipenv", env=cmd_envvars)
|
||||
# ctx.run("python2.7 -m pip install --upgrade --upgrade-strategy=eager -e {}".format(root.as_posix()))
|
||||
# ctx.run("python2.7 -m pipenv install safety", env=cmd_envvars)
|
||||
# requirements_txt = ctx.run("python2.7 -m pipenv lock -r", env=cmd_envvars, quiet=True).out
|
||||
requirements = [
|
||||
r.as_line(include_hashes=False, include_markers=False)
|
||||
for r in lf.requirements
|
||||
]
|
||||
safety_dir.joinpath("requirements.txt").write_text("\n".join(requirements))
|
||||
if build_dir.exists() and build_dir.is_dir():
|
||||
log("dropping pre-existing build dir at {0}".format(build_dir.as_posix()))
|
||||
drop_dir(build_dir)
|
||||
pip_command = "pip download -b {0} --no-binary=:all: --no-clean --no-deps -d {1} pyyaml safety".format(
|
||||
build_dir.absolute().as_posix(), str(download_dir.name),
|
||||
)
|
||||
)
|
||||
safety_dir = build_dir / 'safety'
|
||||
yaml_build_dir = build_dir / 'pyyaml'
|
||||
main_file = safety_dir / '__main__.py'
|
||||
main_content = """
|
||||
import sys
|
||||
yaml_lib = 'yaml{0}'.format(sys.version_info[0])
|
||||
locals()[yaml_lib] = __import__(yaml_lib)
|
||||
sys.modules['yaml'] = sys.modules[yaml_lib]
|
||||
from safety.cli import cli
|
||||
log("downloading deps via pip: {0}".format(pip_command))
|
||||
ctx.run(pip_command)
|
||||
safety_build_dir = build_dir / "safety"
|
||||
yaml_build_dir = build_dir / "pyyaml"
|
||||
lib_dir = safety_dir.joinpath("lib")
|
||||
|
||||
# Disable insecure warnings.
|
||||
import urllib3
|
||||
from urllib3.exceptions import InsecureRequestWarning
|
||||
urllib3.disable_warnings(InsecureRequestWarning)
|
||||
|
||||
cli(prog_name="safety")
|
||||
""".strip()
|
||||
with open(str(main_file), 'w') as fh:
|
||||
fh.write(main_content)
|
||||
|
||||
with ctx.cd(str(safety_dir)):
|
||||
ctx.run('pip install --no-compile --no-binary=:all: -t . .')
|
||||
safety_dir = safety_dir.absolute()
|
||||
yaml_dir = safety_dir / 'yaml'
|
||||
if yaml_dir.exists():
|
||||
version_choices = ['2', '3']
|
||||
version_choices.remove(str(sys.version_info[0]))
|
||||
mkdir_p(str(safety_dir / 'yaml{0}'.format(sys.version_info[0])))
|
||||
for fn in yaml_dir.glob('*.py'):
|
||||
fn.rename(str(safety_dir.joinpath('yaml{0}'.format(sys.version_info[0]), fn.name)))
|
||||
if version_choices[0] == '2':
|
||||
lib = yaml_build_dir / 'lib' / 'yaml'
|
||||
else:
|
||||
lib = yaml_build_dir / 'lib3' / 'yaml'
|
||||
shutil.copytree(str(lib.absolute()), str(safety_dir / 'yaml{0}'.format(version_choices[0])))
|
||||
requests_dir = safety_dir / 'requests'
|
||||
cacert = vendor_dir / 'requests' / 'cacert.pem'
|
||||
if not cacert.exists():
|
||||
from pipenv.vendor import requests
|
||||
cacert = Path(requests.certs.where())
|
||||
target_cert = requests_dir / 'cacert.pem'
|
||||
target_cert.write_bytes(cacert.read_bytes())
|
||||
ctx.run("sed -i 's/r = requests.get(url=url, timeout=REQUEST_TIMEOUT, headers=headers)/r = requests.get(url=url, timeout=REQUEST_TIMEOUT, headers=headers, verify=False)/g' {0}".format(str(safety_dir / 'safety' / 'safety.py')))
|
||||
for egg in safety_dir.glob('*.egg-info'):
|
||||
drop_dir(egg.absolute())
|
||||
for dep in ignore_subdeps:
|
||||
dep_dir = safety_dir / dep
|
||||
if dep_dir.exists():
|
||||
drop_dir(dep_dir)
|
||||
for dep in ignore_files:
|
||||
fn = safety_dir / dep
|
||||
if fn.exists():
|
||||
fn.unlink()
|
||||
zip_name = '{0}/safety'.format(str(vendor_dir))
|
||||
shutil.make_archive(zip_name, format='zip', root_dir=str(safety_dir), base_dir='./')
|
||||
drop_dir(build_dir)
|
||||
download_dir.cleanup()
|
||||
with ctx.cd(str(safety_dir)):
|
||||
lib_dir.mkdir(exist_ok=True)
|
||||
install_cmd = "python2.7 -m pip install --ignore-requires-python -t {0} -r {1}".format(lib_dir.as_posix(), safety_dir.joinpath("requirements.txt").as_posix())
|
||||
log("installing dependencies: {0}".format(install_cmd))
|
||||
ctx.run(install_cmd)
|
||||
safety_dir = safety_dir.absolute()
|
||||
yaml_dir = lib_dir / "yaml"
|
||||
yaml_lib_dir_map = {
|
||||
"2": {
|
||||
"current_path": yaml_build_dir / "lib/yaml",
|
||||
"destination": lib_dir / "yaml2",
|
||||
},
|
||||
"3": {
|
||||
"current_path": yaml_build_dir / "lib3/yaml",
|
||||
"destination": lib_dir / "yaml3",
|
||||
},
|
||||
}
|
||||
if yaml_dir.exists():
|
||||
drop_dir(yaml_dir)
|
||||
log("Mapping yaml paths for python 2 and 3...")
|
||||
for py_version, path_dict in yaml_lib_dir_map.items():
|
||||
path_dict["current_path"].rename(path_dict["destination"])
|
||||
log("Ensuring certificates are available...")
|
||||
requests_dir = lib_dir / "requests"
|
||||
cacert = vendor_dir / "certifi" / "cacert.pem"
|
||||
if not cacert.exists():
|
||||
from pipenv.vendor import requests
|
||||
cacert = Path(requests.certs.where())
|
||||
target_cert = requests_dir / "cacert.pem"
|
||||
target_cert.write_bytes(cacert.read_bytes())
|
||||
log("dropping ignored files...")
|
||||
for pattern in ignore_patterns:
|
||||
for path in lib_dir.rglob(pattern):
|
||||
log("removing {0!s}".format(path))
|
||||
path.unlink()
|
||||
for dep in ignore_subdeps:
|
||||
if lib_dir.joinpath(dep).exists():
|
||||
log("cleaning up {0}".format(dep))
|
||||
drop_dir(lib_dir.joinpath(dep))
|
||||
for path in itertools.chain.from_iterable((
|
||||
lib_dir.rglob("{0}*.egg-info".format(dep)),
|
||||
lib_dir.rglob("{0}*.dist-info".format(dep))
|
||||
)):
|
||||
log("cleaning up {0}".format(path))
|
||||
drop_dir(path)
|
||||
for fn in ignore_files:
|
||||
for path in lib_dir.rglob(fn):
|
||||
log("cleaning up {0}".format(path))
|
||||
path.unlink()
|
||||
zip_name = "{0}/safety.zip".format(str(patched_dir))
|
||||
log("writing zipfile...")
|
||||
with zipfile.ZipFile(zip_name, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=6) as zf:
|
||||
_recursive_write_to_zip(zf, safety_dir)
|
||||
drop_dir(build_dir)
|
||||
drop_dir(lib_dir)
|
||||
|
||||
|
||||
def rename_if_needed(ctx, vendor_dir, item):
|
||||
rename_dict = LIBRARY_RENAMES if vendor_dir.name != 'patched' else PATCHED_RENAMES
|
||||
rename_dict = LIBRARY_RENAMES if vendor_dir.name != "patched" else PATCHED_RENAMES
|
||||
new_path = None
|
||||
if item.name in rename_dict or item.name in LIBRARY_DIRNAMES:
|
||||
new_name = rename_dict.get(item.name, LIBRARY_DIRNAMES.get(item.name))
|
||||
new_path = item.parent / new_name
|
||||
log('Renaming %s => %s' % (item.name, new_path))
|
||||
log("Renaming %s => %s" % (item.name, new_path))
|
||||
# handle existing directories
|
||||
try:
|
||||
item.rename(str(new_path))
|
||||
@@ -288,55 +341,89 @@ def rename_if_needed(ctx, vendor_dir, item):
|
||||
|
||||
|
||||
def write_backport_imports(ctx, vendor_dir):
|
||||
backport_dir = vendor_dir / 'backports'
|
||||
backport_dir = vendor_dir / "backports"
|
||||
if not backport_dir.exists():
|
||||
return
|
||||
backport_init = backport_dir / '__init__.py'
|
||||
backport_init = backport_dir / "__init__.py"
|
||||
backport_libs = detect_vendored_libs(backport_dir)
|
||||
init_py_lines = backport_init.read_text().splitlines()
|
||||
for lib in backport_libs:
|
||||
lib_line = 'from . import {0}'.format(lib)
|
||||
lib_line = "from . import {0}".format(lib)
|
||||
if lib_line not in init_py_lines:
|
||||
log('Adding backport %s to __init__.py exports' % lib)
|
||||
log("Adding backport %s to __init__.py exports" % lib)
|
||||
init_py_lines.append(lib_line)
|
||||
backport_init.write_text('\n'.join(init_py_lines) + '\n')
|
||||
backport_init.write_text("\n".join(init_py_lines) + "\n")
|
||||
|
||||
|
||||
def _ensure_package_in_requirements(ctx, requirements_file, package):
|
||||
requirement = None
|
||||
log('using requirements file: %s' % requirements_file)
|
||||
log("using requirements file: %s" % requirements_file)
|
||||
req_file_lines = [l for l in requirements_file.read_text().splitlines()]
|
||||
if package:
|
||||
match = [r for r in req_file_lines if r.strip().lower().startswith(package)]
|
||||
matched_req = None
|
||||
if match:
|
||||
for m in match:
|
||||
specifiers = [m.index(s) for s in ['>', '<', '=', '~'] if s in m]
|
||||
if m.lower() == package or (specifiers and m[:min(specifiers)].lower() == package):
|
||||
specifiers = [m.index(s) for s in [">", "<", "=", "~"] if s in m]
|
||||
if m.lower() == package or (
|
||||
specifiers and m[: min(specifiers)].lower() == package
|
||||
):
|
||||
matched_req = "{0}".format(m)
|
||||
requirement = matched_req
|
||||
log("Matched req: %r" % matched_req)
|
||||
if not matched_req:
|
||||
req_file_lines.append("{0}".format(package))
|
||||
log("Writing requirements file: %s" % requirements_file)
|
||||
requirements_file.write_text('\n'.join(req_file_lines))
|
||||
requirements_file.write_text("\n".join(req_file_lines))
|
||||
requirement = "{0}".format(package)
|
||||
return requirement
|
||||
|
||||
|
||||
def install_pyyaml(ctx, vendor_dir):
|
||||
build_dir = vendor_dir / "build"
|
||||
if build_dir.exists() and build_dir.is_dir():
|
||||
log("dropping pre-existing build dir at {0}".format(build_dir.as_posix()))
|
||||
drop_dir(build_dir)
|
||||
with TemporaryDirectory(prefix="pipenv-", suffix="-safety") as download_dir:
|
||||
pip_command = "pip download -b {0} --no-binary=:all: --no-clean --no-deps -d {1} pyyaml safety".format(
|
||||
build_dir.absolute().as_posix(), str(download_dir.name),
|
||||
)
|
||||
log("downloading deps via pip: {0}".format(pip_command))
|
||||
ctx.run(pip_command)
|
||||
safety_build_dir = build_dir / "safety"
|
||||
yaml_build_dir = build_dir / "pyyaml"
|
||||
yaml_dir = vendor_dir / "yaml"
|
||||
yaml_lib_dir_map = {
|
||||
"2": {
|
||||
"current_path": yaml_build_dir / "lib/yaml",
|
||||
"destination": vendor_dir / "yaml2",
|
||||
},
|
||||
"3": {
|
||||
"current_path": yaml_build_dir / "lib3/yaml",
|
||||
"destination": vendor_dir / "yaml3",
|
||||
},
|
||||
}
|
||||
if yaml_dir.exists():
|
||||
drop_dir(yaml_dir)
|
||||
log("Mapping yaml paths for python 2 and 3...")
|
||||
for py_version, path_dict in yaml_lib_dir_map.items():
|
||||
path_dict["current_path"].rename(path_dict["destination"])
|
||||
path_dict["destination"].joinpath("LICENSE").write_text(yaml_build_dir.joinpath("LICENSE").read_text())
|
||||
drop_dir(build_dir)
|
||||
|
||||
|
||||
def install(ctx, vendor_dir, package=None):
|
||||
requirements_file = vendor_dir / "{0}.txt".format(vendor_dir.name)
|
||||
requirement = "-r {0}".format(requirements_file.as_posix())
|
||||
log('Using requirements file: %s' % requirement)
|
||||
log("Using requirements file: %s" % requirement)
|
||||
if package:
|
||||
requirement = _ensure_package_in_requirements(ctx, requirements_file, package)
|
||||
# We use --no-deps because we want to ensure that all of our dependencies
|
||||
# are added to vendor.txt, this includes all dependencies recursively up
|
||||
# the chain.
|
||||
ctx.run(
|
||||
'pip install -t {0} --no-compile --no-deps --upgrade {1}'.format(
|
||||
vendor_dir.as_posix(),
|
||||
requirement,
|
||||
"pip install -t {0} --no-compile --no-deps --upgrade {1}".format(
|
||||
vendor_dir.as_posix(), requirement,
|
||||
)
|
||||
)
|
||||
# read licenses from distinfo files if possible
|
||||
@@ -346,24 +433,32 @@ def install(ctx, vendor_dir, package=None):
|
||||
if not license_file.exists():
|
||||
continue
|
||||
if vendor_dir.joinpath(pkg).exists():
|
||||
vendor_dir.joinpath(pkg).joinpath("LICENSE").write_text(license_file.read_text())
|
||||
vendor_dir.joinpath(pkg).joinpath("LICENSE").write_text(
|
||||
license_file.read_text()
|
||||
)
|
||||
elif vendor_dir.joinpath("{0}.py".format(pkg)).exists():
|
||||
vendor_dir.joinpath("{0}.py.LICENSE".format(pkg)).write_text(license_file.read_text())
|
||||
vendor_dir.joinpath("{0}.py.LICENSE".format(pkg)).write_text(
|
||||
license_file.read_text()
|
||||
)
|
||||
else:
|
||||
matched_path = next(iter(pth for pth in vendor_dir.glob("{0}*".format(pkg))), None)
|
||||
matched_path = next(
|
||||
iter(pth for pth in vendor_dir.glob("{0}*".format(pkg))), None
|
||||
)
|
||||
if matched_path is not None:
|
||||
vendor_dir.joinpath("{0}.LICENSE".format(matched_path)).write_text(license_file.read_text())
|
||||
vendor_dir.joinpath("{0}.LICENSE".format(matched_path)).write_text(
|
||||
license_file.read_text()
|
||||
)
|
||||
|
||||
|
||||
def post_install_cleanup(ctx, vendor_dir):
|
||||
remove_all(vendor_dir.glob('*.dist-info'))
|
||||
remove_all(vendor_dir.glob('*.egg-info'))
|
||||
remove_all(vendor_dir.glob("*.dist-info"))
|
||||
remove_all(vendor_dir.glob("*.egg-info"))
|
||||
|
||||
# Cleanup setuptools unneeded parts
|
||||
drop_dir(vendor_dir / 'bin')
|
||||
drop_dir(vendor_dir / 'tests')
|
||||
drop_dir(vendor_dir / 'shutil_backports')
|
||||
remove_all(vendor_dir.glob('toml.py'))
|
||||
drop_dir(vendor_dir / "bin")
|
||||
drop_dir(vendor_dir / "tests")
|
||||
drop_dir(vendor_dir / "shutil_backports")
|
||||
remove_all(vendor_dir.glob("toml.py"))
|
||||
|
||||
|
||||
@invoke.task
|
||||
@@ -373,24 +468,24 @@ def apply_patches(ctx, patched=False, pre=False):
|
||||
else:
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
log("Applying pre-patches...")
|
||||
patch_dir = Path(__file__).parent / 'patches' / vendor_dir.name
|
||||
patch_dir = Path(__file__).parent / "patches" / vendor_dir.name
|
||||
if pre:
|
||||
if not patched:
|
||||
pass
|
||||
for patch in patch_dir.glob('*.patch'):
|
||||
if not patch.name.startswith('_post'):
|
||||
for patch in patch_dir.glob("*.patch"):
|
||||
if not patch.name.startswith("_post"):
|
||||
apply_patch(ctx, patch)
|
||||
else:
|
||||
patches = patch_dir.glob('*.patch' if not patched else '_post*.patch')
|
||||
patches = patch_dir.glob("*.patch" if not patched else "_post*.patch")
|
||||
for patch in patches:
|
||||
apply_patch(ctx, patch)
|
||||
|
||||
|
||||
def vendor(ctx, vendor_dir, package=None, rewrite=True):
|
||||
log('Reinstalling vendored libraries')
|
||||
is_patched = vendor_dir.name == 'patched'
|
||||
log("Reinstalling vendored libraries")
|
||||
is_patched = vendor_dir.name == "patched"
|
||||
install(ctx, vendor_dir, package=package)
|
||||
log('Running post-install cleanup...')
|
||||
log("Running post-install cleanup...")
|
||||
post_install_cleanup(ctx, vendor_dir)
|
||||
# Detect the vendored packages/modules
|
||||
vendored_libs = detect_vendored_libs(_get_vendor_dir(ctx))
|
||||
@@ -401,18 +496,17 @@ def vendor(ctx, vendor_dir, package=None, rewrite=True):
|
||||
if is_patched:
|
||||
apply_patches(ctx, patched=is_patched, pre=True)
|
||||
log("Removing scandir library files...")
|
||||
remove_all(vendor_dir.glob('*.so'))
|
||||
drop_dir(vendor_dir / 'setuptools')
|
||||
drop_dir(vendor_dir / 'pkg_resources' / '_vendor')
|
||||
drop_dir(vendor_dir / 'pkg_resources' / 'extern')
|
||||
drop_dir(vendor_dir / 'bin')
|
||||
for extension in ("*.so", "*.pyd", "*.egg-info", "*.dist-info"):
|
||||
remove_all(vendor_dir.glob(extension))
|
||||
for dirname in ("setuptools", "pkg_resources/_vendor", "pkg_resources/extern", "bin"):
|
||||
drop_dir(vendor_dir / dirname)
|
||||
|
||||
# Global import rewrites
|
||||
log('Renaming specified libs...')
|
||||
log("Renaming specified libs...")
|
||||
for item in vendor_dir.iterdir():
|
||||
if item.is_dir():
|
||||
if rewrite and not package or (package and item.name.lower() in package):
|
||||
log('Rewriting imports for %s...' % item)
|
||||
log("Rewriting imports for %s..." % item)
|
||||
rewrite_imports(item, vendored_libs, vendor_dir)
|
||||
rename_if_needed(ctx, vendor_dir, item)
|
||||
elif item.name not in FILE_WHITE_LIST:
|
||||
@@ -422,23 +516,23 @@ def vendor(ctx, vendor_dir, package=None, rewrite=True):
|
||||
if not package:
|
||||
apply_patches(ctx, patched=is_patched, pre=False)
|
||||
if is_patched:
|
||||
piptools_vendor = vendor_dir / 'piptools' / '_vendored'
|
||||
piptools_vendor = vendor_dir / "piptools" / "_vendored"
|
||||
if piptools_vendor.exists():
|
||||
drop_dir(piptools_vendor)
|
||||
msgpack = vendor_dir / 'notpip' / '_vendor' / 'msgpack'
|
||||
msgpack = vendor_dir / "notpip" / "_vendor" / "msgpack"
|
||||
if msgpack.exists():
|
||||
remove_all(msgpack.glob('*.so'))
|
||||
remove_all(msgpack.glob("*.so"))
|
||||
|
||||
|
||||
@invoke.task
|
||||
def redo_imports(ctx, library):
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
log('Using vendor dir: %s' % vendor_dir)
|
||||
log("Using vendor dir: %s" % vendor_dir)
|
||||
vendored_libs = detect_vendored_libs(vendor_dir)
|
||||
item = vendor_dir / library
|
||||
library_name = vendor_dir / '{0}.py'.format(library)
|
||||
library_name = vendor_dir / "{0}.py".format(library)
|
||||
log("Detected vendored libraries: %s" % ", ".join(vendored_libs))
|
||||
log('Rewriting imports for %s...' % item)
|
||||
log("Rewriting imports for %s..." % item)
|
||||
if item.is_dir():
|
||||
rewrite_imports(item, vendored_libs, vendor_dir)
|
||||
else:
|
||||
@@ -448,7 +542,7 @@ def redo_imports(ctx, library):
|
||||
@invoke.task
|
||||
def rewrite_all_imports(ctx):
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
log('Using vendor dir: %s' % vendor_dir)
|
||||
log("Using vendor dir: %s" % vendor_dir)
|
||||
vendored_libs = detect_vendored_libs(vendor_dir)
|
||||
log("Detected vendored libraries: %s" % ", ".join(vendored_libs))
|
||||
log("Rewriting all imports related to vendored libs")
|
||||
@@ -460,16 +554,21 @@ def rewrite_all_imports(ctx):
|
||||
|
||||
|
||||
@invoke.task
|
||||
def packages_missing_licenses(ctx, vendor_dir=None, requirements_file='vendor.txt', package=None):
|
||||
def packages_missing_licenses(
|
||||
ctx, vendor_dir=None, requirements_file="vendor.txt", package=None
|
||||
):
|
||||
if not vendor_dir:
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
requirements = vendor_dir.joinpath(requirements_file).read_text().splitlines()
|
||||
new_requirements = []
|
||||
LICENSE_EXTS = ("rst", "txt", "APACHE", "BSD", "md")
|
||||
LICENSES = [".".join(lic) for lic in itertools.product(("LICENSE", "LICENSE-MIT"), LICENSE_EXTS)]
|
||||
LICENSES = [
|
||||
".".join(lic)
|
||||
for lic in itertools.product(("LICENSE", "LICENSE-MIT"), LICENSE_EXTS)
|
||||
]
|
||||
for i, req in enumerate(requirements):
|
||||
pkg = req.strip().split("=")[0]
|
||||
possible_pkgs = [pkg, pkg.replace('-', '_')]
|
||||
possible_pkgs = [pkg, pkg.replace("-", "_")]
|
||||
match_found = False
|
||||
if pkg in PY2_DOWNLOAD:
|
||||
match_found = True
|
||||
@@ -506,29 +605,37 @@ def packages_missing_licenses(ctx, vendor_dir=None, requirements_file='vendor.tx
|
||||
|
||||
@invoke.task
|
||||
def download_licenses(
|
||||
ctx, vendor_dir=None, requirements_file='vendor.txt', package=None, only=False,
|
||||
patched=False
|
||||
ctx,
|
||||
vendor_dir=None,
|
||||
requirements_file="vendor.txt",
|
||||
package=None,
|
||||
only=False,
|
||||
patched=False,
|
||||
):
|
||||
log('Downloading licenses')
|
||||
log("Downloading licenses")
|
||||
if not vendor_dir:
|
||||
if patched:
|
||||
vendor_dir = _get_patched_dir(ctx)
|
||||
requirements_file = 'patched.txt'
|
||||
requirements_file = "patched.txt"
|
||||
else:
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
requirements_file = vendor_dir / requirements_file
|
||||
requirements = packages_missing_licenses(ctx, vendor_dir, requirements_file, package=package)
|
||||
requirements = packages_missing_licenses(
|
||||
ctx, vendor_dir, requirements_file, package=package
|
||||
)
|
||||
|
||||
with NamedTemporaryFile(prefix="pipenv", suffix="vendor-reqs", delete=False, mode="w") as fh:
|
||||
with NamedTemporaryFile(
|
||||
prefix="pipenv", suffix="vendor-reqs", delete=False, mode="w"
|
||||
) as fh:
|
||||
fh.write("\n".join(requirements))
|
||||
new_requirements_file = fh.name
|
||||
new_requirements_file = Path(new_requirements_file)
|
||||
log(requirements)
|
||||
tmp_dir = vendor_dir / '__tmp__'
|
||||
tmp_dir = vendor_dir / "__tmp__"
|
||||
# TODO: Fix this whenever it gets sorted out (see https://github.com/pypa/pip/issues/5739)
|
||||
cmd = "pip download --no-binary :all: --only-binary requests_download --no-deps"
|
||||
enum_cmd = "pip download --no-deps"
|
||||
ctx.run('pip install flit') # needed for the next step
|
||||
ctx.run("pip install flit") # needed for the next step
|
||||
for req in requirements_file.read_text().splitlines():
|
||||
if req.startswith("enum34"):
|
||||
exe_cmd = "{0} -d {1} {2}".format(enum_cmd, tmp_dir.as_posix(), req)
|
||||
@@ -553,9 +660,7 @@ def download_licenses(
|
||||
backend, _, _ = backend.partition(".")
|
||||
ctx.run("pip install {0}".format(backend))
|
||||
ctx.run(
|
||||
"{0} --no-build-isolation -d {1} {2}".format(
|
||||
cmd, tmp_dir.as_posix(), req
|
||||
)
|
||||
"{0} --no-build-isolation -d {1} {2}".format(cmd, tmp_dir.as_posix(), req)
|
||||
)
|
||||
for sdist in tmp_dir.iterdir():
|
||||
extract_license(vendor_dir, sdist)
|
||||
@@ -564,18 +669,18 @@ def download_licenses(
|
||||
|
||||
|
||||
def extract_license(vendor_dir, sdist):
|
||||
if sdist.stem.endswith('.tar'):
|
||||
if sdist.stem.endswith(".tar"):
|
||||
ext = sdist.suffix[1:]
|
||||
with tarfile.open(sdist, mode='r:{}'.format(ext)) as tar:
|
||||
with tarfile.open(sdist, mode="r:{}".format(ext)) as tar:
|
||||
found = find_and_extract_license(vendor_dir, tar, tar.getmembers())
|
||||
elif sdist.suffix in ('.zip', '.whl'):
|
||||
elif sdist.suffix in (".zip", ".whl"):
|
||||
with zipfile.ZipFile(sdist) as zip:
|
||||
found = find_and_extract_license(vendor_dir, zip, zip.infolist())
|
||||
else:
|
||||
raise NotImplementedError('new sdist type!')
|
||||
raise NotImplementedError("new sdist type!")
|
||||
|
||||
if not found:
|
||||
log('License not found in {}, will download'.format(sdist.name))
|
||||
log("License not found in {}, will download".format(sdist.name))
|
||||
license_fallback(vendor_dir, sdist.name)
|
||||
|
||||
|
||||
@@ -586,10 +691,10 @@ def find_and_extract_license(vendor_dir, tar, members):
|
||||
name = member.name
|
||||
except AttributeError: # zipfile
|
||||
name = member.filename
|
||||
if 'LICENSE' in name or 'COPYING' in name:
|
||||
if '/test' in name:
|
||||
if "LICENSE" in name or "COPYING" in name:
|
||||
if "/test" in name:
|
||||
# some testing licenses in hml5lib and distlib
|
||||
log('Ignoring {}'.format(name))
|
||||
log("Ignoring {}".format(name))
|
||||
continue
|
||||
found = True
|
||||
extract_license_member(vendor_dir, tar, member, name)
|
||||
@@ -600,13 +705,13 @@ def license_fallback(vendor_dir, sdist_name):
|
||||
"""Hardcoded license URLs. Check when updating if those are still needed"""
|
||||
libname = libname_from_dir(sdist_name)
|
||||
if libname not in HARDCODED_LICENSE_URLS:
|
||||
raise ValueError('No hardcoded URL for {} license'.format(libname))
|
||||
raise ValueError("No hardcoded URL for {} license".format(libname))
|
||||
|
||||
url = HARDCODED_LICENSE_URLS[libname]
|
||||
_, _, name = url.rpartition('/')
|
||||
_, _, name = url.rpartition("/")
|
||||
dest = license_destination(vendor_dir, libname, name)
|
||||
r = requests.get(url, allow_redirects=True)
|
||||
log('Downloading {}'.format(url))
|
||||
log("Downloading {}".format(url))
|
||||
r.raise_for_status()
|
||||
dest.write_bytes(r.content)
|
||||
|
||||
@@ -614,11 +719,11 @@ def license_fallback(vendor_dir, sdist_name):
|
||||
def libname_from_dir(dirname):
|
||||
"""Reconstruct the library name without it's version"""
|
||||
parts = []
|
||||
for part in dirname.split('-'):
|
||||
for part in dirname.split("-"):
|
||||
if part[0].isdigit():
|
||||
break
|
||||
parts.append(part)
|
||||
return '-'.join(parts)
|
||||
return "-".join(parts)
|
||||
|
||||
|
||||
def license_destination(vendor_dir, libname, filename):
|
||||
@@ -626,10 +731,10 @@ def license_destination(vendor_dir, libname, filename):
|
||||
normal = vendor_dir / libname
|
||||
if normal.is_dir():
|
||||
return normal / filename
|
||||
lowercase = vendor_dir / libname.lower().replace('-', '_')
|
||||
lowercase = vendor_dir / libname.lower().replace("-", "_")
|
||||
if lowercase.is_dir():
|
||||
return lowercase / filename
|
||||
rename_dict = LIBRARY_RENAMES if vendor_dir.name != 'patched' else PATCHED_RENAMES
|
||||
rename_dict = LIBRARY_RENAMES if vendor_dir.name != "patched" else PATCHED_RENAMES
|
||||
# Short circuit all logic if we are renaming the whole library
|
||||
if libname in rename_dict:
|
||||
return vendor_dir / rename_dict[libname] / filename
|
||||
@@ -637,15 +742,15 @@ def license_destination(vendor_dir, libname, filename):
|
||||
override = vendor_dir / LIBRARY_DIRNAMES[libname]
|
||||
if not override.exists() and override.parent.exists():
|
||||
# for flattened subdeps, specifically backports/weakref.py
|
||||
return (
|
||||
vendor_dir / override.parent
|
||||
) / '{0}.{1}'.format(override.name, filename)
|
||||
return (vendor_dir / override.parent) / "{0}.{1}".format(
|
||||
override.name, filename
|
||||
)
|
||||
license_path = Path(LIBRARY_DIRNAMES[libname]) / filename
|
||||
if license_path.as_posix() in LICENSE_RENAMES:
|
||||
return vendor_dir / LICENSE_RENAMES[license_path.as_posix()]
|
||||
return vendor_dir / LIBRARY_DIRNAMES[libname] / filename
|
||||
# fallback to libname.LICENSE (used for nondirs)
|
||||
return vendor_dir / '{}.{}'.format(libname, filename)
|
||||
return vendor_dir / "{}.{}".format(libname, filename)
|
||||
|
||||
|
||||
def extract_license_member(vendor_dir, tar, member, name):
|
||||
@@ -653,7 +758,7 @@ def extract_license_member(vendor_dir, tar, member, name):
|
||||
dirname = list(mpath.parents)[-2].name # -1 is .
|
||||
libname = libname_from_dir(dirname)
|
||||
dest = license_destination(vendor_dir, libname, mpath.name)
|
||||
log('Extracting {} into {}'.format(name, dest))
|
||||
log("Extracting {} into {}".format(name, dest))
|
||||
try:
|
||||
fileobj = tar.extractfile(member)
|
||||
dest.write_bytes(fileobj.read())
|
||||
@@ -662,18 +767,20 @@ def extract_license_member(vendor_dir, tar, member, name):
|
||||
|
||||
|
||||
@invoke.task()
|
||||
def generate_patch(ctx, package_path, patch_description, base='HEAD'):
|
||||
def generate_patch(ctx, package_path, patch_description, base="HEAD"):
|
||||
pkg = Path(package_path)
|
||||
if len(pkg.parts) != 2 or pkg.parts[0] not in ('vendor', 'patched'):
|
||||
raise ValueError('example usage: generate-patch patched/piptools some-description')
|
||||
if len(pkg.parts) != 2 or pkg.parts[0] not in ("vendor", "patched"):
|
||||
raise ValueError(
|
||||
"example usage: generate-patch patched/piptools some-description"
|
||||
)
|
||||
if patch_description:
|
||||
patch_fn = '{0}-{1}.patch'.format(pkg.parts[1], patch_description)
|
||||
patch_fn = "{0}-{1}.patch".format(pkg.parts[1], patch_description)
|
||||
else:
|
||||
patch_fn = '{0}.patch'.format(pkg.parts[1])
|
||||
command = 'git diff {base} -p {root} > {out}'.format(
|
||||
patch_fn = "{0}.patch".format(pkg.parts[1])
|
||||
command = "git diff {base} -p {root} > {out}".format(
|
||||
base=base,
|
||||
root=Path('pipenv').joinpath(pkg),
|
||||
out=Path(__file__).parent.joinpath('patches', pkg.parts[0], patch_fn),
|
||||
root=Path("pipenv").joinpath(pkg),
|
||||
out=Path(__file__).parent.joinpath("patches", pkg.parts[0], patch_fn),
|
||||
)
|
||||
with ctx.cd(str(_get_git_root(ctx))):
|
||||
log(command)
|
||||
@@ -717,14 +824,20 @@ def unpin_and_copy_requirements(ctx, requirement_file, name="requirements.txt"):
|
||||
target = Path(tempdir.name).joinpath("requirements.txt")
|
||||
contents = unpin_file(requirement_file.read_text())
|
||||
target.write_text(contents)
|
||||
env = {"PIPENV_IGNORE_VIRTUALENVS": "1", "PIPENV_NOSPIN": "1", "PIPENV_PYTHON": "2.7"}
|
||||
env = {
|
||||
"PIPENV_IGNORE_VIRTUALENVS": "1",
|
||||
"PIPENV_NOSPIN": "1",
|
||||
"PIPENV_PYTHON": "2.7",
|
||||
}
|
||||
with ctx.cd(tempdir.name):
|
||||
ctx.run("pipenv install -r {0}".format(target.as_posix()), env=env, hide=True)
|
||||
result = ctx.run("pipenv lock -r", env=env, hide=True).stdout.strip()
|
||||
ctx.run("pipenv --rm", env=env, hide=True)
|
||||
result = list(sorted([line.strip() for line in result.splitlines()[1:]]))
|
||||
new_requirements = requirement_file.parent.joinpath(name)
|
||||
requirement_file.rename(requirement_file.parent.joinpath("{}.bak".format(name)))
|
||||
requirement_file.rename(
|
||||
requirement_file.parent.joinpath("{}.bak".format(name))
|
||||
)
|
||||
new_requirements.write_text("\n".join(result))
|
||||
return result
|
||||
|
||||
@@ -743,7 +856,7 @@ def unpin_and_update_vendored(ctx, vendor=True, patched=False):
|
||||
def main(ctx, package=None):
|
||||
vendor_dir = _get_vendor_dir(ctx)
|
||||
patched_dir = _get_patched_dir(ctx)
|
||||
log('Using vendor dir: %s' % vendor_dir)
|
||||
log("Using vendor dir: %s" % vendor_dir)
|
||||
if package:
|
||||
vendor(ctx, vendor_dir, package=package)
|
||||
download_licenses(ctx, vendor_dir, package=package)
|
||||
@@ -752,13 +865,20 @@ def main(ctx, package=None):
|
||||
clean_vendor(ctx, vendor_dir)
|
||||
clean_vendor(ctx, patched_dir)
|
||||
vendor(ctx, vendor_dir)
|
||||
install_pyyaml(ctx, patched_dir)
|
||||
vendor(ctx, patched_dir, rewrite=True)
|
||||
download_all_licenses(ctx, include_pip=True)
|
||||
# from .vendor_passa import vendor_passa
|
||||
# log("Vendoring passa...")
|
||||
# vendor_passa(ctx)
|
||||
# update_safety(ctx)
|
||||
log('Revendoring complete')
|
||||
log("Revendoring complete")
|
||||
|
||||
|
||||
@invoke.task
|
||||
def install_yaml(ctx):
|
||||
patched_dir = _get_patched_dir(ctx)
|
||||
install_pyyaml(ctx, patched_dir)
|
||||
|
||||
|
||||
@invoke.task
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
diff --git a/pipenv/patched/safety/__main__.py b/pipenv/patched/safety/__main__.py
|
||||
index d9a0bdab..f905408a 100644
|
||||
--- a/pipenv/patched/safety/__main__.py
|
||||
+++ b/pipenv/patched/safety/__main__.py
|
||||
@@ -1,8 +1,51 @@
|
||||
"""Allow safety to be executable through `python -m safety`."""
|
||||
from __future__ import absolute_import
|
||||
|
||||
-from .cli import cli
|
||||
+import os
|
||||
+import sys
|
||||
+import sysconfig
|
||||
+
|
||||
+
|
||||
+PATCHED_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
+PIPENV_DIR = os.path.dirname(PATCHED_DIR)
|
||||
+VENDORED_DIR = os.path.join("PIPENV_DIR", "vendor")
|
||||
+
|
||||
+
|
||||
+def get_site_packages():
|
||||
+ prefixes = {sys.prefix, sysconfig.get_config_var('prefix')}
|
||||
+ try:
|
||||
+ prefixes.add(sys.real_prefix)
|
||||
+ except AttributeError:
|
||||
+ pass
|
||||
+ form = sysconfig.get_path('purelib', expand=False)
|
||||
+ py_version_short = '{0[0]}.{0[1]}'.format(sys.version_info)
|
||||
+ return {
|
||||
+ form.format(base=prefix, py_version_short=py_version_short)
|
||||
+ for prefix in prefixes
|
||||
+ }
|
||||
+
|
||||
+
|
||||
+def insert_before_site_packages(*paths):
|
||||
+ site_packages = get_site_packages()
|
||||
+ index = None
|
||||
+ for i, path in enumerate(sys.path):
|
||||
+ if path in site_packages:
|
||||
+ index = i
|
||||
+ break
|
||||
+ if index is None:
|
||||
+ sys.path += list(paths)
|
||||
+ else:
|
||||
+ sys.path = sys.path[:index] + list(paths) + sys.path[index:]
|
||||
+
|
||||
+
|
||||
+def insert_pipenv_dirs():
|
||||
+ insert_before_site_packages(os.path.dirname(PIPENV_DIR), PATCHED_DIR, VENDORED_DIR)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
+ insert_pipenv_dirs()
|
||||
+ yaml_lib = "pipenv.patched.yaml{0}".format(sys.version_info[0])
|
||||
+ locals()[yaml_lib] = __import__(yaml_lib)
|
||||
+ sys.modules["yaml"] = sys.modules[yaml_lib]
|
||||
+ from safety.cli import cli
|
||||
cli(prog_name="safety")
|
||||
@@ -0,0 +1,13 @@
|
||||
diff --git a/pipenv/vendor/dparse/parser.py b/pipenv/vendor/dparse/parser.py
|
||||
index c01ebab4..9b2a0728 100644
|
||||
--- a/pipenv/vendor/dparse/parser.py
|
||||
+++ b/pipenv/vendor/dparse/parser.py
|
||||
@@ -6,7 +6,7 @@ import yaml
|
||||
|
||||
from io import StringIO
|
||||
|
||||
-from configparser import SafeConfigParser, NoOptionError
|
||||
+from six.moves.configparser import SafeConfigParser, NoOptionError
|
||||
|
||||
|
||||
from .regex import URL_REGEX, HASH_REGEX
|
||||
@@ -0,0 +1,9 @@
|
||||
[[source]]
|
||||
name = "pypi"
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[packages]
|
||||
safety = "*"
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Allow safety to be executable through `python -m safety`."""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
import sys
|
||||
import sysconfig
|
||||
|
||||
LIBPATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib")
|
||||
|
||||
|
||||
def get_site_packages():
|
||||
prefixes = {sys.prefix, sysconfig.get_config_var('prefix')}
|
||||
try:
|
||||
prefixes.add(sys.real_prefix)
|
||||
except AttributeError:
|
||||
pass
|
||||
form = sysconfig.get_path('purelib', expand=False)
|
||||
py_version_short = '{0[0]}.{0[1]}'.format(sys.version_info)
|
||||
return {
|
||||
form.format(base=prefix, py_version_short=py_version_short)
|
||||
for prefix in prefixes
|
||||
}
|
||||
|
||||
|
||||
def insert_before_site_packages(*paths):
|
||||
site_packages = get_site_packages()
|
||||
index = None
|
||||
for i, path in enumerate(sys.path):
|
||||
if path in site_packages:
|
||||
index = i
|
||||
break
|
||||
if index is None:
|
||||
sys.path += list(paths)
|
||||
else:
|
||||
sys.path = sys.path[:index] + list(paths) + sys.path[index:]
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
insert_before_site_packages(LIBPATH)
|
||||
yaml_lib = 'yaml{0}'.format(sys.version_info[0])
|
||||
locals()[yaml_lib] = __import__(yaml_lib)
|
||||
sys.modules['yaml'] = sys.modules[yaml_lib]
|
||||
from safety.cli import cli
|
||||
cli(prog_name="safety")
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user