@@ -321,6 +321,35 @@ def get_mapping(location=None):
321321 return mapping
322322
323323
324+ def get_output_mapping (location ):
325+ """
326+ Return a mapping of About key names to user key names by reading the
327+ user's input file from location. The format of the user key names will
328+ NOT be formatted (i.e. keys will NOT be forced to convert to lower case)
329+ """
330+ if not os .path .exists (location ):
331+ return {}
332+
333+ mapping = {}
334+ try :
335+ with open (location ) as mapping_file :
336+ for line in mapping_file :
337+ if not line or not line .strip () or line .strip ().startswith ('#' ):
338+ continue
339+
340+ if ':' in line :
341+ key , sep , value = line .partition (':' )
342+ user_key = key .strip ()
343+ about_key = value .strip ()
344+ mapping [about_key ] = user_key
345+
346+ except Exception as e :
347+ print (repr (e ))
348+ print ('Cannot open or process file at %(location)r.' % locals ())
349+ # FIXME: this is rather brutal
350+ sys .exit (errno .EACCES )
351+ return mapping
352+
324353def apply_mapping (abouts , alternate_mapping = None ):
325354 """
326355 Given a list of About data dictionaries and a dictionary of
@@ -578,3 +607,32 @@ def inventory_filter(abouts, filter_dict):
578607 # The current about object does not have the defined attribute
579608 continue
580609 return updated_abouts
610+
611+
612+ def update_fieldnames (fieldnames , mapping_output ):
613+ map = get_output_mapping (mapping_output )
614+ updated_header = []
615+ for name in fieldnames :
616+ try :
617+ updated_header .append (map [name ])
618+ except :
619+ updated_header .append (name )
620+ return updated_header
621+
622+
623+ def update_about_dictionary_keys (about_dictionary_list , mapping_output ):
624+ output_map = get_output_mapping (mapping_output )
625+ updated_dict_list = []
626+ for element in about_dictionary_list :
627+ updated_ordered_dict = OrderedDict ()
628+ for about_key , value in element .items ():
629+ update_key = False
630+ for custom_key in output_map :
631+ if about_key == custom_key :
632+ update_key = True
633+ updated_ordered_dict [output_map [custom_key ]] = value
634+ break
635+ if not update_key :
636+ updated_ordered_dict [about_key ] = value
637+ updated_dict_list .append (updated_ordered_dict )
638+ return updated_dict_list
0 commit comments