registry.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import json
  3. from info import __version__
  4. import copy
  5. import transport
  6. import importlib
  7. import importlib.util
  8. import shutil
  9. from io import StringIO
  10. """
  11. This class manages data from the registry and allows (read only)
  12. @TODO: add property to the DATA attribute
  13. """
  14. REGISTRY_PATH=os.sep.join([os.environ.get('HOME','USERPROFILE'),'.data-transport'])
  15. #
  16. # This path can be overriden by an environment variable ...
  17. #
  18. if 'DATA_TRANSPORT_REGISTRY_PATH' in os.environ :
  19. REGISTRY_PATH = os.environ['DATA_TRANSPORT_REGISTRY_PATH']
  20. REGISTRY_FILE= 'transport-registry.json'
  21. DATA = {}
  22. def isloaded ():
  23. return DATA not in [{},None]
  24. def exists (path=REGISTRY_PATH,_file=REGISTRY_FILE) :
  25. """
  26. This function determines if there is a registry at all
  27. """
  28. p = os.path.exists(path)
  29. q = os.path.exists( os.sep.join([path,_file]))
  30. return p and q
  31. def load (_path=REGISTRY_PATH,_file=REGISTRY_FILE):
  32. global DATA
  33. if exists(_path) :
  34. path = os.sep.join([_path,_file])
  35. f = open(path)
  36. DATA = json.loads(f.read())
  37. f.close()
  38. def init (email,path=REGISTRY_PATH,override=False,_file=REGISTRY_FILE):
  39. """
  40. Initializing the registry and will raise an exception in the advent of an issue
  41. """
  42. p = '@' in email
  43. q = False if '.' not in email else email.split('.')[-1] in ['edu','com','io','ai','org']
  44. if p and q :
  45. _config = {"email":email,'version':__version__}
  46. if not os.path.exists(path):
  47. os.makedirs(path)
  48. filename = os.sep.join([path,_file])
  49. if not os.path.exists(filename) or override == True :
  50. f = open(filename,'w')
  51. f.write( json.dumps(_config))
  52. f.close()
  53. # _msg = f"""{CHECK_MARK} Successfully wrote configuration to {path} from {email}"""
  54. else:
  55. raise Exception (f"""Unable to write configuration, Please check parameters (or help) and try again""")
  56. else:
  57. raise Exception (f"""Invalid Input, {email} is not well formatted, provide an email with adequate format""")
  58. def lookup (label):
  59. global DATA
  60. return label in DATA
  61. has = lookup
  62. def get (label='default') :
  63. global DATA
  64. return copy.copy(DATA[label]) if label in DATA else {}
  65. def set (label, auth_file, default=False,path=REGISTRY_PATH) :
  66. """
  67. This function will add a label (auth-file data) into the registry and can set it as the default
  68. """
  69. if label == 'default' :
  70. raise Exception ("""Invalid label name provided, please change the label name and use the switch""")
  71. reg_file = os.sep.join([path,REGISTRY_FILE])
  72. if os.path.exists(path) and os.path.exists(reg_file):
  73. if type(auth_file) == str and os.path.exists (auth_file) :
  74. f = open(auth_file)
  75. elif type(auth_file) == StringIO:
  76. f = auth_file
  77. _info = json.loads(f.read())
  78. f.close()
  79. f = open(reg_file)
  80. _config = json.loads(f.read())
  81. f.close()
  82. #
  83. # set the proposed label
  84. _object = transport.factory.instance(**_info)
  85. if _object :
  86. _config[label] = _info
  87. if default :
  88. _config['default'] = _info
  89. #
  90. # now we need to write this to the location
  91. f = open(reg_file,'w')
  92. f.write(json.dumps(_config))
  93. f.close()
  94. else:
  95. raise Exception( f"""Unable to load file locate at {path},\nLearn how to generate auth-file with wizard found at https://healthcareio.the-phi.com/data-transport""")
  96. pass
  97. else:
  98. pass
  99. pass