Initial revision
[pli.git] / pli / pattern / store / stored.py
blobfb648e62c847c5c16057cc3151c5228d00862815
1 #=======================================================================
3 __version__ = '''0.0.11'''
4 __sub_version__ = '''20040313225846'''
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 '__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):
43 '''
44 this will store the object.
45 '''
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__
51 # store the object...
52 getattr(cls, cls.__class_store_attr_name__)[name] = obj
55 #-------------------------------------------------------_StoredObject---
56 class _StoredObject(_StoredClass):
57 '''
58 '''
59 # this will define the name of the attribute to define the object
60 # store...
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.
65 __auto_name__ = False
67 def __call__(cls, *p, **n):
68 '''
69 '''
70 store_attr_name = cls.__object_store_attr_name__
71 # create an object...
72 obj = cls.__new__(cls, *p, **n)
73 # init the object...
74 obj.__init__(*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
80 # as posible!)
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()
94 else:
95 raise TypeError, 'incompatible type of cls.__auto_name__.'
96 else:
97 name = p[0]
98 ## # init the object...
99 ## obj.__init__(*p, **n)
100 # register object in store....
101 ##!!! REVIZE !!!##
102 cls.storeobject(name, obj)
103 return obj
104 ##!!! REVIZE !!!##
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):
127 pass
131 #=======================================================================
132 # vim:set ts=4 sw=4 nowrap :