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