23 from collections
import UserDict
24 from collections
import UserList
33 def cmp(x, y): (x > y) - (x < y)
37 """handles all Atlas objects""" 40 """usage: Object(id="human", objtype="class", parents=["living"]) 41 acts like normal python class and dictionary at the same time 42 in addition looks for atributes from parent objects 45 kw[
"from"] = kw[
"from_"]
47 UserDict.__init__(self, kw)
49 def __setattr__(self, name, value):
58 """look first for attribute at this object 59 if not available, then go trough all parent object looking 67 if len(name) > 1
and name[-1] ==
"_" and name[-2] !=
"_":
68 return getattr(self, name[:-1])
69 if name ==
"data":
return self.
__dict__ 74 if name
in self.__class__.__dict__:
75 return self.__class__.__dict__[name]
81 elif "parent" in self.__class__.__dict__:
83 parent = self.__class__.__dict__[
"parent"]
90 if (isinstance(parent, Object)
or class_inherited_from_Object(parent)) \
91 and hasattr(parent, name):
92 return getattr(parent, name)
94 raise AttributeError(name)
97 """is attribute plain?""" 98 value = getattr(self, name)
99 return is_plain(name, value)
102 """convert all references to parents, etc.. objects to string ids""" 103 value = getattr(self, name)
104 return convert2plain(name, value)
106 def get_attr_pos(self, a):
108 orig_order = self.specification_file.attribute_order
109 except AttributeError:
112 pos = orig_order.index(a[0])
114 pos = len(orig_order)
115 if a[0] ==
"specification_file":
119 def key_func(self, value):
122 def items(self, convert2plain_flag=1, original_order=1,
124 """like dictionary items method: 125 original_order: tries to preserver specification order if possible 126 all: list also inherited attributes (if possible) 130 convert2plain_flag=convert2plain_flag).
items())
134 attrs = sorted(attrs, key=self.
key_func)
138 """list all attributes defined in this object: 139 returns dictionary: use items() for list""" 140 if convert2plain_flag:
145 res_dict[name] = convert2plain(name, value)
151 """list all attributes including inherited ones: 152 returns dictionary: use get_all_attributes().items() for list""" 153 if result_dict ==
None:
155 parent = self.
__dict__.get(
"parent")
156 if isinstance(parent, Object):
157 parent.get_all_attributes(result_dict)
162 """give object that defines given attribute""" 165 parent = self.
__dict__.get(
"parent")
166 if isinstance(parent, Object)
and hasattr(parent, name):
167 return parent.attribute_definition(name)
168 raise AttributeError(name)
170 def has_parent(self, parent):
171 if not isinstance(parent, str): parent = parent.id
172 if self.
id == parent:
return 1
173 parent_obj = self.
__dict__.get(
"parent")
174 if isinstance(parent_obj, Object)
and \
175 parent_obj.has_parent(parent):
179 def get_objtype(self):
182 except AttributeError:
187 add = string_list.append
189 add(
'%s = %s' % (name, repr(value)))
190 return "Object(%s)" %
", ".join(string_list)
193 return gen_bach(self)
196 def Operation(parent, arg=Object(), **kw):
197 kw[
"parent"] = parent
204 """list of operations""" 206 def __init__(self, *args):
208 if len(args) == 1
and (isinstance(args[0], UserList)
or isinstance(args[0], list)):
209 UserList.__init__(self, args[0])
211 UserList.__init__(self, list(args))
213 def get_objtype(self):
217 return gen_bach(self)
222 def class_inherited_from_Object(cl):
223 if not isinstance(cl, type):
225 if cl == Object:
return 1
226 for base
in cl.__bases__:
227 if class_inherited_from_Object(base):
return 1
231 uri_type = {
"from": 1,
"to": 1}
232 uri_list_type = {
"parent": 1,
"children": 1}
235 def attribute_is_type(name, type):
236 """is attribute of certain type somewhere in type hierarchy?""" 237 if type ==
"uri" and name
in uri_type:
239 if type ==
"uri_list" and name
in uri_list_type:
243 def is_plain(name, value):
244 if isinstance(value, dict)
and attribute_is_type(name,
"uri"):
246 if isinstance(value, list)
and attribute_is_type(name,
"uri_list"):
249 if isinstance(value2, dict):
254 def convert2plain(name, value):
255 """convert all references to parents, etc.. objects to string ids""" 256 if not is_plain(name, value):
257 if isinstance(value, dict):
261 for i
in range(len(value)):
263 value[i] = value[i].id
267 def find_ids_in_list(id_list, objects):
268 """finds all ids in list from objects dictionary keyed by ids""" 269 for i
in range(len(id_list)):
270 if isinstance(id_list[i], str):
271 id_list[i] = objects[id_list[i]]
274 def find_parents_children_objects(objects):
275 """replace parent and children id strings with actual objects""" 276 for obj
in list(objects.values()):
277 if hasattr(obj,
"parent")
and isinstance(obj.parent, str)
and obj.parent !=
"":
278 obj.parent = objects[obj.parent]
281 find_ids_in_list(obj.children, objects)
284 def has_parent(obj, parent, objects={}):
285 """has parent somewhere in hierarchy: 286 obj can be either string or object 287 (when it's string you need to provide 288 dictionary where it can be found) 289 parent can be either string or object 291 if isinstance(obj, str): obj = objects[obj]
292 return obj.has_parent(parent)
296 def make_object_from_dict(dict):
297 return Object(*(), **dict)
300 def resolve_pointer2(base_dict, id):
301 id_lst = id.split(
".")
304 if isinstance(obj, list):
309 if not id_lst:
return obj, id
311 raise KeyError(
"empty id")
314 def resolve_pointer(base_dict, id):
315 obj, id = resolve_pointer2(base_dict, id)
320 return id.split(
".")[0]
323 def get_last_part(id):
324 return id.split(
".")[-1]
327 def print_parents(obj):
328 print(obj.id, end=
' ')
330 if hasattr(o2,
"parent"):
332 if hasattr(o2,
"id"):
333 print(
"->", o2.id, end=
' ')
335 print(
"->", o2, end=
' ')
def attribute_definition(self, name)
def key_func(self, value)
def get_attributes(self, convert2plain_flag=1)
def items(self, convert2plain_flag=1, original_order=1, all=0)
def get_plain_attribute(self, name)
def __getattr__(self, name)
def get_all_attributes(self, result_dict=None, convert2plain_flag=1)
def get_attr_pos(self, a)
def is_plain_attribute(self, name)