Jean-Frédéric has uploaded a new change for review.
https://gerrit.wikimedia.org/r/303428
Change subject: Refactor database configuration handling
......................................................................
Refactor database configuration handling
- Move data from monuments_config to dedicated YAML file
- Read this YAML file from database_connection methods
- Extract hardcoded values for Commons database
- Add PyYAML to requirementst.txt
- Add YAML linting via Tox
Change-Id: Iec629818938ae56c83fe6f540526f20dc15851d9
---
A erfgoedbot/database_config.yml
M erfgoedbot/database_connection.py
M erfgoedbot/monuments_config.py
M requirements.txt
M tox.ini
5 files changed, 37 insertions(+), 14 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/labs/tools/heritage
refs/changes/28/303428/1
diff --git a/erfgoedbot/database_config.yml b/erfgoedbot/database_config.yml
new file mode 100644
index 0000000..811c63a
--- /dev/null
+++ b/erfgoedbot/database_config.yml
@@ -0,0 +1,8 @@
+---
+monuments_db:
+ server: 'tools-db'
+ db_name: 's51138__heritage_p'
+
+commons_db:
+ server: 'commonswiki.labsdb'
+ db_name: 'commonswiki_p'
diff --git a/erfgoedbot/database_connection.py
b/erfgoedbot/database_connection.py
index 7899b38..890075a 100644
--- a/erfgoedbot/database_connection.py
+++ b/erfgoedbot/database_connection.py
@@ -1,24 +1,37 @@
# -*- coding: utf-8 -*-
+
+import os
import MySQLdb
-import monuments_config as mconfig
-from pywikibot import config
+import yaml
+
+from pywikibot import config as pywikibot_config
+
+
+def get_database_config():
+ ""
+ current_dir = os.path.dirname(os.path.abspath(__file__))
+ config_file = os.path.join(current_dir, 'database_config.yml')
+ return yaml.safe_load(open(config_file, 'r'))
def connect_to_monuments_database():
- """Connect to the mysql monuments database, if it fails, go down in
flames."""
+ """Connect to the mysql monuments database."""
+ db_config = get_database_config()['monuments_db']
conn = MySQLdb.connect(
- host=mconfig.db_server, db=mconfig.db, user=config.db_username,
- passwd=config.db_password, use_unicode=True, charset='utf8')
+ host=db_config['server'], db=db_config['db_name'],
+ user=pywikibot_config.db_username, passwd=pywikibot_config.db_password,
+ use_unicode=True, charset='utf8')
conn.ping(True)
cursor = conn.cursor()
return (conn, cursor)
def connect_to_commons_database():
- '''
- Connect to the commons mysql database, if it fails, go down in flames
- '''
- conn = MySQLdb.connect('commonswiki.labsdb', db='commonswiki_p',
- user=config.db_username, passwd=config.db_password,
use_unicode=True, charset='latin1')
+ """Connect to the commons mysql database."""
+ db_config = get_database_config()['commons_db']
+ conn = MySQLdb.connect(
+ host=db_config['server'], db=db_config['db_name'],
+ user=pywikibot_config.db_username, passwd=pywikibot_config.db_password,
+ use_unicode=True, charset='latin1')
cursor = conn.cursor()
return (conn, cursor)
diff --git a/erfgoedbot/monuments_config.py b/erfgoedbot/monuments_config.py
index a462ddd..7a2bbc1 100755
--- a/erfgoedbot/monuments_config.py
+++ b/erfgoedbot/monuments_config.py
@@ -4,10 +4,6 @@
Configuration for the monuments bot.
'''
-
-db_server = 'tools-db'
-db = 's51138__heritage_p'
-
countries = {
('ad', 'ca'): { # Monuments in Andorra in Catalan table
'project': u'wikipedia',
diff --git a/requirements.txt b/requirements.txt
index 54f6c5c..9b2a26e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
pywikibot
MySQL-python
requests
+PyYAML
diff --git a/tox.ini b/tox.ini
index 6923cf0..d6c6ff7 100644
--- a/tox.ini
+++ b/tox.ini
@@ -13,6 +13,11 @@
deps = flake8
commands = flake8
+[testenv:yaml]
+deps = PyYAML
+commands =
+ /bin/bash -c \'set -euo pipefail && find erfgoedbot/ -name "*.yml" |
xargs python -c "import sys,yaml; [yaml.load(open(x)) for x in sys.argv[1:]];"
\'
+
[flake8]
exclude = .venv,.tox
ignore = E501,F841
--
To view, visit https://gerrit.wikimedia.org/r/303428
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec629818938ae56c83fe6f540526f20dc15851d9
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/heritage
Gerrit-Branch: master
Gerrit-Owner: Jean-Frédéric <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits