1 #=======================================================================
3 __version__
= '''0.0.11'''
4 __sub_version__
= '''20040313225846'''
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 '__auto_register_type__' in ns and ns['__auto_register_type__']:
36 if ('__ignore_registration__' not in ns
or not ns
['__ignore_registration__']) and \
37 hasattr(cls
, '__auto_register_type__') and cls
.__auto
_register
_type
__:
38 if not hasattr(cls
, store_attr_name
) or getattr(cls
, store_attr_name
) == None:
39 # might be good to do this in __init__ ... (?)
40 raise TypeError, 'a store object must be defined for type "%s" for auto registration.' % cls
.__name
__
41 cls
.storeclass(name
, cls
)
42 def storeclass(cls
, name
, obj
):
44 this will store the object.
46 ## # set name information...
47 ## obj.__name__ = name
48 ## # set type information...
49 ## if not hasattr(obj, '__type_name__'):
50 ## obj.__type_name__ = obj.__class__.__name__
52 getattr(cls
, cls
.__class
_store
_attr
_name
__)[name
] = obj
55 #-------------------------------------------------------_StoredObject---
56 class _StoredObject(_StoredClass
):
59 # this will define the name of the attribute to define the object
61 __object_store_attr_name__
= '__store__'
62 # if this is set the type will auto register on class creation.
63 __auto_register_type__
= False
64 # this if set must define a name generator function.
67 def __call__(cls
, *p
, **n
):
70 store_attr_name
= cls
.__object
_store
_attr
_name
__
72 obj
= cls
.__new
__(cls
, *p
, **n
)
75 # check if a store is defined...
76 if not hasattr(cls
, store_attr_name
) or getattr(cls
,store_attr_name
) == None:
77 raise TypeError, 'a store object must be defined for type "%s".' % cls
.__name
__
78 store
= getattr(cls
, cls
.__object
_store
_attr
_name
__)
79 # check if object is in store... (this should be done as soon
81 if hasattr(store
, '__strictnames__') and store
.__strictnames
__ and p
[0] in store
:
82 raise TypeError, 'an object with the id "%s" already exists.' % p
[0]
83 ## # create an object...
84 ## obj = cls.__new__(cls, *p, **n)
85 # create/get an object name...
86 if hasattr(obj
, '__auto_name__') and obj
.__auto
_name
__ != False:
87 auto_name
= obj
.__auto
_name
__
88 if callable(auto_name
):
89 # NOTE: the __auto_name__ method must match the
90 # signiture of __init__
91 name
= auto_name(*p
, **n
)
92 elif type(auto_name
) is str:
93 name
= auto_name
% time
.time()
95 raise TypeError, 'incompatible type of cls.__auto_name__.'
98 ## # init the object...
99 ## obj.__init__(*p, **n)
100 # register object in store....
102 cls
.storeobject(name
, obj
)
105 def storeobject(cls
, name
, obj
):
107 this will store the object.
109 ## # set name information...
110 ## obj.__name__ = name
111 ## # set type information...
112 ## if not hasattr(obj, '__type_name__'):
113 ## obj.__type_name__ = obj.__class__.__name__
114 # store the object...
115 getattr(cls
, cls
.__object
_store
_attr
_name
__)[name
] = obj
118 #--------------------------------------------------------StoredObject---
119 # TODO add onStoredObjectLoad, onStoredObjectSave,
120 # onStoredObjectReload, .... events...
122 class StoredObject(object):
125 __metaclass__
= _StoredObject
126 def __init__(self
, name
):
131 #=======================================================================
132 # vim:set ts=4 sw=4 nowrap :