|
@@ -29,6 +29,8 @@ from ..utils import parse_anything |
|
|
__all__ = ["Template"] |
|
|
__all__ = ["Template"] |
|
|
|
|
|
|
|
|
FLAGS = re.DOTALL | re.UNICODE |
|
|
FLAGS = re.DOTALL | re.UNICODE |
|
|
|
|
|
# Used to allow None as a valid fallback value |
|
|
|
|
|
_UNSET = object() |
|
|
|
|
|
|
|
|
class Template(Node): |
|
|
class Template(Node): |
|
|
"""Represents a template in wikicode, like ``{{foo}}``.""" |
|
|
"""Represents a template in wikicode, like ``{{foo}}``.""" |
|
@@ -208,23 +210,30 @@ class Template(Node): |
|
|
return True |
|
|
return True |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
has_param = lambda self, name, ignore_empty=False: \ |
|
|
|
|
|
self.has(name, ignore_empty) |
|
|
|
|
|
has_param.__doc__ = "Alias for :meth:`has`." |
|
|
|
|
|
|
|
|
def has_param(self, name, ignore_empty=False): |
|
|
|
|
|
"""Alias for :meth:`has`.""" |
|
|
|
|
|
return self.has(name, ignore_empty) |
|
|
|
|
|
|
|
|
def get(self, name): |
|
|
|
|
|
|
|
|
def get(self, name, default=_UNSET): |
|
|
"""Get the parameter whose name is *name*. |
|
|
"""Get the parameter whose name is *name*. |
|
|
|
|
|
|
|
|
The returned object is a :class:`.Parameter` instance. Raises |
|
|
The returned object is a :class:`.Parameter` instance. Raises |
|
|
:exc:`ValueError` if no parameter has this name. Since multiple |
|
|
|
|
|
parameters can have the same name, we'll return the last match, since |
|
|
|
|
|
the last parameter is the only one read by the MediaWiki parser. |
|
|
|
|
|
|
|
|
:exc:`ValueError` if no parameter has this name. If *default* is set, |
|
|
|
|
|
returns that instead. Since multiple parameters can have the same name, |
|
|
|
|
|
we'll return the last match, since the last parameter is the only one |
|
|
|
|
|
read by the MediaWiki parser. |
|
|
""" |
|
|
""" |
|
|
name = str(name).strip() |
|
|
name = str(name).strip() |
|
|
for param in reversed(self.params): |
|
|
for param in reversed(self.params): |
|
|
if param.name.strip() == name: |
|
|
if param.name.strip() == name: |
|
|
return param |
|
|
return param |
|
|
raise ValueError(name) |
|
|
|
|
|
|
|
|
if default is _UNSET: |
|
|
|
|
|
raise ValueError(name) |
|
|
|
|
|
else: |
|
|
|
|
|
return default |
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, name): |
|
|
|
|
|
return self.get(name) |
|
|
|
|
|
|
|
|
def add(self, name, value, showkey=None, before=None, |
|
|
def add(self, name, value, showkey=None, before=None, |
|
|
preserve_spacing=True): |
|
|
preserve_spacing=True): |
|
@@ -306,6 +315,9 @@ class Template(Node): |
|
|
self.params.append(param) |
|
|
self.params.append(param) |
|
|
return param |
|
|
return param |
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, name, value): |
|
|
|
|
|
return self.add(name, value) |
|
|
|
|
|
|
|
|
def remove(self, param, keep_field=False): |
|
|
def remove(self, param, keep_field=False): |
|
|
"""Remove a parameter from the template, identified by *param*. |
|
|
"""Remove a parameter from the template, identified by *param*. |
|
|
|
|
|
|
|
@@ -351,3 +363,6 @@ class Template(Node): |
|
|
raise ValueError(name) |
|
|
raise ValueError(name) |
|
|
for i in reversed(to_remove): |
|
|
for i in reversed(to_remove): |
|
|
self.params.pop(i) |
|
|
self.params.pop(i) |
|
|
|
|
|
|
|
|
|
|
|
def __delitem__(self, param): |
|
|
|
|
|
return self.remove(param) |