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