1 # Copyright (c) 2006-2009 Nathan Binkert <nate@binkert.org>
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 def __init__(self
, formatter
, frame
, *args
, **kwargs
):
35 self
.formatter
= formatter
36 self
.dict = self
.formatter
._dict
41 def __setitem__(self
, item
, val
):
42 self
.locals[item
] = val
44 def __getitem__(self
, item
):
45 if item
in self
.locals:
46 return self
.locals[item
]
48 if item
in self
.kwargs
:
49 return self
.kwargs
[item
]
51 if item
== '__file__':
52 return self
.frame
.f_code
.co_filename
54 if item
== '__line__':
55 return self
.frame
.f_lineno
58 return self
.dict[item
]
60 if self
.formatter
.locals or self
.formatter
.globals:
61 if self
.formatter
.locals and item
in self
.frame
.f_locals
:
62 return self
.frame
.f_locals
[item
]
64 if self
.formatter
.globals and item
in self
.frame
.f_globals
:
65 return self
.frame
.f_globals
[item
]
67 if item
in __builtins__
:
68 return __builtins__
[item
]
72 return self
.args
[item
]
75 raise IndexError, "Could not find '%s'" % item
77 class code_formatter_meta(type):
80 %(delim)s(?P<escaped>%(delim)s) | # escaped delimiter
81 ^(?P<indent>[ ]*)%(delim)s(?P<lone>%(ident)s)$ | # lone identifier
82 %(delim)s(?P<ident>%(ident)s) | # identifier
83 %(delim)s%(lb)s(?P<b_ident>%(ident)s)%(rb)s | # braced identifier
84 %(delim)s(?P<pos>%(pos)s) | # positional parameter
85 %(delim)s%(lb)s(?P<b_pos>%(pos)s)%(rb)s | # braced positional
86 %(delim)s%(ldb)s(?P<eval>.*?)%(rdb)s | # double braced expression
87 %(delim)s(?P<invalid>) # ill-formed delimiter exprs
90 def __init__(cls
, name
, bases
, dct
):
91 super(code_formatter_meta
, cls
).__init
__(name
, bases
, dct
)
95 # tuple expansion to ensure strings are proper length
97 lb1
,lb2
,rb2
,rb1
= cls
.double_braced
98 pat
= code_formatter_meta
.pattern
% {
99 'delim' : re
.escape(cls
.delim
),
102 'lb' : re
.escape(lb
),
103 'rb' : re
.escape(rb
),
104 'ldb' : re
.escape(lb1
+lb2
),
105 'rdb' : re
.escape(rb2
+rb1
),
107 cls
.pattern
= re
.compile(pat
, re
.VERBOSE | re
.DOTALL | re
.MULTILINE
)
109 class code_formatter(object):
110 __metaclass__
= code_formatter_meta
116 double_braced
= r
'{{}}'
121 def __init__(self
, *args
, **kwargs
):
124 self
._indent
_level
= 0
125 self
._indent
_spaces
= 4
126 self
.globals = kwargs
.pop('globals',type(self
).globals)
127 self
.locals = kwargs
.pop('locals', type(self
).locals)
128 self
._fix
_newlines
= \
129 kwargs
.pop('fix_newlines', type(self
).fix_newlines
)
135 self
._indent
_level
+= self
._indent
_spaces
138 assert self
._indent
_level
>= self
._indent
_spaces
139 self
._indent
_level
-= self
._indent
_spaces
141 def fix(self
, status
):
142 previous
= self
._fix
_newlines
143 self
._fix
_newlines
= status
147 previous
= self
._fix
_newlines
148 self
._fix
_newlines
= False
154 def write(self
, *args
):
155 f
= file(os
.path
.join(*args
), "w")
156 for data
in self
._data
:
161 data
= string
.join(self
._data
, '')
162 self
._data
= [ data
]
165 def __getitem__(self
, item
):
166 return self
._dict
[item
]
168 def __setitem__(self
, item
, value
):
169 self
._dict
[item
] = value
171 def __delitem__(self
, item
):
174 def __contains__(self
, item
):
175 return item
in self
._dict
177 def __iadd__(self
, data
):
180 def append(self
, data
):
181 if isinstance(data
, code_formatter
):
182 self
._data
.extend(data
._data
)
184 self
._append
(str(data
))
186 def _append(self
, data
):
187 if not self
._fix
_newlines
:
188 self
._data
.append(data
)
191 initial_newline
= not self
._data
or self
._data
[-1] == '\n'
192 for line
in data
.splitlines():
194 if self
._indent
_level
:
195 self
._data
.append(' ' * self
._indent
_level
)
196 self
._data
.append(line
)
198 if line
or not initial_newline
:
199 self
._data
.append('\n')
201 initial_newline
= False
203 def insert_newline(self
):
204 self
._data
.append('\n')
206 def __call__(self
, format
, *args
, **kwargs
):
207 frame
= inspect
.currentframe().f_back
209 l
= lookup(self
, frame
, *args
, **kwargs
)
211 ident
= match
.group('lone')
212 # check for a lone identifier
214 indent
= match
.group('indent') # must be spaces
215 lone
= '%s' % (l
[ident
], )
217 def indent_lines(gen
):
221 return ''.join(indent_lines(lone
.splitlines(True)))
223 # check for an identifier, braced or not
224 ident
= match
.group('ident') or match
.group('b_ident')
225 if ident
is not None:
226 return '%s' % (l
[ident
], )
228 # check for a positional parameter, braced or not
229 pos
= match
.group('pos') or match
.group('b_pos')
234 ('Positional parameter #%d not found in pattern' % pos
,
235 code_formatter
.pattern
)
236 return '%s' % (args
[int(pos
)], )
238 # check for a double braced expression
239 eval_expr
= match
.group('eval')
240 if eval_expr
is not None:
241 result
= eval(eval_expr
, {}, l
)
242 return '%s' % (result
, )
244 # check for an escaped delimiter
245 if match
.group('escaped') is not None:
248 # At this point, we have to match invalid
249 if match
.group('invalid') is None:
250 # didn't match invalid!
251 raise ValueError('Unrecognized named group in pattern',
252 code_formatter
.pattern
)
254 i
= match
.start('invalid')
259 lines
= format
[:i
].splitlines(True)
260 colno
= i
- reduce(lambda x
,y
: x
+y
, (len(z
) for z
in lines
))
263 raise ValueError('Invalid format string: line %d, col %d' %
266 d
= code_formatter
.pattern
.sub(convert
, format
)
269 __all__
= [ "code_formatter" ]
271 if __name__
== '__main__':
272 from code_formatter
import code_formatter
276 def __init__(self
, **kwargs
):
278 def __getattr__(self
, attr
):
282 l
= [ [Foo(x
=[Foo(y
=9)])] ]
291 f('''$__file__:$__line__
293 f("${{', '.join(str(x) for x in xrange(4))}}")
301 f('$1 $0', "zero", "one")
303 f('${0}asdf', "he went")
307 ${{l[0][0]["x"][0].y}}