User:Legoktm/toolforge library

From Wikitech
Jump to: navigation, search

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.

Installation

pip install toolforge.

Examples

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....

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; tools.mycooltool@tools.wmflabs.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)