22# -*- coding: utf-8 -*-
33""" java2python.compiler.template -> Base classes for writing Python source. """
44##
5- # This module defines templates, blocks of Python source code, that
6- # can be easily manipulated and written. Each base provides string
7- # methods (__str__, dump, dumps) for serializing instances as source
8- # code. The base types also provide many utility methods.
5+ # This module defines templates -- blocks of Python source code --
6+ # that can be easily manipulated and written. Each base provides
7+ # string methods (__str__, dump, dumps) for serializing instances as a
8+ # source code string. The base types also provide many utility
9+ # methods.
910#
1011# The Factory class is used to to provide runtime lookup of concrete
1112# classes; this was necessary to accommodate splitting the behavior of
1213# the compiler subpackage into multiple modules. So-called patterns
1314# are usually a sign of a bad design and/or language limitations, and
1415# this case is no exception.
1516
17+ from cStringIO import StringIO
1618from functools import partial
1719from itertools import chain , ifilter , imap
18- from StringIO import StringIO
1920
2021from java2python .lang import tokens
2122from java2python .lib import FS
2223from java2python .lib .colortools import *
2324
2425
2526class Factory (object ):
26- """ Factory -> creates pre-configured callables for new block instances. """
27+ """ Factory -> creates pre-configured callables for new block instances.
28+
29+ The templates use an instance of this class as a quick and simple
30+ interface to create new templates like this:
31+
32+ stat = self.factory.statement()
33+
34+ The `__getattr__` method does the work of looking up and returning
35+ the appropriate template class. The lookup depends on the types
36+ registry, which is populated by the FactoryTypeDetector metaclass
37+ below.
38+
39+ The important thing to realize regarding this factory is this:
40+ when an attribute is requested (`self.factory.expr` for example),
41+ the factory locates the type and returns a constructor for it with
42+ the config object pre-applied.
43+
44+ """
2745 types = {}
2846
2947 def __init__ (self , config ):
@@ -39,6 +57,16 @@ def __getattr__(self, name):
3957class FactoryTypeDetector (type ):
4058 """ FactoryTypeDetector -> detects factory-creatable types as they are defined.
4159
60+ As subclasses are created they are checked for an attribute called
61+ `factoryTypeName`. If present, that key is used to populate the
62+ factory type registry above.
63+
64+ Note that the actual subclasses are not created here (none of
65+ these specify a `factoryTypeName`). Actual factory types are
66+ created in `java2python.compiler.block`. This is because we're
67+ after not templates, but visitors combined with templates, aka
68+ blocks. Refer to the `blocks` module for the specific factory
69+ type names.
4270 """
4371 def __init__ (cls , name , bases , namespace ):
4472 try :
@@ -50,6 +78,38 @@ def __init__(cls, name, bases, namespace):
5078class Base (object ):
5179 """ Base -> base class for formatting Python output.
5280
81+ This class defines a large set of attributes and methods for the
82+ other concrete templates defined below. The items defined here
83+ can be grouped as follows:
84+
85+ * References
86+
87+ This class defines `bases`, `children`, `decorators`, etc. for
88+ tracking the relationship between this instance and other blocks.
89+
90+ * Type Information
91+
92+ This class defines many is-A properties, such as isClass,
93+ isModule, isVoid, etc. Subclasses typically override one or more
94+ of these with an attribute or property.
95+
96+ * Configuration
97+
98+ This class provides utility methods for retrieving values from the
99+ run-time configuration. See the definition of `configHandler` and
100+ `configHandlers` for details.
101+
102+ * Serialization
103+
104+ This class provides a default implementation for subclasses to
105+ serialize their instances as Python source code strings. Notably,
106+ the `__str__` method is provided, which in turn defers most of its
107+ work to the `dumps` method. Subclasses provide different
108+ implementations of these methods where needed.
109+
110+ Also, the `__repr__` method is defined by this class for printing
111+ a the template as tree for debugging.
112+
53113 """
54114 __metaclass__ = FactoryTypeDetector
55115 isAnnotation = isClass = isComment = isEnum = isExpression = \
@@ -92,9 +152,7 @@ def adopt(self, child, index=-1):
92152
93153 def altIdent (self , name ):
94154 """ Returns an alternate identifier for the one given. """
95- #print '## looking for name:', name, 'parent count:', [(type(x), type(x.parent)) for x in self.parents()]
96155 for klass in self .parents (lambda v :v .isClass ):
97- #print '#### looking inside', klass.name, klass.variables
98156 if name in klass .variables :
99157 try :
100158 method = self .parents (lambda v :v .isMethod ).next ()
@@ -105,7 +163,6 @@ def altIdent(self, name):
105163 if name in method .variables :
106164 return name
107165 return ('cls' if method .isStatic else 'self' ) + '.' + name
108- #print
109166 return name
110167
111168 def configHandler (self , part , suffix = 'Handler' , default = None ):
@@ -282,12 +339,6 @@ class Comment(Expression):
282339 """
283340 isComment = True
284341
285- def __init__ (self , config , left = '' , right = '' , fs = FS .lr , parent = None , tail = '' ):
286- super (Comment , self ).__init__ (config , left , right , fs , parent , tail )
287- if False :# not fs.strip().startswith('#'): # wha?
288- prefix = self .config .last ('commentPrefix' , '# ' )
289- self .fs = prefix + self .fs
290-
291342 def __repr__ (self ):
292343 """ Returns the debug string representation of this comment. """
293344 parts = [white (self .typeName + ':' ),
0 commit comments