Skip to content

Commit 9c1ff71

Browse files
committed
Fixed #501 consistant code for opening file
* instead of using codes.open, the code now use the default open() function Signed-off-by: Chin Yeung Li <[email protected]>
1 parent f0dd16b commit 9c1ff71

File tree

9 files changed

+28
-26
lines changed

9 files changed

+28
-26
lines changed

src/attributecode/attrib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def generate_from_file(abouts, is_about_input, license_dict, scancode, min_licen
229229
template_loc = add_unc(DEFAULT_TEMPLATE_FILE)
230230
else:
231231
template_loc = add_unc(template_loc)
232-
with io.open(template_loc, encoding='utf-8', errors='replace') as tplf:
232+
with open(template_loc, encoding='utf-8', errors='replace') as tplf:
233233
tpls = tplf.read()
234234
return generate(abouts, is_about_input, license_dict, scancode, min_license_score, template=tpls, vartext=vartext)
235235

@@ -267,7 +267,7 @@ def generate_and_save(abouts, is_about_input, license_dict, output_location, sca
267267

268268
if rendered:
269269
output_location = add_unc(output_location)
270-
with io.open(output_location, 'w', encoding='utf-8', errors='replace') as of:
270+
with open(output_location, 'w', encoding='utf-8', errors='replace') as of:
271271
of.write(rendered)
272272

273273
return errors, rendered

src/attributecode/cmd.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ def gen_license(location, output, djc, scancode, verbose):
340340
api_key = djc[1].strip("'").strip('"')
341341

342342
click.echo('Fetching licenses...')
343-
license_dict, lic_errors = pre_process_and_fetch_license_dict(abouts, api_url, api_key, scancode)
343+
from_check = False
344+
license_dict, lic_errors = pre_process_and_fetch_license_dict(abouts, from_check, api_url, api_key, scancode)
344345

345346
if lic_errors:
346347
errors.extend(lic_errors)
@@ -370,7 +371,7 @@ def validate_template(ctx, param, value):
370371
if not value:
371372
return None
372373

373-
with io.open(value, encoding='utf-8', errors='replace') as templatef:
374+
with open(value, encoding='utf-8', errors='replace') as templatef:
374375
template_error = check_template(templatef.read())
375376

376377
if template_error:
@@ -523,7 +524,8 @@ def attrib(input, output, api_url, api_key, scancode, min_license_score, referen
523524
api_key = ''
524525
api_url = api_url.strip("'").strip('"')
525526
api_key = api_key.strip("'").strip('"')
526-
license_dict, lic_errors = pre_process_and_fetch_license_dict(abouts, api_url, api_key, scancode, reference)
527+
from_check = False
528+
license_dict, lic_errors = pre_process_and_fetch_license_dict(abouts, from_check, api_url, api_key, scancode, reference)
527529
errors.extend(lic_errors)
528530
sorted_license_dict = sorted(license_dict)
529531

@@ -823,7 +825,7 @@ def report_errors(errors, quiet, verbose, log_file_loc=None):
823825
for msg in log_msgs:
824826
click.echo(msg)
825827
if log_msgs and log_file_loc:
826-
with io.open(log_file_loc, 'w', encoding='utf-8', errors='replace') as lf:
828+
with open(log_file_loc, 'w', encoding='utf-8', errors='replace') as lf:
827829
lf.write('\n'.join(log_msgs))
828830
click.echo("Error log: " + log_file_loc)
829831
return severe_errors_count

src/attributecode/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def check_duplicated_columns(location):
4444
at location.
4545
"""
4646
location = add_unc(location)
47-
with codecs.open(location, 'rb', encoding='utf-8-sig', errors='replace') as csvfile:
47+
with open(location, mode='r', encoding='utf-8-sig', errors='replace') as csvfile:
4848
reader = csv.reader(csvfile)
4949
columns = next(reader)
5050
columns = [col for col in columns]

src/attributecode/model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def _validate(self, *args, **kwargs):
585585
try:
586586
# TODO: we have lots the location by replacing it with a text
587587
location = add_unc(location)
588-
with io.open(location, encoding='utf-8', errors='replace') as txt:
588+
with open(location, encoding='utf-8', errors='replace') as txt:
589589
text = txt.read()
590590
self.value[path] = text
591591
except Exception as e:
@@ -995,7 +995,7 @@ def load(self, location):
995995
errors = []
996996
try:
997997
loc = add_unc(loc)
998-
with io.open(loc, encoding='utf-8', errors='replace') as txt:
998+
with open(loc, encoding='utf-8', errors='replace') as txt:
999999
input_text = txt.read()
10001000
if not input_text:
10011001
msg = 'ABOUT file is empty: %(location)r'
@@ -1235,7 +1235,7 @@ def dump(self, location, lic_dict=None):
12351235
if on_windows:
12361236
about_file_path = add_unc(about_file_path)
12371237

1238-
with io.open(about_file_path, mode='w', encoding='utf-8', errors='replace') as dumped:
1238+
with open(about_file_path, mode='w', encoding='utf-8', errors='replace') as dumped:
12391239
dumped.write(genereated_tk_version)
12401240
dumped.write(self.dumps(lic_dict))
12411241

@@ -1246,7 +1246,7 @@ def dump_android_notice(self, path, context):
12461246
if on_windows:
12471247
path = add_unc(path)
12481248

1249-
with io.open(path, mode='w', encoding='utf-8', errors='replace') as dumped:
1249+
with open(path, mode='w', encoding='utf-8', errors='replace') as dumped:
12501250
dumped.write(context)
12511251

12521252
def android_module_license(self, about_parent_path):
@@ -1317,7 +1317,7 @@ def dump_lic(self, location, license_dict):
13171317
license_name, license_filename, license_context, license_url, spdx_license_key = license_dict[lic_key]
13181318
license_info = (lic_key, license_name, license_filename, license_context, license_url, spdx_license_key)
13191319
license_key_name_context_url.append(license_info)
1320-
with io.open(license_path, mode='w', encoding='utf-8', newline='\n', errors='replace') as lic:
1320+
with open(license_path, mode='w', encoding='utf-8', newline='\n', errors='replace') as lic:
13211321
lic.write(license_context)
13221322
else:
13231323
# Invalid license issue is already handled
@@ -1372,7 +1372,7 @@ def collect_abouts_license_expression(location):
13721372
for loc in about_locations:
13731373
try:
13741374
loc = add_unc(loc)
1375-
with io.open(loc, encoding='utf-8', errors='replace') as txt:
1375+
with open(loc, encoding='utf-8', errors='replace') as txt:
13761376
input_text = txt.read()
13771377
# saneyaml.load() will have parsing error if the input has
13781378
# tab value. Therefore, we should check if the input contains
@@ -1627,12 +1627,12 @@ def write_output(abouts, location, format): # NOQA
16271627
save_as_excel(location, about_dicts)
16281628

16291629
def save_as_json(location, about_dicts):
1630-
with io.open(location, mode='w') as output_file:
1630+
with open(location, mode='w') as output_file:
16311631
data = util.format_about_dict_for_json_output(about_dicts)
16321632
output_file.write(json.dumps(data, indent=2))
16331633

16341634
def save_as_csv(location, about_dicts, field_names):
1635-
with io.open(location, mode='w', encoding='utf-8', newline='', errors='replace') as output_file:
1635+
with open(location, mode='w', encoding='utf-8', newline='', errors='replace') as output_file:
16361636
writer = csv.DictWriter(output_file, field_names)
16371637
writer.writeheader()
16381638
csv_formatted_list = util.format_about_dict_output(about_dicts)

src/attributecode/transform.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def from_file(cls, location):
282282
Load and return a Transformer instance from a YAML configuration file at
283283
`location`.
284284
"""
285-
with io.open(location, encoding='utf-8', errors='replace') as conf:
285+
with open(location, encoding='utf-8', errors='replace') as conf:
286286
data = saneyaml.load(replace_tab_with_spaces(conf.read()))
287287
return cls(
288288
field_renamings=data.get('field_renamings', {}),
@@ -381,7 +381,7 @@ def read_csv_rows(location):
381381
"""
382382
Yield rows (as a list of values) from a CSV file at `location`.
383383
"""
384-
with io.open(location, encoding='utf-8', errors='replace') as csvfile:
384+
with open(location, encoding='utf-8', errors='replace') as csvfile:
385385
reader = csv.reader(csvfile)
386386
for row in reader:
387387
yield row
@@ -391,7 +391,7 @@ def read_json(location):
391391
"""
392392
Yield rows (as a list of values) from a CSV file at `location`.
393393
"""
394-
with io.open(location, encoding='utf-8', errors='replace') as jsonfile:
394+
with open(location, encoding='utf-8', errors='replace') as jsonfile:
395395
return json.load(jsonfile)
396396

397397

@@ -400,7 +400,7 @@ def write_csv(location, data, field_names): # NOQA
400400
Write a CSV file at `location` the `data` list of ordered dicts using the
401401
`field_names`.
402402
"""
403-
with io.open(location, 'w', encoding='utf-8', newline='\n', errors='replace') as csvfile:
403+
with open(location, 'w', encoding='utf-8', newline='\n', errors='replace') as csvfile:
404404
writer = csv.DictWriter(csvfile, fieldnames=field_names)
405405
writer.writeheader()
406406
writer.writerows(data)
@@ -429,7 +429,7 @@ def read_excel(location):
429429
while index <= max_col:
430430
value = sheet_obj.cell(row=1, column=index).value
431431
if value in col_keys:
432-
msg = 'Duplicated column name, ' + str(value) + ', detected.'
432+
msg = 'Duplicated column name, ' + str(value) + ', detected.'
433433
errors.append(Error(CRITICAL, msg))
434434
return errors, results
435435
if value in mapping_dict:

src/attributecode/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def load_csv(location):
262262
for each row.
263263
"""
264264
results = []
265-
with codecs.open(location, mode='rb', encoding='utf-8-sig',
265+
with open(location, mode='r', encoding='utf-8-sig',
266266
errors='replace') as csvfile:
267267
for row in csv.DictReader(csvfile):
268268
# convert all the column keys to lower case
@@ -689,7 +689,7 @@ def load_excel(location):
689689
while index <= max_col:
690690
value = sheet_obj.cell(row=1, column=index).value
691691
if value in col_keys:
692-
msg = 'Duplicated column name, ' + str(value) + ', detected.'
692+
msg = 'Duplicated column name, ' + str(value) + ', detected.'
693693
errors.append(Error(CRITICAL, msg))
694694
return errors, results
695695
if value in mapping_dict:
@@ -721,7 +721,7 @@ def write_licenses(lic_dict, location):
721721
try:
722722
for lic in lic_dict:
723723
output_location = posixpath.join(loc, lic)
724-
with io.open(output_location, 'w', encoding='utf-8', errors='replace') as out:
724+
with open(output_location, 'w', encoding='utf-8', errors='replace') as out:
725725
out.write(lic_dict[lic])
726726
except Exception as e:
727727
msg = str(e)

tests/test_attrib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_check_template_all_builtin_templates_are_valid(self):
6363
builtin_templates_dir = os.path.dirname(attrib.DEFAULT_TEMPLATE_FILE)
6464
for template in os.listdir(builtin_templates_dir):
6565
template_loc = os.path.join(builtin_templates_dir, template)
66-
with io.open(template_loc, 'r', encoding='utf-8', errors='replace') as tmpl:
66+
with open(template_loc, 'r', encoding='utf-8', errors='replace') as tmpl:
6767
template = tmpl.read()
6868
try:
6969
assert None == attrib.check_template(template)

tests/test_cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_report_errors_can_write_to_logfile():
156156
result_file = get_temp_file()
157157
_ec = cmd.report_errors(errors, quiet=False, verbose=True,
158158
log_file_loc=result_file)
159-
with io.open(result_file, 'r', encoding='utf-8', errors='replace') as rf:
159+
with open(result_file, 'r', encoding='utf-8', errors='replace') as rf:
160160
result = rf.read()
161161
expected = [
162162
'Command completed with 6 errors or warnings.',

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def get_unicode_content(location):
9393
"""
9494
Read file at location and return a unicode string.
9595
"""
96-
with io.open(location, encoding='utf-8', errors='replace') as doc:
96+
with open(location, encoding='utf-8', errors='replace') as doc:
9797
return doc.read()
9898

9999

0 commit comments

Comments
 (0)