def unpack(self, string): old_regex = re.compile( r'''{(?P<x>-?\d+),(?P<y>-?\d+),(?P<w>-?\d+),(?P<h>-?\d+)}(?P<text>.*)''', flags = re.MULTILINE | re.DOTALL # must match multiple lines of text ) match = old_regex.match(string) if match: # this is an old-format card self._x = int(match.group('x')) self._y = int(match.group('y')) self._w = int(match.group('w')) self._h = int(match.group('h')) self._text = match.group('text') else: try: data = minijson.decode(string) self._x = data['x'] self._y = data['y'] self._w = data['w'] self._h = data['h'] self._text = data['text'] except ValueError: raise InvalidCard("Could not parse card at all!") except KeyError: raise InvalidCard("Card data did not contain all required fields!") # make sure w and h are valid if self._w <= 0: raise InvalidCard('Card width must be > 0!') if self._h <= 0: raise InvalidCard('Card height must be > 0!')
def unpack(self, string): old_regex = re.compile( r'''{(?P<x>-?\d+),(?P<y>-?\d+),(?P<w>-?\d+),(?P<h>-?\d+)}(?P<text>.*)''', flags=re.MULTILINE | re.DOTALL # must match multiple lines of text ) match = old_regex.match(string) if match: # this is an old-format card self._x = int(match.group('x')) self._y = int(match.group('y')) self._w = int(match.group('w')) self._h = int(match.group('h')) self._text = match.group('text') else: try: data = minijson.decode(string) self._x = data['x'] self._y = data['y'] self._w = data['w'] self._h = data['h'] self._text = data['text'] except ValueError: raise InvalidCard("Could not parse card at all!") except KeyError: raise InvalidCard( "Card data did not contain all required fields!") # make sure w and h are valid if self._w <= 0: raise InvalidCard('Card width must be > 0!') if self._h <= 0: raise InvalidCard('Card height must be > 0!')
def load(self, datastore, oid): dat = datastore.get(oid) if dat: try: dat = minijson.decode(dat) if isinstance(dat, dict): self.clear() self.update(dat) self.oid = oid else: raise Error('StorableDict must be loaded from dict, key: %s' % oid) except ValueError: raise Error('StorableDict data at %s is invalid' % oid) else: raise Error('StorableDict got invalid key: %s' % oid)