registry.py 3.7 KB

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