Skip to main content

MediaWiki template parser and editor

Project description

Build status Test coverage Latest Version Supported Python versions Downloads

mwtemplates is a simple MediaWiki wikitext template parser and editor, based on a Python rewrite of the MediaWiki preprocessorDOM.php. Tested with python 2.6, 2.7, 3.2, 3.3 and 3.4.

Installation

The package is on PyPI, so you can install it using pip, easy_install or similar:

$ pip install mwtemplates

Running tests

To run tests, clone the repo and do:

$ pip install -r requirements.txt
$ pip install -r test_requirements.txt
$ py.test -x tests --pep8 mwtemplates -v --cov mwtemplates --doctest-modules

Usage examples

Editing a template:

from mwtemplates import TemplateEditor
txt = u"""
{{Infoboks geografi
| kart = Svalbard_kart1.png
| land = Norge
| status = Øygruppe
| administrasjon = [[Odd Olsen Ingerø]] ''<small>(2009)</small>''
| administrasjonsnavn = [[Sysselmannen på Svalbard|Sysselmann]]
| areal = 61022
| befolkning = 2527
| befolkningsår = [[2011]]
| url = www.sysselmannen.svalbard.no
}}
"""
te = TemplateEditor(txt)
te.templates['infoboks geografi'][0].parameters['land'] = 'Russland'
print te.wikitext()

Updating a page on Wikipedia using mwclient

from mwclient import Site
from mwtemplates import TemplateEditor

site = Site('en.wikipedia.org')
site.login('USERNAME', 'PASSWORD')
page = site.pages['SOME_PAGE']
te = TemplateEditor(page.edit())
if 'SOME_TEMPLATE' in page.templates:
   tpl = te.templates['SOME_TEMPLATE'][0]
   tpl.parameters['test'] = 'Hello'
page.save(te.wikitext(), summary='...')

Removing a template argument:

from mwtemplates import TemplateEditor
te = TemplateEditor(u"Hello {{mytpl | a=2 | b=3 | c=4 }} world")
te.templates['mytpl'].parameters.remove('b')

Removing the first instance of a template:

from mwtemplates import TemplateEditor
te = TemplateEditor(u"Hello {{mytpl}} world {{mytpl}}")
te.templates['mytpl'][0].remove()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwtemplates-0.3.2.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file mwtemplates-0.3.2.tar.gz.

File metadata

  • Download URL: mwtemplates-0.3.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mwtemplates-0.3.2.tar.gz
Algorithm Hash digest
SHA256 f483caf5d95d9dfe5eb1da48dc0d3c9beec551308674329ea60bda8858413b98
MD5 e1e41bade4732fb5022feb3d05e22174
BLAKE2b-256 ee7dfaa12ec725f84908311004c1acce090413e8ebea71e77590070cb86f0a51

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page