diff --git a/LastFM/__init__.py b/LastFM/__init__.py index 8d3bdc8..cc822f8 100644 --- a/LastFM/__init__.py +++ b/LastFM/__init__.py @@ -50,19 +50,20 @@ __contributors__ = { supybot.Author("Pavel Dvořák", "czshadow", "czshadow@gmail.com"): ["misc"], supybot.Author('James Lu', 'GLolol', - 'GLolol@overdrivenetworks.com'): + 'james@overdrivenetworks.com'): ['Python 3 support', 'rewriting most of the plugin'] } # This is a url where the most recent plugin package can be downloaded. -__url__ = 'https://github.com/GLolol/SupyPlugins/tree/master/LastFM' +__url__ = 'https://github.com/jlu5/SupyPlugins/tree/master/LastFM' from . import config from . import plugin from imp import reload reload(plugin) # In case we're being reloaded. -# Add more reloads here if you add third-party modules and want them to be -# reloaded when this plugin is reloaded. Don't forget to import them as well! + +from .local import accountsdb +reload(accountsdb) if world.testing: from . import test diff --git a/LastFM/local/__init__.py b/LastFM/local/__init__.py new file mode 100644 index 0000000..e86e97b --- /dev/null +++ b/LastFM/local/__init__.py @@ -0,0 +1 @@ +# Stub so local is a module, used for third-party modules diff --git a/LastFM/local/accountsdb.py b/LastFM/local/accountsdb.py new file mode 120000 index 0000000..316f271 --- /dev/null +++ b/LastFM/local/accountsdb.py @@ -0,0 +1 @@ +../../accountsdb.py \ No newline at end of file diff --git a/LastFM/plugin.py b/LastFM/plugin.py index 7c49900..f1ee6c9 100644 --- a/LastFM/plugin.py +++ b/LastFM/plugin.py @@ -43,61 +43,7 @@ import supybot.ircdb as ircdb import json from datetime import datetime -import pickle - -class LastFMDB(): - """ - Holds the database LastFM IDs of all known LastFM IDs. - - This stores users by their bot account first, falling back to their - ident@host if they are not logged in. - """ - - def __init__(self, *args, **kwargs): - """ - Loads the existing database, creating a new one in memory if none - exists. - """ - self.db = {} - try: - with open(filename, 'rb') as f: - self.db = pickle.load(f) - except Exception as e: - log.debug('LastFM: Unable to load database, creating ' - 'a new one: %s', e) - - def flush(self): - """Exports the database to a file.""" - try: - with open(filename, 'wb') as f: - pickle.dump(self.db, f, 2) - except Exception as e: - log.warning('LastFM: Unable to write database: %s', e) - - def set(self, prefix, newId): - """Sets a user ID given the user's prefix.""" - - try: # Try to first look up the caller as a bot account. - userobj = ircdb.users.getUser(prefix) - except KeyError: # If that fails, store them by nick@host. - user = prefix.split('!', 1)[1] - else: - user = userobj.name - - self.db[user] = newId - - def get(self, prefix): - """Sets a user ID given the user's prefix.""" - - try: # Try to first look up the caller as a bot account. - userobj = ircdb.users.getUser(prefix) - except KeyError: # If that fails, store them by nick@host. - user = prefix.split('!', 1)[1] - else: - user = userobj.name - - # Automatically returns None if entry does not exist - return self.db.get(user) +from .local import accountsdb class LastFM(callbacks.Plugin): threaded = True @@ -105,7 +51,7 @@ class LastFM(callbacks.Plugin): def __init__(self, irc): self.__parent = super(LastFM, self) self.__parent.__init__(irc) - self.db = LastFMDB(filename) + self.db = accountsdb.AccountsDB("LastFM", filename) world.flushers.append(self.db.flush) # 2.0 API (see http://www.lastfm.de/api/intro) diff --git a/accountsdb.py b/accountsdb.py new file mode 100644 index 0000000..c205434 --- /dev/null +++ b/accountsdb.py @@ -0,0 +1,63 @@ +""" +Shared code for database handling. +""" + +import pickle + +from supybot import ircdb, log + +class AccountsDB(): + """ + Abstraction to map users to third-party account names. + + This stores users by their bot account first, falling back to their + ident@host if they are not logged in. + """ + + def __init__(self, plugin_name, filename): + """ + Loads the existing database, creating a new one in memory if none + exists. + """ + self.db = {} + self._plugin_name = plugin_name + self.filename = filename + try: + with open(self.filename, 'rb') as f: + self.db = pickle.load(f) + except Exception as e: + log.debug('%s: Unable to load database, creating ' + 'a new one: %s', self._plugin_name, e) + + def flush(self): + """Exports the database to a file.""" + try: + with open(self.filename, 'wb') as f: + pickle.dump(self.db, f, 2) + except Exception as e: + log.warning('%s: Unable to write database: %s', self._plugin_name, e) + + def set(self, prefix, newId): + """Sets a user ID given the user's prefix.""" + + try: # Try to first look up the caller as a bot account. + userobj = ircdb.users.getUser(prefix) + except KeyError: # If that fails, store them by nick@host. + user = prefix.split('!', 1)[1] + else: + user = userobj.name + + self.db[user] = newId + + def get(self, prefix): + """Sets a user ID given the user's prefix.""" + + try: # Try to first look up the caller as a bot account. + userobj = ircdb.users.getUser(prefix) + except KeyError: # If that fails, store them by nick@host. + user = prefix.split('!', 1)[1] + else: + user = userobj.name + + # Automatically returns None if entry does not exist + return self.db.get(user)