Skip to content

Commit

Permalink
Merge branch 'develop' of ssh://git.ecmwf.int/mag/magics into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvielamythepaut committed Sep 4, 2020
2 parents 3b95377 + 7c278d1 commit 7ff1644
Showing 1 changed file with 68 additions and 12 deletions.
80 changes: 68 additions & 12 deletions tools/xref.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand All @@ -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()

0 comments on commit 7ff1644

Please sign in to comment.