parser.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """
  2. This class refactors the default parsing class (better & streamlined implementation)
  3. The class will rely on the new plug/play architectural style perform parsing
  4. """
  5. from multiprocessing import Process, RLock
  6. import os
  7. import json
  8. # from healthcareio.x12.util
  9. from healthcareio import x12
  10. import numpy as np
  11. import transport
  12. import copy
  13. # from healthcareio.x12.util import file as File, document as Document
  14. import time
  15. class BasicParser (Process) :
  16. def __init__(self,**_args):
  17. super().__init__()
  18. self._plugins = _args['plugins']
  19. self._parents = _args['parents']
  20. self._files = _args['files']
  21. self._store = _args['store']
  22. def apply(self,**_args):
  23. _content = _args['content']
  24. _filetype = _args['x12']
  25. _doc = _args['document'] #{}
  26. _documentHandler = x12.util.document.Builder(plugins = self._plugins,parents=self._parents)
  27. try:
  28. for _row in _content :
  29. # _data = None
  30. _data,_meta = _documentHandler.bind(row=_row,x12=_filetype)
  31. if _data and _meta :
  32. _doc = _documentHandler.build(data=_data,document=_doc,meta=_meta,row=_row)
  33. # print (['*** ',_doc])
  34. pass
  35. except Exception as e:
  36. #
  37. # Log something here ....
  38. print (_row)
  39. print (e)
  40. # print (_row,_doc.keys())
  41. pass
  42. return _doc
  43. def run(self):
  44. _handleContent = x12.util.file.Content()
  45. _handleDocument = x12.util.document.Builder(plugins = self._plugins,parents=self._parents)
  46. _template = _plugins,_parents = x12.util.template(plugins=self._plugins)
  47. for _absolute_path in self._files :
  48. try:
  49. _content = _handleContent.read(filename=_absolute_path)
  50. _content,_filetype = _handleContent.split(_content)
  51. #
  52. # The first row is the header (it will be common to all claims)
  53. _header = copy.deepcopy(_template[_filetype])
  54. _header = self.apply(content=_content[0],x12=_filetype, document=_header)
  55. _docs = []
  56. for _rawclaim in _content[1:] :
  57. _document = copy.deepcopy(_header) #copy.deepcopy(_template[_filetype])
  58. # _document = dict(_document,**_header)
  59. if type(_absolute_path) == str:
  60. _document['filename'] = _absolute_path
  61. _doc = self.apply(content=_rawclaim,x12=_filetype, document=_document)
  62. if _doc :
  63. #
  64. # @TODO: Make sure the test here is the existence of the primary key
  65. # _doc = _handleDocument.merge(_doc,_header)
  66. _docs.append(_doc)
  67. else:
  68. # print (['wtf ...',_rawclaim])
  69. pass
  70. #
  71. # Let us submit the batch we have thus far
  72. #
  73. self.post(documents=_docs,type=_filetype)
  74. except Exception as e:
  75. print (e)
  76. def post(self,**_args):
  77. pass
  78. class X12Parser(BasicParser):
  79. def __init__(self,**_args):
  80. super().__init__(**_args)
  81. self._store = _args['store']
  82. def post(self,**_args):
  83. """
  84. Writing the files to a persistent storage in JSON format (hopefully)
  85. """
  86. _documents = _args['documents']
  87. if _documents :
  88. _store = copy.copy(self._store,**{})
  89. TABLE = 'claims' if _args['type'] in ['837','claims'] else 'remits'
  90. _store['table'] = TABLE
  91. _writer = transport.factory.instance(**_store)
  92. _writer.write(_documents)
  93. if getattr(_writer,'close') :
  94. _writer.close()
  95. def instance (**_args):
  96. """
  97. :path
  98. """
  99. # _files = x12.util.Files.get(_args['file'])
  100. # #
  101. # # We can split these files (multi-processing)
  102. # #
  103. # _jobCount = 1 if 'jobs' not in _args else int (_args['jobs'])
  104. # _files = np.array_split(_files,_jobCount)
  105. # PATH = os.sep.join([os.environ['HOME'],'.healthcareio','config.json'])
  106. # if 'config' in _args :
  107. # PATH = _args['config']
  108. # f = open(PATH)
  109. # _config = json.loads(f.read())
  110. # f.close()
  111. # jobs = []
  112. # for _batch in _files :
  113. # pthread = Parser(files=_batch,config=_config)
  114. # pthread.start()
  115. # jobs.append(pthread)
  116. # time.sleep(1)
  117. pass
  118. # class parser (Process) :
  119. # _CONFIGURATION = {}
  120. # def __init__(self,path=None) :
  121. # if not parser._CONFIGURATION :
  122. # _path = path if path else os.sep.join([os.environ['HOME'],'.healthcareio/config.json'])
  123. # #
  124. # # @TODO: Load custom configuration just in case we need to do further processing
  125. # config = json.loads(open(path).read())
  126. # parser._CONFIGURATION = config['parser']
  127. # #
  128. # # do we have a custom configuration in this location
  129. # #
  130. # _custompath = _path.replace('config.json','')
  131. # _custompath = _custompath if not _custompath.endswith(os.sep) else _custompath[:-1]
  132. # _custompath = os.sep.join([_custompath,'custom'])
  133. # if os.exists(_custompath) :
  134. # files = os.listdir(_custompath)
  135. # if files :
  136. # _filename = os.sep.join([_custompath,files[0]])
  137. # _customconf = json.loads(open(_filename).read())
  138. # #
  139. # # merge with existing configuration
  140. # else:
  141. # pass
  142. # #
  143. # #
  144. # class getter :
  145. # def value(self,) :
  146. # pass
  147. # class setter :
  148. # def files(self,files):
  149. # pass