diff --git a/edi/__main__.py b/edi/__main__.py index 7ef6997..3ddda33 100644 --- a/edi/__main__.py +++ b/edi/__main__.py @@ -30,7 +30,7 @@ Usage : """ from params import SYS_ARGS from transport import factory -from parser import * +from parser import get_content import os import json import sys diff --git a/edi/parser.py b/edi/parser.py index c4c01d6..7057a6b 100644 --- a/edi/parser.py +++ b/edi/parser.py @@ -168,11 +168,12 @@ def get_content(filename,config,section=None) : section = section if section else config['SECTION'] x12_file = open(filename).read().split('\n') + if len(x12_file) == 1 : x12_file = x12_file[0].split('~') - + partitions = '\n'.join(x12_file).split(section+'*') locations = get_locations(x12_file,section) claims = [] @@ -191,12 +192,18 @@ def get_content(filename,config,section=None) : N = len(locations) - for index in range(0,N-1): - beg = locations[index] - end = locations[index+1] - claim = {} - for row in x12_file[beg:end] : - row = split(row) + # for index in range(0,N-1): + # beg = locations[index] + # end = locations[index+1] + # claim = {} + for segment in partitions : + + claim = {} + # for row in x12_file[beg:end] : + segment = segment.replace('\n','').split('~') + for row in segment : + row = split(row) + _info = get_config(config,row) if _info : try: @@ -205,11 +212,14 @@ def get_content(filename,config,section=None) : # pointer = eval(_info['parser']) # print (pointer(row)) tmp = get_map(row,_info,VERSION) + except Exception as e: if sys.version_info[0] > 2 : - logs.append ({"version":VERSION,"filename":filename,"msg":e.args[0],"X12":x12_file[beg:end]}) + # logs.append ({"version":VERSION,"filename":filename,"msg":e.args[0],"X12":x12_file[beg:end]}) + logs.append ({"version":VERSION,"filename":filename,"msg":e.args[0],"X12":row}) else: - logs.append ({"version":VERSION,"filename":filename,"msg":e.message,"X12":x12_file[beg:end]}) + # logs.append ({"version":VERSION,"filename":filename,"msg":e.message,"X12":x12_file[beg:end]}) + logs.append ({"version":VERSION,"filename":filename,"msg":e.message,"X12":row}) claim = {} break @@ -231,6 +241,7 @@ def get_content(filename,config,section=None) : if type(tmp) == list : claim[label] = tmp if label not in claim else claim[label] + tmp + else: if label not in claim: claim[label] = [tmp] @@ -240,14 +251,22 @@ def get_content(filename,config,section=None) : claim[label][index] = dict(claim[label][index],**tmp) else: claim[label].append(tmp) - + if len(claim[label]) > 0 : + labels = [] + for item in claim[label] : + if item not in labels : + labels.append(item) + claim[label] = labels + # claim[label] = list( set(claim[label])) #-- removing redundancies + if claim and 'claim_id' in claim: claim = dict(claim,**_default_value) - claim['name'] = filename[:-5].split(os.sep)[-1] #.replace(ROOT,'') - claim['index'] = index + claim['name'] = filename.split(os.sep)[-1] #.replace(ROOT,'') + claim['index'] = len(claims) if len(claims) > 0 else 0 claims.append(claim) + return claims,logs