Skip to content

Commit 1f49745

Browse files
committed
Add specific config for Princeton's algs4 Java code base/libraries
1 parent f4e1db3 commit 1f49745

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed

java2python/config/algs4.py

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# This is the default configuration file for java2python. Unless
5+
# explicity disabled with the '-n' or '--nodefaults' option, the j2py
6+
# script will import this module for runtime configuration.
7+
8+
from java2python.mod import basic, transform
9+
from java2python.lang.selector import *
10+
11+
12+
# Leading indent character or characters. Four spaces are used
13+
# because that is the recommendation of PEP 8.
14+
indentPrefix = ' ' * 4
15+
16+
17+
# Prefix character or characters for comments. The hash+space is
18+
# recommended by PEP 8.
19+
commentPrefix = '# '
20+
21+
22+
# These values are strings or generators that yield strings
23+
# for a module prologue.
24+
modulePrologueHandlers = [
25+
basic.shebangLine,
26+
basic.simpleDocString,
27+
'from __future__ import print_function',
28+
basic.maybeBsr,
29+
basic.maybeAbstractHelpers,
30+
basic.maybeSyncHelpers,
31+
]
32+
33+
34+
# These generators yield lines for a module epilogue.
35+
moduleEpilogueHandlers = [
36+
basic.scriptMainStanza,
37+
]
38+
39+
40+
# These generators yield (possibly modified) source strings for a
41+
# module. The `basic.outputSubs` handler references the list of
42+
# regular expression substitutions near the end of this module.
43+
moduleOutputHandlers = [
44+
basic.outputSubs,
45+
]
46+
47+
48+
# These generators yield doc strings for a class.
49+
classHeadHandlers = [
50+
basic.simpleDocString,
51+
]
52+
53+
methodParamHandlers = [
54+
basic.defaultParams,
55+
]
56+
57+
# This is the name of the callable used to construct locks for an object with
58+
# the synchronized keyword.
59+
methodLockFunctionName = 'lock_for_object'
60+
61+
classBaseHandlers = [
62+
basic.defaultBases,
63+
]
64+
65+
interfaceBaseHandlers = [
66+
basic.defaultBases,
67+
]
68+
69+
# These generators are called after a class has been completely
70+
# generated. The class content sorter sorts the methods of a class by
71+
# name. It's commented out because its output differs so greatly
72+
# from its input, and because it's really not very useful.
73+
classPostWalkHandlers = [
74+
basic.moveStaticExpressions,
75+
## basic.classContentSort,
76+
]
77+
78+
79+
enumHeadHandlers = [
80+
basic.simpleDocString,
81+
]
82+
83+
84+
interfaceHeadHandlers = [
85+
basic.simpleDocString,
86+
'__metaclass__ = ABCMeta',
87+
]
88+
89+
90+
interfacePostWalkMutators = [
91+
]
92+
93+
94+
methodHeadHandlers = [
95+
basic.simpleDocString,
96+
]
97+
98+
99+
methodPrologueHandlers = [
100+
basic.maybeAbstractMethod,
101+
basic.maybeClassMethod,
102+
basic.overloadedClassMethods,
103+
# NB: synchronized should come after classmethod
104+
basic.maybeSynchronizedMethod,
105+
]
106+
107+
108+
# This handler creates enum values on enum classes after they've been
109+
# defined. The handler tries to match Java semantics by using
110+
# strings. Refer to the documentation for details.
111+
enumValueHandler = basic.enumConstStrings
112+
113+
# Alternatively, you can use this handler to construct enum values as
114+
# integers.
115+
#enumValueHandler = basic.enumConstInts
116+
117+
118+
# When the compiler needs to make up a variable name (for example, to
119+
# emulate assignment expressions), it calls this handler to produce a
120+
# new one.
121+
expressionVariableNamingHandler = basic.globalNameCounter
122+
123+
124+
# This handler simply creates comments in the file for package
125+
# declarations.
126+
modulePackageDeclarationHandler = basic.commentedPackages
127+
128+
129+
# This handler can be used instead to create __init__.py files for
130+
# 'namespace packages' via pkgutil.
131+
# modulePackageDeclarationHandler = basic.namespacePackages
132+
133+
134+
# This handler is turns java imports into python imports. No mapping
135+
# of packages is performed:
136+
# moduleImportDeclarationHandler = basic.simpleImports
137+
138+
# This import decl. handler can be used instead to produce comments
139+
# instead of import statements:
140+
# moduleImportDeclarationHandler = basic.commentedImports
141+
142+
# The AST transformation function uses these declarations to modify an
143+
# AST before compiling it to python source. Having these declarations
144+
# in a config file gives clients an opportunity to change the
145+
# transformation behavior.
146+
147+
astTransforms = [
148+
(Type('NULL'), transform.null2None),
149+
(Type('FALSE'), transform.false2False),
150+
(Type('TRUE'), transform.true2True),
151+
(Type('IDENT'), transform.keywordSafeIdent),
152+
153+
(Type('DECIMAL_LITERAL'), transform.syntaxSafeDecimalLiteral),
154+
(Type('FLOATING_POINT_LITERAL'), transform.syntaxSafeFloatLiteral),
155+
156+
(Type('TYPE') > Type('BOOLEAN'), transform.typeSub),
157+
(Type('TYPE') > Type('BYTE'), transform.typeSub),
158+
(Type('TYPE') > Type('CHAR'), transform.typeSub),
159+
(Type('TYPE') > Type('FLOAT'), transform.typeSub),
160+
(Type('TYPE') > Type('INT'), transform.typeSub),
161+
(Type('TYPE') > Type('SHORT'), transform.typeSub),
162+
(Type('TYPE') > Type('LONG'), transform.typeSub),
163+
(Type('TYPE') > Type('DOUBLE'), transform.typeSub),
164+
165+
(Type('METHOD_CALL') > Type('DOT') > Type('IDENT', 'length'),
166+
transform.lengthToLen),
167+
168+
(Type('METHOD_CALL') > Type('DOT') > (
169+
Type('IDENT', 'String') +
170+
Type('IDENT', 'format')
171+
),
172+
transform.formatString),
173+
174+
(Type('TYPE') > Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'),
175+
transform.typeSub),
176+
177+
]
178+
179+
180+
# not implemented:
181+
182+
# minimum parameter count to trigger indentation of parameter names
183+
# in method declarations. set to 0 to disable.
184+
#minIndentParams = 5
185+
186+
# Specifies handler for cast operations of non-primitive types are handled
187+
# (primitive types are automatically handled). Use basic.castDrop to leave
188+
# cast expressions out of generated source. Use basic.castCtor to transform
189+
# casts into constructor calls. Or you can specify a function of your own.
190+
expressionCastHandler = basic.castDrop
191+
192+
193+
# Values below are used by the handlers. They're here for
194+
# convenience.
195+
196+
197+
# module output subs.
198+
moduleOutputSubs = [
199+
(r'System\.out\.println\((.*)\)', r'print(\1)'),
200+
(r'StdOut\.println\((.*)\)', r'print(\1)'),
201+
(r'System\.err\.println\((.*)\)', r'print(\1, file=sys.stderr)'),
202+
(r'System\.out\.print_\((.*?)\)', r'print(\1, end="")'),
203+
(r'(.*?)\.equals\((.*?)\)', r'\1 == \2'),
204+
(r'(.*?)\.equalsIgnoreCase\((.*?)\)', r'\1.lower() == \2.lower()'),
205+
(r'([\w.]+)\.size\(\)', r'len(\1)'),
206+
#(r'(\w+)\.get\((.*?)\)', r'\1[\2]'),
207+
(r'(\s)(\S*?)(\.toString\(\))', r'\1\2.__str__()'),
208+
(r'(\s)def toString', r'\1def __str__'),
209+
(r'(\s)(\S*?)(\.toLowerCase\(\))', r'\1\2.lower()'),
210+
(r'(.*?)IndexOutOfBoundsException\((.*?)\)', r'\1IndexError(\2)'),
211+
(r'\.__class__\.getName\(\)', '.__class__.__name__'),
212+
(r'\.getClass\(\)', '.__class__'),
213+
(r'\.getName\(\)', '.__name__'),
214+
(r'\.getInterfaces\(\)', '.__bases__'),
215+
(r'String\.valueOf\((.*?)\)', r'str(\1)'),
216+
#(r'(\s)(\S*?)(\.toString\(\))', r'\1str(\2)'),
217+
(r'Math\.', ''),
218+
]
219+
220+
221+
typeSubs = {
222+
'Boolean' : 'bool',
223+
'boolean' : 'bool',
224+
225+
'Byte' : 'int',
226+
'byte' : 'int',
227+
228+
'Char' : 'str',
229+
'char' : 'str',
230+
231+
'Integer' : 'int',
232+
'int' : 'int',
233+
234+
'Short' : 'int',
235+
'short' : 'int',
236+
237+
'Long' : 'long',
238+
'long' : 'long',
239+
240+
'Float' : 'float',
241+
'float' : 'float',
242+
243+
'Double' : 'float',
244+
'double' : 'float',
245+
246+
'String' : 'str',
247+
'java.lang.String' : 'str',
248+
249+
'Object' : 'object',
250+
251+
'IndexOutOfBoundsException' : 'IndexError',
252+
'IOException': 'IOError',
253+
'NoSuchElementException': 'Exception'
254+
}

0 commit comments

Comments
 (0)