get_form_fields.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ## IMPORTS
  2. # Utilities to read/write csv files
  3. import json
  4. import re
  5. def getFormFields(mapfilename):
  6. with open (mapfilename) as mapfile:
  7. triple_blocks = json.load(mapfile)
  8. all_csvs = []
  9. for block in triple_blocks:
  10. all_csvs = all_csvs + extractFields(block)
  11. all_csvs_filtered = []
  12. for csv in all_csvs:
  13. if csv in all_csvs_filtered:
  14. continue
  15. all_csvs_filtered.append(csv)
  16. return all_csvs_filtered
  17. def extractFields(entry: dict):
  18. try:
  19. subject_val = entry['subject']['value'] if type(entry['subject']) is dict else entry['subject']
  20. all_csvs = list( csvsFromVals(subject_val) )
  21. objs = map(lambda el: el['object'], entry['content'])
  22. for obj in objs:
  23. val = obj['value'] if type(obj) is dict else obj
  24. obj_csvs = csvsFromVals(val)
  25. all_csvs = all_csvs + list(obj_csvs)
  26. return all_csvs
  27. except:
  28. raise Exception('Malformed Configuration File')
  29. def csvsFromVals(value: str):
  30. val_parts = re.split('#', value)
  31. val_csvs = list( filter(lambda str1: str1.startswith('csv:'), val_parts) )
  32. val_csvs = map(lambda str1: str1.replace('csv:', ''), val_csvs)
  33. return val_csvs