[Zope-Checkins] CVS: Zope3/lib/python/Interface - Document.py:1.4 IInterface.py:1.4 Method.py:1.12 _InterfaceClass.py:1.6 pyskel.py:1.5
Jim Fulton
jim@zope.com
Mon, 1 Jul 2002 14:12:30 -0400
Update of /cvs-repository/Zope3/lib/python/Interface
In directory cvs.zope.org:/tmp/cvs-serv21785/lib/python/Interface
Modified Files:
Document.py IInterface.py Method.py _InterfaceClass.py
pyskel.py
Log Message:
Added several interface mapping operations and methods: __getitem__,
get, __contains__, and __iter__.
Changed the code to extract metadata from functions to:
- Use the name assigned in the interface, rather than the name used
in the function. This is to allow aliasing::
class I(Interface):
def f1(blah): pass
f2 = f1
- Copy function attributes to tagged values. This will allow us to
set meta-data for things like optional methods or attributes
defined by instances rather than classes::
class I(Interface):
def f1(blah): pass
f1.optional = 1 # Want's to be True ;)
Changed pyskel to:
- Not include doc strings that reference an interface,
- Not generate comments for interfaces that don't define any names.
- Generate code to interface-defined doc strings to generated classes and
their method skeletin. Of course, this won't work with
documentation tools that parse source. :(
=== Zope3/lib/python/Interface/Document.py 1.3 => 1.4 ===
outp(_justify_and_indent("Attributes:", level, munge)+'\n\n')
level = level + 1
- for name, desc in I.namesAndDescriptions():
+
+ namesAndDescriptions = I.namesAndDescriptions()
+ namesAndDescriptions.sort()
+
+ for name, desc in namesAndDescriptions:
if not hasattr(desc, 'getSignatureString'): # ugh...
item = "%s -- %s" % (desc.getName(),
desc.getDoc() or 'no documentation')
@@ -56,7 +60,7 @@
outp(_justify_and_indent("Methods:", level, munge)+'\n\n')
level = level + 1
- for name, desc in I.namesAndDescriptions():
+ for name, desc in namesAndDescriptions:
if hasattr(desc, 'getSignatureString'): # ugh...
item = "%s%s -- %s" % (desc.getName(),
desc.getSignatureString(),
=== Zope3/lib/python/Interface/IInterface.py 1.3 => 1.4 ===
"""Get the description for a name
- If the named attribute does not exist, a KeyError is raised.
+ If the named attribute is not defines, a KeyError is raised.
"""
+ __getitem__ = getDescriptionFor
+
def queryDescriptionFor(name, default=None):
"""Look up the description for a name
- If the named attribute does not exist, the default is
+ If the named attribute is not defined, the default is
returned.
+ """
+
+ get = queryDescriptionFor
+
+ def __contains__(name):
+ """Test whether the name is defined by the interface"""
+
+ def __iter__():
+ """Return an iterator over the names defined by the interface
+
+ The names iterated include all of the names defined by the
+ interface directly and indirectly by base interfaces.
"""
=== Zope3/lib/python/Interface/Method.py 1.11 => 1.12 ===
-def fromFunction(func, interface='', imlevel=0):
- m=Method(func.__name__, func.__doc__)
+def fromFunction(func, interface='', imlevel=0, name=None):
+ name = name or func.__name__
+ m=Method(name, func.__doc__)
defaults=func.func_defaults or ()
c=func.func_code
na=c.co_argcount-imlevel
@@ -99,6 +100,11 @@
m.kwargs = None
m.interface=interface
+
+ for k, v in func.__dict__.items():
+ m.setTaggedValue(k, v)
+
+
return m
def fromMethod(meth, interface=''):
=== Zope3/lib/python/Interface/_InterfaceClass.py 1.5 => 1.6 ===
v.__name__ = k
elif isinstance(v, FunctionType):
- attrs[k]=fromFunction(v, name)
+ attrs[k]=fromFunction(v, name, name=k)
else:
raise Exceptions.InvalidInterface(
"Concrete attribute, %s" % k)
@@ -133,6 +133,9 @@
for name in base.names(all):
r[name] = 1
return r.keys()
+
+ def __iter__(self):
+ return iter(self.names(1))
def namesAndDescriptions(self, all=0):
"""Return the attribute names and descriptions defined by the interface
@@ -160,6 +163,11 @@
raise KeyError, name
+ __getitem__ = getDescriptionFor
+
+ def __contains__(self, name):
+ return self.queryDescriptionFor(name) is not None
+
def queryDescriptionFor(self, name, default=None):
"""Return the attribute description for the given name
"""
@@ -172,6 +180,8 @@
return r
return default
+
+ get = queryDescriptionFor
def deferred(self):
"""Return a defered class corresponding to the interface
=== Zope3/lib/python/Interface/pyskel.py 1.4 => 1.5 ===
-def rskel(iface, print_iface=1):
+def rskel(iface, top, print_iface=1):
name = "%s.%s" % (iface.__module__, iface.__name__)
file = resolve(iface.__module__).__file__
@@ -50,6 +50,11 @@
order = guessOrder(open(file))
namesAndDescriptions = getAttributesInOrder(iface, order)
+ namesAndDescriptions = filter(lambda ades:
+ isinstance(ades[1], Method) or
+ isinstance(ades[1], Attribute),
+ namesAndDescriptions)
+
if namesAndDescriptions and print_iface:
print
print " ######################################"
@@ -62,16 +67,22 @@
else: sig = "self"
print
print " def %s(%s):" % (aname, sig)
- print " 'See %s'" % name
+ print
+ print
+ print " %s.__doc__ = %s['%s'].__doc__" % (
+ aname, top.__name__, aname)
elif isinstance(ades, Attribute):
print
- print " # See %s" % name
print " %s = None" %aname
+ else:
+ print
+ print 'Waaaa', ades
+
for base in iface.__bases__:
if base.__name__ not in ('Interface',):
- rskel(base)
+ rskel(base, top)
def skel(name):
iface = resolve(name)
@@ -81,6 +92,7 @@
print "from %s import %s" % (iface.__module__, iface.__name__)
print
print "class %s:" %class_name
+ print " __doc__ = %s.__doc__" % iface.__name__
print
print " __implements__ = ", iface.__name__
print
@@ -88,7 +100,7 @@
print " # Implementation methods for interface"
print " #", name
- rskel(iface, 0)
+ rskel(iface, iface, 0)
print
print " #"