[java2python] r185 committed - Moves colortools into lib/__init__. Other minor fixups.

1 view
Skip to first unread message

java2...@googlecode.com

unread,
Oct 27, 2011, 7:17:09 PM10/27/11
to java2pyth...@googlegroups.com
Revision: 185
Author: troy.melhase
Date: Thu Oct 27 16:16:24 2011
Log: Moves colortools into lib/__init__. Other minor fixups.
http://code.google.com/p/java2python/source/detail?r=185

Added:
/trunk/test/configs/Interface3.py
Deleted:
/trunk/java2python/lib/colortools.py
Modified:
/trunk/bin/j2py
/trunk/java2python/compiler/template.py
/trunk/java2python/config/default.py
/trunk/java2python/lang/base.py
/trunk/java2python/lib/__init__.py
/trunk/java2python/mod/basic.py
/trunk/test/selector/test_all.py

=======================================
--- /dev/null
+++ /trunk/test/configs/Interface3.py Thu Oct 27 16:16:24 2011
@@ -0,0 +1,22 @@
+from java2python.mod import basic
+
+
+def setZopeInterface(iface):
+ print '##### iface.bases:', iface.bases
+ iface.bases[:] = ['zope.interface.Interface']
+ print '#### set zope interface', iface.bases
+
+interfacePostWalkHandlers = [
+ setZopeInterface,
+ ]
+
+
+# override, not supplement:
+interfaceHeadHandlers = [
+# basic.simpleDocString,
+ ]
+
+
+methodPrologueHandlers = [
+
+ ]
=======================================
--- /trunk/java2python/lib/colortools.py Thu Jul 1 15:12:49 2010
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-from functools import partial
-
-
-colors = {
- 'BLACK' : '\033[90m',
- 'BLUE' : '\033[94m',
- 'CYAN' : '\033[96m',
- 'GREEN' : '\033[92m',
- 'MAGENTA' : '\033[95m',
- 'NORMAL' : '\033[0m',
- 'RED' : '\033[91m',
- 'WHITE' : '\033[97m',
- 'YELLOW' : '\033[93m',
-}
-
-
-def escape(color, value):
- return colors.get(color, '') + str(value) + colors.get('NORMAL', '')
-
-
-black = partial(escape, 'BLACK')
-blue = partial(escape, 'BLUE')
-cyan = partial(escape, 'CYAN')
-green = partial(escape, 'GREEN')
-magenta = partial(escape, 'MAGENTA')
-red = partial(escape, 'RED')
-white = partial(escape, 'WHITE')
-yellow = partial(escape, 'YELLOW')
=======================================
--- /trunk/bin/j2py Mon Oct 24 11:13:39 2011
+++ /trunk/bin/j2py Thu Oct 27 16:16:24 2011
@@ -15,7 +15,7 @@

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


version = '0.5'
@@ -221,7 +221,7 @@
## these next statements don't belong here, but this is as good a
## place as any.
if isWindows() or options.nocolor:
- colors.clear()
+ escapes.clear()
fmt = '# %(levelname)s %(funcName)s: %(message)s'
basicConfig(level=options.loglevel, format=fmt)
return options
=======================================
--- /trunk/java2python/compiler/template.py Mon Oct 24 11:13:39 2011
+++ /trunk/java2python/compiler/template.py Thu Oct 27 16:16:24 2011
@@ -19,8 +19,7 @@
from itertools import chain, ifilter, imap

from java2python.lang import tokens
-from java2python.lib import FS
-from java2python.lib.colortools import *
+from java2python.lib import FS, colors


class Factory(object):
@@ -132,12 +131,12 @@

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]
+ name = colors.white('name:') + colors.cyan(self.name) if self.name else ''
+ parts = [colors.green(self.typeName), name]
if self.type:
- parts.append(white('type:') + cyan(self.type))
+ parts.append(colors.white('type:') + colors.cyan(self.type))
if self.modifiers:
- parts.append(white('modifiers:') + cyan(','.join(self.modifiers)))
+ parts.append(colors.white('modifiers:') +
colors.cyan(','.join(self.modifiers)))
return ' '.join(parts)

def __str__(self):
@@ -295,17 +294,17 @@

def __repr__(self):
""" Returns the debug string representation of this template. """
- parts, parent, showfs = [blue(self.typeName)], self.parent, True
+ parts, parent, showfs = [colors.blue(self.typeName)], self.parent, True
if isinstance(self.left, (basestring, )) and self.left:
- parts.append(white('left:') + yellow(self.left))
+ parts.append(colors.white('left:') + colors.yellow(self.left))
showfs = False
if isinstance(self.right, (basestring, )) and self.right:
- parts.append(white('right:') + yellow(self.right))
+ parts.append(colors.white('right:') + colors.yellow(self.right))
showfs = False
if showfs:
- parts.append(white('format:') + yellow(self.fs))
+ parts.append(colors.white('format:') + colors.yellow(self.fs))
if self.tail:
- parts.append(white('tail:') + black(self.tail))
+ parts.append(colors.white('tail:') + colors.black(self.tail))
return ' '.join(parts)

def __str__(self):
@@ -341,8 +340,8 @@

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


@@ -361,7 +360,7 @@

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

def iterPrologue(self):
=======================================
--- /trunk/java2python/config/default.py Mon Oct 24 11:13:39 2011
+++ /trunk/java2python/config/default.py Thu Oct 27 16:16:24 2011
@@ -50,10 +50,11 @@


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


=======================================
--- /trunk/java2python/lang/base.py Tue Oct 18 21:33:24 2011
+++ /trunk/java2python/lang/base.py Thu Oct 27 16:16:24 2011
@@ -52,7 +52,7 @@
from antlr3 import ANTLRStringStream as StringStream, CommonTokenStream as
TokenStream
from antlr3.tree import CommonTreeAdaptor, CommonTree

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


class Tokens(object):
@@ -125,15 +125,15 @@

"""
colorTypeMap = {
- 'CLASS' : colortools.green,
- 'JAVA_SOURCE' : colortools.green,
- 'VOID_METHOD_DECL' : colortools.green,
- 'IDENT' : colortools.yellow,
- 'TYPE' : colortools.magenta,
- 'EXPR' : colortools.blue,
- 'TRUE' : colortools.yellow,
- 'FALSE' : colortools.yellow,
- 'NULL' : colortools.yellow,
+ 'CLASS' : colors.green,
+ 'JAVA_SOURCE' : colors.green,
+ 'VOID_METHOD_DECL' : colors.green,
+ 'IDENT' : colors.yellow,
+ 'TYPE' : colors.magenta,
+ 'EXPR' : colors.blue,
+ 'TRUE' : colors.yellow,
+ 'FALSE' : colors.yellow,
+ 'NULL' : colors.yellow,
}

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

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

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

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

def dump(self, fd, level=0):
""" Writes a debug representation of this tree to the given file. """
@@ -175,7 +175,7 @@
if line:
idxes = 'line={}{}{}'.format(line, ', ' if idxes else '',
idxes)
idxes = ' [{}]'.format(idxes) if idxes else ''
- idxes = colortools.black(idxes)
+ idxes = colors.black(idxes)
args = [indent, self.colorType(ttyp), '', idxes, '']
if extras(token.text, ttyp):
args[2] = ' ' + self.colorText(ttyp, token.text)
=======================================
--- /trunk/java2python/lib/__init__.py Sat Oct 15 15:17:26 2011
+++ /trunk/java2python/lib/__init__.py Thu Oct 27 16:16:24 2011
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from functools import partial


class FS(object):
@@ -25,3 +26,31 @@
if op == '>>>=':
return l + ' = bsr(' + l + ', ' + r + ')'
return l + ' ' + op + ' ' + r
+
+
+escapes = {
+ 'BLACK' : '\033[90m',
+ 'BLUE' : '\033[94m',
+ 'CYAN' : '\033[96m',
+ 'GREEN' : '\033[92m',
+ 'MAGENTA' : '\033[95m',
+ 'NORMAL' : '\033[0m',
+ 'RED' : '\033[91m',
+ 'WHITE' : '\033[97m',
+ 'YELLOW' : '\033[93m',
+}
+
+
+def escape(color, value):
+ return escapes.get(color, '') + str(value) + escapes.get('NORMAL', '')
+
+
+class colors:
+ black = partial(escape, 'BLACK')
+ blue = partial(escape, 'BLUE')
+ cyan = partial(escape, 'CYAN')
+ green = partial(escape, 'GREEN')
+ magenta = partial(escape, 'MAGENTA')
+ red = partial(escape, 'RED')
+ white = partial(escape, 'WHITE')
+ yellow = partial(escape, 'YELLOW')
=======================================
--- /trunk/java2python/mod/basic.py Mon Oct 24 11:13:39 2011
+++ /trunk/java2python/mod/basic.py Thu Oct 27 16:16:24 2011
@@ -161,13 +161,10 @@
methods = [item for item in group if isMethod(item)]
return methods[0].name if methods else -1

- gx = list(iterBody(obj.children))
- sgx = sorted(gx, key=sortBody)
- sgx = [item for sublist in sgx for item in sublist]
- obj.children = sgx
-
- return
- obj.children.sort(lambda x, y:-1 if x.isClass else 1)
+ grp = list(iterBody(obj.children))
+ grpsrt = sorted(grp, key=sortBody)
+ obj.children = [item for grp in grpsrt for item in grp]
+


def zopeInterfaceMethodMutator(obj):
=======================================
--- /trunk/test/selector/test_all.py Mon Oct 24 11:13:39 2011
+++ /trunk/test/selector/test_all.py Thu Oct 27 16:16:24 2011
@@ -7,7 +7,7 @@
from java2python.compiler import buildAST
from java2python.lang import tokens
from java2python.lang.selector import Token, Type
-from java2python.lib import colortools
+from java2python.lib import colors


def setUpModule():
@@ -26,7 +26,7 @@

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

@classmethod

Reply all
Reply to author
Forward
0 new messages