User:Legoktm/toolforge library

From Wikitech
Jump to navigation Jump to search

toolforge is a simple Python 3 library to help with some common tasks that Toolforge users might encounter. A Rust version is in development now. Source code is published on gitlab.wikimedia.org (Python) and GitLab (Rust). Patches welcome.

Installation

Python:

pip install toolforge

Rust:

[dependencies]
toolforge = "5.0"

Python usage

See the API reference and user guide for usage instructions.

Rust usage

Connect to databases

You will need to enable the mysql feature in the toolforge crate for this functionality.

An example how to connect to a replicating database using the mysql crate.

use mysql::*;
use mysql::prelude::*;

let db_url = toolforge::connection_info!("enwiki")?;
let pool = Pool::new(db_url.to_string())?;
let mut conn = pool.get_conn()?;

Please keep the connection handling policy in mind – web tools should do this per request, not during application initialization.

Set proper user-agent

Get a user-agent that complies with the Wikimedia User-Agent policy:

// Generic:
const USER_AGENT: &str = toolforge::user_agent!("mycooltool");

// mediawiki crate:
let mut api = Api::new(...);
api.set_user_agent(toolforge::user_agent!("mycooltool"));