Skip to content

Commit b6f0305

Browse files
committed
#403 - Restore fixes for incorrect data for boolean field
1 parent 289dc8c commit b6f0305

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/attributecode/model.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161
from attributecode import saneyaml
6262
from attributecode import util
6363
from attributecode.util import add_unc
64+
from attributecode.util import boolean_fields
6465
from attributecode.util import copy_license_notice_files
6566
from attributecode.util import csv
6667
from attributecode.util import filter_errors
6768
from attributecode.util import is_valid_name
6869
from attributecode.util import on_windows
70+
from attributecode.util import wrap_boolean_value
6971
from attributecode.util import UNC_PREFIX
7072
from attributecode.util import ungroup_licenses
7173
from attributecode.util import unique
@@ -119,7 +121,7 @@ def validate(self, *args, **kwargs):
119121
# present fields should have content ...
120122
# The boolean value can be True, False and None
121123
# The value True or False is the content of boolean fields
122-
if not self.has_content:
124+
if not name in boolean_fields and not self.has_content:
123125
# ... especially if required
124126
if self.required:
125127
msg = u'Field %(name)s is required and empty'
@@ -585,8 +587,7 @@ def _validate(self, *args, **kwargs):
585587
self.value = None
586588
elif flag is None:
587589
name = self.name
588-
msg = (u'Field %(name)s: field is empty. '
589-
u'Defaulting flag to no.' % locals())
590+
msg = (u'Field %(name)s: field is present but empty. ' % locals())
590591
errors.append(Error(INFO, msg))
591592
self.value = None
592593
else:
@@ -794,8 +795,21 @@ def __eq__(self, other):
794795
def all_fields(self):
795796
"""
796797
Return the list of all Field objects.
798+
If with_absent, include absent (not present) fields.
799+
If with_empty, include empty fields.
797800
"""
798-
return list(self.fields.values()) + list(self.custom_fields.values())
801+
all_fields = []
802+
803+
for field in list(self.fields.values()) + list(self.custom_fields.values()):
804+
if field.required:
805+
all_fields.append(field)
806+
else:
807+
if field.present:
808+
if field.value:
809+
all_fields.append(field)
810+
elif field.name in boolean_fields and not field.value == None:
811+
all_fields.append(field)
812+
return all_fields
799813

800814
def as_dict(self):
801815
"""
@@ -926,6 +940,10 @@ def load(self, location):
926940
loc = add_unc(loc)
927941
with io.open(loc, encoding='utf-8') as txt:
928942
input_text = txt.read()
943+
# The 'Yes' and 'No' will be converted to 'True' and 'False' in the yaml.load()
944+
# Therefore, we need to wrap the original value in quote to prevent
945+
# the conversion
946+
input = wrap_boolean_value(input_text)
929947
# FIXME: this should be done in the commands, not here
930948
"""
931949
The running_inventory defines if the current process is 'inventory' or not.
@@ -937,7 +955,7 @@ def load(self, location):
937955
and then join with the 'about_resource'
938956
"""
939957
running_inventory = True
940-
data = saneyaml.load(input_text, allow_duplicate_keys=False)
958+
data = saneyaml.load(input, allow_duplicate_keys=False)
941959
errs = self.load_dict(data, base_dir, running_inventory)
942960
errors.extend(errs)
943961
except Exception as e:
@@ -1127,7 +1145,6 @@ def collect_inventory(location):
11271145
msg = (about_file_path + ": " + message)
11281146
errors.append(Error(severity, msg))
11291147
abouts.append(about)
1130-
11311148
return unique(errors), abouts
11321149

11331150

src/attributecode/util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
on_windows = 'win32' in sys.platform
5353

54+
# boolean field name
55+
boolean_fields = ['redistribute', 'attribute', 'track_change', 'modified', 'internal_use_only']
5456

5557
def to_posix(path):
5658
"""
@@ -123,6 +125,23 @@ def check_file_names(paths):
123125
seen[path] = orig_path
124126
return errors
125127

128+
def wrap_boolean_value(context):
129+
updated_context = ''
130+
for line in context.splitlines():
131+
"""
132+
wrap the boolean value in quote
133+
"""
134+
key = line.partition(':')[0]
135+
value = line.partition(':')[2].strip()
136+
value = '"' + value + '"'
137+
print(value)
138+
if key in boolean_fields and not value == "":
139+
updated_context += key + ': ' + value + '\n'
140+
else:
141+
updated_context += line + '\n'
142+
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
143+
print(updated_context)
144+
return updated_context
126145

127146
# TODO: rename to normalize_path
128147
def get_absolute(location):

0 commit comments

Comments
 (0)