Repository navigation
Expand file tree
/
Copy pathtemplate.py
More file actions
429 lines (341 loc) · 12.9 KB
/
Copy pathtemplate.py
File metadata and controls
429 lines (341 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" java2python.compiler.template -> Base classes for writing Python source. """
##
# This module defines templates, blocks of Python source code, that
# can be easily manipulated and written. Each base provides string
# methods (__str__, dump, dumps) serializing instances as source code.
# The base types also provide many utility methods.
#
# The Factory class is used to to provide runtime lookup of concrete
# classes; this was necessary to accommodate splitting the behavior of
# the compiler subpackage into multiple modules. So-called patterns
# are usually a sign of a bad design and/or language limitations, and
# this case is no exception.
from functools import partial
from itertools import chain, ifilter, imap
from StringIO import StringIO
from java2python.lang import tokens
from java2python.lib import FS
from java2python.lib.colortools import *
class Factory(object):
""" Factory -> creates pre-configured callables for new block instances. """
types = {}
def __init__(self, config):
self.config = config
def __getattr__(self, name):
try:
return partial(self.types[name], self.config)
except (KeyError, ):
raise AttributeError('Factory missing "{0}" type.'.format(name))
class FactoryTypeDetector(type):
""" FactoryTypeDetector -> detects factory-creatable types as they are defined.
"""
def __init__(cls, name, bases, namespace):
try:
Factory.types[cls.factoryTypeName] = cls
except (AttributeError, ):
pass
class Base(object):
""" Base -> base class for formatting Python output.
"""
__metaclass__ = FactoryTypeDetector
isAnnotation = isClass = isComment = isEnum = isExpression = \
isInterface = isMethod = isModule = isStatement = False
def __init__(self, config, name=None, type=None, parent=None):
self.bases = []
self.children = []
self.config = config
self.decorators = []
self.factory = Factory(config)
self.modifiers = []
self.name = name
self.parameters = []
self.parent = parent
self.type = type
self.variables = []
if parent:
parent.children.append(self)
def __repr__(self):
""" Returns the debug string representation of this template. """
name = white('name:') + cyan(self.name) if self.name else ''
parts = [green(self.typeName), name]
if self.type:
parts.append(white('type:') + cyan(self.type))
if self.modifiers:
parts.append(white('modifiers:') + cyan(','.join(self.modifiers)))
return ' '.join(parts)
def __str__(self):
""" Returns the Python source code representation of this template. """
handlers = self.configHandlers('Output')
return reduce(lambda v, func:func(self, v), handlers, self.dumps(-1))
def adopt(self, child, index=-1):
""" Adds child to this objecs children and sets the childs parent. """
self.children.insert(index, child)
child.parent = self
def altIdent(self, name):
""" Returns an alternate identifier for the one given. """
for klass in self.parents(lambda v:v.isClass):
if name in klass.variables:
method = self.parents(lambda v:v.isMethod).next()
if name in [p['name'] for p in method.parameters]:
return name
return ('cls' if method.isStatic else 'self') + '.' + name
return name
def configHandler(self, part, suffix='Handler', default=None):
""" Returns the config handler for this type of template. """
name = '{0}{1}{2}'.format(self.typeName, part, suffix)
return self.config.last(name, default)
def configHandlers(self, part, suffix='Handlers'):
""" Returns config handlers for this type of template """
name = '{0}{1}{2}'.format(self.typeName, part, suffix)
return imap(self.toIter, self.config.last(name, ()))
def dump(self, fd, level=0):
""" Writes the Python source code for this template to the given file. """
indent, isNotNone = level * self.indent, lambda x:x is not None
lineFormat = '{0}{1}\n'.format
for line in ifilter(isNotNone, self.iterPrologue()):
line = lineFormat(indent, line)
fd.write(line if line.strip() else '\n')
for item in ifilter(isNotNone, self.iterHead()):
item.dump(fd, level+1)
for item in self.iterBody():
item.dump(fd, level+1)
for line in ifilter(isNotNone, self.iterEpilogue()):
line = lineFormat(indent, line)
fd.write(line if line.strip() else '\n')
def dumps(self, level=0):
""" Dumps this template to a string. """
fd = StringIO()
self.dump(fd, level)
return fd.getvalue()
def dumpRepr(self, fd, level=0):
""" Writes a debug string for this template to the given file. """
indent, default = self.indent, lambda x, y:None
fd.write('{0}{1!r}\n'.format(indent*level, self))
for child in ifilter(None, self.children):
getattr(child, 'dumpRepr', default)(fd, level+1)
@property
def indent(self):
""" Returns the indent string for this item. """
return self.config.last('indentPrefix', ' ')
@property
def isPublic(self):
""" True if this item is static. """
return 'public' in self.modifiers
@property
def isStatic(self):
""" True if this item is static. """
return 'static' in self.modifiers
@property
def isVoid(self):
""" True if this item is void. """
return 'void' == self.type
def iterPrologue(self):
""" Yields the items in the prologue of this template. """
return chain(*(h(self) for h in self.configHandlers('Prologue')))
def iterHead(self):
""" Yields the items in the head of this template. """
items = chain(*(h(self) for h in self.configHandlers('Head')))
return imap(self.toExpr, items)
def iterBody(self):
""" Yields the items in the body of this template. """
return iter(self.children)
def iterEpilogue(self):
""" Yields the items in the epilogue of this template. """
return chain(*(h(self) for h in self.configHandlers('Epilogue')))
def makeParam(self, name, type, **kwds):
""" Creates a parameter as a mapping. """
param = dict(name=name, type=type)
if 'default' in kwds:
param['default'] = kwds['default']
return param
def parents(self, pred=lambda v:True):
""" Yield each parent in the family tree. """
while self:
if pred(self):
yield self
self = self.parent
@property
def className(self):
""" Returns the name of the class of this item. """
return self.__class__.__name__
@property
def typeName(self):
""" Returns the name of this template type. """
return self.className.lower()
def toExpr(self, value):
""" Returns an expression for the given value if it is a string. """
try:
return self.factory.expr(left=value+'')
except (TypeError, ):
return value
def toIter(self, value):
""" Returns an iterator for the given value if it is a string. """
try:
value + ''
except (TypeError, ):
return value
else:
def wrapper(*a, **b):
yield value
return wrapper
class Expression(Base):
""" Expression -> formatting for Python expressions.
"""
isExpression = True
def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
super(Expression, self).__init__(config, parent=parent)
self.left, self.right, self.fs, self.tail = left, right, fs, tail
def __repr__(self):
""" Returns the debug string representation of this template. """
parts, parent, showfs = [blue(self.typeName)], self.parent, True
if isinstance(self.left, (basestring, )) and self.left:
parts.append(white('left:') + yellow(self.left))
showfs = False
if isinstance(self.right, (basestring, )) and self.right:
parts.append(white('right:') + yellow(self.right))
showfs = False
if showfs:
parts.append(white('format:') + yellow(self.fs))
if self.tail:
parts.append(white('tail:') + black(self.tail))
return ' '.join(parts)
def __str__(self):
""" Returns the Python source code representation of this template. """
return self.fs.format(left=self.left, right=self.right) + self.tail
def dump(self, fd, level=0):
""" Writes the Python source code for this template to the given file. """
line = '{0}{1}\n'.format(self.indent*level, self)
fd.write(line if line.strip() else '\n')
def dumpRepr(self, fd, level=0):
""" Writes a debug string for this template to the given file. """
fd.write('{0}{1!r}\n'.format(self.indent*level, self))
for obj in (self.left, self.right):
dumper = getattr(obj, 'dumpRepr', lambda x, y:None)
dumper(fd, level+1)
@property
def isComment(self):
""" True if this expression is a comment. """
try:
return self.left.strip().startswith('#')
except (AttributeError, ):
return False
class Comment(Expression):
""" Comment -> formatting for Python comments.
"""
isComment = True
def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
super(Comment, self).__init__(config, left, right, fs, parent, tail)
if False:# not fs.strip().startswith('#'): # wha?
prefix = self.config.last('commentPrefix', '# ')
self.fs = prefix + self.fs
def __repr__(self):
""" Returns the debug string representation of this comment. """
parts = [white(self.typeName+':'),
black(self.left) + black(self.right) + black(self.tail), ]
return ' '.join(parts)
class Statement(Base):
""" Statement -> formatting for Python statements.
"""
isStatement = True
def __init__(self, config, keyword, fs=FS.lr, parent=None):
super(Statement, self).__init__(config, parent=parent)
self.keyword = keyword
self.expr = self.factory.expr(left=keyword, fs=fs)
self.expr.parent = self
def __repr__(self):
""" Returns the debug string representation of this statement. """
parts = [green(self.typeName), white('keyword:')+cyan(self.keyword)]
return ' '.join(parts)
def iterPrologue(self):
""" Yields the keyword (and clause, if any) for this statement . """
yield self.expr
class Module(Base):
""" Module -> formatting for Python modules.
"""
isModule = True
def iterBody(self):
""" Yields the items in the body of this template. """
blank, prev = self.factory.expr(), None
for child in super(Module, self).iterBody():
if prev and not prev.isComment:
yield blank
if prev and prev.isClass and child and child.isClass:
yield blank
yield child
prev = child
class ClassMethodSharedMixin(object):
""" ClassMethodSharedMixin -> shared methods for Class and Method types. """
def iterPrologue(self):
""" Yields the items in the prologue of this template. """
prologue = super(ClassMethodSharedMixin, self).iterPrologue()
return chain(prologue, self.decorators, self.iterDecl())
class Class(ClassMethodSharedMixin, Base):
""" Class -> formatting for Python classes.
"""
isClass = True
def iterBases(self):
""" Yields the base classes for this type. """
return iter(self.bases or ['object'])
def iterDecl(self):
""" Yields the declaration for this type. """
bases = ', '.join(self.iterBases())
bases = '({0})'.format(bases) if bases else ''
yield 'class {0}{1}:'.format(self.name, bases)
def iterBody(self):
""" Yields the items in the body of this template. """
def sprinkleBlanks(body):
blank, prev = self.factory.expr(), None
for item in body:
if prev:
if type(prev) != type(item) and not prev.isComment:
yield blank
elif item.isMethod and prev.isMethod:
yield blank
elif prev.isClass:
yield blank
yield item
prev = item
head = any(self.iterHead())
body = list(super(Class, self).iterBody())
tail = () if (body or head) else [self.factory.expr(left='pass')]
body = () if tail else sprinkleBlanks(body)
return chain(body, tail)
class Annotation(Class):
""" Annotation -> formatting for annotations converted to Python classes.
"""
isAnnotation = True
def __init__(self, config, name=None, type=None, parent=None):
super(Annotation, self).__init__(config, name, type, parent)
class Enum(Class):
""" Enum -> formatting for enums converted to Python classes.
"""
isEnum = True
class Interface(Class):
""" Interface -> formatting for interfaces converted to Python classes.
"""
isInterface = True
class MethodContent(Base):
""" MethodContent -> formatting for content within Python methods. """
class Method(ClassMethodSharedMixin, Base):
""" Method -> formatting for Python methods.
"""
isMethod = True
def __init__(self, config, name=None, type=None, parent=None):
super(Method, self).__init__(config, name, type, parent)
self.parameters.append(self.makeParam('self', 'object'))
def iterDecl(self):
""" Yields the declaration for this method template. """
def formatParam(p):
if 'default' in p:
return '{0}={1}'.format(p['name'], p['default'])
return p['name']
params = ', '.join(formatParam(param) for param in self.parameters)
yield 'def {0}({1}):'.format(self.name, params)
def iterBody(self):
""" Yields the items in the body of this method template. """
head = any(self.iterHead())
body = list(super(Method, self).iterBody())
tail = () if (body or head) else [self.factory.expr(left='pass')]
return chain(body, tail)