# HG changeset patch # User Thinker K.F. Li # Date 1308115811 -28800 # Node ID ee8aeb299f106fd4b1f1afbcc4bade9869338a0c # Parent 0b9ac7cef6e5262f2e2e91120b156e9564236e8c Support wild (*) character in structpath.py diff -r 0b9ac7cef6e5 -r ee8aeb299f10 paraspace/structpath.py --- a/paraspace/structpath.py Wed Jun 15 12:29:46 2011 +0800 +++ b/paraspace/structpath.py Wed Jun 15 13:30:11 2011 +0800 @@ -122,6 +122,18 @@ return getattr(obj, attrname) +def _obj_attr_objs(obj, attrname): + if attrname == '*': + if isinstance(obj, list): + return obj + elif isinstance(obj, dict): + return list(obj.values()) + else: + raise ValueError, '\'*\' is invliad here' + pass + return [_obj_attr(obj, attrname)] + + def _handle_path_part_obj(ctx, part, obj): attr, pred = _split_attr_pred(part) @@ -132,7 +144,7 @@ new_objs = ctx.class_instances[class_name] else: try: - new_objs = [_obj_attr(obj, attr)] + new_objs = _obj_attr_objs(obj, attr) except AttributeError: return [] pass diff -r 0b9ac7cef6e5 -r ee8aeb299f10 paraspace/tests/structpath_test.py --- a/paraspace/tests/structpath_test.py Wed Jun 15 12:29:46 2011 +0800 +++ b/paraspace/tests/structpath_test.py Wed Jun 15 13:30:11 2011 +0800 @@ -116,6 +116,9 @@ car_ctx = _build_car() root = car_ctx.root + wheels = structpath.find_objs_path(car_ctx, '.wheel') + assert len(wheels) == 4 + wheels = structpath.find_objs_path(car_ctx, '.wheel[num == 2]') assert len(wheels) == 1 a_wheel = wheels[0] @@ -128,3 +131,19 @@ assert a_wheel.num == 1 assert a_wheel is root.wheels[0] pass + + +def structpath_wild_test(): + car_ctx = _build_car() + root = car_ctx.root + + wheels = structpath.find_objs_path(car_ctx, '/wheels/*') + assert len(wheels) == 4 + + try: + handles = structpath.find_objs_path(car_ctx, '/handle/*') + except ValueError: + pass + else: + raise ValueError, '* smybol should be invalid, but it is valid' + pass