Skip to content

Commit 07bd8d2

Browse files
committed
Format code and other minor cleanups
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 2bb617b commit 07bd8d2

File tree

10 files changed

+162
-166
lines changed

10 files changed

+162
-166
lines changed

src/attributecode/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
try:
2525
basestring # Python 2
2626
except NameError:
27-
basestring = str # Python 3
27+
basestring = str # Python 3 #NOQA
2828

2929

3030
__version__ = '3.2.2'

src/attributecode/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
try: # Python 2
2424
from urllib import urlencode, quote
25-
from urllib2 import urlopen, Request, HTTPError, URLError
25+
from urllib2 import urlopen, Request, HTTPError
2626
except ImportError: # Python 3
2727
from urllib.parse import urlencode, quote
2828
from urllib.request import urlopen, Request
29-
from urllib.error import HTTPError, URLError
29+
from urllib.error import HTTPError
3030

3131
from attributecode import ERROR
3232
from attributecode import Error

src/attributecode/cmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def cli():
108108
@click.argument('output', nargs=1, required=True,
109109
type=click.Path(exists=False, dir_okay=False, resolve_path=True))
110110

111-
@click.option('--filter', nargs=1, multiple=True,
111+
@click.option('--filter', nargs=1, multiple=True,
112112
help='Filter for the output inventory. e.g. "license_expression=gpl-2.0')
113113

114114
@click.option('-f', '--format', is_flag=False, default='csv', show_default=True,
@@ -139,7 +139,7 @@ def cli():
139139

140140
@click.help_option('-h', '--help')
141141

142-
def inventory(location, output, mapping, mapping_file, mapping_output, filter, quiet, format, verbose):
142+
def inventory(location, output, mapping, mapping_file, mapping_output, filter, quiet, format, verbose): # NOQA
143143
"""
144144
Collect a JSON or CSV inventory of components from .ABOUT files.
145145
@@ -155,7 +155,7 @@ def inventory(location, output, mapping, mapping_file, mapping_output, filter, q
155155
# FIXME: return error code?
156156
return
157157

158-
click.echo('Collecting inventory from: %(location)s and writing output to: %(output)s' % locals()) #(location, output))
158+
click.echo('Collecting inventory from: %(location)s and writing output to: %(output)s' % locals())
159159

160160
# FIXME: do we really want to continue support zip as an input?
161161
if location.lower().endswith('.zip'):
@@ -320,7 +320,7 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
320320
@click.option('--template', type=click.Path(exists=True), nargs=1,
321321
help='Path to an optional custom attribution template used for generation.')
322322

323-
@click.option('--vartext', nargs=1, multiple=True,
323+
@click.option('--vartext', nargs=1, multiple=True,
324324
help='Variable texts to the attribution template.')
325325

326326
@click.option('--verbose', is_flag=True, default=False,
@@ -478,7 +478,7 @@ def log_errors(errors, err_count, quiet, verbose, base_dir=False):
478478

479479

480480
def have_problematic_error(errors):
481-
for severity, message in errors:
481+
for severity, message in errors: # NOQA
482482
sever = severities[severity]
483483
if sever in problematic_errors:
484484
return True

src/attributecode/gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
from posixpath import basename, dirname, exists, join, normpath
2525
import sys
2626

27-
if sys.version_info[0] < 3:
27+
if sys.version_info[0] < 3:
2828
# Python 2
29-
import backports.csv as csv
29+
import backports.csv as csv #NOQA
3030
else:
3131
# Python 3
32-
import csv
32+
import csv #NOQA
3333

3434
from attributecode import ERROR
3535
from attributecode import CRITICAL

src/attributecode/model.py

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@
4040
import sys
4141

4242
if sys.version_info[0] < 3: # Python 2
43-
import backports.csv as csv
44-
from itertools import izip_longest as zip_longest
45-
from urlparse import urljoin, urlparse
46-
from urllib2 import urlopen, Request, HTTPError
43+
import backports.csv as csv # NOQA
44+
from itertools import izip_longest as zip_longest # NOQA
45+
from urlparse import urljoin, urlparse # NOQA
46+
from urllib2 import urlopen, Request, HTTPError # NOQA
4747
else: # Python 3
48-
basestring = str
49-
import csv
50-
from itertools import zip_longest
51-
from urllib.parse import urljoin, urlparse
52-
from urllib.request import urlopen, Request
53-
from urllib.error import HTTPError
48+
basestring = str # NOQA
49+
import csv # NOQA
50+
from itertools import zip_longest # NOQA
51+
from urllib.parse import urljoin, urlparse # NOQA
52+
from urllib.request import urlopen, Request # NOQA
53+
from urllib.error import HTTPError # NOQA
5454

5555
from license_expression import Licensing
5656

@@ -67,7 +67,6 @@
6767
from attributecode.util import on_windows
6868
from attributecode.util import ungroup_licenses
6969
from attributecode.util import UNC_PREFIX
70-
from attributecode.util import UNC_PREFIX_POSIX
7170

7271

7372
class Field(object):
@@ -666,7 +665,7 @@ def __eq__(self, other):
666665
and self.value == other.value)
667666

668667

669-
def validate_fields(fields, about_file_path, running_inventory, base_dir,
668+
def validate_fields(fields, about_file_path, running_inventory, base_dir,
670669
license_notice_text_location=None):
671670
"""
672671
Validate a sequence of Field objects. Return a list of errors.
@@ -707,7 +706,7 @@ def create_fields(self):
707706
is simpler.
708707
"""
709708
self.fields = OrderedDict([
710-
#('about_resource', ListField(required=True)),
709+
# ('about_resource', ListField(required=True)),
711710
# ('about_resource', AboutResourceField(required=True)),
712711
('about_resource', AboutResourceField(required=True)),
713712
('name', SingleLineField(required=True)),
@@ -874,29 +873,6 @@ def as_dict(self, with_paths=False, with_absent=True, with_empty=True):
874873
if with_paths:
875874
afpa = self.about_file_path_attr
876875
as_dict[afpa] = self.about_file_path
877-
arpa = self.about_resource_path_attr
878-
"""
879-
if self.about_resource_path.present:
880-
as_dict[arpa] = self.resolved_resources_paths()
881-
else:
882-
arp = OrderedDict()
883-
# Create a relative 'about_resource_path' if user has not defined
884-
if self.about_resource.present:
885-
for resource_name in self.about_resource.value:
886-
key = u''
887-
if resource_name == '.':
888-
key = resource_name
889-
else:
890-
key = './' + resource_name
891-
arp[key] = None
892-
as_dict[arpa] = arp
893-
# Return an empty 'about_resource_path' if the 'about_resource'
894-
# key is not found
895-
else:
896-
key = u''
897-
arp[key] = None
898-
as_dict[arpa] = arp
899-
"""
900876

901877
for field in self.all_fields(with_absent=with_absent,
902878
with_empty=with_empty):
@@ -1050,9 +1026,9 @@ def load(self, location, use_mapping=False, mapping_file=None):
10501026
and then join with the 'about_resource'
10511027
"""
10521028
running_inventory = True
1053-
# wrap the value of the boolean field in quote to avoid
1029+
# wrap the value of the boolean field in quote to avoid
10541030
# automatically conversion from yaml.load
1055-
input = util.wrap_boolean_value(input_text)
1031+
input = util.wrap_boolean_value(input_text) # NOQA
10561032
errs = self.load_dict(saneyaml.load(input), base_dir, running_inventory, use_mapping, mapping_file)
10571033
errors.extend(errs)
10581034
except Exception as e:
@@ -1092,7 +1068,7 @@ def load_dict(self, fields_dict, base_dir, running_inventory=False,
10921068
licenses_field = (key, value)
10931069
fields.remove(licenses_field)
10941070
errors = self.process(
1095-
fields, about_file_path, running_inventory, base_dir,
1071+
fields, about_file_path, running_inventory, base_dir,
10961072
license_notice_text_location, use_mapping, mapping_file)
10971073
self.errors = errors
10981074
return errors
@@ -1141,7 +1117,7 @@ def dumps(self, use_mapping=False, mapping_file=False, with_absent=False, with_e
11411117
if lic_group[2]:
11421118
lic_dict['file'] = lic_group[2]
11431119
if lic_group[3]:
1144-
lic_dict['url'] = lic_group[3]
1120+
lic_dict['url'] = lic_group[3]
11451121
about_data.setdefault('licenses', []).append(lic_dict)
11461122
formatted_about_data = util.format_output(about_data, use_mapping, mapping_file)
11471123
return saneyaml.dump(formatted_about_data)
@@ -1210,7 +1186,7 @@ def dump_lic(self, location, license_dict):
12101186
# line in the form of "name: value"
12111187
field_declaration = re.compile(
12121188
r'^'
1213-
+ field_name +
1189+
+field_name +
12141190
r'\s*:\s*'
12151191
r'(?P<value>.*)'
12161192
r'\s*$'
@@ -1382,7 +1358,7 @@ def about_object_to_list_of_dictionary(abouts, with_absent=False, with_empty=Tru
13821358
return abouts_dictionary_list
13831359

13841360

1385-
def write_output(abouts, location, format, mapping_output=None, with_absent=False, with_empty=True):
1361+
def write_output(abouts, location, format, mapping_output=None, with_absent=False, with_empty=True): # NOQA
13861362
"""
13871363
Write a CSV/JSON file at location given a list of About objects
13881364
"""

src/attributecode/saneyaml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
try:
3535
unicode # Python 2
3636
except NameError:
37-
unicode = str # Python 3
37+
unicode = str # Python 3 #NOQA
3838

3939
try:
4040
basestring # Python 2
4141
except NameError:
42-
basestring = str # Python 3
42+
basestring = str # Python 3 #NOQA
4343

4444

4545
"""

src/attributecode/util.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
import sys
3535

3636
if sys.version_info[0] < 3: # Python 2
37-
from itertools import izip_longest as zip_longest
37+
from itertools import izip_longest as zip_longest # NOQA
3838
else: # Python 3
39-
from itertools import zip_longest
39+
from itertools import zip_longest # NOQA
4040

4141

42-
import yaml
4342
from yaml.reader import Reader
4443
from yaml.scanner import Scanner
4544
from yaml.parser import Parser
@@ -50,10 +49,10 @@
5049

5150
if sys.version_info[0] < 3:
5251
# Python 2
53-
import backports.csv as csv
52+
import backports.csv as csv # NOQA
5453
else:
5554
# Python 3
56-
import csv
55+
import csv # NOQA
5756

5857
try:
5958
# Python 2
@@ -62,7 +61,7 @@
6261
# Python 3
6362
import http.client as httplib
6463

65-
from attributecode import CRITICAL, INFO
64+
from attributecode import CRITICAL
6665
from attributecode import Error
6766

6867

@@ -159,7 +158,7 @@ def check_duplicate_keys_about_file(context):
159158

160159
def wrap_boolean_value(context):
161160
bool_fields = ['redistribute', 'attribute', 'track_changes', 'modified']
162-
input = []
161+
input = [] # NOQA
163162
for line in context.splitlines():
164163
key = line.partition(':')[0]
165164
if key in bool_fields:
@@ -300,7 +299,7 @@ def __next__(self):
300299
self.fieldnames
301300
row = next(self.reader)
302301
self.line_num = self.reader.line_num
303-
302+
304303
# unlike the basic reader, we prefer not to return blanks,
305304
# because we will typically wind up with a dict full of None
306305
# values
@@ -381,6 +380,7 @@ def get_output_mapping(location):
381380
sys.exit(errno.EACCES)
382381
return mapping
383382

383+
384384
def apply_mapping(abouts, alternate_mapping=None):
385385
"""
386386
Given a list of About data dictionaries and a dictionary of
@@ -422,6 +422,7 @@ def get_mapping_key_order(mapping_file):
422422
mapping = get_mapping()
423423
return mapping.keys()
424424

425+
425426
def format_output(about_data, use_mapping, mapping_file):
426427
"""
427428
Convert the about_data dictionary to an ordered dictionary for saneyaml.dump()
@@ -459,6 +460,7 @@ def format_output(about_data, use_mapping, mapping_file):
459460
order_dict[other_key] = about_data[other_key]
460461
return order_dict
461462

463+
462464
def get_about_file_path(location, use_mapping=False, mapping_file=None):
463465
"""
464466
Read file at location, return a list of about_file_path.
@@ -497,10 +499,14 @@ def load_csv(location, use_mapping=False, mapping_file=None):
497499

498500
def load_json(location, use_mapping=False, mapping_file=None):
499501
"""
500-
Read JSON at location, return a list of ordered mappings, one for each entry.
502+
Read JSON file at `location` and return a list of ordered mappings, one for
503+
each entry.
501504
"""
505+
# FIXME: IMHO we should know where the JSON is from and its shape
506+
# TODO use: object_pairs_hook=OrderedDict
502507
with open(location) as json_file:
503508
results = json.load(json_file)
509+
504510
# If the loaded JSON is not a list,
505511
# - JSON output from AboutCode Manager:
506512
# look for the "components" field as it is the field
@@ -542,20 +548,22 @@ def load_json(location, use_mapping=False, mapping_file=None):
542548
# "name": "test",
543549
# ...
544550
# }
545-
if not isinstance(results, list):
551+
if isinstance(results, list):
552+
updated_results = sorted(results)
553+
else:
546554
if u'aboutcode_manager_notice' in results:
547555
updated_results = results['components']
548556
elif u'scancode_notice' in results:
549557
updated_results = results['files']
550558
else:
551559
updated_results = [results]
552-
else:
553-
updated_results = sorted(results)
554560

561+
about_ordered_list = updated_results
562+
563+
# FIXME: why this double test? either have a mapping file and we use mapping or we do not.
564+
# FIXME: IMHO only one argument is needed
555565
if use_mapping or mapping_file:
556566
about_ordered_list = apply_mapping(updated_results, mapping_file)
557-
else:
558-
about_ordered_list = updated_results
559567
return about_ordered_list
560568

561569

@@ -674,11 +682,11 @@ def inventory_filter(abouts, filter_dict):
674682

675683

676684
def update_fieldnames(fieldnames, mapping_output):
677-
map = get_output_mapping(mapping_output)
685+
mapping = get_output_mapping(mapping_output)
678686
updated_header = []
679687
for name in fieldnames:
680688
try:
681-
updated_header.append(map[name])
689+
updated_header.append(mapping[name])
682690
except:
683691
updated_header.append(name)
684692
return updated_header
@@ -701,6 +709,7 @@ def update_about_dictionary_keys(about_dictionary_list, mapping_output):
701709
updated_dict_list.append(updated_ordered_dict)
702710
return updated_dict_list
703711

712+
704713
def ungroup_licenses(licenses):
705714
"""
706715
Ungroup multiple licenses information
@@ -785,7 +794,7 @@ def format_about_dict_for_json_output(about_dictionary_list):
785794
lic_dict['file'] = lic_group[2]
786795
if lic_group[3]:
787796
lic_dict['url'] = lic_group[3]
788-
licenses_list.append(lic_dict)
797+
licenses_list.append(lic_dict)
789798
row_list['licenses'] = licenses_list
790799
json_formatted_list.append(row_list)
791800
return json_formatted_list
@@ -837,4 +846,4 @@ def __init__(self, stream):
837846
Parser.__init__(self)
838847
Composer.__init__(self)
839848
NoDuplicateConstructor.__init__(self)
840-
Resolver.__init__(self)
849+
Resolver.__init__(self)

0 commit comments

Comments
 (0)