get_form_fields.py 1.5 KB

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