Package pyx12 :: Module path
[hide private]

Source Code for Module pyx12.path

  1  ###################################################################### 
  2  # Copyright (c) 2005 Kalamazoo Community Mental Health Services, 
  3  #   John Holland <jholland@kazoocmh.org> <john@zoner.org> 
  4  # All rights reserved. 
  5  # 
  6  # This software is licensed as described in the file LICENSE.txt, which 
  7  # you should have received as part of this distribution. 
  8  # 
  9  ###################################################################### 
 10   
 11  #    $Id: path.py 942 2007-03-21 18:29:16Z johnholland $ 
 12   
 13  """ 
 14  Parses an x12 path 
 15   
 16  An x12 path is comprised of a sequence of loop identifiers, a segment 
 17  identifier, and element position, and a composite position. 
 18   
 19  The last loop id might be a segment id.  It is difficult to parse 
 20   
 21  /LOOP_1/LOOP_2 
 22  /LOOP_1/LOOP_2/SEG 
 23  /LOOP_1/LOOP_2/SEG02 
 24  /LOOP_1/LOOP_2/SEG[424]02-1 
 25  SEG[434]02-1 
 26  02-1 
 27  02 
 28   
 29  """ 
 30   
 31  import re 
 32   
 33  from pyx12.errors import * 
 34   
35 -class path(object):
36 """ 37 Interface to an x12 path 38 """ 39
40 - def __init__(self, path_str):
41 """ 42 @param path_str: 43 @type path_str: string 44 45 """ 46 self.loop_list = path_str.split('/') 47 self.seg_id = None 48 self.id_val = None 49 self.ele_idx = None 50 self.subele_idx = None 51 52 if len(self.loop_list) == 0: 53 return None 54 if self.loop_list[0] == '': 55 self.relative = False 56 del self.loop_list[0] 57 else: 58 self.relative = True 59 60 seg_str = self.loop_list[-1] 61 re_seg_id = '(?P<seg_id>[A-Z][A-Z0-9]{1,2})' 62 re_id_val = '(\[(?P<id_val>[A-Z0-9]+)\])?' 63 re_ele_idx = '(?P<ele_idx>[0-9]{2})?' 64 re_subele_idx = '(-(?P<subele_idx>[0-9]+))?' 65 re_str = '^%s%s%s%s$' % (re_seg_id, re_id_val, re_ele_idx, re_subele_idx) 66 m = re.compile(re_str, re.S).search(seg_str) 67 if m is not None: 68 self.seg_id = m.group('seg_id') 69 self.id_val = m.group('id_val') 70 self.ele_idx = m.group('ele_idx') 71 self.subele_idx = m.group('subele_idx') 72 del self.loop_list[-1]
73 # raise EngineError, 'Invalid segment path: %s' % (seg_str) 74
75 - def __parse_ele_path(self, ele_str):
76 """ 77 @param ele_str: An element path in the form '03' or '03-5' 78 @type ele_str: string 79 """ 80 #m = re.compile("^-?[0-9]*(\.[0-9]+)?", re.S).search(str_val) 81 re_str = '^(?P<seg_id>[A-Z][A-Z0-9]{1,2})(?P<ele_idx>[0-9]{2})?(-(?P<subele_idx>[0-9]+))?$' 82 m = re.compile(re_str, re.S).search(ele_str) 83 if not m: 84 raise IsValidError # nothing matched 85 #if m.group(0) != ele_str: # matched substring != original, bad 86 87 if ele_str.find('-') != -1: 88 ele_idx = ele_str[:ele_str.find('-')] 89 subele_idx = ele_str[ele_str.find('-')+1:] 90 else: 91 ele_idx = ele_str 92 subele_idx = None 93 try: 94 a = int(ele_idx) 95 except: 96 raise EngineError, 'Invalid element path: %s' % (ele_str) 97 try: 98 if subele_idx is not None: 99 a = int(subele_idx) 100 except: 101 raise EngineError, 'Invalid element path: %s' % (ele_str) 102 if len(ele_idx) != 2: 103 raise EngineError, 'Invalid element path: %s' % (ele_str) 104 return (ele_idx, subele_idx)
105 106 # def __len__(self): 107 # """ 108 # @rtype: int 109 # """ 110 # return 1 111
112 - def __repr__(self):
113 """ 114 @return: Formatted path 115 @rtype: string 116 """ 117 ret = '' 118 if not self.relative: ret += '/' 119 ret += '/'.join(self.loop_list) 120 if self.seg_id: 121 ret += '/' + self.seg_id 122 if self.id_val: 123 ret += '[%s]' % self.id_val 124 if self.ele_idx: 125 ret += self.ele_idx 126 if self.subele_idx: 127 ret += '-%s' % self.subele_idx 128 return ret
129
130 - def format(self):
131 """ 132 @rtype: string 133 """ 134 return self.__repr__()
135