[java2python] r187 committed - Work on better zope interface example.

6 views
Skip to first unread message

java2...@googlecode.com

unread,
Nov 2, 2011, 7:28:44 PM11/2/11
to java2pyth...@googlegroups.com
Revision: 187
Author: troy.melhase
Date: Wed Nov 2 16:27:47 2011
Log: Work on better zope interface example.
http://code.google.com/p/java2python/source/detail?r=187

Modified:
/trunk/java2python/compiler/template.py
/trunk/java2python/config/default.py
/trunk/java2python/mod/basic.py
/trunk/test/Interface3.java
/trunk/test/configs/Interface3.py
/trunk/test/configs/__init__.py

=======================================
--- /trunk/java2python/compiler/template.py Sat Oct 29 22:05:12 2011
+++ /trunk/java2python/compiler/template.py Wed Nov 2 16:27:47 2011
@@ -253,6 +253,15 @@
yield self
self = self.parent

+ def find(self, pred=lambda v:True):
+ """ Yield each child in the family tree. """
+ for child in self.children:
+ if pred(child):
+ yield child
+ if hasattr(child, 'find'):
+ for value in child.find(pred):
+ yield value
+
@property
def className(self):
""" Returns the name of the class of this item. """
@@ -403,9 +412,7 @@

def iterBases(self):
""" Yields the base classes for this type. """
- for handler in self.configHandlers('Base'):
- for base in handler(self):
- yield base
+ return chain(*(h(self) for h in self.configHandlers('Base')))

def iterDecl(self):
""" Yields the declaration for this type. """
@@ -474,13 +481,17 @@
super(Method, self).__init__(config, name, type, parent)
self.parameters.append(self.makeParam('self', 'object'))

+ def iterParams(self):
+ """ Yields the parameters of this method template. """
+ return chain(*(h(self) for h in self.configHandlers('Param')))
+
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)
+ params = ', '.join(formatParam(param) for param in self.iterParams())
yield 'def {0}({1}):'.format(self.name, params)

def iterBody(self):
=======================================
--- /trunk/java2python/config/default.py Sat Oct 29 22:05:12 2011
+++ /trunk/java2python/config/default.py Wed Nov 2 16:27:47 2011
@@ -48,12 +48,16 @@
basic.simpleDocString,
]

+methodParamHandlers = [
+ basic.defaultParams,
+]
+
classBaseHandlers = [
basic.defaultBases,
]

interfaceBaseHandlers = [
- basic.zopeInterfaceBases,
+ basic.defaultBases,
]

# These generators are called after a class has been completely
@@ -77,7 +81,6 @@


interfacePostWalkMutators = [
- basic.zopeInterfaceMethodMutator,
]


=======================================
--- /trunk/java2python/mod/basic.py Sat Oct 29 22:05:12 2011
+++ /trunk/java2python/mod/basic.py Wed Nov 2 16:27:47 2011
@@ -166,16 +166,43 @@
obj.children = [item for grp in grpsrt for item in grp]


-
-def zopeInterfaceMethodMutator(obj):
- for method in [c for c in obj.children if c.isMethod]:
- if method.parameters and method.parameters[0]['name'] == 'self':
- method.parameters.pop(0)
+def defaultParams(obj):
+ return iter(obj.parameters)
+
+def zopeInterfaceMethodParams(obj):
+ if not obj.parent.isInterface:
+ for param in obj.parameters:
+ yield param
+ else:
+ for index, param in enumerate(obj.parameters):
+ if index != 0 and param['name'] != 'self':
+ yield param
+
+
+normalBases = ('object', )


def defaultBases(obj):
- return iter(obj.bases or ['object'])
+ return iter(obj.bases or normalBases)


def zopeInterfaceBases(obj):
- return ['zope.interface.Interface']
+ return iter(obj.bases or ['zope.interface.Interface'])
+
+
+def implAny(obj):
+ for module in obj.parents(lambda x:x.isModule):
+ for name in obj.bases:
+ if any(module.find(lambda v:v.name == name)):
+ return True
+
+
+
+def zopeImplementsClassBases(obj):
+ return iter(normalBases) if implAny(obj) else defaultBases(obj)
+
+
+def zopeImplementsClassHead(obj):
+ if implAny(obj):
+ for cls in obj.bases:
+ yield 'zope.interface.implements({})'.format(cls)
=======================================
--- /trunk/test/Interface3.java Fri Jul 9 12:37:04 2010
+++ /trunk/test/Interface3.java Wed Nov 2 16:27:47 2011
@@ -12,6 +12,8 @@
}


+class Nothing {};
+
class Interface3 implements C0 {
public void m(int x) {
System.out.println(x);
=======================================
--- /trunk/test/configs/Interface3.py Thu Oct 27 16:16:24 2011
+++ /trunk/test/configs/Interface3.py Wed Nov 2 16:27:47 2011
@@ -1,22 +1,49 @@
+##
+# This is an example of how to perform per-module configuration
+# for translating java interfaces to zope interfaces.
+#
from java2python.mod import basic
+from java2python.config import default


-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 = [
-
- ]
+# the j2py default head handlers for interfaces includes ABCMeta as a
+# metaclass. we certainly don't want that, so we redefine the list
+# here to only the doc string handler:
+interfaceHeadHandlers = [
+ basic.simpleDocString,
+]
+
+
+# this j2py default is also not what we want, so we redefine it:
+methodPrologueHandlers = [
+ basic.maybeClassMethod,
+]
+
+
+# instead of the default bases, this handler supplies the base zope
+# Interface class for Java interfaces:
+interfaceBaseHandlers = [
+ basic.zopeInterfaceBases,
+]
+
+
+# the parser adds implemented interfaces to the class bases list.
+# this handler checks to see if any of those bases are interfaces, and
+# if so, supresses them in favor of 'object' as the only base:
+classBaseHandlers = [
+ basic.zopeImplementsClassBases,
+]
+
+
+# this handler adds a line like "zope.interface.implements(IFoo)" for
+# each interface implemented by a Java class:
+classHeadHandlers = [
+ basic.zopeImplementsClassHead,
+]
+
+
+# this handler supresses the "self" parameter on method signatures for
+# zope Interface definitions:
+methodParamHandlers = [
+ basic.zopeInterfaceMethodParams,
+]
=======================================
--- /trunk/test/configs/__init__.py Sun Oct 16 16:26:06 2011
+++ /trunk/test/configs/__init__.py Wed Nov 2 16:27:47 2011
@@ -6,6 +6,7 @@
modulePrologueHandlers = default.modulePrologueHandlers + [
'from configs.overloading import overloaded',
'from abc import ABCMeta, abstractmethod',
+ 'import zope.interface',
]


Reply all
Reply to author
Forward
0 new messages