__init__.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. This package contains tools used across the various modules, these tools actually "do the work"
  3. We intend to have these tools be Object-Oriented by design so as to not run into any concurrency issues
  4. """
  5. from . import file, document
  6. from healthcareio import x12
  7. from multiprocessing import Process
  8. # class X12Engine(Process):
  9. # def __init__(self,**_args):
  10. # """
  11. # :files group of files to be processed
  12. # """
  13. # self.files = _args['files']
  14. # self._cHandler = file.Content()
  15. # self._dHandler = document.Builder(plugins=_args['plugins'],parents=_args['plugins'])
  16. # def run(self):
  17. # """
  18. # This function performs parsing given
  19. # """
  20. # for _location in self.files :
  21. # _content = self._cHandler.read(_location)
  22. # _content = self._cHandler.split(_content)
  23. # pass
  24. def merge (_x,_y):
  25. """
  26. This function will merge two objects _x, _y
  27. """
  28. _zcols = list(set(_x.keys()) & set(_y.keys())) #--common columns
  29. if _zcols :
  30. _out = dict(_x,**{})
  31. for _key in list(_y.keys()) :
  32. if _key not in _zcols and _key:
  33. _out[_key] = _y[_key]
  34. else:
  35. if type(_out[_key]) == list :
  36. for value in _y[_key] :
  37. if value not in _out[_key] :
  38. _out[_key].append(value)
  39. # _out[_key] += _y[_key]
  40. elif type(_out[_key]) == dict:
  41. _out[_key] = dict(_out[_key],**_y[_key])
  42. else:
  43. _out[_key] = _y[_key]
  44. return _out
  45. else:
  46. return dict(_x,**_y)
  47. def template(**_args) :
  48. """
  49. This function generates an object template to be used in object assignment and export functionalities
  50. We chose to proceed in this manner so as to enforce consistency of the parser
  51. :plugins {*,837,835} with element and pointers associated
  52. """
  53. _plugins = _args['plugins']
  54. _object = {'837':{},'835':{}}
  55. for _x12 in _plugins :
  56. _pointers = _plugins[_x12]
  57. for _element in _pointers :
  58. _meta = _pointers[_element].meta
  59. _values = _meta['map'].values() if 'map' in _meta else _meta['columns']
  60. #
  61. # where do the attributes go ..
  62. #
  63. _attr = []
  64. for _item in list(_values) :
  65. if type(_item) == list :
  66. _attr = _attr + _item
  67. else:
  68. _attr.append(_item)
  69. _field = []
  70. if 'field' in _meta or 'container' in _meta :
  71. _field = _meta['field'] if 'field' in _meta else _meta['container']
  72. if 'anchor' in _meta : #-- No parents are expected
  73. _field = _meta['anchor'].values()
  74. elif _meta['parent'] :
  75. #
  76. # It means the attributes will be
  77. _parentPlug = x12.plugins.filter(elements=[_meta['parent']],plugins=_plugins)
  78. _pid = list(_parentPlug.keys())[0]
  79. _parentMeta = _parentPlug[_pid][_meta['parent']].meta
  80. _attr = _attr + list(_parentMeta['map'].values()) if 'map' in _parentMeta else _parentMeta['columns']
  81. if 'anchor' in _parentMeta :
  82. _field = list(_parentMeta['anchor'].values())
  83. _field = [_field] if type(_field) == str else _field
  84. _attr = dict.fromkeys(_attr,'')
  85. if not _field :
  86. _info = (_attr)
  87. else:
  88. _info = (dict.fromkeys(_field,_attr))
  89. if _x12 == '*' :
  90. _object['837']= merge(_object['837'], _info)
  91. _object['835']= merge (_object['835'], _info)
  92. else:
  93. _object[_x12] = merge(_object[_x12],_info)
  94. return _object
  95. def getPrimaryKey(**_args):
  96. """
  97. :plugins plugins that are loaded
  98. :x12 837|835
  99. """
  100. _plugins=_args['plugins']
  101. _pointer = x12.plugins.filter(elements=['CLM'],plugins=_plugins)
  102. _keys = {}
  103. for _element in ['CLM','CLP'] :
  104. _pointer = x12.plugins.filter(elements=[_element],plugins=_plugins)
  105. if not _pointer :
  106. continue
  107. _pointer = list(_pointer.values())[0]
  108. _meta = _pointer[_element].meta
  109. _name = _meta['map'][1] if 'map' in _meta else _meta['columns'][0]
  110. _id = '837' if _element == 'CLM' else '835'
  111. _keys[_id] = _name
  112. return _keys[_args['x12']]
  113. # def template(**_args) :
  114. # """
  115. # This function generates an object template to be used in object assignment and export functionalities
  116. # We chose to proceed in this manner so as to enforce consistency of the parser
  117. # :plugins {*,837,835} with element and pointers associated
  118. # """
  119. # _plugins = _args['plugins']
  120. # _object = {'837':{},'835':{}}
  121. # for _x12 in _plugins :
  122. # _pointers = _plugins[_x12]
  123. # for _element in _pointers :
  124. # _meta = _pointers[_element].meta
  125. # _values = _meta['map'].values() if 'map' in _meta else _meta['columns']
  126. # #
  127. # # where do the attributes go ..
  128. # #
  129. # _attr = []
  130. # for _item in list(_values) :
  131. # if type(_item) == list :
  132. # _attr = _attr + _item
  133. # else:
  134. # _attr.append(_item)
  135. # _field = []
  136. # if 'field' in _meta or 'container' in _meta :
  137. # _field = _meta['field'] if 'field' in _meta else _meta['container']
  138. # if 'anchor' in _meta : #-- No parents are expected
  139. # _field = _meta['anchor'].values()
  140. # elif _meta['parent'] :
  141. # #
  142. # # It means the attributes will be
  143. # _parentPlug = filter(elements=[_meta['parent']],plugins=_plugins)
  144. # _pid = list(_parentPlug.keys())[0]
  145. # _parentMeta = _parentPlug[_pid][_meta['parent']].meta
  146. # _attr = _attr + list(_parentMeta['map'].values()) if 'map' in _parentMeta else _parentMeta['columns']
  147. # if 'anchor' in _parentMeta :
  148. # _field = list(_parentMeta['anchor'].values())
  149. # _field = [_field] if type(_field) == str else _field
  150. # _attr = dict.fromkeys(_attr,'')
  151. # if not _field :
  152. # _info = (_attr)
  153. # else:
  154. # _info = (dict.fromkeys(_field,_attr))
  155. # if _x12 == '*' :
  156. # _object['837']= merge(_object['837'], _info)
  157. # _object['835']= merge (_object['835'], _info)
  158. # else:
  159. # _object[_x12] = merge(_object[_x12],_info)
  160. # return _object