From 7c278d1b1e5dc85e298477da077e3720fb2bc48f Mon Sep 17 00:00:00 2001 From: Baudouin Raoult Date: Fri, 4 Sep 2020 06:30:23 +0100 Subject: [PATCH] ref --- tools/xref.py | 80 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/tools/xref.py b/tools/xref.py index 72aaaea35..bf08687f3 100755 --- a/tools/xref.py +++ b/tools/xref.py @@ -1,16 +1,56 @@ #!/usr/bin/env python3 import os import xmltodict -import json -from collections import defaultdict +import yaml +from collections import defaultdict, OrderedDict + +yaml.Dumper.ignore_aliases = lambda *args : True DEFS = {} +T = {'on':True, 'off': False, 'no': False, 'stringarray()': [], 'floatarray()': []} + +def tidy(x): + + if isinstance(x, (list, tuple)): + return [tidy(y) for y in x] + + if isinstance(x, (dict, OrderedDict)): + d = { + } + for k, v in x.items(): + d[tidy(k)] = tidy(v) + + return d + + if isinstance(x, str): + if x.startswith('@'): + return x[1:] + + try: + return float(x) + except: + pass + + try: + return int(x) + except: + pass + + x = T.get(x, x) + + if isinstance(x, str): + return x.strip().replace('\n', ' ').replace('\t', ' ').replace(' ', ' ') + + return x + def load(n): with open(n) as f: x = xmltodict.parse(f.read()) + print(yaml.dump(tidy(x)['magics'], default_flow_style=False, default_style=None, canonical=False, explicit_start=True)) + klass = x['magics']['class'] klass['PATH'] = n @@ -28,9 +68,11 @@ def load(n): # exit(1) -for n in sorted(os.listdir('.')): +root = os.path.join(os.path.dirname(os.path.dirname(__file__)), "src", "params") + +for n in sorted(os.listdir(root)): if n.endswith('.xml'): # and n.startswith("SimpleP"): - load(n) + load(os.path.join(root, n)) def action(klass): @@ -41,23 +83,37 @@ def action(klass): return [action(x) for x in klass['@inherits'].split('/')] return action(DEFS[klass['@inherits']]) +ACTIONS = defaultdict(set) PARAMS = defaultdict(set) for n, klass in DEFS.items(): parms = klass.get('parameter', []) if not isinstance(parms, list): parms = [parms] - for p in parms: - try: - PARAMS[p['@name']].add(action(klass)) - except: - print(klass) - raise + a = action(klass) + if a: + for p in parms: + + try: + PARAMS[p['@name']].add(a) + except: + print(klass) + raise + + ACTIONS[a].add(p['@name']) # print(n, action(klass)) # if '@action' in klass: # print(n, json.dumps(klass, indent=4)) # load("Contour.xml") -for p, v in sorted(PARAMS.items()): - print(p, v) +# for p, v in sorted(PARAMS.items()): +# print(p, v) + +# for p, v in sorted(ACTIONS.items()): +# print("def", p,"(") +# for x in sorted(v): +# print(" ", x, "=None,") +# print(" ):") +# print(" pass") +# print()