Skip to main content

MediaWiki template parser and editor

Project description

Build status Test coverage Latest Version Supported Python versions Downloads

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

Installation

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

$ pip install -U mwtemplates

Or you can grab the latest zip from releases.

Introduction

Let’s start by importing TemplateEditor and giving it some wikitext to eat:

>>> from mwtemplates import TemplateEditor
>>> txt = u"""{{Infobox cheese
... | name = Mozzarella
... | protein = 7
... }}
... Mozzarella is a cheese…{{tr}}"""
>>> te = TemplateEditor(txt)

First, we can see what templates the editor found in the text:

>>> te.templates
[<Template:"Infobox cheese" at line 2>, <Template:"Tr" at line 6>]

Each template is an instance of a Template class. Also notice that template names are normalized by upper-casing the first character. Now, we can try investigating the Infobox cheese template:

>>> te.templates['Infobox cheese']
[<Template:"Infobox cheese" at line 2>]

Since there can be several instances of the same template, an array is always returned, and so we need to ask for te.templates[‘Infobox cheese’][0] to get the actual Template. To get the parameters:

>>> te.templates['Infobox cheese'][0].parameters
<Parameters: name="Mozzarella", protein="10">

Let’s say we want to change the value of the protein parameter from 10 to 7. We then use the wikitext() method to return our new wikitext:

>>> te.templates['Infobox cheese'][0].parameters['protein'] = 7
>>> print te.wikitext()
{{Infobox cheese
| name = Mozzarella
| protein = 10
}}
Mozzarella is a cheese{{tr}}

Notice that formatting is preserved. We could now go and add a new parameter like so:

>>> te.templates['Infobox cheese'][0].parameters['fat'] = 25
>>> print te.wikitext()
{{Infobox cheese
| name = Mozzarella
| protein = 7
| fat = 25
}}
Mozzarella is a cheese{{tr}}

Usage with mwclient to edit pages on Wikipedia

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.text())
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()

Contributing

Pull requests are very welcome. Please run tests before submitting:

$ python setup.py test

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.4.0.tar.gz (12.2 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for mwtemplates-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a265958c469d33d7609f91a263b321dee8afac89ba57d918b03ee59eba2a8a28
MD5 6ad21d1045fb3fecb2e456422b910e6f
BLAKE2b-256 df240fb60d8b32695def78bcd27adf749c178638a42dc3de4ea859a4b15290f2

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