Skip to content

Commit 9f9930e

Browse files
committed
Fixed #408 - tab value cause error in saneyaml.load()
* Replace tab ('\t') with spaces to prevent saneyaml load error
1 parent 564e7d1 commit 9f9930e

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/attributecode/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from attributecode.util import filter_errors
6868
from attributecode.util import is_valid_name
6969
from attributecode.util import on_windows
70+
from attributecode.util import replace_tab_with_spaces
7071
from attributecode.util import wrap_boolean_value
7172
from attributecode.util import UNC_PREFIX
7273
from attributecode.util import ungroup_licenses
@@ -934,7 +935,8 @@ def load(self, location):
934935
# The 'Yes' and 'No' will be converted to 'True' and 'False' in the yaml.load()
935936
# Therefore, we need to wrap the original value in quote to prevent
936937
# the conversion
937-
input = wrap_boolean_value(input_text)
938+
pre_input = wrap_boolean_value(input_text)
939+
input = replace_tab_with_spaces(pre_input)
938940
# FIXME: this should be done in the commands, not here
939941
"""
940942
The running_inventory defines if the current process is 'inventory' or not.

src/attributecode/util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33
# ============================================================================
4-
# Copyright (c) 2013-2018 nexB Inc. http://www.nexb.com/ - All rights reserved.
4+
# Copyright (c) 2013-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
77
# You may obtain a copy of the License at
@@ -140,6 +140,15 @@ def wrap_boolean_value(context):
140140
updated_context += line + '\n'
141141
return updated_context
142142

143+
def replace_tab_with_spaces(context):
144+
updated_context = ''
145+
for line in context.splitlines():
146+
"""
147+
Replace tab with 4 spaces
148+
"""
149+
updated_context += line.replace('\t', ' ') + '\n'
150+
return updated_context
151+
143152
# TODO: rename to normalize_path
144153
def get_absolute(location):
145154
"""

tests/test_model.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2014-2018 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -37,6 +37,7 @@
3737
from attributecode.util import add_unc
3838
from attributecode.util import load_csv
3939
from attributecode.util import to_posix
40+
from attributecode.util import replace_tab_with_spaces
4041

4142
from testing_utils import extract_test_loc
4243
from testing_utils import get_temp_file
@@ -303,6 +304,19 @@ def test_saneyaml_load_can_parse_verbatim_text_unstripped(self):
303304

304305
assert expected == list(result.items())
305306

307+
def test_saneyaml_load_can_parse_verbatim_tab_text_unstripped(self):
308+
test = get_test_content('test_model/parse/continuation_verbatim_with_tab.about')
309+
data = replace_tab_with_spaces(test)
310+
result = saneyaml.load(data)
311+
312+
expected = [
313+
(u'single_line', u'optional'),
314+
(u'other_field', u'value'),
315+
(u'multi_line', u'This is a long description\nwith tab.\n')
316+
]
317+
318+
assert expected == list(result.items())
319+
306320
def test_saneyaml_load_report_error_for_invalid_field_name(self):
307321
test = get_test_content('test_model/parse/invalid_names.about')
308322
try:
@@ -699,8 +713,6 @@ def test_About_dumps_does_all_non_empty_present_fields(self):
699713
Error(INFO, 'Field custom2 is a custom field.'),
700714
Error(INFO, 'Field custom2 is present but empty.')
701715
]
702-
print("########################################")
703-
print(a.errors)
704716
assert sorted(expected_error) == sorted(a.errors)
705717

706718
expected = '''about_resource: .
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
single_line: optional
2+
other_field: value
3+
4+
5+
multi_line: |
6+
This is a long description
7+
with tab.

0 commit comments

Comments
 (0)