annotate src/parpg/grease/component/field.py @ 27:09b581087d68

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