Changeset 5194b5a76f8bf7294f69b55754bc847b6427aeaf
- Timestamp:
- 10/18/09 21:49:03 (3 years ago)
- Children:
- 51bab5c0845a67636db84196cfce95dfa0cbab93, 66119c008ae513e8f008a02af9b8c97d18fbcf15
- Parents:
- 1149eb6005e93948d6779fae19ae09c783cbc897
- git-committer:
- Flupke <luper.rouch@…> (10/18/09 21:49:03)
- Location:
- boxsort
- Files:
-
- 1 added
- 1 edited
-
boxsort/__init__.py (modified) (5 diffs)
-
tests/reppos-tab.txt (added)
Legend:
- Unmodified
- Added
- Removed
-
boxsort/boxsort/__init__.py
r1149eb6 r5194b5a 30 30 31 31 32 box_line_pattern = re.compile( 33 "^([A-Z]) +" # module 34 "([0-9]{2}) +" # deck 35 "([0-9]{2} [0-9]{2})" # size 36 "(.{42}) +" # name 37 "([0-9]{2}/[0-9]{2}) +" # qty 38 "(.+)$" # code 39 ) 32 # Format constants 33 spaces_format = 0 34 tabs_format = 1 35 36 # Formats headers 37 format_headers = [ 38 # spaces_format 39 "REPORT TOPOGRAPHICAL", 40 # tabs_format 41 "MODULO PIANO INIZIO FINE ARTICOLO CODICE", 42 ] 43 44 # Number of lines to skip after header line 45 skip_lines = [ 46 3, # spaces_format 47 0, # tabs_format 48 ] 49 50 # Lines patterns 51 box_line_patterns = [ 52 # spaces_format 53 re.compile( 54 "^(?P<module>[A-Z]) +" 55 "(?P<deck>[0-9]{2}) +" 56 "(?P<p1>[0-9]{2}) " 57 "(?P<p2>[0-9]{2})" 58 "(?P<name>.{42}) +" 59 "(?P<qty>[0-9]{2}/[0-9]{2}) +" 60 "(?P<code>.+)$" 61 ), 62 # tabs_format 63 re.compile( 64 "^(?P<module>[A-Z])\t" 65 "(?P<deck>[0-9]+)\t" 66 "(?P<p1>[0-9]+)\t" 67 "(?P<p2>[0-9]+)\t" 68 "(?P<name>.+)\t" 69 "(?P<code>.+)$" 70 ), 71 ] 72 40 73 module_letters = [chr(ord("A") + i) for i in range(26)] 41 74 … … 46 79 """ 47 80 48 def __init__(self, module, deck, size, name, qty, code): 81 def __init__(self, module, deck, p1, p2, name, code, qty=None, 82 format=spaces_format): 49 83 self.module = module 50 84 self.deck = int(deck) 51 self.p1, self.p2 = [int(x) for x in size.split(" ")] 85 self.p1 = int(p1) 86 self.p2 = int(p2) 52 87 self.name = name.strip() 53 88 self.qty = qty 54 89 self.code = code 90 self.format = format 55 91 56 92 @property … … 64 100 65 101 def __repr__(self): 66 return "%(module)s " \ 67 "%(deck)02i " \ 68 "%(p1)02i %(p2)02i" \ 69 "%(name)42s " \ 70 "%(qty)s " \ 71 "%(code)s" % self.__dict__ 102 if self.format == spaces_format: 103 return "%(module)s " \ 104 "%(deck)02i " \ 105 "%(p1)02i %(p2)02i" \ 106 "%(name)42s " \ 107 "%(qty)s " \ 108 "%(code)s" % self.__dict__ 109 elif self.format == tabs_format: 110 return "%(module)s\t" \ 111 "%(deck)i\t" \ 112 "%(p1)i\t%(p2)i\t" \ 113 "%(name)s\t" \ 114 "%(code)s\t" % self.__dict__ 72 115 73 116 … … 162 205 163 206 164 def parse(infile , prefix_size=4):207 def parse(infile): 165 208 """ 166 209 Parse a file, returning the file prefix and the raw decks lists. 167 210 """ 168 # Read prefix 169 prefix = "" 170 for i in range(prefix_size): 211 # Detect format 212 prefix = infile.readline() 213 current_line = 1 214 for format, header in enumerate(format_headers): 215 if prefix.startswith(header): 216 break 217 else: 218 raise InvalidInputError(u"format de fichier non reconnu") 219 for i in range(skip_lines[format]): 171 220 prefix += infile.readline() 221 current_line += 1 172 222 # Parse decks 173 223 decks = [] 174 224 current_deck = [] 175 current_line = 4176 225 for line in infile: 177 226 line = line.strip() … … 180 229 # EOF 181 230 break 182 match = box_line_pattern .match(line)231 match = box_line_patterns[format].match(line) 183 232 if not match: 184 233 raise InvalidInputError(u"erreur à la ligne %d: format " 185 234 u"invalide." % current_line) 186 current_deck.append(Box( *match.groups()))235 current_deck.append(Box(format=format, **match.groupdict())) 187 236 else: 188 237 # Empty line, starting new deck
Note: See TracChangeset
for help on using the changeset viewer.
