misc: string.join has been removed in python3
[gem5.git] / src / python / m5 / util / code_formatter.py
1 # Copyright (c) 2006-2009 Nathan Binkert <nate@binkert.org>
2 # All rights reserved.
3 #
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.
14 #
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.
26
27 from __future__ import print_function
28 from six import add_metaclass
29
30 try:
31 import builtins
32 except ImportError:
33 # Python 2 fallback
34 import __builtin__ as builtins
35 import inspect
36 import os
37 import re
38
39 class lookup(object):
40 def __init__(self, formatter, frame, *args, **kwargs):
41 self.frame = frame
42 self.formatter = formatter
43 self.dict = self.formatter._dict
44 self.args = args
45 self.kwargs = kwargs
46 self.locals = {}
47
48 def __setitem__(self, item, val):
49 self.locals[item] = val
50
51 def __getitem__(self, item):
52 if item in self.locals:
53 return self.locals[item]
54
55 if item in self.kwargs:
56 return self.kwargs[item]
57
58 if item == '__file__':
59 return self.frame.f_code.co_filename
60
61 if item == '__line__':
62 return self.frame.f_lineno
63
64 if self.formatter.locals and item in self.frame.f_locals:
65 return self.frame.f_locals[item]
66
67 if item in self.dict:
68 return self.dict[item]
69
70 if self.formatter.globals and item in self.frame.f_globals:
71 return self.frame.f_globals[item]
72
73 if item in builtins.__dict__:
74 return builtins.__dict__[item]
75
76 try:
77 item = int(item)
78 return self.args[item]
79 except ValueError:
80 pass
81 raise IndexError("Could not find '%s'" % item)
82
83 class code_formatter_meta(type):
84 pattern = r"""
85 (?:
86 %(delim)s(?P<escaped>%(delim)s) | # escaped delimiter
87 ^(?P<indent>[ ]*)%(delim)s(?P<lone>%(ident)s)$ | # lone identifier
88 %(delim)s(?P<ident>%(ident)s) | # identifier
89 %(delim)s%(lb)s(?P<b_ident>%(ident)s)%(rb)s | # braced identifier
90 %(delim)s(?P<pos>%(pos)s) | # positional parameter
91 %(delim)s%(lb)s(?P<b_pos>%(pos)s)%(rb)s | # braced positional
92 %(delim)s%(ldb)s(?P<eval>.*?)%(rdb)s | # double braced expression
93 %(delim)s(?P<invalid>) # ill-formed delimiter exprs
94 )
95 """
96 def __init__(cls, name, bases, dct):
97 super(code_formatter_meta, cls).__init__(name, bases, dct)
98 if 'pattern' in dct:
99 pat = cls.pattern
100 else:
101 # tuple expansion to ensure strings are proper length
102 lb,rb = cls.braced
103 lb1,lb2,rb2,rb1 = cls.double_braced
104 pat = code_formatter_meta.pattern % {
105 'delim' : re.escape(cls.delim),
106 'ident' : cls.ident,
107 'pos' : cls.pos,
108 'lb' : re.escape(lb),
109 'rb' : re.escape(rb),
110 'ldb' : re.escape(lb1+lb2),
111 'rdb' : re.escape(rb2+rb1),
112 }
113 cls.pattern = re.compile(pat, re.VERBOSE | re.DOTALL | re.MULTILINE)
114
115 @add_metaclass(code_formatter_meta)
116 class code_formatter(object):
117 delim = r'$'
118 ident = r'[_A-z]\w*'
119 pos = r'[0-9]+'
120 braced = r'{}'
121 double_braced = r'{{}}'
122
123 globals = True
124 locals = True
125 fix_newlines = True
126 def __init__(self, *args, **kwargs):
127 self._data = []
128 self._dict = {}
129 self._indent_level = 0
130 self._indent_spaces = 4
131 self.globals = kwargs.pop('globals', type(self).globals)
132 self.locals = kwargs.pop('locals', type(self).locals)
133 self._fix_newlines = \
134 kwargs.pop('fix_newlines', type(self).fix_newlines)
135
136 if args:
137 self.__call__(args)
138
139 def indent(self, count=1):
140 self._indent_level += self._indent_spaces * count
141
142 def dedent(self, count=1):
143 assert self._indent_level >= (self._indent_spaces * count)
144 self._indent_level -= self._indent_spaces * count
145
146 def fix(self, status):
147 previous = self._fix_newlines
148 self._fix_newlines = status
149 return previous
150
151 def nofix(self):
152 previous = self._fix_newlines
153 self._fix_newlines = False
154 return previous
155
156 def clear():
157 self._data = []
158
159 def write(self, *args):
160 f = open(os.path.join(*args), "w")
161 for data in self._data:
162 f.write(data)
163 f.close()
164
165 def __str__(self):
166 data = ''.join(self._data)
167 self._data = [ data ]
168 return data
169
170 def __getitem__(self, item):
171 return self._dict[item]
172
173 def __setitem__(self, item, value):
174 self._dict[item] = value
175
176 def __delitem__(self, item):
177 del self._dict[item]
178
179 def __contains__(self, item):
180 return item in self._dict
181
182 def __iadd__(self, data):
183 self.append(data)
184
185 def append(self, data):
186 if isinstance(data, code_formatter):
187 self._data.extend(data._data)
188 else:
189 self._append(str(data))
190
191 def _append(self, data):
192 if not self._fix_newlines:
193 self._data.append(data)
194 return
195
196 initial_newline = not self._data or self._data[-1] == '\n'
197 for line in data.splitlines():
198 if line:
199 if self._indent_level:
200 self._data.append(' ' * self._indent_level)
201 self._data.append(line)
202
203 if line or not initial_newline:
204 self._data.append('\n')
205
206 initial_newline = False
207
208 def __call__(self, *args, **kwargs):
209 if not args:
210 self._data.append('\n')
211 return
212
213 format = args[0]
214 args = args[1:]
215
216 frame = inspect.currentframe().f_back
217
218 l = lookup(self, frame, *args, **kwargs)
219 def convert(match):
220 ident = match.group('lone')
221 # check for a lone identifier
222 if ident:
223 indent = match.group('indent') # must be spaces
224 lone = '%s' % (l[ident], )
225
226 def indent_lines(gen):
227 for line in gen:
228 yield indent
229 yield line
230 return ''.join(indent_lines(lone.splitlines(True)))
231
232 # check for an identifier, braced or not
233 ident = match.group('ident') or match.group('b_ident')
234 if ident is not None:
235 return '%s' % (l[ident], )
236
237 # check for a positional parameter, braced or not
238 pos = match.group('pos') or match.group('b_pos')
239 if pos is not None:
240 pos = int(pos)
241 if pos > len(args):
242 raise ValueError \
243 ('Positional parameter #%d not found in pattern' % pos,
244 code_formatter.pattern)
245 return '%s' % (args[int(pos)], )
246
247 # check for a double braced expression
248 eval_expr = match.group('eval')
249 if eval_expr is not None:
250 result = eval(eval_expr, {}, l)
251 return '%s' % (result, )
252
253 # check for an escaped delimiter
254 if match.group('escaped') is not None:
255 return '$'
256
257 # At this point, we have to match invalid
258 if match.group('invalid') is None:
259 # didn't match invalid!
260 raise ValueError('Unrecognized named group in pattern',
261 code_formatter.pattern)
262
263 i = match.start('invalid')
264 if i == 0:
265 colno = 1
266 lineno = 1
267 else:
268 lines = format[:i].splitlines(True)
269 colno = i - reduce(lambda x,y: x+y, (len(z) for z in lines))
270 lineno = len(lines)
271
272 raise ValueError('Invalid format string: line %d, col %d' %
273 (lineno, colno))
274
275 d = code_formatter.pattern.sub(convert, format)
276 self._append(d)
277
278 __all__ = [ "code_formatter" ]
279
280 if __name__ == '__main__':
281 from .code_formatter import code_formatter
282 f = code_formatter()
283
284 class Foo(dict):
285 def __init__(self, **kwargs):
286 self.update(kwargs)
287 def __getattr__(self, attr):
288 return self[attr]
289
290 x = "this is a test"
291 l = [ [Foo(x=[Foo(y=9)])] ]
292
293 y = code_formatter()
294 y('''
295 {
296 this_is_a_test();
297 }
298 ''')
299 f(' $y')
300 f('''$__file__:$__line__
301 {''')
302 f("${{', '.join(str(x) for x in range(4))}}")
303 f('${x}')
304 f('$x')
305 f.indent()
306 for i in range(5):
307 f('$x')
308 f('$i')
309 f('$0', "zero")
310 f('$1 $0', "zero", "one")
311 f('${0}', "he went")
312 f('${0}asdf', "he went")
313 f.dedent()
314
315 f('''
316 ${{l[0][0]["x"][0].y}}
317 }
318 ''', 1, 9)
319
320 print(f, end=' ')