From f62b82505c988e23812b2b9467415042771562ee Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Mon, 3 Apr 2017 14:16:44 +0100 Subject: [PATCH] Migrate faction plugin to Python 3.5. --- plugin/faction/faction.py | 167 +++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 90 deletions(-) diff --git a/plugin/faction/faction.py b/plugin/faction/faction.py index 035a87c..6052669 100644 --- a/plugin/faction/faction.py +++ b/plugin/faction/faction.py @@ -38,56 +38,52 @@ class Faction(DBConnection): self.dbh.commit() self.close_db() - def get_all_tracked_factions(self, client, servers = []): + def get_all_tracked_factions(self, servers = []): for row in self.get_all_from_table(client, 'factions', servers): yield TrackedFaction(row) - def get_tracked_faction(self, client, id): + def get_tracked_faction(self, id): row = self.get_from_table(client, 'factions', id) return TrackedFaction(row) if row else None - def get_tracked_factions(self, client): + def get_tracked_factions(self): now = self.now() cursor = self.query("select id, server_id, member_id, channel_id, interval, name, state_id, updated from factions where updated is null or updated < ? - interval and bot_id=?", [now, client.user.id]) for row in cursor.fetchall(): yield TrackedFaction(row) self.close_db() - def create_tracked_faction(self, client, **args): + def create_tracked_faction(self, **args): return self.insert_into_table(client, 'factions', **args) - def update_tracked_faction(self, client, id, **args): + def update_tracked_faction(self, id, **args): return self.update_table(client, 'factions', id, **args) - def delete_tracked_faction(self, client, id): + def delete_tracked_faction(self, id): return self.delete_from_table(client, 'factions', id) def valid_commands(self): return ['faction'] - @asyncio.coroutine - def on_ready(self): + async def on_ready(self): waittime = 60 while True: # Convert to list because we will be sharing the cursor. - for faction in list(self.get_tracked_factions(client)): - yield from self.report_tracked_faction(faction) + for faction in list(self.get_tracked_factions()): + await self.report_tracked_faction(faction) - yield from asyncio.sleep(waittime) + await asyncio.sleep(waittime) - @asyncio.coroutine - def handle_command(self, message, command, raw): + async def handle_command(self, message, command, raw): if command not in self.valid_commands(): return PluginCommand.ignored - yield from self.manage_faction(message, shlex.split(raw)[1:]) + await self.manage_faction(message, shlex.split(raw)[1:]) return PluginCommand.exclusive - @asyncio.coroutine - def handle_help(self, message, command, *args): - yield from self.help_faction(message, *args) + async def handle_help(self, message, command, *args): + await self.help_faction(message, *args) - @asyncio.coroutine - def help_faction(self, message, command = None): + async def help_faction(self, message, command = None): if command is None: lines = [ 'Commands to track faction states are:', @@ -134,35 +130,33 @@ class Faction(DBConnection): ] else: lines = ['*shrugs*'] - yield from bot.say_many(message.channel, lines) + await bot.say_many(message.channel, lines) - @asyncio.coroutine - def manage_faction(self, message, args): + async def manage_faction(self, message, args): if not len(args): - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) return command = args[0] if command == 'help': - yield from self.help_faction(message) + await self.help_faction(message) return elif command == 'list': - yield from self.list_tracked_factions(message) + await self.list_tracked_factions(message) elif command == 'show': - yield from self.show_faction(message, args[1]) + await self.show_faction(message, args[1]) elif command == 'edit': - yield from self.edit_faction(message, args[1:]) + await self.edit_faction(message, args[1:]) elif command == 'track': - yield from self.track_faction(message, args[1:]) + await self.track_faction(message, args[1:]) elif command == 'untrack': - yield from self.untrack_faction(message, args[1:]) + await self.untrack_faction(message, args[1:]) else: - yield from self.show_faction(message, args[0]) + await self.show_faction(message, args[0]) - @asyncio.coroutine - def list_tracked_factions(self, message): + async def list_tracked_factions(self, message): results = [] - for faction in self.get_all_tracked_factions(client, self.get_message_servers(client, message, True)): + for faction in self.get_all_tracked_factions(self.get_message_servers(client, message, True)): if faction.is_private and not faction.owned_by(message.author): continue text = 'Tracking **{}** faction *{}*'.format(faction.id, faction.name) @@ -177,13 +171,12 @@ class Faction(DBConnection): results.append(text) if len(results): - yield from bot.say_many(message.channel, results) + await bot.say_many(message.channel, results) else: - yield from cat.shrug(message.channel) + await cat.shrug(message.channel) - @asyncio.coroutine - def show_tracked_faction(self, message, id): - faction = yield from self.can_manage_factions(message.author, message.channel, 'show', id = id) + async def show_tracked_faction(self, message, id): + faction = await self.can_manage_factions(message.author, message.channel, 'show', id = id) if not faction: return lines = [] @@ -219,15 +212,14 @@ class Faction(DBConnection): text += ' in <#{}>'.format(faction.channel_id) lines.append(text) - yield from bot.say_many(message.channel, lines) + await bot.say_many(message.channel, lines) - @asyncio.coroutine - def show_faction(self, message, name): + async def show_faction(self, message, name): if self.is_uuid(name): - yield from self.show_tracked_faction(message, name) + await self.show_tracked_faction(message, name) return - faction = yield from self.get_faction(message.channel, name) + faction = await self.get_faction(message.channel, name) if faction is None: return @@ -245,8 +237,8 @@ class Faction(DBConnection): if state == 'None': state = 'possible conflict outside home system' text += ' in {}'.format(state) - yield from bot.say(message.channel, text) - yield from self.report_faction(message.channel, TrackedFaction(faction), True) + await bot.say(message.channel, text) + await self.report_faction(message.channel, TrackedFaction(faction), True) def state_advice(self, state): if state == 'Boom': @@ -271,8 +263,7 @@ class Faction(DBConnection): return "EDDB only records conflict states when they occur within a faction's home system. If the external state is Election, **combat** does not affect influence. If it is War or Civil War, *only* **combat** affects influence." return None - @asyncio.coroutine - def report_faction(self, destination, faction, terse = False): + async def report_faction(self, destination, faction, terse = False): state = eddb.state_name(faction.state_id) advice = self.state_advice(state) if state == 'None': @@ -283,10 +274,9 @@ class Faction(DBConnection): if advice is not None: lines.append(advice) if len(lines): - yield from bot.say_many(destination, lines) + await bot.say_many(destination, lines) - @asyncio.coroutine - def report_tracked_faction(self, tracked): + async def report_tracked_faction(self, tracked): update = {} update['updated'] = self.now() @@ -295,7 +285,7 @@ class Faction(DBConnection): for server in client.servers: try: member = server.get_member(tracked.member_id) - channel = yield from client.start_private_message(member) + channel = await client.start_private_message(member) break except: log.exception('report_tracked_faction') @@ -303,43 +293,42 @@ class Faction(DBConnection): channel = client.get_channel(tracked.channel_id) if channel is not None: - result = yield from self.get_faction(None, tracked.name) + result = await self.get_faction(None, tracked.name) if result is not None: faction = TrackedFaction(result) update['state_id'] = faction.state_id - yield from self.report_faction(channel, faction) + await self.report_faction(channel, faction) else: log.warning("Can't find tracked faction {}!".format(tracked.name)) else: log.warning("Can't get channel for faction {}".format(tracked.id)) - self.update_tracked_faction(client, tracked.id, **update) + self.update_tracked_faction(tracked.id, **update) - @asyncio.coroutine - def get_faction(self, destination, name): + async def get_faction(self, destination, name): factions = eddb.find_faction(name) if not len(factions): log.info('No factions matching {}.'.format(name)) if destination is not None: - yield from bot.say(destination, '{}?'.format(name)) + await bot.say(destination, '{}?'.format(name)) return None if len(factions) > 1: if factions[0]['name'].lower() == name.lower(): return factions[0] log.info('Multiple factions matching {}.'.format(name)) if destination is not None: - yield from bot.say_many(destination, [faction['name'] for faction in factions]) + await bot.say_many(destination, [faction['name'] for faction in factions]) return None return factions[0] - def can_manage_factions(self, author, channel, command, **args): + async def can_manage_factions(self, author, channel, command, **args): # Anyone can list factions. if command == 'list': log.debug('Anyone can list factions.') return True if 'id' in args: - faction = self.get_tracked_faction(client, args['id']) + faction = self.get_tracked_faction(args['id']) elif 'create' in args: faction = args['create'] else: @@ -387,7 +376,7 @@ class Faction(DBConnection): log.debug('Member {} with role {} on server {} can create factions.'.format(member.name, member.top_role.name, server.name)) return faction - yield from cat.hiss(channel) + await cat.hiss(channel) # Map a database key to a syntax parameter. def faction_key(self, k): @@ -400,8 +389,7 @@ class Faction(DBConnection): else: return k - @asyncio.coroutine - def parse_faction(self, message, args, editing = False): + async def parse_faction(self, message, args, editing = False): parsed = {} name = None @@ -484,23 +472,23 @@ class Faction(DBConnection): if not ok: log.info('Failed to parse faction track. Got: {}'.format(parsed)) - yield from bot.say(message.channel, '{}?'.format(arg)) + await bot.say(message.channel, '{}?'.format(arg)) return None if not editing and 'name' not in parsed: log.info('No faction name provided.') - yield from bot.say(message.channel, 'Who?') + await bot.say(message.channel, 'Who?') return None if not editing and 'channel_id' not in parsed: log.info('No channel provided.') - yield from bot.say(message.channel, 'Channel?') + await bot.say(message.channel, 'Channel?') return None if not editing: parsed['updated'] = self.now() if 'name' in parsed or not editing: - faction = yield from self.get_faction(message.channel, parsed['name']) + faction = await self.get_faction(message.channel, parsed['name']) if faction is None: return None parsed['name'] = faction['name'] @@ -508,63 +496,62 @@ class Faction(DBConnection): return parsed - def track_faction(self, message, args): - create = yield from self.parse_faction(message, args) + async def track_faction(self, message, args): + create = await self.parse_faction(message, args) if create is None: return - faction = yield from self.can_manage_factions(message.author, message.channel, 'create', create = TrackedFaction(create)) + faction = await self.can_manage_factions(message.author, message.channel, 'create', create = TrackedFaction(create)) if not faction: return - id = self.create_tracked_faction(client, **create) + id = self.create_tracked_faction(**create) if id: create['id'] = id log.info('Tracking {}'.format(create)) - yield from bot.say(message.channel, id) - yield from cat.purr(None, bot.voice_channel_for_user(message.author), join = False) - yield from self.report_tracked_faction(faction) + await bot.say(message.channel, id) + await cat.purr(None, bot.voice_channel_for_user(message.author), join = False) + await self.report_tracked_faction(self.get_tracked_faction(id)) else: log.info('Failed to track faction: {}'.format(create)) - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - def edit_faction(self, message, args): - update = yield from self.parse_faction(message, args, True) + async def edit_faction(self, message, args): + update = await self.parse_faction(message, args, True) if update is None: return id = update['id'] del(update['id']) - faction = yield from self.can_manage_factions(message.author, message.channel, 'edit', id = id) + faction = await self.can_manage_factions(message.author, message.channel, 'edit', id = id) if not faction: return if not len(update.keys()): - yield from bot.say(message.channel, '?') + await bot.say(message.channel, '?') return - if self.update_tracked_faction(client, id, **update): + if self.update_tracked_faction(id, **update): log.info('Edited faction {}: {}'.format(id, update)) - yield from bot.say(message.channel, ', '.join([self.faction_key(k) for k in update])) - yield from cat.purr(None, bot.voice_channel_for_user(message.author), join = False) + await bot.say(message.channel, ', '.join([self.faction_key(k) for k in update])) + await cat.purr(None, bot.voice_channel_for_user(message.author), join = False) else: log.info('Failed to edit faction {}: {}'.format(id, update)) - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def untrack_faction(self, message, args): + async def untrack_faction(self, message, args): if not len(args): - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) id = args[0] - faction = yield from self.can_manage_factions(message.author, message.channel, 'delete', id = id) + faction = await self.can_manage_factions(message.author, message.channel, 'delete', id = id) if not faction: return if bot.get('dryrun'): log.info('Not deleting faction {}'.format(id)) else: log.info('Deleting faction {}'.format(id)) - if self.delete_tracked_faction(client, id): - yield from cat.purr(message.channel) + if self.delete_tracked_faction(id): + await cat.purr(message.channel) else: - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) -- 2.7.4