Cleanup, all selects, and deletes working

This commit is contained in:
Doug Blank 2013-12-26 00:18:39 -05:00
parent 6528044b1c
commit 7b9fa14ef7

View File

@ -436,52 +436,35 @@ class Struct(object):
# for where eval: # for where eval:
if attr in self.struct: if attr in self.struct:
attr = self.getitem(attr) attr = self.getitem(attr)
if isinstance(attr, (list, dict)): # more struct... if isinstance(attr, (list, tuple, dict)): # more struct...
return Struct(attr, self.db) return Struct(attr, self.db)
elif isinstance(attr, HandleClass): # more struct...
return self.get_ref_struct(attr)
else: # just a value: else: # just a value:
return attr return attr
else: else:
raise AttributeError("no such attribute '%s'" % attr) raise AttributeError("no such attribute '%s'" % attr)
def __getitem__(self, path): def __getitem__(self, item):
""" """
Given a path to a struct part, return the part, or None. Given a path to a struct part, return the part, or None.
>>> Struct(struct)["primary_name"] >>> Struct(struct)["primary_name"]
""" """
# For where eval: # For where eval:
return self.getitem(path) return self.getitem(item)
# if isinstance(path, int):
# return self.get_ref_struct(self.struct[path])
# # Work way down to last part:
# return self.getitem_from_path(parse(path))
# def getitem_from_path(self, path):
# """
# Given a path that is already parsed, return item.
# """
# struct = self.struct
# for p in range(len(path)):
# part = path[p]
# struct = self.getitem(part, struct)
# if isinstance(struct, Struct):
# return struct.getitem_from_path(path[p+1:])
# if struct is None:
# return None
# return struct
def get_ref_struct(self, item): def get_ref_struct(self, item):
""" """
If the item is a handle, look up reference object. If the item is a handle, look up reference object.
""" """
if hasattr(item, "classname") and self.db: # HandleClass if isinstance(item, HandleClass) and self.db:
obj = self.db.get_from_name_and_handle(item.classname, str(item)) obj = self.db.get_from_name_and_handle(item.classname, str(item))
if obj: if obj:
return Struct(obj.to_struct(), self.db) return Struct(obj.to_struct(), self.db)
else: else:
return None return None
elif isinstance(item, dict): elif isinstance(item, (dict, tuple, list)):
return Struct(item, self.db) return Struct(item, self.db)
else: else:
return item return item
@ -494,7 +477,7 @@ class Struct(object):
if struct is None: if struct is None:
struct = self.struct struct = self.struct
# Get part # Get part
if isinstance(struct, (list, tuple)): if isinstance(struct, (list, tuple, tuple)):
pos = int(item) pos = int(item)
if pos < len(struct): if pos < len(struct):
return self.get_ref_struct(struct[int(item)]) return self.get_ref_struct(struct[int(item)])
@ -509,10 +492,7 @@ class Struct(object):
else: else:
return None return None
elif hasattr(struct, item): elif hasattr(struct, item):
return getattr(struct, item) return Struct(getattr(struct, item), self.db)
elif item.startswith("("):
args = eval(item[1:-1] + ",") # tuple of args
return struct(*args)
else: else:
return None return None