*** empty log message ***
[pli.git] / pli / pattern / store / stored.py
blobf9f62666ab08c08d7c8dae5fca5dc2eca0efee12
1 #=======================================================================
3 __version__ = '''0.0.11'''
4 __sub_version__ = '''20040909163900'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 ##from store import *
13 #-----------------------------------------------------------------------
14 #--------------------------------------------------------_StoredClass---
15 class _StoredClass(type):
16 '''
17 '''
18 # this will define the name of the attribute to define the class
19 # store...
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
25 # store.
26 # NOTE: this is not inherited.
27 __ignore_registration__ = False
29 def __init__(cls, name, bases, ns):
30 '''
31 this will add the object type to the store.
32 '''
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):
42 '''
43 this will store the object.
44 '''
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__
50 # store the object...
51 getattr(cls, cls.__class_store_attr_name__)[name] = obj
54 #-------------------------------------------------------_StoredObject---
55 class _StoredObject(_StoredClass):
56 '''
57 '''
58 # this will define the name of the attribute to define the object
59 # store...
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.
64 __auto_name__ = False
66 def __call__(cls, *p, **n):
67 '''
68 '''
69 store_attr_name = cls.__object_store_attr_name__
70 # create an object...
71 obj = cls.__new__(cls, *p, **n)
72 # init the object...
73 obj.__init__(*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
79 # as posible!)
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()
93 else:
94 raise TypeError, 'incompatible type of cls.__auto_name__.'
95 else:
96 name = p[0]
97 ## # init the object...
98 ## obj.__init__(*p, **n)
99 # register object in store....
100 ##!!! REVIZE !!!##
101 cls.storeobject(name, obj)
102 return obj
103 ##!!! REVIZE !!!##
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):
135 pass
139 #=======================================================================
140 # vim:set ts=4 sw=4 nowrap :