def test_good_cardset(self): config_txt = """\ PySolFC solitaire cardset;4;.gif;1;52;7 123-dondorf;Dondorf 79 123 8 16 25 7 7 back01.gif back01.gif """ reference = CardsetConfig() reference.update( dict( version=4, ext='.gif', type=CSI.TYPE_FRENCH, ncards=52, styles=[7], ident='123-dondorf;Dondorf', name='Dondorf', CARDW=79, CARDH=123, CARDD=8, CARD_XOFFSET=16, CARD_YOFFSET=25, SHADOW_XOFFSET=7, SHADOW_YOFFSET=7, backindex=0, backnames=['back01.gif'], )) self._assertCcEqual( parse_cardset_config(config_txt.split('\n')), reference, 'parse_cardset_config should parse well-formed v4 config.txt ' + 'correctly')
def _readCardsetConfig(self, dirname, filename): with open(filename, "r") as f: lines = f.readlines() lines = [l.strip() for l in lines] if not lines[0].startswith("PySol"): return None config = CardsetConfig() if not self._parseCardsetConfig(config, lines): # print filename, 'invalid config' return None if config.CARDD > self.top.winfo_screendepth(): return None cs = Cardset() cs.dir = dirname cs.update(config.__dict__) return cs
def parse_cardset_config(lines_list): cs = CardsetConfig() if len(lines_list) < 6: _perr(1, msg='not enough lines in file') return None # lines_list[0]: magic identifier, possible version information fields = [f.strip() for f in lines_list[0].split(';')] if len(fields) >= 2: try: cs.version = int(fields[1]) except ValueError: # version 1 is implied cs.version = 1 if cs.version >= 3: if len(fields) < 5: _perr(1, msg='not enough fields') return None cs.ext = fields[2] try: cs.type = int(fields[3]) except ValueError: _perr(1, 3, 'not integer') return None try: cs.ncards = int(fields[4]) except ValueError: _perr(1, 4, 'not integer') return None if cs.version >= 4: if len(fields) < 6: _perr(1, msg='not enough fields') return None try: styles = (int(s.strip()) for s in fields[5].split(",")) cs.styles = list(set(styles)) except ValueError: _perr(1, 5, 'not integer') return None if cs.version >= 5: if len(fields) < 7: _perr(1, msg='not enough fields') return None try: cs.year = int(fields[6]) except ValueError: _perr(1, 6, 'not integer') return None if len(cs.ext) < 2 or cs.ext[0] != ".": _perr(1, msg='specifies an invalid file extension') return None # lines_list[1]: identifier/name if not lines_list[1]: _perr(2, msg='unexpected empty line') return None cs.ident = lines_list[1] split_ident = cs.ident.split(';') if len(split_ident) == 1: cs.name = cs.ident elif len(split_ident) == 2: cs.name = split_ident[1].strip() else: _perr(2, msg='invalid format') return None # lines_list[2]: CARDW, CARDH, CARDD try: cs.CARDW, cs.CARDH, cs.CARDD = (int(x) for x in lines_list[2].split()) except ValueError: _perr(3, msg='invalid format') return None # lines_list[3]: CARD_UP_YOFFSET, CARD_DOWN_YOFFSET, # SHADOW_XOFFSET, SHADOW_YOFFSET try: (cs.CARD_XOFFSET, cs.CARD_YOFFSET, cs.SHADOW_XOFFSET, cs.SHADOW_YOFFSET) = \ (int(x) for x in lines_list[3].split()) except ValueError: _perr(4, msg='invalid format') return None # lines_list[4]: default background back = lines_list[4] if not back: _perr(5, msg='unexpected empty line') return None # lines_list[5]: all available backgrounds cs.backnames = [f.strip() for f in lines_list[5].split(';')] if back in cs.backnames: cs.backindex = cs.backnames.index(back) else: cs.backnames.insert(0, back) cs.backindex = 0 # if cs.type != 1: print cs.type, cs.name return cs