annotate bGrease/component/field.py @ 65:e856b604b650

Changed "import bGrease" to "import parpg.bGrease".
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 16:10:14 +0200
parents ff3e395abf91
children
rev   line source
65
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
1 #############################################################################
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
2 #
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
4 # All Rights Reserved.
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
5 #
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
6 # This software is subject to the provisions of the MIT License
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
7 # A copy of the license should accompany this distribution.
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
11 #
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
12 #############################################################################
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
13
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
14 __version__ = '$Id$'
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
15
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
16 import operator
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
17 from parpg.bGrease.geometry import Vec2d, Vec2dArray, Rect
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
18 from parpg.bGrease import color
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
19
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
20 # Allowed field types -> default values
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
21 types = {int:lambda: 0,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
22 float:lambda: 0.0,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
23 bool:lambda: False,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
24 str:lambda:"",
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
25 object:lambda:None,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
26 Vec2d:lambda: Vec2d(0,0),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
27 Vec2dArray:lambda: Vec2dArray(),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
28 color.RGBA: lambda: color.RGBA(0.0, 0.0, 0.0, 0.0),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
29 Rect: lambda: Rect(0.0, 0.0, 0.0, 0.0),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
30 list: lambda: list(),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
31 dict: lambda: dict(),
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
32 }
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
33
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
34 class Schema(dict):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
35 """Field schema definition for custom components"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
36
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
37 def __init__(self, **fields):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
38 for ftype in fields.values():
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
39 assert ftype in types, fname + " has an illegal field type"
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
40 self.update(fields)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
41
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
42
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
43 class FieldAccessor(object):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
44 """Facade for manipulating a field for a set of entities"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
45
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
46 __field = None
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
47 __entities = None
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
48 __attrs = None
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
49 __getter = None
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
50 __parent_getters = ()
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
51
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
52 def __init__(self, field, entities, attrs=()):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
53 self.__field = field
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
54 self.__entities = entities
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
55 field_getter = operator.attrgetter(field.name)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
56 self.__attrs = attrs
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
57 if attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
58 getters = [field_getter] + [operator.attrgetter(attr) for attr in attrs]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
59 def get(entity):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
60 value = entity
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
61 for getter in getters:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
62 value = getter(value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
63 return value
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
64 self.__getter = get
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
65 self.__parent_getters = getters[:-1]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
66 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
67 self.__getter = field_getter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
68
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
69 def __getattr__(self, name):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
70 """Return a FieldAccessor for the child attribute"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
71 return self.__class__(self.__field, self.__entities, self.__attrs + (name,))
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
72
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
73 def __setattr__(self, name, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
74 if value is self:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
75 return # returned by mutators
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
76 if hasattr(self.__class__, name):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
77 # Set local attr
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
78 self.__dict__[name] = value
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
79 elif not name.startswith('_'):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
80 getattr(self, name).__set__(value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
81 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
82 raise AttributeError("Cannot set field attribute: %s" % name)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
83
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
84 @property
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
85 def __setter(self):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
86 """Return the proper setter function for setting the field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
87 if not self.__attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
88 return setattr
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
89 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
90 parent_getters = self.__parent_getters
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
91 def setter(data, name, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
92 for getter in parent_getters:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
93 data = getter(data)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
94 setattr(data, name, value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
95 self.__setter = setter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
96 return setter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
97
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
98 def __set__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
99 """Set field values en masse"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
100 # Mass set field attr
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
101 setter = self.__setter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
102 component = self.__field.component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
103 if self.__attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
104 name = self.__attrs[-1]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
105 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
106 name = self.__field.name
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
107 if isinstance(value, FieldAccessor):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
108 # Join set between two entity sets
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
109 if not self.__attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
110 cast = self.__field.cast
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
111 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
112 cast = lambda x: x
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
113 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
114 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
115 setter(component[entity], name, cast(value[entity]))
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
116 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
117 pass
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
118 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
119 if not self.__attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
120 value = self.__field.cast(value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
121 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
122 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
123 setter(component[entity], name, value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
124 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
125 pass
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
126
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
127 def __getitem__(self, entity):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
128 """Return the field value for a single entity (used for joins)"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
129 if entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
130 return self.__getter(self.__field.component[entity])
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
131 raise KeyError(entity)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
132
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
133 def __contains__(self, entity):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
134 return entity in self.__entities
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
135
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
136 def __repr__(self):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
137 return '<%s %s @ %x>' % (
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
138 self.__class__.__name__,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
139 '.'.join((self.__field.name,) + self.__attrs), id(self))
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
140
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
141 def __nonzero__(self):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
142 return bool(self.__entities)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
143
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
144 def __iter__(self):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
145 """Return an iterator of all field values in the set"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
146 component = self.__field.component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
147 getter = self.__getter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
148 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
149 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
150 data = component[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
151 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
152 continue
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
153 yield getter(data)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
154
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
155 ## batch comparison operators ##
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
156
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
157 def __match(self, value, op):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
158 component = self.__field.component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
159 getter = self.__getter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
160 matches = set()
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
161 add = matches.add
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
162 if isinstance(value, FieldAccessor):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
163 # Join match between entity sets
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
164 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
165 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
166 data = component[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
167 other = value[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
168 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
169 continue
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
170 if op(getter(data), other):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
171 add(entity)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
172 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
173 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
174 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
175 data = component[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
176 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
177 continue
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
178 if op(getter(data), value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
179 add(entity)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
180 return matches
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
181
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
182 def __eq__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
183 """Return an entity set of all entities with a matching field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
184 return self.__match(value, operator.eq)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
185
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
186 def __ne__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
187 """Return an entity set of all entities not matching field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
188 return self.__match(value, operator.ne)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
189
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
190 def __gt__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
191 """Return an entity set of all entities with a greater field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
192 return self.__match(value, operator.gt)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
193
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
194 def __ge__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
195 """Return an entity set of all entities with a greater or equal field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
196 return self.__match(value, operator.ge)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
197
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
198 def __lt__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
199 """Return an entity set of all entities with a lesser field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
200 return self.__match(value, operator.lt)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
201
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
202 def __le__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
203 """Return an entity set of all entities with a lesser or equal field value"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
204 return self.__match(value, operator.le)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
205
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
206 def _contains(self, values):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
207 """Return an entity set of all entities with a field value contained in values"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
208 return self.__match(values, operator.contains)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
209
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
210 ## Batch in-place mutator methods
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
211
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
212 def __mutate(self, value, op):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
213 component = self.__field.component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
214 if self.__attrs:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
215 name = self.__attrs[-1]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
216 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
217 name = self.__field.name
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
218 getter = self.__getter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
219 setter = self.__setter
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
220 if isinstance(value, FieldAccessor):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
221 # Join between entity sets
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
222 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
223 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
224 data = component[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
225 other = value[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
226 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
227 continue
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
228 setter(data, name, op(getter(data), other))
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
229 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
230 for entity in self.__entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
231 try:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
232 data = component[entity]
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
233 except KeyError:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
234 continue
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
235 setter(data, name, op(getter(data), value))
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
236 return self
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
237
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
238 def __iadd__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
239 return self.__mutate(value, operator.iadd)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
240
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
241 def __isub__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
242 return self.__mutate(value, operator.isub)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
243
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
244 def __imul__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
245 return self.__mutate(value, operator.imul)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
246
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
247 def __idiv__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
248 return self.__mutate(value, operator.idiv)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
249
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
250 def __itruediv__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
251 return self.__mutate(value, operator.itruediv)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
252
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
253 def __ifloordiv__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
254 return self.__mutate(value, operator.ifloordiv)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
255
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
256 def __imod__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
257 return self.__mutate(value, operator.imod)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
258
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
259 def __ipow__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
260 return self.__mutate(value, operator.ipow)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
261
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
262 def __ilshift__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
263 return self.__mutate(value, operator.ilshift)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
264
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
265 def __irshift__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
266 return self.__mutate(value, operator.irshift)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
267
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
268 def __iand__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
269 return self.__mutate(value, operator.iand)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
270
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
271 def __ior__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
272 return self.__mutate(value, operator.ior)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
273
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
274 def __ixor__(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
275 return self.__mutate(value, operator.ixor)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
276
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
277
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
278 class Field(object):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
279 """Component field metadata and accessor interface"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
280
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
281 def __init__(self, component, name, type, accessor_factory=FieldAccessor):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
282 self.component = component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
283 self.name = name
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
284 self.type = type
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
285 self.default = types.get(type)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
286 self.accessor_factory = accessor_factory
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
287
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
288 def cast(self, value):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
289 """Cast value to the appropriate type for thi field"""
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
290 if self.type is not object:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
291 return self.type(value)
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
292 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
293 return value
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
294
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
295 def accessor(self, entities=None):
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
296 """Return the field accessor for the entities in the component,
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
297 or all entities in the set specified that are also in the component
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
298 """
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
299 if entities is None or entities is self.component.entities:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
300 entities = self.component.entities
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
301 else:
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
302 entities = entities & self.component.entities
e856b604b650 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 41
diff changeset
303 return self.accessor_factory(self, entities)