Skip to content

Commit 7bf44bd

Browse files
committed
Use new saneyaml library
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 6d52e73 commit 7bf44bd

File tree

9 files changed

+62
-314
lines changed

9 files changed

+62
-314
lines changed

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ def read(*names, **kwargs):
6868
],
6969
install_requires=[
7070
'jinja2 >= 2.9, < 3.0',
71+
7172
'click >= 6.7, < 7.0',
73+
7274
"backports.csv ; python_version<'3.6'",
73-
'PyYAML >= 3.0, < 4.0',
75+
76+
# required by saneyaml
77+
'PyYAML >= 3.11, <=3.13',
78+
'saneyaml',
79+
7480
'boolean.py >= 3.5, < 4.0',
7581
'license_expression >= 0.94, < 1.0',
7682
],

src/attributecode/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424

2525
try:
2626
# Python 2
27-
unicode
27+
unicode # NOQA
2828
except NameError: # pragma: nocover
29-
# Python 3
30-
unicode = str #NOQA
29+
# Python 3
30+
unicode = str # NOQA
3131

32+
import saneyaml
3233

3334
__version__ = '3.3.0'
3435

src/attributecode/model.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import posixpath
3737
import re
3838

39-
import yaml
4039

4140
from attributecode.util import python2
4241

@@ -180,7 +179,7 @@ def serialize(self):
180179
# insert 4 spaces for newline values
181180
value = u' '.join(value)
182181
else:
183-
# See https://github.com/nexB/aboutcode-toolkit/issues/323
182+
# FIXME: See https://github.com/nexB/aboutcode-toolkit/issues/323
184183
# The yaml.load() will throw error if the parsed value
185184
# contains ': ' character. A work around is to put a pipe, '|'
186185
# to indicate the whole value as a string
@@ -1017,7 +1016,7 @@ def load(self, location, mapping_file=None):
10171016
with codecs.open(loc, encoding='utf-8') as txt:
10181017
input_text = txt.read()
10191018
# Check for duplicated key
1020-
yaml.load(input_text, Loader=util.NoDuplicateLoader)
1019+
saneyaml.load(input_text, allow_duplicate_keys=False)
10211020
"""
10221021
The running_inventory defines if the current process is 'inventory' or not.
10231022
This is used for the validation of the path of the 'about_resource'.
@@ -1028,10 +1027,12 @@ def load(self, location, mapping_file=None):
10281027
and then join with the 'about_resource'
10291028
"""
10301029
running_inventory = True
1030+
# FIXME: why??
10311031
# wrap the value of the boolean field in quote to avoid
10321032
# automatically conversion from yaml.load
10331033
input = util.wrap_boolean_value(input_text) # NOQA
1034-
errs = self.load_dict(saneyaml.load(input), base_dir, running_inventory, mapping_file)
1034+
data = saneyaml.load(input, allow_duplicate_keys=False)
1035+
errs = self.load_dict(data, base_dir, running_inventory, mapping_file)
10351036
errors.extend(errs)
10361037
except Exception as e:
10371038
msg = 'Cannot load invalid ABOUT file: %(location)r: %(e)r\n' + str(e)

src/attributecode/saneyaml.py

Lines changed: 0 additions & 217 deletions
This file was deleted.

src/attributecode/util.py

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def norm(p):
245245
def to_native(path):
246246
"""
247247
Return a path using the current OS path separator given a path that may
248-
contain posix or windows separators, converting "/" to "\\" on windows
248+
contain posix or windows separators, converting "/" to "\\" on windows
249249
and "\\" to "/" on posix OSes.
250250
"""
251251
path = path.replace(ntpath.sep, os.path.sep)
@@ -532,7 +532,7 @@ def have_network_connection():
532532
else:
533533
import http.client as httplib # NOQA
534534

535-
http_connection = httplib.HTTPConnection('dejacode.org', timeout=10)
535+
http_connection = httplib.HTTPConnection('dejacode.org', timeout=10) # NOQA
536536
try:
537537
http_connection.connect()
538538
except socket.error:
@@ -605,7 +605,7 @@ def copy_license_notice_files(fields, base_dir, reference_dir, afp):
605605
where reference license an notice files are stored and the `afp`
606606
about_file_path value, this function will copy to the base_dir the
607607
license_file or notice_file if found in the reference_dir
608-
608+
609609
"""
610610
lic_name = ''
611611
for key, value in fields:
@@ -769,66 +769,3 @@ def unique(sequence):
769769
if item not in deduped:
770770
deduped.append(item)
771771
return deduped
772-
773-
774-
# FIXME: remove and replace by saneyaml
775-
from collections import Hashable
776-
777-
from yaml.reader import Reader
778-
from yaml.scanner import Scanner
779-
from yaml.parser import Parser
780-
from yaml.composer import Composer
781-
from yaml.constructor import Constructor, ConstructorError
782-
from yaml.resolver import Resolver
783-
from yaml.nodes import MappingNode
784-
785-
# FIXME: add docstring
786-
class NoDuplicateConstructor(Constructor):
787-
def construct_mapping(self, node, deep=False):
788-
if not isinstance(node, MappingNode):
789-
raise ConstructorError(
790-
None, None,
791-
"expected a mapping node, but found %s" % node.id,
792-
node.start_mark)
793-
mapping = {}
794-
for key_node, value_node in node.value:
795-
# keys can be list -> deep
796-
key = self.construct_object(key_node, deep=True)
797-
# lists are not hashable, but tuples are
798-
if not isinstance(key, Hashable):
799-
if isinstance(key, list):
800-
key = tuple(key)
801-
802-
if sys.version_info.major == 2:
803-
try:
804-
hash(key)
805-
except TypeError as exc:
806-
raise ConstructorError(
807-
"while constructing a mapping", node.start_mark,
808-
"found unacceptable key (%s)" %
809-
exc, key_node.start_mark)
810-
else:
811-
if not isinstance(key, Hashable):
812-
raise ConstructorError(
813-
"while constructing a mapping", node.start_mark,
814-
"found unhashable key", key_node.start_mark)
815-
816-
value = self.construct_object(value_node, deep=deep)
817-
818-
# Actually do the check.
819-
if key in mapping:
820-
raise KeyError("Got duplicate key: {!r}".format(key))
821-
822-
mapping[key] = value
823-
return mapping
824-
825-
826-
# FIXME: add docstring
827-
class NoDuplicateLoader(Reader, Scanner, Parser, Composer, NoDuplicateConstructor, Resolver):
828-
def __init__(self, stream):
829-
Reader.__init__(self, stream)
830-
Scanner.__init__(self)
831-
Parser.__init__(self)
832-
Composer.__init__(self)
833-
NoDuplicateConstructor.__init__(self)
834-
Resolver.__init__(self)

0 commit comments

Comments
 (0)