diff --git a/LastFM/local/accountsdb.py b/LastFM/local/accountsdb.py deleted file mode 120000 index 316f271..0000000 --- a/LastFM/local/accountsdb.py +++ /dev/null @@ -1 +0,0 @@ -../../accountsdb.py \ No newline at end of file diff --git a/LastFM/local/accountsdb.py b/LastFM/local/accountsdb.py new file mode 100644 index 0000000..8a22351 --- /dev/null +++ b/LastFM/local/accountsdb.py @@ -0,0 +1,64 @@ +# Autogenerated by update-modules.py on Sun Dec 30 13:09:36 2018 - DO NOT EDIT THIS COPY DIRECTLY! +""" +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) diff --git a/NuWeather/local/accountsdb.py b/NuWeather/local/accountsdb.py deleted file mode 120000 index 316f271..0000000 --- a/NuWeather/local/accountsdb.py +++ /dev/null @@ -1 +0,0 @@ -../../accountsdb.py \ No newline at end of file diff --git a/NuWeather/local/accountsdb.py b/NuWeather/local/accountsdb.py new file mode 100644 index 0000000..8a22351 --- /dev/null +++ b/NuWeather/local/accountsdb.py @@ -0,0 +1,64 @@ +# Autogenerated by update-modules.py on Sun Dec 30 13:09:36 2018 - DO NOT EDIT THIS COPY DIRECTLY! +""" +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) diff --git a/update-modules.py b/update-modules.py new file mode 100755 index 0000000..41844a6 --- /dev/null +++ b/update-modules.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# Refresh shared modules so that PluginDownloader downloads don't break. + +CONFIG = {'accountsdb.py': ('LastFM/local', 'NuWeather/local')} + +import os +import time + +for filename, paths in CONFIG.items(): + with open(filename) as f: + data = f.read() + for path in paths: + target = os.path.join(path, filename) + if os.path.exists(target): + print("Deleting existing %s" % target) + os.remove(target) + print("Writing %s => %s" % (filename, target)) + with open(target, 'w') as wf: + wf.write('# Autogenerated by update-modules.py on %s - DO NOT EDIT THIS COPY DIRECTLY!\n' % time.ctime()) + wf.write(data)