From 3744a34f28f88c94f71aa79bc823ba20aca2b3c3 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 7 Jul 2012 22:59:15 -0400 Subject: [PATCH] Allow templated SQL connection info. --- docs/toolset.rst | 3 ++- earwigbot/wiki/sitesdb.py | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/toolset.rst b/docs/toolset.rst index c7808d2..fcdfc6d 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -47,7 +47,8 @@ wikis, you can usually use code like this:: site = bot.wiki.add_site(project=project, lang=lang) This works because EarwigBot assumes that the URL for the site is -``"//{lang}.{project}.org"`` and the API is at ``/w/api.php``; this might +``"//{lang}.{project}.org"``, the API is at ``/w/api.php``, and the SQL +connection info (if any) are stored as ``config.wiki["sql"]``. This might change if you're dealing with non-WMF wikis, where the code might look something more like:: diff --git a/earwigbot/wiki/sitesdb.py b/earwigbot/wiki/sitesdb.py index fd3c521..cdff1fe 100644 --- a/earwigbot/wiki/sitesdb.py +++ b/earwigbot/wiki/sitesdb.py @@ -196,6 +196,12 @@ class SitesDB(object): nltk_dir = path.join(self.config.root_dir, ".nltk") search_config["nltk_dir"] = nltk_dir + if not sql: + sql = config.wiki.get("sql", {}) + for key, value in sql.iteritems(): + if "$1" in value: + sql[key] = value.replace("$1", name) + return Site(name=name, project=project, lang=lang, base_url=base_url, article_path=article_path, script_path=script_path, sql=sql, namespaces=namespaces, login=login, @@ -336,13 +342,12 @@ class SitesDB(object): the script path (meaning the API is located at ``"{base_url}{script_path}/api.php"`` -> ``"//{lang}.{project}.org/w/api.php"``), so this is the default. If - your wiki is different, provide the script_path as an argument. The - only other argument to :py:class:`~earwigbot.wiki.site.Site` that we - can't get from config files or by querying the wiki itself is SQL - connection info, so provide a dict of kwargs as *sql* and Site will - pass it to :py:func:`oursql.connect(**sql) `, allowing - you to make queries with :py:meth:`site.sql_query - `. + your wiki is different, provide the script_path as an argument. SQL + connection settings are guessed automatically using config's template + value. If this is wrong or not specified, provide a dict of kwargs as + *sql* and Site will pass it to :py:func:`oursql.connect(**sql) + `, allowing you to make queries with + :py:meth:`site.sql_query `. Returns ``True`` if the site was added successfully or ``False`` if the site is already in our sitesdb (this can be done purposefully to update @@ -375,6 +380,12 @@ class SitesDB(object): nltk_dir = path.join(self.config.root_dir, ".nltk") search_config["nltk_dir"] = nltk_dir + if not sql: + sql = config.wiki.get("sql", {}) + for key, value in sql.iteritems(): + if "$1" in value: + sql[key] = value.replace("$1", name) + # Create a Site object to log in and load the other attributes: site = Site(base_url=base_url, script_path=script_path, sql=sql, login=login, cookiejar=cookiejar, user_agent=user_agent,