diff options
author | Rafael H. Schloming <rhs@apache.org> | 2007-07-25 03:23:07 +0000 |
---|---|---|
committer | Rafael H. Schloming <rhs@apache.org> | 2007-07-25 03:23:07 +0000 |
commit | da2f970efa6cc29aa30dee4c80e4c48543907742 (patch) | |
tree | e6b0cfdc71c096371e9dc12ee28c454c15a0b4ba /python/mllib/dom.py | |
parent | 7e4d3a26342e99f79bf6416d6167df3508a77887 (diff) | |
download | qpid-python-da2f970efa6cc29aa30dee4c80e4c48543907742.tar.gz |
made query[...] support both navigation and filtering
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@559299 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/mllib/dom.py')
-rw-r--r-- | python/mllib/dom.py | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/python/mllib/dom.py b/python/mllib/dom.py index 80f1f2a409..5a5d017ff9 100644 --- a/python/mllib/dom.py +++ b/python/mllib/dom.py @@ -190,16 +190,7 @@ class Filter(View): def __init__(self, predicate, source): View.__init__(self, source) - if callable(predicate): - self.predicate = predicate - elif predicate[0] == "#": - type = predicate[1:] - self.predicate = lambda x: x.is_type(type) - elif predicate[0] == "@": - name = predicate[1:] - self.predicate = lambda x: x[0] == name - else: - self.predicate = lambda x: isinstance(x, Tag) and x.name == predicate + self.predicate = predicate def __iter__(self): for nd in self.source: @@ -239,23 +230,45 @@ class Values(View): for name, value in self.source: yield value +def flatten_path(path): + if isinstance(path, basestring): + for part in path.split("/"): + yield part + elif callable(path): + yield path + else: + for p in path: + for fp in flatten_path(p): + yield fp + class Query(View): def __iter__(self): for nd in self.source: yield nd - def __getitem__(self, predicate): - if isinstance(predicate, basestring): - path = predicate.split("/") - else: - path = [predicate] - + def __getitem__(self, path): query = self.source - for p in path: - if isinstance(p, basestring) and p[0] == "@": - query = Values(Filter(p, Attributes(query))) + for p in flatten_path(path): + if callable(p): + select = Query + pred = p + source = query + elif isinstance(p, basestring): + if p[0] == "@": + select = Values + pred = lambda x, n=p[1:]: x[0] == n + source = Attributes(query) + elif p[0] == "#": + select = Query + pred = lambda x, t=p[1:]: x.is_type(t) + source = Children(query) + else: + select = Query + pred = lambda x, n=p: isinstance(x, Tag) and x.name == n + source = Flatten(Children(query)) else: - query = Query(Filter(p, Flatten(Children(query)))) + raise ValueError(p) + query = select(Filter(pred, source)) return query |