Changeset 5194b5a76f8bf7294f69b55754bc847b6427aeaf


Ignore:
Timestamp:
10/18/09 21:49:03 (3 years ago)
Author:
Flupke <luper.rouch@…>
Children:
51bab5c0845a67636db84196cfce95dfa0cbab93, 66119c008ae513e8f008a02af9b8c97d18fbcf15
Parents:
1149eb6005e93948d6779fae19ae09c783cbc897
git-committer:
Flupke <luper.rouch@…> (10/18/09 21:49:03)
Message:

boxsort: support for tab separated input files

Location:
boxsort
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • boxsort/boxsort/__init__.py

    r1149eb6 r5194b5a  
    3030 
    3131 
    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 
     33spaces_format = 0 
     34tabs_format = 1 
     35 
     36# Formats headers 
     37format_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 
     45skip_lines = [         
     46        3,                          # spaces_format         
     47        0,                          # tabs_format 
     48    ] 
     49 
     50# Lines patterns 
     51box_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 
    4073module_letters = [chr(ord("A") + i) for i in range(26)] 
    4174 
     
    4679    """ 
    4780 
    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): 
    4983        self.module = module 
    5084        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) 
    5287        self.name = name.strip() 
    5388        self.qty = qty 
    5489        self.code = code 
     90        self.format = format 
    5591 
    5692    @property 
     
    64100 
    65101    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__ 
    72115 
    73116 
     
    162205 
    163206 
    164 def parse(infile, prefix_size=4): 
     207def parse(infile): 
    165208    """ 
    166209    Parse a file, returning the file prefix and the raw decks lists. 
    167210    """ 
    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]): 
    171220        prefix += infile.readline() 
     221        current_line += 1 
    172222    # Parse decks 
    173223    decks = [] 
    174224    current_deck = [] 
    175     current_line = 4 
    176225    for line in infile: 
    177226        line = line.strip() 
     
    180229                # EOF 
    181230                break 
    182             match = box_line_pattern.match(line) 
     231            match = box_line_patterns[format].match(line) 
    183232            if not match: 
    184233                raise InvalidInputError(u"erreur à la ligne %d: format " 
    185234                        u"invalide." % current_line) 
    186             current_deck.append(Box(*match.groups()))         
     235            current_deck.append(Box(format=format, **match.groupdict())) 
    187236        else: 
    188237            # Empty line, starting new deck 
Note: See TracChangeset for help on using the changeset viewer.