changeset 37:ee8aeb299f10

Support wild (*) character in structpath.py
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 15 Jun 2011 13:30:11 +0800
parents 0b9ac7cef6e5
children 0766bd54c9d3
files paraspace/structpath.py paraspace/tests/structpath_test.py
diffstat 2 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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