mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-04-26 13:01:07 -05:00
Pull LastFMDB into a generic accountsdb module
This commit is contained in:
parent
4e08442cde
commit
f0546d521d
@ -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
|
||||
|
1
LastFM/local/__init__.py
Normal file
1
LastFM/local/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# Stub so local is a module, used for third-party modules
|
1
LastFM/local/accountsdb.py
Symbolic link
1
LastFM/local/accountsdb.py
Symbolic link
@ -0,0 +1 @@
|
||||
../../accountsdb.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)
|
||||
|
63
accountsdb.py
Normal file
63
accountsdb.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user