예제 #1
0
파일: main.py 프로젝트: j-rogers/pbabot
    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}')
예제 #2
0
    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)
예제 #3
0
 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__()
예제 #4
0
 def test_exception_wrong_bytes(self):
     with self.assertRaises(DeserializationError):
         jsons.loadb('{"key": "value"}')
예제 #5
0
 def _decode(self, byte_object: bytes) -> ModelTV:
     return jsons.loadb(byte_object, self._object_type)
예제 #6
0
 def _setup(self):
     keys: bytes = self._db.get(self._list_name)
     if keys is not None:
         super().__init__(list(jsons.loadb(keys)))
예제 #7
0
def test_model_encode(cowboy):
    cowboy_bytes_serialized = cowboy.encode()
    cowboy_deserialized = jsons.loadb(cowboy_bytes_serialized, Cowboy)

    assert cowboy_deserialized == cowboy