1# Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
20
21
22from ._base import Node
23from ..utils import parse_anything
24
25__all__ = ["Argument"]
26
27
28class Argument(Node):
29    """Represents a template argument substitution, like ``{{{foo}}}``."""
30
31    def __init__(self, name, default=None):
32        super().__init__()
33        self.name = name
34        self.default = default
35
36    def __str__(self):
37        start = "{{{" + str(self.name)
38        if self.default is not None:
39            return start + "|" + str(self.default) + "}}}"
40        return start + "}}}"
41
42    def __children__(self):
43        yield self.name
44        if self.default is not None:
45            yield self.default
46
47    def __strip__(self, **kwargs):
48        if self.default is not None:
49            return self.default.strip_code(**kwargs)
50        return None
51
52    def __showtree__(self, write, get, mark):
53        write("{{{")
54        get(self.name)
55        if self.default is not None:
56            write("    | ")
57            mark()
58            get(self.default)
59        write("}}}")
60
61    @property
62    def name(self):
63        """The name of the argument to substitute."""
64        return self._name
65
66    @property
67    def default(self):
68        """The default value to substitute if none is passed.
69
70        This will be ``None`` if the argument wasn't defined with one. The
71        MediaWiki parser handles this by rendering the argument itself in the
72        result, complete braces. To have the argument render as nothing, set
73        default to ``""`` (``{{{arg}}}`` vs. ``{{{arg|}}}``).
74        """
75        return self._default
76
77    @name.setter
78    def name(self, value):
79        self._name = parse_anything(value)
80
81    @default.setter
82    def default(self, default):
83        if default is None:
84            self._default = None
85        else:
86            self._default = parse_anything(default)
87