__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 template(**_args) :
  96. # """
  97. # This function generates an object template to be used in object assignment and export functionalities
  98. # We chose to proceed in this manner so as to enforce consistency of the parser
  99. # :plugins {*,837,835} with element and pointers associated
  100. # """
  101. # _plugins = _args['plugins']
  102. # _object = {'837':{},'835':{}}
  103. # for _x12 in _plugins :
  104. # _pointers = _plugins[_x12]
  105. # for _element in _pointers :
  106. # _meta = _pointers[_element].meta
  107. # _values = _meta['map'].values() if 'map' in _meta else _meta['columns']
  108. # #
  109. # # where do the attributes go ..
  110. # #
  111. # _attr = []
  112. # for _item in list(_values) :
  113. # if type(_item) == list :
  114. # _attr = _attr + _item
  115. # else:
  116. # _attr.append(_item)
  117. # _field = []
  118. # if 'field' in _meta or 'container' in _meta :
  119. # _field = _meta['field'] if 'field' in _meta else _meta['container']
  120. # if 'anchor' in _meta : #-- No parents are expected
  121. # _field = _meta['anchor'].values()
  122. # elif _meta['parent'] :
  123. # #
  124. # # It means the attributes will be
  125. # _parentPlug = filter(elements=[_meta['parent']],plugins=_plugins)
  126. # _pid = list(_parentPlug.keys())[0]
  127. # _parentMeta = _parentPlug[_pid][_meta['parent']].meta
  128. # _attr = _attr + list(_parentMeta['map'].values()) if 'map' in _parentMeta else _parentMeta['columns']
  129. # if 'anchor' in _parentMeta :
  130. # _field = list(_parentMeta['anchor'].values())
  131. # _field = [_field] if type(_field) == str else _field
  132. # _attr = dict.fromkeys(_attr,'')
  133. # if not _field :
  134. # _info = (_attr)
  135. # else:
  136. # _info = (dict.fromkeys(_field,_attr))
  137. # if _x12 == '*' :
  138. # _object['837']= merge(_object['837'], _info)
  139. # _object['835']= merge (_object['835'], _info)
  140. # else:
  141. # _object[_x12] = merge(_object[_x12],_info)
  142. # return _object