toolforge is a simple Python 3 library to help with some common tasks that Toolforge users might encounter. Source code is published on Github. Patches welcome.
pip install toolforge.
An example how to connect to a replicating database without needing to remember all the various options.
import toolforge conn = toolforge.connect('enwiki') # You can also use "enwiki_p" # conn is a pymysql.connection object. with conn.cursor() as cur: cur.execute(query) # Or something....
(Please keep the connection handling policy in mind – web tools should do this per request, not during application initialization.)
Set the default requests user-agent to one that complies with the Wikimedia User-Agent policy:
import requests import toolforge toolforge.set_user_agent('mycooltool') # Sets user-agent to: # mycooltool (https://tools.wmflabs.org/mycooltool; firstname.lastname@example.org) python-requests/2.13.0 requests.get('...')
Make flask tools require HTTPS:
from flask import Flask import toolforge app = Flask(__name__) app.before_request(toolforge.redirect_to_https)