diff options
author | Roland Hedberg <roland.hedberg@adm.umu.se> | 2016-04-16 09:01:21 +0200 |
---|---|---|
committer | Roland Hedberg <roland.hedberg@adm.umu.se> | 2016-04-16 09:01:21 +0200 |
commit | 9e25cc73728e67cdafe07850566796ba0ce81473 (patch) | |
tree | 4298842a6be0789c902454740b9d4288791c0372 /src/saml2/argtree.py | |
parent | 9dd3ee910aed4fb6b322b9ab022b044c7e753ab4 (diff) | |
download | pysaml2-9e25cc73728e67cdafe07850566796ba0ce81473.tar.gz |
Added functionality needed by the saml2test tool.
Diffstat (limited to 'src/saml2/argtree.py')
-rw-r--r-- | src/saml2/argtree.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/saml2/argtree.py b/src/saml2/argtree.py new file mode 100644 index 00000000..2104940f --- /dev/null +++ b/src/saml2/argtree.py @@ -0,0 +1,70 @@ +__author__ = 'roland' + + +def find_paths(cls, arg, path=None, seen=None, res=None, lev=0): + if lev == 0 and res is None: + res = [] + + if path is None: + path = [] + + if seen is None: + seen = [cls] + else: + if cls in seen: + return None + + seen.append(cls) + + for cn, c in cls.c_children.values(): + _path = path + [cn] + if cn == arg: + if res is not None: + res.append(_path) + else: + if isinstance(c, list): + _c = c[0] + else: + _c = c + + find_paths(_c, arg, _path, seen, res) + + for an, typ, mult in cls.c_attributes.values(): + if an == arg: + if res is not None: + res.append(path + [an]) + + if lev == 0: + return res + + +def set_arg(cls, arg, value): + res = [] + for path in find_paths(cls, arg): + x = y = {} + for arc in path[:-1]: + y[arc] = {} + y = y[arc] + y[path[-1]] = value + res.append(x) + + return res + + +def add_path(tdict, path): + """ + + :param tdict: a dictionary representing a argument tree + :param path: a path list + :return: a dictionary + """ + t = tdict + for step in path[:-2]: + try: + t = t[step] + except KeyError: + t[step] = {} + t = t[step] + t[path[-2]] = path[-1] + + return tdict |