From 8c492bae7f9cf16cd0edc197734dd95bde6170cc Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 12 Oct 2019 00:10:11 +0100 Subject: [PATCH] Complete migration over to new 'f-string' syntax --- houdini/commands.py | 15 +++--- houdini/handlers/__init__.py | 2 +- houdini/handlers/login/__init__.py | 2 +- houdini/handlers/login/login.py | 14 +++--- houdini/handlers/play/experience.py | 10 ++-- houdini/handlers/play/item.py | 4 +- houdini/handlers/play/player.py | 4 +- houdini/handlers/play/stampbook.py | 6 +-- houdini/houdini.py | 6 +-- houdini/penguin.py | 78 ++++++++++------------------- houdini/plugins/example/__init__.py | 6 +-- 11 files changed, 61 insertions(+), 86 deletions(-) diff --git a/houdini/commands.py b/houdini/commands.py index ace2f80..c528741 100644 --- a/houdini/commands.py +++ b/houdini/commands.py @@ -85,16 +85,15 @@ class CommandManager(_AbstractManager): if name in parent_commands and len(parent_commands[name]): conflict_command = parent_commands[name][0] if config.commands['ConflictMode'] == ConflictResolution.Exception: - raise NameError('Command name conflict: \'{}\' from plugin \'{}\' ' - 'conflicts with \'{}\' from module \'{}\'' - .format(name, module.__class__.__name__, conflict_command.name, - conflict_command.instance.__class__.__name__)) + raise NameError(f'Command name conflict: \'{name}\' from plugin \'{module.__class__.__name__}\' ' + f'conflicts with \'{conflict_command.name}\' from ' + f'module \'{conflict_command.instance.__class__.__name__}\'') elif config.commands['ConflictMode'] == ConflictResolution.Append: parent_commands[name].append(command_object) elif config.commands['ConflictMode'] == ConflictResolution.Silent: - module.server.logger.warning( - 'Command \'{}\' from module \'{}\' disabled due to conflict with \'{}\''.format( - name, module.__class__.__name__, conflict_command.instance.__class__.__name__)) + module.server.logger.warning(f'Command \'{name}\' from module \'{module.__class__.__name__}\' ' + f'disabled due to conflict with ' + f'\'{conflict_command.instance.__class__.__name__}\'') else: parent_commands[name] = [command_object] @@ -131,7 +130,7 @@ async def invoke_command_string(commands, p, command_string): async def invoke_command_objects(commands, p, data): command_identifier = data.pop(0) if command_identifier not in commands: - raise UnknownCommandException('Command \'{}\' does not exist'.format(command_identifier)) + raise UnknownCommandException(f'Command \'{command_identifier}\' does not exist') command_objects = commands[command_identifier] for command_object in command_objects: diff --git a/houdini/handlers/__init__.py b/houdini/handlers/__init__.py index 56c2a8f..66999e0 100644 --- a/houdini/handlers/__init__.py +++ b/houdini/handlers/__init__.py @@ -75,7 +75,7 @@ class _XTListener(_Listener): async def __call__(self, p, packet_data): if not self.pre_login and not p.joined_world: await p.close() - raise AuthorityError('{} tried sending XT packet before authentication!'.format(p)) + raise AuthorityError(f'{p} tried sending XT packet before authentication!') await super()._check_cooldown(p) super()._check_list(p) diff --git a/houdini/handlers/login/__init__.py b/houdini/handlers/login/__init__.py index 9e4440a..5ce7339 100644 --- a/houdini/handlers/login/__init__.py +++ b/houdini/handlers/login/__init__.py @@ -43,7 +43,7 @@ async def get_server_presence(p, pid): else int(server_population) // (server_config['Capacity'] // 6)) \ if server_population else 0 - world_populations.append('{},{}'.format(server_config['Id'], server_population)) + world_populations.append(f'{server_config["Id"]},{server_population}') server_key = f'houdini.players.{server_config["Id"]}' if await p.server.redis.scard(server_key): diff --git a/houdini/handlers/login/login.py b/houdini/handlers/login/login.py index d14e9d6..48e4e7b 100644 --- a/houdini/handlers/login/login.py +++ b/houdini/handlers/login/login.py @@ -21,22 +21,22 @@ async def handle_login(p, credentials: Credentials): loop = asyncio.get_event_loop() username, password = credentials.username, credentials.password - p.logger.info('{} is logging in!'.format(username)) + p.logger.info(f'{username} is logging in!') data = await Penguin.query.where(Penguin.username == username).gino.first() if data is None: - p.logger.info('{} failed to login: penguin does not exist'.format(username)) + p.logger.info(f'{username} failed to login: penguin does not exist') return await p.send_error_and_disconnect(100) password_correct = await loop.run_in_executor(None, bcrypt.checkpw, password.encode('utf-8'), data.password.encode('utf-8')) ip_addr = p.peer_name[0] - flood_key = '{}.flood'.format(ip_addr) + flood_key = f'{ip_addr}.flood' if not password_correct: - p.logger.info('{} failed to login: incorrect password'.format(username)) + p.logger.info(f'{username} failed to login: incorrect password') if await p.server.redis.exists(flood_key): tr = p.server.redis.multi_exec() @@ -91,15 +91,15 @@ async def handle_login(p, credentials: Credentials): else: await p.send_error_and_disconnect(601, hours_left) - p.logger.info('{} has logged in successfully'.format(username)) + p.logger.info(f'{username} has logged in successfully') random_key = Crypto.generate_random_key() login_key = Crypto.hash(random_key[::-1]) confirmation_hash = Crypto.hash(os.urandom(24)) tr = p.server.redis.multi_exec() - tr.setex('{}.lkey'.format(data.username), p.server.server_config['KeyTTL'], login_key) - tr.setex('{}.ckey'.format(data.username), p.server.server_config['KeyTTL'], confirmation_hash) + tr.setex(f'{data.username}.lkey', p.server.server_config['KeyTTL'], login_key) + tr.setex(f'{data.username}.ckey', p.server.server_config['KeyTTL'], confirmation_hash) await tr.execute() world_populations, buddy_presence = await get_server_presence(p, data.id) diff --git a/houdini/handlers/play/experience.py b/houdini/handlers/play/experience.py index e7d0d04..710b670 100644 --- a/houdini/handlers/play/experience.py +++ b/houdini/handlers/play/experience.py @@ -11,11 +11,11 @@ from aiocache import cached def get_status_key(_, p): - return 'quest.status.{}'.format(p.data.id) + return f'quest.status.{p.data.id}' def get_settings_key(_, p): - return 'quest.settings.{}'.format(p.room.id) + return f'quest.settings.{p.room.id}' @cached(alias='default', key_builder=get_status_key) @@ -128,7 +128,7 @@ async def load_active_quests(p): @handlers.handler(XTPacket('j', 'js'), after=handle_join_server) @handlers.allow_once async def handle_quest_join_server(p): - await p.server.cache.delete('quest.status.{}'.format(p.data.id)) + await p.server.cache.delete(f'quest.status.{p.data.id}') await load_active_quests(p) await p.send_xt('nxquestsettings', await get_quest_settings(p)) @@ -167,7 +167,7 @@ async def handle_quest_join_room(p): @handlers.handler(XTPacket('nx', 'nxquestaward')) async def handle_quest_award(p, quest_id: int): - await p.server.cache.delete('quest.status.{}'.format(p.data.id)) + await p.server.cache.delete(f'quest.status.{p.data.id}') quest = await Quest.load(items=QuestAwardItem, furniture=QuestAwardFurniture, @@ -184,7 +184,7 @@ async def handle_quest_award(p, quest_id: int): @handlers.handler(XTPacket('nx', 'nxquestactivate')) @handlers.allow_once async def handle_quest_activate(p): - await p.server.cache.delete('quest.status.{}'.format(p.data.id)) + await p.server.cache.delete(f'quest.status.{p.data.id}') await init_all_quests(p) await p.send_xt('nxquestdata', await get_player_quest_status(p)) diff --git a/houdini/handlers/play/item.py b/houdini/handlers/play/item.py index 5a1b911..3f8f4c9 100644 --- a/houdini/handlers/play/item.py +++ b/houdini/handlers/play/item.py @@ -8,11 +8,11 @@ import operator def get_pin_string_key(_, p, player_id): - return 'pins.{}'.format(player_id) + return f'pins.{player_id}' def get_awards_string_key(_, p, player_id): - return 'awards.{}'.format(player_id) + return f'awards.{player_id}' @cached(alias='default', key_builder=get_pin_string_key) diff --git a/houdini/handlers/play/player.py b/houdini/handlers/play/player.py index 95705bf..ed1474f 100644 --- a/houdini/handlers/play/player.py +++ b/houdini/handlers/play/player.py @@ -11,11 +11,11 @@ import time def get_player_string_key(_, p, player_id): - return 'player.{}'.format(player_id) + return f'player.{player_id}' def get_mascot_string_key(_, p, mascot_id): - return 'mascot.{}'.format(mascot_id) + return f'mascot.{mascot_id}' @cached(alias='default', key_builder=get_player_string_key) diff --git a/houdini/handlers/play/stampbook.py b/houdini/handlers/play/stampbook.py index de66c08..70a1bc6 100644 --- a/houdini/handlers/play/stampbook.py +++ b/houdini/handlers/play/stampbook.py @@ -8,11 +8,11 @@ from aiocache import cached def get_book_cover_key(_, p, player_id): - return 'book.{}'.format(player_id) + return f'book.{player_id}' def get_player_stamps_key(_, p, player_id): - return 'stamps.{}'.format(player_id) + return f'stamps.{player_id}' @cached(alias='default', key_builder=get_book_cover_key) @@ -133,4 +133,4 @@ async def handle_update_book_cover(p, color: int, highlight: int, pattern: int, book_modified=1).apply() stringified_cover = '%'.join(cover) - await p.server.cache.set('book.{}'.format(p.data.id), f'{color}%{highlight}%{pattern}%{icon}%{stringified_cover}') + await p.server.cache.set(f'book.{p.data.id}', f'{color}%{highlight}%{pattern}%{icon}%{stringified_cover}') diff --git a/houdini/houdini.py b/houdini/houdini.py index 12eaf1a..1766cbd 100644 --- a/houdini/houdini.py +++ b/houdini/houdini.py @@ -228,9 +228,9 @@ class Houdini: self.permissions = await PermissionCrumbsCollection.get_collection() - self.logger.info('Multi-client support is {}'.format( - 'enabled' if self.config.client['MultiClientSupport'] else 'disabled')) - self.logger.info('Listening on {}:{}'.format(self.server_config['Address'], self.server_config['Port'])) + self.logger.info(f'Multi-client support is ' + f'{"enabled" if self.config.client["MultiClientSupport"] else "disabled"}') + self.logger.info(f'Listening on {self.server_config["Address"]}:{self.server_config["Port"]}') if self.config.client['AuthStaticKey'] != 'houdini': self.logger.warning('The static key has been changed from the default, ' diff --git a/houdini/penguin.py b/houdini/penguin.py index e72df87..27eb47a 100644 --- a/houdini/penguin.py +++ b/houdini/penguin.py @@ -105,8 +105,7 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('au', igloo.id, self.data.coins) - self.logger.info('{} added \'{}\' to their igloos inventory'.format( - self.data.username, igloo.name)) + self.logger.info(f'{self.data.username} added \'{igloo.name}\' to their igloos inventory') return True @@ -126,8 +125,7 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('papi', self.data.coins, care_item.id, quantity) - self.logger.info('{} added \'{}\' to their puffle care inventory'.format( - self.data.username, care_item.name)) + self.logger.info(f'{self.data.username} added \'{care_item.name}\' to their puffle care inventory') return True @@ -147,8 +145,7 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('af', furniture.id, self.data.coins) - self.logger.info('{} added \'{}\' to their furniture inventory'.format( - self.data.username, furniture.name)) + self.logger.info(f'{self.data.username} added \'{furniture.name}\' to their furniture inventory') return True @@ -161,8 +158,7 @@ class Penguin(Spheniscidae): else: await self.data.cards.set(card.id) - self.logger.info('{} added \'{}\' to their ninja deck'.format( - self.data.username, card.name)) + self.logger.info(f'{self.data.username} added \'{card.name}\' to their ninja deck') return True @@ -176,8 +172,7 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('ag', flooring.id, self.data.coins) - self.logger.info('{} added \'{}\' to their flooring inventory'.format( - self.data.username, flooring.name)) + self.logger.info(f'{self.data.username} added \'{flooring.name}\' to their flooring inventory') return True @@ -191,8 +186,7 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('aloc', location.id, self.data.coins) - self.logger.info('{} added \'{}\' to their location inventory'.format( - self.data.username, location.name)) + self.logger.info(f'{self.data.username} added \'{location.name}\' to their location inventory') return True @@ -205,8 +199,8 @@ class Penguin(Spheniscidae): if notify: await self.send_xt('aabs', stamp.id) - self.logger.info('{} earned stamp \'{}\''.format(self.data.username, stamp.name)) - await self.server.cache.delete('stamps.{}'.format(self.data.id)) + self.logger.info(f'{self.data.username} earned stamp \'{stamp.name}\'') + await self.server.cache.delete(f'stamps.{self.data.id}') return True @@ -221,98 +215,80 @@ class Penguin(Spheniscidae): if permission not in self.data.permissions: await self.data.permissions.set(permission) - self.logger.info('{} was assigned permission \'{}\''.format( - self.data.username, permission)) + self.logger.info(f'{self.data.username} was assigned permission \'{permission}\'') return True async def set_color(self, item): await self.data.update(color=item.id).apply() await self.room.send_xt('upc', self.data.id, item.id) - self.logger.info('{} updated their color to \'{}\' '.format( - self.data.username, item.name)) + self.logger.info(f'{self.data.username} updated their color to \'{item.name}\' ') async def set_head(self, item): item_id = None if item is None else item.id await self.data.update(head=item_id).apply() await self.room.send_xt('uph', self.data.id, item_id or 0) - self.logger.info('{} updated their head item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their head item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their head item to \'{item.name}\' ' if item else + f'{self.data.username} removed their head item') async def set_face(self, item): item_id = None if item is None else item.id await self.data.update(face=item_id).apply() await self.room.send_xt('upf', self.data.id, item_id or 0) - self.logger.info('{} updated their face item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their face item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their face item to \'{item.name}\' ' if item else + f'{self.data.username} removed their face item') async def set_neck(self, item): item_id = None if item is None else item.id await self.data.update(neck=item_id).apply() await self.room.send_xt('upn', self.data.id, item_id or 0) - self.logger.info('{} updated their neck item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their neck item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their neck item to \'{item.name}\' ' if item else + f'{self.data.username} removed their neck item') async def set_body(self, item): item_id = None if item is None else item.id await self.data.update(body=item_id).apply() await self.room.send_xt('upb', self.data.id, item_id or 0) - self.logger.info('{} updated their body item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their body item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their body item to \'{item.name}\' ' if item else + f'{self.data.username} removed their body item') async def set_hand(self, item): item_id = None if item is None else item.id await self.data.update(hand=item_id).apply() await self.room.send_xt('upa', self.data.id, item_id or 0) - self.logger.info('{} updated their hand item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their hand item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their hand item to \'{item.name}\' ' if item else + f'{self.data.username} removed their hand item') async def set_feet(self, item): item_id = None if item is None else item.id await self.data.update(feet=item_id).apply() await self.room.send_xt('upe', self.data.id, item_id or 0) - self.logger.info('{} updated their feet item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their feet item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their feet item to \'{item.name}\' ' if item else + f'{self.data.username} removed their feet item') async def set_flag(self, item): item_id = None if item is None else item.id await self.data.update(flag=item_id).apply() await self.room.send_xt('upl', self.data.id, item_id or 0) - self.logger.info('{} updated their flag item to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their flag item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their flag item to \'{item.name}\' ' if item else + f'{self.data.username} removed their flag item') async def set_photo(self, item): item_id = None if item is None else item.id await self.data.update(photo=item_id).apply() await self.room.send_xt('upp', self.data.id, item_id or 0) - self.logger.info('{} updated their background to \'{}\' '.format( - self.data.username, item.name) if item else - '{} removed their background item'.format( - self.data.username)) + self.logger.info(f'{self.data.username} updated their background to \'{item.name}\' ' if item else + f'{self.data.username} removed their background item') def __repr__(self): if self.data is not None: - return ''.format(self.data.id, self.data.username) + return f'' return super().__repr__() diff --git a/houdini/plugins/example/__init__.py b/houdini/plugins/example/__init__.py index 88abe4a..6df6a86 100644 --- a/houdini/plugins/example/__init__.py +++ b/houdini/plugins/example/__init__.py @@ -17,15 +17,15 @@ class Example(IPlugin): async def ready(self): self.server.logger.info('Example.ready()') - await self.server.permissions.register('houdini.ping') + # await self.server.permissions.insert(name='houdini.ping') async def message_cooling(self, p): - print("{}, Message was sent during cooldown".format(p)) + print(f'{p}, Message was sent during cooldown') @handlers.handler(XTPacket('m', 'sm')) @handlers.cooldown(1, callback=message_cooling) async def handle_send_message(self, p, penguin_id: int, message: str): - print('Do stuff with {}'.format(message)) + print(f'Do stuff with {message}') @commands.command('ping') @permissions.has('houdini.ping')