Mercurial > parpg-source
annotate bGrease/component/base.py @ 166:a6bbb732b27b
Added .hgeol file to automatically convert line endings.
author | KarstenBock@gmx.net |
---|---|
date | Thu, 12 Jan 2012 18:42:48 +0100 |
parents | ff3e395abf91 |
children |
rev | line source |
---|---|
166
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
1 class ComponentBase(object): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
2 """Component abstract base class |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
3 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
4 Strictly speaking you do not need to derive from this class to create your |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
5 own components, but it does serve to document the full interface that a |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
6 component implements and it provides some basic implementations for |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
7 certain methods |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
8 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
9 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
10 ## Optional attributes and methods ## |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
11 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
12 def set_manager(self, manager): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
13 """Set the manager of this component. If this method exists it will be |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
14 automatically called when the component is added to a manager. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
15 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
16 This method stores the manager and allows the component to be added |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
17 only once to a single manager. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
18 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
19 assert getattr(self, 'manager', None) is None, 'Component cannot be added to multiple managers' |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
20 self.manager = manager |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
21 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
22 def __del__(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
23 """Break circrefs to allow faster collection""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
24 if hasattr(self, 'manager'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
25 del self.manager |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
26 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
27 ## Mandatory methods ## |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
28 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
29 def add(self, entity_id, data=None, **data_kw): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
30 """Add a data entry in the component for the given entity. Additional |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
31 data (if any) for the entry can be provided in the data argument or as |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
32 keyword arguments. Additional data is optional and if omitted then |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
33 suitable defaults will be used. Return an entity data object |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
34 for the new entity entry. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
35 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
36 The semantics of the data arguments is up to the component. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
37 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
38 An entity_id is a unique key, thus multiple separate data entries for |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
39 a given entity are not allowed. Components can indivdually decide |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
40 what to do if an entity_id is added multiple times to the component. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
41 Potential options include, raising an exception, replacing the |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
42 existing data or coalescing it somehow. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
43 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
44 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
45 def remove(self, entity_id): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
46 """Remove the entity data entry from the component. If the |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
47 entity is not in the component, raise KeyError |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
48 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
49 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
50 def __delitem_(self, entity_id): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
51 """Same as remove()""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
52 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
53 def __len__(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
54 """Return the number of entities in the component""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
55 raise NotImplementedError() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
56 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
57 def __iter__(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
58 """Return an iterator of entity data objects in this component |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
59 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
60 No order is defined for these data objects |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
61 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
62 raise NotImplementedError() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
63 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
64 def __contains__(self, entity_id): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
65 """Return True if the entity is contained in the component""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
66 raise NotImplementedError() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
67 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
68 def __getitem__(self, entity_id): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
69 """Return the entity data object for the given entity. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
70 The entity data object returned may be mutable, immutable or a |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
71 mutable copy of the data at the discretion of the component |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
72 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
73 raise NotImplementedError() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
74 |