Browse Source

Handle XDG paths for the database file

The default file name is now "{muc}.db", with "{muc}" substituted with
the name of the current MUC. The substitution done in the user-provided
database file name as well. This makes easy to handle multiple instances
of the bot connecting to several MUCs.

This will also allow one to set up an alternative data directory in the
(future) configuration file, as simply as:
  database = /var/lib/kaabot/{muc}.db

This commit also changes the short option for --database from -db to -b
(like base). This avoids having a single-dash long option.
Matteo Cypriani 3 years ago
1 changed files with 27 additions and 3 deletions
  1. +27

+ 27
- 3 View File

@@ -28,7 +28,8 @@ class KaaBot(sleekxmpp.ClientXMPP):
self.muc = muc
self.nick = nick
self.online_timestamp = None
self.db = dataset.connect('sqlite:///{db}'.format(db=database),
database_path = self.find_database(database, muc)
self.db = dataset.connect('sqlite:///{db}'.format(db=database_path),
engine_kwargs={'connect_args': {
'check_same_thread': False}})

@@ -49,6 +50,27 @@ class KaaBot(sleekxmpp.ClientXMPP):
self.add_event_handler("muc::%s::got_offline" % self.muc,

def find_database(database, muc):
"""Returns the path to the database to use for the given MUC.

If `database` is empty, the file name is generated from the MUC's name
`muc` in the first "kaabot" XDG data dir (usually

If it contains a value, it is assumed to be the path to the database. If
the name contains the string "{muc}", it will be substituted with the
MUC's name.

The returned path may or may not exist in the file system.
if database:
return database.format(muc=muc)

data_dir = xdg.BaseDirectory.save_data_path("kaabot")
database = "{muc}.db".format(muc=muc)
return "{}/{}".format(data_dir, database)

def init_vocabulary(vocabulary_file):
"""Reads the vocabulary from a JSON file.
@@ -314,8 +336,10 @@ if __name__ == '__main__':
dest='loglevel', const=logging.DEBUG,
argp.add_argument("-db", "--database", dest="database",
help="database to use", default="muc_log.db")
argp.add_argument("-b", "--database", dest="database",
help='path to an alternative database; the "{muc}" string'
" in the name will be substituted with the MUC's name as"
" provided by the --muc option")
argp.add_argument("-j", "--jid", dest="jid", help="JID to use")
argp.add_argument("-p", "--password", dest="password",
help="password to use")