Browse Source

Template.has_param() -> Template.has()

tags/v0.3
Ben Kurtovic 10 years ago
parent
commit
a7dda77474
7 changed files with 29 additions and 21 deletions
  1. +2
    -0
      CHANGELOG
  2. +1
    -1
      README.rst
  3. +8
    -5
      docs/changelog.rst
  4. +1
    -1
      docs/usage.rst
  5. +5
    -2
      mwparserfromhell/nodes/template.py
  6. +1
    -1
      tests/test_docs.py
  7. +11
    -11
      tests/test_template.py

+ 2
- 0
CHANGELOG View File

@@ -11,6 +11,8 @@ v0.3 (unreleased):
remove() now accepts other Wikicode objects and strings representing parts of remove() now accepts other Wikicode objects and strings representing parts of
wikitext, instead of just nodes. These methods also make all possible wikitext, instead of just nodes. These methods also make all possible
substitutions instead of just one. substitutions instead of just one.
- Renamed Template.has_param() to has() for consistency with Template's other
methods; has_param() is now an alias.
- The C tokenizer extension now works on Python 3 in addition to Python 2.7. - The C tokenizer extension now works on Python 3 in addition to Python 2.7.
- Various fixes and cleanup. - Various fixes and cleanup.




+ 1
- 1
README.rst View File

@@ -90,7 +90,7 @@ whitespace::
>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
>>> code = mwparserfromhell.parse(text) >>> code = mwparserfromhell.parse(text)
>>> for template in code.filter_templates(): >>> for template in code.filter_templates():
... if template.name.matches("Cleanup") and not template.has_param("date"):
... if template.name.matches("Cleanup") and not template.has("date"):
... template.add("date", "July 2012") ... template.add("date", "July 2012")
... ...
>>> print code >>> print code


+ 8
- 5
docs/changelog.rst View File

@@ -16,11 +16,14 @@ Unreleased
if you rely on any filter() methods being non-recursive by default.** if you rely on any filter() methods being non-recursive by default.**
- Added a :py:meth:`.matches` method to :py:class:`~.Wikicode` for - Added a :py:meth:`.matches` method to :py:class:`~.Wikicode` for
page/template name comparisons. page/template name comparisons.
- The *obj* param of :py:meth:`Wikicode.insert_before <.insert_before>`,
:py:meth:`~.insert_after`, :py:meth:`~.replace`, and :py:meth:`~.remove` now
accepts :py:class:`~.Wikicode` objects and strings representing parts of
wikitext, instead of just nodes. These methods also make all possible
substitutions instead of just one.
- The *obj* param of :py:meth:`Wikicode.insert_before() <.insert_before>`,
:py:meth:`~.insert_after`, :py:meth:`~.Wikicode.replace`, and
:py:meth:`~.Wikicode.remove` now accepts :py:class:`~.Wikicode` objects and
strings representing parts of wikitext, instead of just nodes. These methods
also make all possible substitutions instead of just one.
- Renamed :py:meth:`Template.has_param() <.has_param>` to
:py:meth:`~.Template.has` for consistency with :py:class:`~.Template`\ 's
other methods; :py:meth:`~.has_param` is now an alias.
- The C tokenizer extension now works on Python 3 in addition to Python 2.7. - The C tokenizer extension now works on Python 3 in addition to Python 2.7.
- Various fixes and cleanup. - Various fixes and cleanup.




+ 1
- 1
docs/usage.rst View File

@@ -58,7 +58,7 @@ names, which takes care of capitalization and whitespace::
>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
>>> code = mwparserfromhell.parse(text) >>> code = mwparserfromhell.parse(text)
>>> for template in code.filter_templates(): >>> for template in code.filter_templates():
... if template.name.matches("Cleanup") and not template.has_param("date"):
... if template.name.matches("Cleanup") and not template.has("date"):
... template.add("date", "July 2012") ... template.add("date", "July 2012")
... ...
>>> print code >>> print code


+ 5
- 2
mwparserfromhell/nodes/template.py View File

@@ -164,7 +164,7 @@ class Template(Node):
def name(self, value): def name(self, value):
self._name = parse_anything(value) self._name = parse_anything(value)


def has_param(self, name, ignore_empty=True):
def has(self, name, ignore_empty=True):
"""Return ``True`` if any parameter in the template is named *name*. """Return ``True`` if any parameter in the template is named *name*.


With *ignore_empty*, ``False`` will be returned even if the template With *ignore_empty*, ``False`` will be returned even if the template
@@ -180,6 +180,9 @@ class Template(Node):
return True return True
return False return False


has_param = lambda self, *args, **kwargs: self.has(*args, **kwargs)
has_param.__doc__ = "Alias for :py:meth:`has`."

def get(self, name): def get(self, name):
"""Get the parameter whose name is *name*. """Get the parameter whose name is *name*.


@@ -226,7 +229,7 @@ class Template(Node):
name, value = parse_anything(name), parse_anything(value) name, value = parse_anything(name), parse_anything(value)
self._surface_escape(value, "|") self._surface_escape(value, "|")


if self.has_param(name):
if self.has(name):
self.remove(name, keep_field=True) self.remove(name, keep_field=True)
existing = self.get(name) existing = self.get(name)
if showkey is not None: if showkey is not None:


+ 1
- 1
tests/test_docs.py View File

@@ -90,7 +90,7 @@ class TestDocs(unittest.TestCase):
text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
code = mwparserfromhell.parse(text) code = mwparserfromhell.parse(text)
for template in code.filter_templates(): for template in code.filter_templates():
if template.name.matches("Cleanup") and not template.has_param("date"):
if template.name.matches("Cleanup") and not template.has("date"):
template.add("date", "July 2012") template.add("date", "July 2012")
res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}" res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}"
self.assertPrint(code, res) self.assertPrint(code, res)


+ 11
- 11
tests/test_template.py View File

@@ -115,23 +115,23 @@ class TestTemplate(TreeEqualityTestCase):
self.assertEqual([], node1.params) self.assertEqual([], node1.params)
self.assertIs(plist, node2.params) self.assertIs(plist, node2.params)


def test_has_param(self):
"""test Template.has_param()"""
def test_has(self):
"""test Template.has()"""
node1 = Template(wraptext("foobar")) node1 = Template(wraptext("foobar"))
node2 = Template(wraptext("foo"), node2 = Template(wraptext("foo"),
[pgenh("1", "bar"), pgens("\nabc ", "def")]) [pgenh("1", "bar"), pgens("\nabc ", "def")])
node3 = Template(wraptext("foo"), node3 = Template(wraptext("foo"),
[pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")])
self.assertFalse(node1.has_param("foobar"))
self.assertTrue(node2.has_param(1))
self.assertTrue(node2.has_param("abc"))
self.assertFalse(node2.has_param("def"))
self.assertTrue(node3.has_param("1"))
self.assertTrue(node3.has_param(" b "))
self.assertFalse(node4.has_param("b"))
self.assertTrue(node3.has_param("b", False))
self.assertTrue(node4.has_param("b", False))
self.assertFalse(node1.has("foobar"))
self.assertTrue(node2.has(1))
self.assertTrue(node2.has("abc"))
self.assertFalse(node2.has("def"))
self.assertTrue(node3.has("1"))
self.assertTrue(node3.has(" b "))
self.assertFalse(node4.has("b"))
self.assertTrue(node3.has("b", False))
self.assertTrue(node4.has("b", False))


def test_get(self): def test_get(self):
"""test Template.get()""" """test Template.get()"""


Loading…
Cancel
Save