comparison paraspace/structpath.py @ 32:9bac21d401fe

Make get_parent() as a method of structpath.context
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 15 Jun 2011 02:03:38 +0800
parents aed662c820d8
children fe1ebf0c3d40
comparison
equal deleted inserted replaced
31:aed662c820d8 32:9bac21d401fe
13 self.class_instances = ctx.class_instances 13 self.class_instances = ctx.class_instances
14 self.root = ctx.root 14 self.root = ctx.root
15 pass 15 pass
16 self.objs = objs 16 self.objs = objs
17 pass 17 pass
18
19 def get_parent(self, obj):
20 raise NotImplementedError, 'get_parent() is not implemented'
18 pass 21 pass
19 22
20 23
21 def _path_split(path): 24 def _path_split(path):
22 parts = [p.strip() 25 parts = [p.strip()
44 47
45 def _is_parent_name(part): 48 def _is_parent_name(part):
46 return part == '..' 49 return part == '..'
47 50
48 51
49 def _get_parent(obj):
50 raise NotImplementedError, '_get_parent() is not implemented'
51
52
53 def _obj_attr(obj, attrname): 52 def _obj_attr(obj, attrname):
54 if isinstance(obj, list): 53 if isinstance(obj, list):
55 idx = int(attrname) 54 idx = int(attrname)
56 return obj[idx] 55 return obj[idx]
57 elif isinstance(obj, dict): 56 elif isinstance(obj, dict):
60 return getattr(obj, attrname) 59 return getattr(obj, attrname)
61 60
62 61
63 def _handle_path_part_obj(ctx, part, obj): 62 def _handle_path_part_obj(ctx, part, obj):
64 if _is_parent_name(part): 63 if _is_parent_name(part):
65 new_objs = [_get_parent(obj)] 64 new_objs = [ctx.get_parent(obj)]
66 elif _is_class(part): 65 elif _is_class(part):
67 class_name = _class_name(part) 66 class_name = _class_name(part)
68 new_objs = ctx.class_instances[class_name] 67 new_objs = ctx.class_instances[class_name]
69 else: 68 else:
70 new_objs = [_obj_attr(obj, part)] 69 new_objs = [_obj_attr(obj, part)]
74 73
75 def _handle_path_part(ctx, part): 74 def _handle_path_part(ctx, part):
76 from itertools import chain 75 from itertools import chain
77 76
78 if not ctx.objs: 77 if not ctx.objs:
79 ctx = context([ctx.root], ctx) 78 ctx = ctx.__class__([ctx.root], ctx)
80 pass 79 pass
81 80
82 objss = [_handle_path_part_obj(ctx, part, obj) 81 objss = [_handle_path_part_obj(ctx, part, obj)
83 for obj in ctx.objs] 82 for obj in ctx.objs]
84 objs = [o for o in chain(*objss)] 83 objs = [o for o in chain(*objss)]
85 new_ctx = context(objs, ctx) 84 new_ctx = ctx.__class__(objs, ctx)
86 return new_ctx 85 return new_ctx
87 86
88 87
89 def _handle_path_parts(ctx, path_parts): 88 def _handle_path_parts(ctx, path_parts):
90 if _is_abs(path_parts): 89 if _is_abs(path_parts):
91 ctx = context([ctx.root], ctx) 90 ctx = ctx.__class__([ctx.root], ctx)
92 path_parts = _rel_of_abs(path_parts) 91 path_parts = _rel_of_abs(path_parts)
93 pass 92 pass
94 93
95 if len(path_parts) == 1 and path_parts[0] == '': 94 if len(path_parts) == 1 and path_parts[0] == '':
96 return ctx 95 return ctx