Commit aaf2edf1 authored by Michel Pelletier's avatar Michel Pelletier

Changes into trunk

parent 35697378
......@@ -23,7 +23,7 @@ class BrokenImplementation(Exception):
The %(name)s attribute was not provided.
""" % self.__dict__
class BrokenMethodImplementation(BrokenImplementation):
class BrokenMethodImplementation(Exception):
"""An method is not completely implemented.
"""
......
class InterfaceBase:
__tagged_values = {}
__meta_data = {}
def getName(self):
""" Returns the name of the object. """
......@@ -11,17 +11,17 @@ class InterfaceBase:
""" Returns the documentation for the object. """
return self.__doc__
def getTaggedValue(self, tag):
""" Returns the value associated with 'tag'. """
return self.__tagged_values[tag]
def getData(self, key):
""" Returns the value associated with 'key'. """
return self.__meta_data[key]
def getTaggedValueTags(self):
""" Returns a list of all tags. """
return self.__tagged_values.keys()
def getDataKeys(self):
""" Returns a list of all keys. """
return self.__meta_data.keys()
def setTaggedValue(self, tag, value):
def setData(self, key, value):
""" Associates 'value' with 'key'. """
self.__tagged_values[tag] = value
self.__meta_data[key] = value
......
......@@ -20,7 +20,7 @@ def _ii(klass, items):
for b in klass.__bases__: _ii(b, items)
return items
def implementedBy(object):
def objectImplements(object):
"""Return the interfaces implemented by the object
"""
r=[]
......@@ -43,7 +43,7 @@ def implementedBy(object):
return r
def implementedByInstancesOf(klass):
def instancesOfObjectImplements(klass):
"""Return the interfaces that instanced implement (by default)
"""
r=[]
......
......@@ -3,10 +3,11 @@ from Standard import Base
import iclass
new=iclass.Interface
del iclass
InterfaceInterface=iclass.InterfaceInterface
# del iclass
from Util import impliedInterface
from Util import assertTypeImplements, implementedBy, implementedByInstancesOf
from Util import assertTypeImplements, objectImplements, instancesOfObjectImplements
from Attr import Attribute
from Method import Method
......
......@@ -63,7 +63,7 @@ class Interface(InterfaceBase):
if b.extends(other): return 1
return 0
def implementedBy(self, object,
def isImplementedBy(self, object,
tiget=_typeImplements.get):
"""Does the given object implement the interface?
"""
......@@ -83,7 +83,7 @@ class Interface(InterfaceBase):
else:
return self.__any(implements)
def implementedByInstancesOf(self, klass,
def isImplementedByInstancesOf(self, klass,
tiget=_typeImplements.get):
"""Do instances of the given class implement the interface?
"""
......@@ -181,3 +181,80 @@ def assertTypeImplements(type, interfaces):
"""Return the interfaces implemented by objects of the given type
"""
_typeImplements[type]=interfaces
class InterfaceBaseInterface(Base):
"""
A base class that defines a common Interface inteface.
"""
def getName(self):
"""
Returns the name of the current interface object.
"""
def getDoc(self):
"""
Returns the documentation for the current interface object.
"""
class InterfaceInterface(InterfaceBaseInterface):
"""
Interface objects describe the behavior of an object by containing
useful information about the object. This information includes:
o Prose documentation about the object. In Python terms, this
is called the "doc string" of the interface. In this element,
you describe how the object works in prose language and any
other useful information about the object.
o Descriptions of attributes. Attribute descriptions include
the name of the attribute and prose documentation describing
the attributes usage.
o Descriptions of methods. Method descriptions can include:
o Prose "doc string" documentation about the method and its
usage.
o A description of the methods arguments; how many arguments
are expected, optional arguments and their default values,
the position or arguments in the signature, whether the
method accepts arbitrary arguments and whether the method
accepts arbitrary keyword arguments.
o Optional tagged data. Interface objects (and their attributes and
methods) can have optional, application specific tagged data
associated with them. Examples uses for this are examples,
security assertions, pre/post conditions, and other possible
information you may want to associate with an Interface or its
attributes.
Not all of this information is mandatory. For example, you may
only want the methods of your interface to have prose
documentation and not describe the arguments of the method in
exact detail. Interface objects are flexible and let you give or
take any of these components.
"""
def getBases(self):
"""
Returns a sequence of base interfaces this interface extends.
"""
def extends(self, other):
"""
"""
Interface.__implements__ = (InterfaceInterface,)
......@@ -3,6 +3,27 @@ import string
""" Pretty-Print an Interface object as structured text (Yum) """
def trim_doc_string(text):
"""
Trims a doc string to make it format
correctly with structured text.
"""
text=string.strip(text)
text=string.replace(text, '\r\n', '\n')
lines=string.split(text, '\n')
nlines=[lines[0]]
if len(lines) > 1:
min_indent=None
for line in lines[1:]:
indent=len(line) - len(string.lstrip(line))
if indent < min_indent or min_indent is None:
min_indent=indent
for line in lines[1:]:
nlines.append(line[min_indent:])
return string.join(nlines, '\n')
def justify_and_indent(text, level, munge=0, width=72):
""" indent and justify text, rejustify (munge) if specified """
......@@ -39,14 +60,14 @@ def interface_as_stx(I, munge=0):
level = 1
if I.getDoc():
outp = outp + justify_and_indent(I.getDoc(), level) + "\n\n"
outp = outp + justify_and_indent(trim_doc_string(I.getDoc()), level) + "\n\n"
if I.getBases():
outp = outp + (" " * level) + "This interface extends:\n\n"
level = level + 1
for b in I.getBases():
item = "o %s" % b.getName()
outp = outp + justify_and_indent(item, level, munge) + "\n\n"
outp = outp + justify_and_indent(trim_doc_string(item), level, munge) + "\n\n"
level = level - 1
level = level + 1
......@@ -56,7 +77,7 @@ def interface_as_stx(I, munge=0):
else:
item = "%s -- %s" % (desc.getName(), desc.getDoc())
outp = outp + justify_and_indent(item, level, munge) + "\n\n"
outp = outp + justify_and_indent(trim_doc_string(item), level, munge) + "\n\n"
return outp
......
......@@ -12,11 +12,11 @@ class C:
IC=Interface.impliedInterface(C)
print "should be 0:", IC.implementedByInstancesOf(C)
print "should be 0:", IC.isImplementedByInstancesOf(C)
C.__implements__=IC
print "should be 1:", IC.implementedByInstancesOf(C)
print "should be 1:", IC.isImplementedByInstancesOf(C)
class I1(Interface.Base):
def ma(self):
......@@ -42,25 +42,25 @@ class E(A, B):
print
for c in A, B, C, D, E:
print "%s implements: %s" % (
c.__name__, Interface.implementedByInstancesOf(c))
c.__name__, Interface.instancesOfObjectImplements(c))
print
for c in A, B, C, D, E:
print "an instance of %s implements: %s" % (
c.__name__,
Interface.implementedBy(c()))
Interface.objectImplements(c()))
for i in I1, I2, I3, I4, IC:
print
for c in A, B, C, D, E:
print "%s is implemented by instances of %s? %s" % (
i.__name__, c.__name__,
i.implementedByInstancesOf(c))
i.isImplementedByInstancesOf(c))
print
for c in A, B, C, D, E:
print "%s is implemented by an instance of %s? %s" % (
i.__name__, c.__name__,
i.implementedBy(c()))
i.isImplementedBy(c()))
a=A()
try:
......@@ -151,16 +151,16 @@ class Blah:
blah_instance = Blah()
if AnABCInterface.implementedBy(concrete_instance):
if AnABCInterface.isImplementedBy(concrete_instance):
print "%s is an instance that implements %s" % (concrete_instance, AnABCInterface.__name__)
if AnABCInterface.implementedByInstancesOf(AConcreteClass):
if AnABCInterface.isImplementedByInstancesOf(AConcreteClass):
print "%s is a class that implements %s" % (AConcreteClass, AnABCInterface.__name__)
if not AnABCInterface.implementedBy(blah_instance):
if not AnABCInterface.isImplementedBy(blah_instance):
print "%s is NOT an instance that implements %s" % (blah_instance, AnABCInterface.__name__)
if not AnABCInterface.implementedByInstancesOf(Blah):
if not AnABCInterface.isImplementedByInstancesOf(Blah):
print "%s is NOT a class that implements %s" % (Blah, AnABCInterface.__name__)
......
......@@ -16,7 +16,7 @@ def verify_class_implementation(iface, klass):
"""
if not iface.implementedByInstancesOf(klass):
if not iface.isImplementedByInstancesOf(klass):
raise DoesNotImplement(iface)
for n, d in iface.namesAndDescriptions():
......@@ -29,7 +29,7 @@ def verify_class_implementation(iface, klass):
elif type(attr) is types.MethodType:
meth = Method().fromMethod(attr, n)
else:
pass # must be an attribute...
break # must be an attribute...
if d.getSignatureInfo() != meth.getSignatureInfo():
raise BrokenMethodImplementation(n)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment