Skip to content

Commit b1af8ac

Browse files
author
Troy Melhase
committed
Moves colortools into lib/__init__. Other minor fixups.
1 parent 2853dfb commit b1af8ac

File tree

9 files changed

+89
-72
lines changed

9 files changed

+89
-72
lines changed

bin/j2py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from time import time
1515

1616
from java2python.compiler import Module, buildAST, transformAST
1717
from java2python.config import Config
18-
from java2python.lib.colortools import colors
18+
from java2python.lib import escapes
1919

2020

2121
version = '0.5'
@@ -221,7 +221,7 @@ def config(argv):
221221
## these next statements don't belong here, but this is as good a
222222
## place as any.
223223
if isWindows() or options.nocolor:
224-
colors.clear()
224+
escapes.clear()
225225
fmt = '# %(levelname)s %(funcName)s: %(message)s'
226226
basicConfig(level=options.loglevel, format=fmt)
227227
return options

java2python/compiler/template.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from itertools import chain, ifilter, imap
2020

2121
from java2python.lang import tokens
22-
from java2python.lib import FS
23-
from java2python.lib.colortools import *
22+
from java2python.lib import FS, colors
2423

2524

2625
class Factory(object):
@@ -132,12 +131,12 @@ def __init__(self, config, name=None, type=None, parent=None):
132131

133132
def __repr__(self):
134133
""" Returns the debug string representation of this template. """
135-
name = white('name:') + cyan(self.name) if self.name else ''
136-
parts = [green(self.typeName), name]
134+
name = colors.white('name:') + colors.cyan(self.name) if self.name else ''
135+
parts = [colors.green(self.typeName), name]
137136
if self.type:
138-
parts.append(white('type:') + cyan(self.type))
137+
parts.append(colors.white('type:') + colors.cyan(self.type))
139138
if self.modifiers:
140-
parts.append(white('modifiers:') + cyan(','.join(self.modifiers)))
139+
parts.append(colors.white('modifiers:') + colors.cyan(','.join(self.modifiers)))
141140
return ' '.join(parts)
142141

143142
def __str__(self):
@@ -295,17 +294,17 @@ def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
295294

296295
def __repr__(self):
297296
""" Returns the debug string representation of this template. """
298-
parts, parent, showfs = [blue(self.typeName)], self.parent, True
297+
parts, parent, showfs = [colors.blue(self.typeName)], self.parent, True
299298
if isinstance(self.left, (basestring, )) and self.left:
300-
parts.append(white('left:') + yellow(self.left))
299+
parts.append(colors.white('left:') + colors.yellow(self.left))
301300
showfs = False
302301
if isinstance(self.right, (basestring, )) and self.right:
303-
parts.append(white('right:') + yellow(self.right))
302+
parts.append(colors.white('right:') + colors.yellow(self.right))
304303
showfs = False
305304
if showfs:
306-
parts.append(white('format:') + yellow(self.fs))
305+
parts.append(colors.white('format:') + colors.yellow(self.fs))
307306
if self.tail:
308-
parts.append(white('tail:') + black(self.tail))
307+
parts.append(colors.white('tail:') + colors.black(self.tail))
309308
return ' '.join(parts)
310309

311310
def __str__(self):
@@ -341,8 +340,8 @@ class Comment(Expression):
341340

342341
def __repr__(self):
343342
""" Returns the debug string representation of this comment. """
344-
parts = [white(self.typeName+':'),
345-
black(self.left) + black(self.right) + black(self.tail), ]
343+
parts = [colors.white(self.typeName+':'),
344+
colors.black(self.left) + colors.black(self.right) + colors.black(self.tail), ]
346345
return ' '.join(parts)
347346

348347

@@ -361,7 +360,7 @@ def __init__(self, config, keyword, fs=FS.lr, parent=None):
361360

362361
def __repr__(self):
363362
""" Returns the debug string representation of this statement. """
364-
parts = [green(self.typeName), white('keyword:')+cyan(self.keyword)]
363+
parts = [colors.green(self.typeName), colors.white('keyword:')+colors.cyan(self.keyword)]
365364
return ' '.join(parts)
366365

367366
def iterPrologue(self):

java2python/config/default.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050

5151

5252
# These generators are called after a class has been completely
53-
# generated; this specific one sorts method bodies by name.
54-
# NB: the code generator doesn't actually use this.
53+
# generated. The class content sorter sorts the methods of a class by
54+
# name. It's commented out because its output differs so greatly
55+
# from its input, and because it's really not very useful.
5556
classPostWalkHandlers = [
56-
basic.classContentSort,
57+
## basic.classContentSort,
5758
]
5859

5960

java2python/lang/base.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from antlr3 import ANTLRStringStream as StringStream, CommonTokenStream as TokenStream
5353
from antlr3.tree import CommonTreeAdaptor, CommonTree
5454

55-
from java2python.lib import colortools
55+
from java2python.lib import colors
5656

5757

5858
class Tokens(object):
@@ -125,15 +125,15 @@ class LocalTree(CommonTree):
125125
126126
"""
127127
colorTypeMap = {
128-
'CLASS' : colortools.green,
129-
'JAVA_SOURCE' : colortools.green,
130-
'VOID_METHOD_DECL' : colortools.green,
131-
'IDENT' : colortools.yellow,
132-
'TYPE' : colortools.magenta,
133-
'EXPR' : colortools.blue,
134-
'TRUE' : colortools.yellow,
135-
'FALSE' : colortools.yellow,
136-
'NULL' : colortools.yellow,
128+
'CLASS' : colors.green,
129+
'JAVA_SOURCE' : colors.green,
130+
'VOID_METHOD_DECL' : colors.green,
131+
'IDENT' : colors.yellow,
132+
'TYPE' : colors.magenta,
133+
'EXPR' : colors.blue,
134+
'TRUE' : colors.yellow,
135+
'FALSE' : colors.yellow,
136+
'NULL' : colors.yellow,
137137
}
138138

139139
def __init__(self, payload, lexer=None, parser=None):
@@ -146,18 +146,18 @@ def childrenOfType(self, type):
146146

147147
def colorType(self, tokenType):
148148
""" Returns a color suitable for the given token type. """
149-
return self.colorTypeMap.get(tokenType, colortools.white)(tokenType)
149+
return self.colorTypeMap.get(tokenType, colors.white)(tokenType)
150150

151151
def colorText(self, tokenType, tokenText):
152152
""" Returns a colorized string from the given token type and text. """
153-
return self.colorTypeMap.get(tokenType, colortools.white)(tokenText)
153+
return self.colorTypeMap.get(tokenType, colors.white)(tokenText)
154154

155155
def colorComments(self, token):
156156
""" Formats, colors, and returns the comment text from the given token. """
157157
ttyp = tokens.map.get(token.type)
158158
text = token.text.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t')
159159
item = '{0} [{1}:{2}] {3}'.format(ttyp, token.start, token.stop, text)
160-
yield colortools.black(item)
160+
yield colors.black(item)
161161

162162
def dump(self, fd, level=0):
163163
""" Writes a debug representation of this tree to the given file. """
@@ -175,7 +175,7 @@ def innerDump(root, offset):
175175
if line:
176176
idxes = 'line={}{}{}'.format(line, ', ' if idxes else '', idxes)
177177
idxes = ' [{}]'.format(idxes) if idxes else ''
178-
idxes = colortools.black(idxes)
178+
idxes = colors.black(idxes)
179179
args = [indent, self.colorType(ttyp), '', idxes, '']
180180
if extras(token.text, ttyp):
181181
args[2] = ' ' + self.colorText(ttyp, token.text)

java2python/lib/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from functools import partial
34

45

56
class FS(object):
@@ -25,3 +26,31 @@ def op(cls, op):
2526
if op == '>>>=':
2627
return l + ' = bsr(' + l + ', ' + r + ')'
2728
return l + ' ' + op + ' ' + r
29+
30+
31+
escapes = {
32+
'BLACK' : '\033[90m',
33+
'BLUE' : '\033[94m',
34+
'CYAN' : '\033[96m',
35+
'GREEN' : '\033[92m',
36+
'MAGENTA' : '\033[95m',
37+
'NORMAL' : '\033[0m',
38+
'RED' : '\033[91m',
39+
'WHITE' : '\033[97m',
40+
'YELLOW' : '\033[93m',
41+
}
42+
43+
44+
def escape(color, value):
45+
return escapes.get(color, '') + str(value) + escapes.get('NORMAL', '')
46+
47+
48+
class colors:
49+
black = partial(escape, 'BLACK')
50+
blue = partial(escape, 'BLUE')
51+
cyan = partial(escape, 'CYAN')
52+
green = partial(escape, 'GREEN')
53+
magenta = partial(escape, 'MAGENTA')
54+
red = partial(escape, 'RED')
55+
white = partial(escape, 'WHITE')
56+
yellow = partial(escape, 'YELLOW')

java2python/lib/colortools.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

java2python/mod/basic.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,10 @@ def sortBody(group):
161161
methods = [item for item in group if isMethod(item)]
162162
return methods[0].name if methods else -1
163163

164-
gx = list(iterBody(obj.children))
165-
sgx = sorted(gx, key=sortBody)
166-
sgx = [item for sublist in sgx for item in sublist]
167-
obj.children = sgx
164+
grp = list(iterBody(obj.children))
165+
grpsrt = sorted(grp, key=sortBody)
166+
obj.children = [item for grp in grpsrt for item in grp]
168167

169-
return
170-
obj.children.sort(lambda x, y:-1 if x.isClass else 1)
171168

172169

173170
def zopeInterfaceMethodMutator(obj):

test/configs/Interface3.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from java2python.mod import basic
2+
3+
4+
def setZopeInterface(iface):
5+
print '##### iface.bases:', iface.bases
6+
iface.bases[:] = ['zope.interface.Interface']
7+
print '#### set zope interface', iface.bases
8+
9+
interfacePostWalkHandlers = [
10+
setZopeInterface,
11+
]
12+
13+
14+
# override, not supplement:
15+
interfaceHeadHandlers = [
16+
# basic.simpleDocString,
17+
]
18+
19+
20+
methodPrologueHandlers = [
21+
22+
]

test/selector/test_all.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from java2python.compiler import buildAST
88
from java2python.lang import tokens
99
from java2python.lang.selector import Token, Type
10-
from java2python.lib import colortools
10+
from java2python.lib import colors
1111

1212

1313
def setUpModule():
@@ -26,7 +26,7 @@ def assertNodes(self, nodes, length):
2626

2727
def shortDescription(self):
2828
fs = 'Description: {}\nSelector: {}\n'
29-
args = (colortools.cyan(self.description), colortools.yellow(self.selector))
29+
args = (colors.cyan(self.description), colors.yellow(self.selector))
3030
return fs.format(*args)
3131

3232
@classmethod

0 commit comments

Comments
 (0)