~ubuntu-branches/ubuntu/utopic/mako/utopic-proposed

« back to all changes in this revision

Viewing changes to mako/ast.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2014-06-10 20:38:26 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20140610203826-5gtppywd9v3gf14a
Tags: 1.0.0-1
* New upstream release
* Add python-changelog and python-sphinx-paramlinks to Build-Depends
  (needed while rebuilding documentation)
* Enable Python 3.X tests during build (add necessary packages to
  Build-Depends)
* Update links to upstream changelog (now points to changelog.rst)
* Add lintian override for source-is-missing doc/searchindex.js
  (this file is generated by sphinx-build, all sources are in the tarball)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# mako/ast.py
2
 
# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
 
2
# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
3
3
#
4
4
# This module is part of Mako and is released under
5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
8
8
code, as well as generating Python from AST nodes"""
9
9
 
10
10
from mako import exceptions, pyparser, compat
11
 
from mako.compat import arg_stringname
12
11
import re
13
12
 
14
13
class PythonCode(object):
107
106
        f.visit(expr)
108
107
        if not hasattr(self, 'funcname'):
109
108
            raise exceptions.CompileException(
110
 
                              "Code '%s' is not a function declaration" % code,
111
 
                              **exception_kwargs)
 
109
                            "Code '%s' is not a function declaration" % code,
 
110
                            **exception_kwargs)
112
111
        if not allow_kwargs and self.kwargs:
113
112
            raise exceptions.CompileException(
114
113
                                "'**%s' keyword argument not allowed here" %
115
 
                                self.argnames[-1], **exception_kwargs)
116
 
 
117
 
    def get_argument_expressions(self, include_defaults=True):
118
 
        """return the argument declarations of this FunctionDecl as a printable
119
 
        list."""
 
114
                                self.kwargnames[-1], **exception_kwargs)
 
115
 
 
116
    def get_argument_expressions(self, as_call=False):
 
117
        """Return the argument declarations of this FunctionDecl as a printable
 
118
        list.
 
119
 
 
120
        By default the return value is appropriate for writing in a ``def``;
 
121
        set `as_call` to true to build arguments to be passed to the function
 
122
        instead (assuming locals with the same names as the arguments exist).
 
123
        """
120
124
 
121
125
        namedecls = []
122
 
        defaults = [d for d in self.defaults]
123
 
        kwargs = self.kwargs
124
 
        varargs = self.varargs
125
 
        argnames = [f for f in self.argnames]
126
 
        argnames.reverse()
127
 
        for arg in argnames:
128
 
            default = None
129
 
            if kwargs:
130
 
                arg = "**" + arg_stringname(arg)
131
 
                kwargs = False
132
 
            elif varargs:
133
 
                arg = "*" + arg_stringname(arg)
134
 
                varargs = False
135
 
            else:
136
 
                default = len(defaults) and defaults.pop() or None
137
 
            if include_defaults and default:
138
 
                namedecls.insert(0, "%s=%s" %
139
 
                            (arg,
140
 
                            pyparser.ExpressionGenerator(default).value()
141
 
                            )
142
 
                        )
143
 
            else:
144
 
                namedecls.insert(0, arg)
 
126
 
 
127
        # Build in reverse order, since defaults and slurpy args come last
 
128
        argnames = self.argnames[::-1]
 
129
        kwargnames = self.kwargnames[::-1]
 
130
        defaults = self.defaults[::-1]
 
131
        kwdefaults = self.kwdefaults[::-1]
 
132
 
 
133
        # Named arguments
 
134
        if self.kwargs:
 
135
            namedecls.append("**" + kwargnames.pop(0))
 
136
 
 
137
        for name in kwargnames:
 
138
            # Keyword-only arguments must always be used by name, so even if
 
139
            # this is a call, print out `foo=foo`
 
140
            if as_call:
 
141
                namedecls.append("%s=%s" % (name, name))
 
142
            elif kwdefaults:
 
143
                default = kwdefaults.pop(0)
 
144
                if default is None:
 
145
                    # The AST always gives kwargs a default, since you can do
 
146
                    # `def foo(*, a=1, b, c=3)`
 
147
                    namedecls.append(name)
 
148
                else:
 
149
                    namedecls.append("%s=%s" % (
 
150
                        name, pyparser.ExpressionGenerator(default).value()))
 
151
            else:
 
152
                namedecls.append(name)
 
153
 
 
154
        # Positional arguments
 
155
        if self.varargs:
 
156
            namedecls.append("*" + argnames.pop(0))
 
157
 
 
158
        for name in argnames:
 
159
            if as_call or not defaults:
 
160
                namedecls.append(name)
 
161
            else:
 
162
                default = defaults.pop(0)
 
163
                namedecls.append("%s=%s" % (
 
164
                    name, pyparser.ExpressionGenerator(default).value()))
 
165
 
 
166
        namedecls.reverse()
145
167
        return namedecls
146
168
 
 
169
    @property
 
170
    def allargnames(self):
 
171
        return tuple(self.argnames) + tuple(self.kwargnames)
 
172
 
147
173
class FunctionArgs(FunctionDecl):
148
174
    """the argument portion of a function declaration"""
149
175