3737from attributecode .util import add_unc
3838from attributecode .util import csv
3939from attributecode .util import file_fields
40+ from attributecode .util import invalid_chars
4041from attributecode .util import to_posix
4142from attributecode .util import UNC_PREFIX_POSIX
4243from attributecode .util import unique
@@ -77,42 +78,42 @@ def check_duplicated_columns(location):
7778 errors .append (Error (ERROR , msg ))
7879 return unique (errors )
7980
80-
81- def check_duplicated_about_resource (inventory_dict ):
81+ def check_duplicated_about_resource (arp , arp_list ):
8282 """
8383 Return a list of errors for duplicated about_resource in a CSV file at location.
8484 """
85- arp_list = []
86- errors = []
87- for component in inventory_dict :
88- # Ignore all the empty path
89- if component ['about_resource' ]:
90- if component ['about_resource' ] in arp_list :
91- msg = ("The input has duplicated values in 'about_resource' "
92- "field: " + component ['about_resource' ])
93- errors .append (Error (CRITICAL , msg ))
94- else :
95- arp_list .append (component ['about_resource' ])
96- return errors
85+ if arp in arp_list :
86+ msg = ("The input has duplicated values in 'about_resource' "
87+ "field: " + arp )
88+ return Error (CRITICAL , msg )
89+ return ''
9790
98-
99- def check_newline_in_file_field (inventory_dict ):
91+ def check_newline_in_file_field (component ):
10092 """
10193 Return a list of errors for newline characters detected in *_file fields.
10294 """
10395 errors = []
104- for component in inventory_dict :
105- for k in component .keys ():
106- if k in file_fields :
107- try :
108- if '\n ' in component [k ]:
109- msg = ("New line character detected in '%s' for '%s' which is not supported."
110- "\n Please use ',' to declare multiple files." ) % (k , component ['about_resource' ])
111- errors .append (Error (CRITICAL , msg ))
112- except :
113- pass
96+ for k in component .keys ():
97+ if k in file_fields :
98+ try :
99+ if '\n ' in component [k ]:
100+ msg = ("New line character detected in '%s' for '%s' which is not supported."
101+ "\n Please use ',' to declare multiple files." ) % (k , component ['about_resource' ])
102+ errors .append (Error (CRITICAL , msg ))
103+ except :
104+ pass
114105 return errors
115106
107+ def check_about_resource_filename (arp ):
108+ """
109+ Return error for invalid/non-support about_resource's filename or
110+ empty string if no error is found.
111+ """
112+ if invalid_chars (arp ):
113+ msg = ("Invalid characters present in 'about_resource' "
114+ "field: " + arp )
115+ return (Error (CRITICAL , msg ))
116+ return ''
116117
117118# TODO: this should be either the CSV or the ABOUT files but not both???
118119def load_inventory (location , base_dir , reference_dir = None ):
@@ -139,15 +140,26 @@ def load_inventory(location, base_dir, reference_dir=None):
139140 inventory = util .load_json (location )
140141
141142 try :
142- # FIXME: this should not be done here.
143- dup_about_resource_err = check_duplicated_about_resource (inventory )
144- if dup_about_resource_err :
145- errors .extend (dup_about_resource_err )
146- return errors , abouts
147- newline_in_file = check_newline_in_file_field (inventory )
148- if newline_in_file :
149- errors .extend (newline_in_file )
143+ arp_list = []
144+ errors = []
145+ for component in inventory :
146+ arp = component ['about_resource' ]
147+ dup_err = check_duplicated_about_resource (arp , arp_list )
148+ if dup_err :
149+ errors .append (dup_err )
150+ else :
151+ arp_list .append (arp )
152+
153+ newline_in_file_err = check_newline_in_file_field (component )
154+ for err in newline_in_file_err :
155+ errors .append (err )
156+
157+ invalid_about_filename = check_about_resource_filename (arp )
158+ if invalid_about_filename :
159+ errors .append (invalid_about_filename )
160+ if errors :
150161 return errors , abouts
162+
151163 except Exception as e :
152164 # TODO: why catch ALL Exception
153165 msg = "The essential field 'about_resource' is not found in the <input>"
0 commit comments