Commit e5806058 authored by Jim Fulton's avatar Jim Fulton

more work on first cut

parent 0318a786
...@@ -4,7 +4,7 @@ class Attribute: ...@@ -4,7 +4,7 @@ class Attribute:
""" """
def __init__(self, __name__=None, __doc__=None): def __init__(self, __name__=None, __doc__=None):
"""Create an 'attribute' description """Create an 'attribute' description
""" """
self.__name__=__name__ self.__name__=__name__
self.__doc__=__doc__ or __name__ self.__doc__=__doc__ or __name__
...@@ -4,13 +4,13 @@ class BrokenImplementation(Exception): ...@@ -4,13 +4,13 @@ class BrokenImplementation(Exception):
""" """
def __init__(self, interface, name): def __init__(self, interface, name):
self.interface=interface self.interface=interface
self.name=name self.name=name
def __str__(self): def __str__(self):
return """An object has failed to implement interface %(interface)s return """An object has failed to implement interface %(interface)s
The %(name)s attribute was not provided. The %(name)s attribute was not provided.
""" % self.__dict__ """ % self.__dict__
class InvalidInterface(Exception): pass class InvalidInterface(Exception): pass
...@@ -6,27 +6,27 @@ from Attr import Attribute ...@@ -6,27 +6,27 @@ from Attr import Attribute
class MethodClass: class MethodClass:
def fromFunction(self, func, interface=''): def fromFunction(self, func, interface=''):
m=Method(func.__name__, func.__doc__) m=Method(func.__name__, func.__doc__)
defaults=func.func_defaults or () defaults=func.func_defaults or ()
c=func.func_code c=func.func_code
na=c.co_argcount na=c.co_argcount
names=c.co_varnames names=c.co_varnames
d={} d={}
nr=na-len(defaults) nr=na-len(defaults)
if nr==0: if nr==0:
defaults=defaults[1:] defaults=defaults[1:]
nr=1 nr=1
for i in range(len(defaults)): for i in range(len(defaults)):
d[names[i+nr]]=defaults[i] d[names[i+nr]]=defaults[i]
m.positional=names[1:na] m.positional=names[1:na]
m.required=names[1:nr] m.required=names[1:nr]
m.optional=d m.optional=d
m.varargs = not not (c.co_flags & 4) m.varargs = not not (c.co_flags & 4)
m.kwargs = not not (c.co_flags & 8) m.kwargs = not not (c.co_flags & 8)
m.interface=interface m.interface=interface
return m return m
class Method(Attribute): class Method(Attribute):
"""Method interfaces """Method interfaces
...@@ -39,12 +39,12 @@ class Method(Attribute): ...@@ -39,12 +39,12 @@ class Method(Attribute):
interface='' interface=''
def __init__(self, __name__=None, __doc__=None): def __init__(self, __name__=None, __doc__=None):
"""Create a 'method' description """Create a 'method' description
""" """
self.__name__=__name__ self.__name__=__name__
self.__doc__=__doc__ or __name__ self.__doc__=__doc__ or __name__
def __call__(self, *args, **kw): def __call__(self, *args, **kw):
raise Exception.BrokenImplementation(self.interface, self.name) raise Exception.BrokenImplementation(self.interface, self.name)
...@@ -23,61 +23,61 @@ class Interface: ...@@ -23,61 +23,61 @@ class Interface:
def __init__(self, name, bases=(), attrs=None, __doc__=None): def __init__(self, name, bases=(), attrs=None, __doc__=None):
"""Create a new interface """Create a new interface
""" """
for b in bases: for b in bases:
if not isinstance(b, Interface): if not isinstance(b, Interface):
raise TypeError, 'Expected base interfaces' raise TypeError, 'Expected base interfaces'
self.__bases__=bases self.__bases__=bases
self.__name__=name self.__name__=name
if attrs is None: attrs={} if attrs is None: attrs={}
if attrs.has_key('__doc__'): if attrs.has_key('__doc__'):
if __doc__ is None: __doc__=attrs['__doc__'] if __doc__ is None: __doc__=attrs['__doc__']
del attrs['__doc__'] del attrs['__doc__']
self.__attrs=attrs self.__attrs=attrs
if __doc__ is not None: self.__doc__=__doc__ if __doc__ is not None: self.__doc__=__doc__
for k, v in attrs.items(): for k, v in attrs.items():
if isinstance(v, Method): if isinstance(v, Method):
v.interface=name v.interface=name
v.__name__=k v.__name__=k
elif isinstance(v, FunctionType): elif isinstance(v, FunctionType):
attrs[k]=Method.fromFunction(v, name) attrs[k]=Method.fromFunction(v, name)
elif not isinstance(v, Attribute): elif not isinstance(v, Attribute):
raise Exceptions.InvalidInterface( raise Exceptions.InvalidInterface(
"Concrete attribute, %s" % k) "Concrete attribute, %s" % k)
def defered(self): def defered(self):
"""Return a defered class corresponding to the interface """Return a defered class corresponding to the interface
""" """
if hasattr(self, "_defered"): return self._defered if hasattr(self, "_defered"): return self._defered
klass={} klass={}
exec "class %s: pass" % self.__name__ in klass exec "class %s: pass" % self.__name__ in klass
klass=klass[self.__name__] klass=klass[self.__name__]
self.__d(klass.__dict__) self.__d(klass.__dict__)
self._defered=klass self._defered=klass
return klass return klass
def __d(self, dict): def __d(self, dict):
for k, v in self.__dict__.items(): for k, v in self.__dict__.items():
if isinstance(v, Method) and not dict.has_key(k): if isinstance(v, Method) and not dict.has_key(k):
dict[k]=v dict[k]=v
for b in self.__bases__: b.__d(dict) for b in self.__bases__: b.__d(dict)
def extends(self, other): def extends(self, other):
"""Does an interface extend another? """Does an interface extend another?
""" """
for b in self.__bases__: for b in self.__bases__:
if b is other: return 1 if b is other: return 1
if b.extends(other): return 1 if b.extends(other): return 1
return 0 return 0
def implementedBy(self, object, def implementedBy(self, object,
tiget=_typeImplements.get): tiget=_typeImplements.get):
...@@ -94,10 +94,10 @@ class Interface: ...@@ -94,10 +94,10 @@ class Interface:
implements=tiget(t, None) implements=tiget(t, None)
if implements is None: return 0 if implements is None: return 0
if isinstance(implements,Interface): if isinstance(implements,Interface):
return implements is self or implements.extends(self) return implements is self or implements.extends(self)
else: else:
return self.__any(implements) return self.__any(implements)
def implementedByInstancesOf(self, klass, def implementedByInstancesOf(self, klass,
tiget=_typeImplements.get): tiget=_typeImplements.get):
...@@ -115,10 +115,10 @@ class Interface: ...@@ -115,10 +115,10 @@ class Interface:
if implements is None: return 0 if implements is None: return 0
if isinstance(implements,Interface): if isinstance(implements,Interface):
return implements is self or implements.extends(self) return implements is self or implements.extends(self)
else: else:
return self.__any(implements) return self.__any(implements)
def names(self): def names(self):
"""Return the attribute names defined by the interface """Return the attribute names defined by the interface
......
Attr.py
Basic.py
Exceptions.py
Mapping.py
Method.py
Number.py
Standard.py
Util.py
__init__.py
iclass.py
R=$1
M=Interface
for f in test.py `cat pyfiles`; do
python1.5.1 /projects/sbin/tabnanny.py $f
done
mkdir "$M-$R" "$M-$R/Interface"
for f in `cat pyfiles`; do
cp $f "$M-$R/Interface/"
done
for f in README.txt test.py; do
cp $f "$M-$R/"
done
tar cvf "$M-$R.tar" "$M-$R"
rm -f "$M-$R.tar.gz"
gzip "$M-$R.tar"
import Interface
class C:
def m1(self, a, b):
"return 1"
return 1
def m2(self, a, b):
"return 2"
return 2
IC=Interface.impliedInterface(C)
print "should be 0:", IC.implementedByInstancesOf(C)
C.__implements__=IC
print "should be 1:", IC.implementedByInstancesOf(C)
class I1(Interface.Base):
def ma(self):
"blah"
class I2(I1): pass
class I3(Interface.Base): pass
class I4(Interface.Base): pass
class A(I1.defered()):
__implements__=I1
class B:
__implements__=I2, I3
class D(A, B): pass
class E(A, B):
__implements__ = A.__implements__, C.__implements__
print
for c in A, B, C, D, E:
print "%s implements: %s" % (
c.__name__, Interface.implementedByInstancesOf(c))
print
for c in A, B, C, D, E:
print "an instance of %s implements: %s" % (
c.__name__,
Interface.implementedBy(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))
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()))
a=A()
try:
a.ma()
print "something's wrong, this should have failed!"
except:
pass
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