annotate grease/component/field.py @ 5:bc88f7d5ca8b

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