Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
48f0cfe9
Commit
48f0cfe9
authored
Mar 13, 2000
by
Michel Pelletier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding interface framework
parent
d88cda61
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
636 additions
and
0 deletions
+636
-0
lib/python/Zope/Interfaces/Interface/Attr.py
lib/python/Zope/Interfaces/Interface/Attr.py
+13
-0
lib/python/Zope/Interfaces/Interface/Basic.py
lib/python/Zope/Interfaces/Interface/Basic.py
+23
-0
lib/python/Zope/Interfaces/Interface/Exceptions.py
lib/python/Zope/Interfaces/Interface/Exceptions.py
+16
-0
lib/python/Zope/Interfaces/Interface/Interface.py
lib/python/Zope/Interfaces/Interface/Interface.py
+8
-0
lib/python/Zope/Interfaces/Interface/Mapping.py
lib/python/Zope/Interfaces/Interface/Mapping.py
+22
-0
lib/python/Zope/Interfaces/Interface/Method.py
lib/python/Zope/Interfaces/Interface/Method.py
+53
-0
lib/python/Zope/Interfaces/Interface/Number.py
lib/python/Zope/Interfaces/Interface/Number.py
+95
-0
lib/python/Zope/Interfaces/Interface/Standard.py
lib/python/Zope/Interfaces/Interface/Standard.py
+20
-0
lib/python/Zope/Interfaces/Interface/Util.py
lib/python/Zope/Interfaces/Interface/Util.py
+82
-0
lib/python/Zope/Interfaces/Interface/__init__.py
lib/python/Zope/Interfaces/Interface/__init__.py
+30
-0
lib/python/Zope/Interfaces/Interface/iclass.py
lib/python/Zope/Interfaces/Interface/iclass.py
+183
-0
lib/python/Zope/Interfaces/InterfaceImplementer.py
lib/python/Zope/Interfaces/InterfaceImplementer.py
+17
-0
lib/python/Zope/Interfaces/InterfaceInterface.py
lib/python/Zope/Interfaces/InterfaceInterface.py
+66
-0
lib/python/Zope/Interfaces/__init__.py
lib/python/Zope/Interfaces/__init__.py
+8
-0
No files found.
lib/python/Zope/Interfaces/Interface/Attr.py
0 → 100644
View file @
48f0cfe9
class
Attribute
:
"""Attribute descriptions
"""
def
__init__
(
self
,
__name__
=
None
,
__doc__
=
None
):
"""Create an 'attribute' description
"""
self
.
__name__
=
__name__
self
.
__doc__
=
__doc__
or
__name__
def
documentation
(
self
):
return
self
.
__doc__
lib/python/Zope/Interfaces/Interface/Basic.py
0 → 100644
View file @
48f0cfe9
from
iclass
import
Base
class
Mutable
(
Base
):
"""Mutable objects
"""
class
Comparable
(
Base
):
"""Objects that can be tested for equality
"""
class
Orderable
(
Comparable
):
"""Objects that can be compared with < > == <= >= !="""
class
Hashable
(
Base
):
"""Objects that support hash"""
class
HashKey
(
Comparable
,
Hashable
):
"""Objects that are immutable with respect to state that
influences comparisons or hash values"""
lib/python/Zope/Interfaces/Interface/Exceptions.py
0 → 100644
View file @
48f0cfe9
class
BrokenImplementation
(
Exception
):
"""An attribute is not completely implemented.
"""
def
__init__
(
self
,
interface
,
name
):
self
.
interface
=
interface
self
.
name
=
name
def
__str__
(
self
):
return
"""An object has failed to implement interface %(interface)s
The %(name)s attribute was not provided.
"""
%
self
.
__dict__
class
InvalidInterface
(
Exception
):
pass
lib/python/Zope/Interfaces/Interface/Interface.py
0 → 100644
View file @
48f0cfe9
from
iclass
import
iclass
class
Interface
(
iclass
):
""" This is a high level interface object. You can just pass a
class to this interface and it will deduce an interface object
from that class """
lib/python/Zope/Interfaces/Interface/Mapping.py
0 → 100644
View file @
48f0cfe9
import
Basic
,
Util
class
Mapping
(
Basic
.
Base
):
"anything supporting __getitem__"
class
Sized
(
Basic
.
Base
):
"anything supporting __len"
class
MutableMapping
(
Basic
.
Mutable
):
"Has __setitem__ and __delitem__"
class
Sequence
(
Mapping
):
"Keys must be integers in a sequence starting at 0."
class
Sequential
(
Sequence
):
"Keys must be used in order"
Util
.
assertTypeImplements
(
type
(()),
(
Sequence
,
Sized
,
Basic
.
HashKey
))
Util
.
assertTypeImplements
(
type
([]),
(
Sequence
,
Sized
,
MutableMapping
))
Util
.
assertTypeImplements
(
type
({}),
(
Mapping
,
Sized
,
MutableMapping
))
lib/python/Zope/Interfaces/Interface/Method.py
0 → 100644
View file @
48f0cfe9
"""Method interfaces
"""
import
Exceptions
from
Attr
import
Attribute
class
MethodClass
:
def
fromFunction
(
self
,
func
,
interface
=
''
):
m
=
Method
(
func
.
__name__
,
func
.
__doc__
)
defaults
=
func
.
func_defaults
or
()
c
=
func
.
func_code
na
=
c
.
co_argcount
names
=
c
.
co_varnames
d
=
{}
nr
=
na
-
len
(
defaults
)
if
nr
==
0
:
defaults
=
defaults
[
1
:]
nr
=
1
for
i
in
range
(
len
(
defaults
)):
d
[
names
[
i
+
nr
]]
=
defaults
[
i
]
m
.
positional
=
names
[
1
:
na
]
m
.
required
=
names
[
1
:
nr
]
m
.
optional
=
d
try
:
m
.
varargs
=
not
not
(
c
.
co_flags
&
4
)
m
.
kwargs
=
not
not
(
c
.
co_flags
&
8
)
except
:
pass
m
.
interface
=
interface
return
m
class
Method
(
Attribute
):
"""Method interfaces
The idea here is that you have objects that describe methods.
This provides an opportunity for rich meta-data.
"""
fromFunction
=
MethodClass
().
fromFunction
interface
=
''
def
__init__
(
self
,
__name__
=
None
,
__doc__
=
None
):
"""Create a 'method' description
"""
self
.
__name__
=
__name__
self
.
__doc__
=
__doc__
or
__name__
## def __call__(self, *args, **kw):
## raise Exception.BrokenImplementation(self.interface, self.name)
lib/python/Zope/Interfaces/Interface/Number.py
0 → 100644
View file @
48f0cfe9
import
Basic
,
Util
class
BitNumber
(
Basic
.
Base
):
"""Numbers that can be interpreted as strings of bits
they support & | ~ ^ << >>
"""
class
Number
(
Basic
.
Base
):
"""Numeric interface
it is assumed that numeric types are embedded in each other in some
way (this is the Scheme numerical tower I think); they support
operators + - *, usually / and %, perhaps **
"""
class
Offsetable
(
Basic
.
Base
):
"""Things that you can subtract and add numbers too
e.g. Date-Time values.
"""
class
Real
(
Number
):
"""any subset of the real numbers (this includes ints and rationals)
"""
class
Complex
(
Number
):
"""has re and im fields (which are themselves real)
"""
class
Exact
(
Number
):
"""e.g. integers and rationals; also fixed point
"""
class
AbritraryPrecision
(
Exact
):
"""e.g. long, rationals
"""
class
FixedPrecisionInt
(
Exact
):
"""Fixed precision integers
"""
class
FP64
(
FixedPrecisionInt
):
"""int() applied to these returns an integer that fits in 32 bits
"""
class
FP32
(
FP64
):
"""int() applied to these returns an integer that fits in 32 bits
"""
class
FP16
(
FP32
):
"""int() applied to these returns an integer that fits in 16 bits
"""
class
FP8
(
FP16
):
"""int() applied to these returns an integer that fits in 16 bits
"""
class
FP1
(
FP8
):
"""int() applied to these returns an integer that fits in 16 bits
"""
class
Signed
(
FixedPrecisionInt
):
"""These can be < 0
"""
class
Unsigned
(
FixedPrecisionInt
):
"""These can not be < 0
"""
class
CheckedOverflow
(
FixedPrecisionInt
):
"""raises OverflowError when results don't fit
"""
class
UncheckedOverflow
(
FixedPrecisionInt
):
"""take results module 2**N (for example)
"""
class
FD
(
Exact
):
"""fixed_decimal<n+m>
a signed fixed decimal number with n digits
before and m digits after the decimal point
"""
class
Inexact
(
Number
):
"""floating point arithmetic
"""
Util
.
assertTypeImplements
(
type
(
1
),
(
FP32
,
BitNumber
,
Signed
))
Util
.
assertTypeImplements
(
type
(
1L
),
(
AbritraryPrecision
,
BitNumber
,
Signed
))
Util
.
assertTypeImplements
(
type
(
1.0
),
(
Inexact
,
BitNumber
,
Signed
))
lib/python/Zope/Interfaces/Interface/Standard.py
0 → 100644
View file @
48f0cfe9
import
iclass
,
Util
Interface
=
Util
.
impliedInterface
(
iclass
.
Interface
,
"Interface"
,
"""Interface of Interface objects
"""
)
iclass
.
Interface
.
__implements__
=
Interface
from
iclass
import
Named
,
Class
from
Basic
import
*
Util
.
assertTypeImplements
(
iclass
.
ClassType
,
Class
)
from
Number
import
*
from
Mapping
import
*
del
iclass
# cruft
del
Util
# cruft
lib/python/Zope/Interfaces/Interface/Util.py
0 → 100644
View file @
48f0cfe9
from
iclass
import
Interface
,
Class
,
ClassType
,
Base
,
assertTypeImplements
from
types
import
FunctionType
def
impliedInterface
(
klass
,
__name__
=
None
,
__doc__
=
None
):
"""Create an interface object from a class
The interface will contain only objects with doc strings and with names
that begin and end with '__' or names that don't begin with '_'.
"""
if
__name__
is
None
:
__name__
=
"%sInterface"
%
klass
.
__name__
if
hasattr
(
klass
,
'__extends__'
):
bases
=
klass
.
__extends__
else
:
## Note that if your class subclasses Interfaces, then your
## inteface attributes will not get picked up...
bases
=
klass
.
__bases__
doc
=
__doc__
or
klass
.
__doc__
return
Interface
(
__name__
,
bases
,
_ii
(
klass
,
{}),
doc
)
def
_ii
(
klass
,
items
):
for
k
,
v
in
klass
.
__dict__
.
items
():
if
type
(
v
)
is
not
FunctionType
or
not
v
.
__doc__
:
continue
if
k
[:
1
]
==
'_'
and
not
(
len
(
k
)
>
4
and
k
[:
2
]
==
'__'
and
k
[
-
2
:]
==
'__'
):
continue
items
[
k
]
=
v
for
b
in
klass
.
__bases__
:
_ii
(
b
,
items
)
return
items
def
implementedBy
(
object
):
"""Return the interfaces implemented by the object
"""
r
=
[]
t
=
type
(
object
)
if
t
is
ClassType
:
if
hasattr
(
object
,
'__class_implements__'
):
implements
=
object
.
__class_implements__
else
:
implements
=
Class
elif
hasattr
(
object
,
'__implements__'
):
implements
=
object
.
__implements__
else
:
implements
=
tiget
(
t
,
None
)
if
implements
is
None
:
return
r
if
isinstance
(
implements
,
Interface
):
r
.
append
(
implements
)
else
:
_wi
(
implements
,
r
.
append
)
return
r
def
implementedByInstancesOf
(
klass
):
"""Return the interfaces that instanced implement (by default)
"""
r
=
[]
if
type
(
klass
)
is
ClassType
:
if
hasattr
(
klass
,
'__implements__'
):
implements
=
klass
.
__implements__
else
:
return
r
elif
hasattr
(
klass
,
'instancesImplements'
):
# Hook for ExtensionClass. :)
implements
=
klass
.
instancesImplements
()
else
:
implements
=
tiget
(
klass
,
None
)
if
implements
is
not
None
:
if
isinstance
(
implements
,
Interface
):
r
.
append
(
implements
)
else
:
_wi
(
implements
,
r
.
append
)
return
r
def
_wi
(
interfaces
,
append
):
for
i
in
interfaces
:
if
isinstance
(
i
,
Interface
):
append
(
i
)
else
:
_wi
(
i
,
append
)
lib/python/Zope/Interfaces/Interface/__init__.py
0 → 100644
View file @
48f0cfe9
from
Standard
import
Base
import
iclass
new
=
iclass
.
Interface
del
iclass
from
Util
import
impliedInterface
Registered_Interfaces
=
{}
def
Interface
(
klass
,
__name__
=
None
,
__doc__
=
None
):
"""
why are we doing this maddness? to hook the interface
registration. This must die!
"""
I
=
impliedInterface
(
klass
,
__name__
,
__doc__
)
Registered_Interfaces
[
I
.
__name__
]
=
I
return
I
from
Util
import
assertTypeImplements
,
implementedBy
,
implementedByInstancesOf
from
Attr
import
Attribute
from
Method
import
Method
from
Exceptions
import
BrokenImplementation
lib/python/Zope/Interfaces/Interface/iclass.py
0 → 100644
View file @
48f0cfe9
"""Python Interfaces
Classes or objects can implement an __implements__ attribute that
names an interface object or a collection of interface objects.
An interface is defined using the class statement and one or more base
interfaces.
"""
from
Method
import
Method
from
Attr
import
Attribute
from
types
import
FunctionType
,
ClassType
import
Exceptions
_typeImplements
=
{}
class
Interface
:
"""Prototype (scarecrow) Interfaces Implementation
"""
def
__init__
(
self
,
name
,
bases
=
(),
attrs
=
None
,
__doc__
=
None
):
"""Create a new interface
"""
for
b
in
bases
:
if
not
isinstance
(
b
,
Interface
):
raise
TypeError
,
'Expected base interfaces'
self
.
__bases__
=
bases
self
.
__name__
=
name
if
attrs
is
None
:
attrs
=
{}
if
attrs
.
has_key
(
'__doc__'
):
if
__doc__
is
None
:
__doc__
=
attrs
[
'__doc__'
]
del
attrs
[
'__doc__'
]
self
.
__attrs
=
attrs
if
__doc__
is
not
None
:
self
.
__doc__
=
__doc__
for
k
,
v
in
attrs
.
items
():
if
isinstance
(
v
,
Method
):
v
.
interface
=
name
v
.
__name__
=
k
elif
isinstance
(
v
,
FunctionType
):
attrs
[
k
]
=
Method
.
fromFunction
(
v
,
name
)
elif
not
isinstance
(
v
,
Attribute
):
raise
Exceptions
.
InvalidInterface
(
"Concrete attribute, %s"
%
k
)
def
defered
(
self
):
"""Return a defered class corresponding to the interface
"""
if
hasattr
(
self
,
"_defered"
):
return
self
.
_defered
klass
=
{}
exec
"class %s: pass"
%
self
.
__name__
in
klass
klass
=
klass
[
self
.
__name__
]
self
.
__d
(
klass
.
__dict__
)
self
.
_defered
=
klass
return
klass
def
__d
(
self
,
dict
):
for
k
,
v
in
self
.
__dict__
.
items
():
if
isinstance
(
v
,
Method
)
and
not
dict
.
has_key
(
k
):
dict
[
k
]
=
v
for
b
in
self
.
__bases__
:
b
.
__d
(
dict
)
def
extends
(
self
,
other
):
"""Does an interface extend another?
"""
for
b
in
self
.
__bases__
:
if
b
is
other
:
return
1
if
b
.
extends
(
other
):
return
1
return
0
def
implementedBy
(
self
,
object
,
tiget
=
_typeImplements
.
get
):
"""Does the given object implement the interface?
"""
t
=
type
(
object
)
if
t
is
ClassType
:
if
hasattr
(
object
,
'__class_implements__'
):
implements
=
object
.
__class_implements__
else
:
implements
=
Class
elif
hasattr
(
object
,
'__implements__'
):
implements
=
object
.
__implements__
else
:
implements
=
tiget
(
t
,
None
)
if
implements
is
None
:
return
0
if
isinstance
(
implements
,
Interface
):
return
implements
is
self
or
implements
.
extends
(
self
)
else
:
return
self
.
__any
(
implements
)
def
implementedByInstancesOf
(
self
,
klass
,
tiget
=
_typeImplements
.
get
):
"""Do instances of the given class implement the interface?
"""
if
type
(
klass
)
is
ClassType
:
if
hasattr
(
klass
,
'__implements__'
):
implements
=
klass
.
__implements__
else
:
return
0
elif
hasattr
(
klass
,
'instancesImplement'
):
# Hook for ExtensionClass. :)
implements
=
klass
.
instancesImplement
()
else
:
implements
=
tiget
(
klass
,
None
)
if
implements
is
None
:
return
0
if
isinstance
(
implements
,
Interface
):
return
implements
is
self
or
implements
.
extends
(
self
)
else
:
return
self
.
__any
(
implements
)
def
interfaceName
(
self
):
""" name? """
return
self
.
__name__
def
implements
(
self
):
""" Returns a list of sequence of base Interfaces """
return
self
.
__bases__
def
names
(
self
):
"""Return the attribute names defined by the interface
"""
return
self
.
__attrs
.
keys
()
def
namesAndDescriptions
(
self
):
"""Return the attribute names and descriptions defined by the interface
"""
return
self
.
__attrs
.
items
()
def
getDescriptionFor
(
self
,
name
,
default
=
None
):
"""Return the attribute description for the given name
"""
return
self
.
__attrs
.
get
(
name
,
default
)
def
__any
(
self
,
interfaces
):
for
i
in
interfaces
:
if
isinstance
(
i
,
Interface
):
if
i
is
self
or
i
.
extends
(
self
):
return
1
else
:
if
self
.
__any
(
i
):
return
1
return
0
def
documentation
(
self
):
""" returns the doc string for the inteface """
return
self
.
__doc__
def
__repr__
(
self
):
return
"<Interface %s at %x>"
%
(
self
.
__name__
,
id
(
self
))
Base
=
Interface
(
"Interface"
)
class
Named
(
Base
):
"Objects that have a name."
__name__
=
Attribute
(
"The name of the object"
)
class
Class
(
Named
):
"""Implement shared instance behavior and create instances
Classes can be called to create an instance. This interface does
not specify what if any arguments are required.
"""
# Note that we don't use a function definition here, because
# we don't want to specify a signature!
__call__
=
Method
(
"Instantiate instances of the class"
)
__bases__
=
Attribute
(
"A sequence of base classes"
)
def
assertTypeImplements
(
type
,
interfaces
):
"""Return the interfaces implemented by objects of the given type
"""
_typeImplements
[
type
]
=
interfaces
lib/python/Zope/Interfaces/InterfaceImplementer.py
0 → 100644
View file @
48f0cfe9
"""
InterfaceImplementer -- Mixin class for objects that support interfaces
"""
class
InterfaceImplementer
:
"""
Mix-in class to support interfaces.
Interface implementation is indicated
with the __implements__ class attribute
that should be a tuple of Interface objects.
"""
def
implementedInterfaces
(
self
):
"""
Returns a sequence of Interface objects
that the object implements.
"""
return
self
.
__implements__
lib/python/Zope/Interfaces/InterfaceInterface.py
0 → 100644
View file @
48f0cfe9
from
Interface
import
Interface
class
_Interface
:
""" This is the Interface for Interface objects """
def
defered
(
self
):
"""
Returns a defered class corresponding to the interface
"""
def
extends
(
self
,
other
):
"""
Does this Interface extend 'other'?
"""
def
implementedBy
(
self
,
object
):
"""
Does 'object' implement this Interface?
"""
def
implementedByInstancesOf
(
self
,
klass
):
"""
Do instances of the given class implement the interface?
"""
def
implements
(
self
):
"""
Returns a sequence of base interfaces
"""
def
names
(
self
):
"""
Returns the attribute names defined by the interface
"""
def
namesAndDescriptions
(
self
):
"""
Return the attribute names and descriptions defined by the interface
"""
def
getDescriptionFor
(
self
,
name
,
default
=
None
):
"""
Return the attribute description for the given name
"""
InterfaceInterface
=
Interface
(
_Interface
,
'InterfaceInterface'
)
lib/python/Zope/Interfaces/__init__.py
0 → 100644
View file @
48f0cfe9
## This sucks, but we'll do it for now.
## "No sir I don't like it, not one bit."
import
InterfaceInterface
Registered_Interfaces
=
Interface
.
Registered_Interfaces
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment