1 #=======================================================================
3 __version__
= '''0.0.11'''
4 __sub_version__
= '''20040909163900'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
13 #-----------------------------------------------------------------------
14 #--------------------------------------------------------_StoredClass---
15 class _StoredClass(type):
18 # this will define the name of the attribute to define the class
20 __class_store_attr_name__
= '__store__'
21 # if this is set the type will auto register on class creation.
22 # NOTE: this is inherited.
23 __auto_register_type__
= True
24 # if this is True the *current class* will not be registred in
26 # NOTE: this is not inherited.
27 __ignore_registration__
= False
29 def __init__(cls
, name
, bases
, ns
):
31 this will add the object type to the store.
33 super(_StoredClass
, cls
).__init
__(name
, bases
, ns
)
34 store_attr_name
= cls
.__class
_store
_attr
_name
__
35 if ('__ignore_registration__' not in ns
or not ns
['__ignore_registration__']) and \
36 hasattr(cls
, '__auto_register_type__') and cls
.__auto
_register
_type
__:
37 if not hasattr(cls
, store_attr_name
) or getattr(cls
, store_attr_name
) == None:
38 # might be good to do this in __init__ ... (?)
39 raise TypeError, 'a store object must be defined for type "%s" for auto registration.' % cls
.__name
__
40 cls
.storeclass(name
, cls
)
41 def storeclass(cls
, name
, obj
):
43 this will store the object.
45 ## # set name information...
46 ## obj.__name__ = name
47 ## # set type information...
48 ## if not hasattr(obj, '__type_name__'):
49 ## obj.__type_name__ = obj.__class__.__name__
51 getattr(cls
, cls
.__class
_store
_attr
_name
__)[name
] = obj
54 #-------------------------------------------------------_StoredObject---
55 class _StoredObject(_StoredClass
):
58 # this will define the name of the attribute to define the object
60 __object_store_attr_name__
= '__store__'
61 # if this is set the type will auto register on class creation.
62 __auto_register_type__
= False
63 # this if set must define a name generator function.
66 def __call__(cls
, *p
, **n
):
69 store_attr_name
= cls
.__object
_store
_attr
_name
__
71 obj
= cls
.__new
__(cls
, *p
, **n
)
74 # check if a store is defined...
75 if not hasattr(cls
, store_attr_name
) or getattr(cls
,store_attr_name
) == None:
76 raise TypeError, 'a store object must be defined for type "%s".' % cls
.__name
__
77 store
= getattr(cls
, cls
.__object
_store
_attr
_name
__)
78 # check if object is in store... (this should be done as soon
80 if hasattr(store
, '__strictnames__') and store
.__strictnames
__ and p
[0] in store
:
81 raise TypeError, 'an object with the id "%s" already exists.' % p
[0]
82 ## # create an object...
83 ## obj = cls.__new__(cls, *p, **n)
84 # create/get an object name...
85 if hasattr(obj
, '__auto_name__') and obj
.__auto
_name
__ != False:
86 auto_name
= obj
.__auto
_name
__
87 if callable(auto_name
):
88 # NOTE: the __auto_name__ method must match the
89 # signiture of __init__
90 name
= auto_name(*p
, **n
)
91 elif type(auto_name
) is str:
92 name
= auto_name
% time
.time()
94 raise TypeError, 'incompatible type of cls.__auto_name__.'
97 ## # init the object...
98 ## obj.__init__(*p, **n)
99 # register object in store....
101 cls
.storeobject(name
, obj
)
104 def storeobject(cls
, name
, obj
):
106 this will store the object.
108 ## # set name information...
109 ## obj.__name__ = name
110 ## # set type information...
111 ## if not hasattr(obj, '__type_name__'):
112 ## obj.__type_name__ = obj.__class__.__name__
113 # store the object...
114 getattr(cls
, cls
.__object
_store
_attr
_name
__)[name
] = obj
117 #---------------------------------------------------------StoredClass---
118 class StoredClass(object):
121 __metaclass__
= _StoredClass
123 __ignore_registration__
= True
126 #--------------------------------------------------------StoredObject---
127 # TODO add onStoredObjectLoad, onStoredObjectSave,
128 # onStoredObjectReload, .... events...
130 class StoredObject(object):
133 __metaclass__
= _StoredObject
134 def __init__(self
, name
):
139 #=======================================================================
140 # vim:set ts=4 sw=4 nowrap :