annotate src/parpg/bGrease/component/base.py @ 66:96af64cf3b81

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