def __init__(self, game: str, data_file: str = DATA): """Init""" # Set command prefix super().__init__(command_prefix='.') # Get all games for name, obj in inspect.getmembers(games, inspect.ismodule): if name != 'base': for n, o in inspect.getmembers(inspect.getmodule(obj), inspect.isclass): if n not in ('Move', 'Game', 'Playbook'): self.GAMES[n.lower()] = o # Bot properties self.game = self.GAMES[game](self) if game else Game(self) self.private_clocks = False self.mc = None # Add commands self.add_cog(ClockCommands(self)) self.add_cog(ContactCommands(self)) self.add_cog(FunctionalCommands(self)) self.add_cog(MiscCommands(self)) self.add_cog(HiddenCommands(self)) self.add_cog(self.game) # Extract data from file self.memories = [] self.dead_characters = {} self.clocks = [] self.contacts = [] self.data_file = data_file try: with open(f'{self.data_file}/clocks.json', 'rb') as file: clocks = jsons.loadb(file.read()) self.clocks = [ Clock(clock['name'], time=clock['time']) for clock in clocks ] with open(f'{self.data_file}/contacts.json', 'rb') as file: contacts = jsons.loadb(file.read()) self.contacts = [ Contact(contact['name'], contact['description']) for contact in contacts ] with open(f'{self.data_file}/memories.json', 'rb') as file: self.memories = jsons.loadb(file.read()) with open(f'{self.data_file}/dead_characters.json', 'rb') as file: self.dead_characters = jsons.loadb(file.read()) print('Data extracted') except EOFError: print('No data in file.') except FileNotFoundError: print('No data file found.') except KeyError as ke: print(f'KeyError while loading data: {ke}')
def test_loadb(self): class A: def __init__(self): self.name = 'A' class B: def __init__(self, a: A): self.a = a self.name = 'B' b = json.dumps({'a': {'name': 'A'}, 'name': 'B'}).encode() loaded_dict = jsons.loadb(b) self.assertDictEqual(loaded_dict, {'a': {'name': 'A'}, 'name': 'B'}) loaded_obj = jsons.loadb(b, B) self.assertEqual('B', loaded_obj.name) self.assertEqual('A', loaded_obj.a.name)
def __init__(self, list_name: str, db: pv.DB): assert list_name is not None and isinstance(db, pv.DB) self._list_name = list_name.encode() self._db = db keys: bytes = self._db.get(self._list_name) if keys is not None: super().__init__(list(jsons.loadb(keys))) else: super().__init__()
def test_exception_wrong_bytes(self): with self.assertRaises(DeserializationError): jsons.loadb('{"key": "value"}')
def _decode(self, byte_object: bytes) -> ModelTV: return jsons.loadb(byte_object, self._object_type)
def _setup(self): keys: bytes = self._db.get(self._list_name) if keys is not None: super().__init__(list(jsons.loadb(keys)))
def test_model_encode(cowboy): cowboy_bytes_serialized = cowboy.encode() cowboy_deserialized = jsons.loadb(cowboy_bytes_serialized, Cowboy) assert cowboy_deserialized == cowboy