Skip to content

Commit 2402cc2

Browse files
committed
Merge branch 'dev'
2 parents 882ac06 + d1b8403 commit 2402cc2

File tree

1 file changed

+83
-52
lines changed

1 file changed

+83
-52
lines changed

ci/parse_pyrad_products.py

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# %%
22
from pathlib import Path
3-
import os
3+
import os
44
import re
55

66
mainpath = Path(__file__).resolve().parent.parent
7-
prodpath = Path(mainpath, 'src', 'pyrad_proc', 'pyrad', 'prod')
8-
OUT_DIRECTORY = str(Path(mainpath, 'doc', 'source', 'overview'))
9-
DOC_PATH = 'https://github.com/MeteoSwiss/pyrad/blob/master/src/pyrad_proc/pyrad'
7+
prodpath = Path(mainpath, "src", "pyrad_proc", "pyrad", "prod")
8+
OUT_DIRECTORY = str(Path(mainpath, "doc", "source", "overview"))
9+
DOC_PATH = "https://github.com/MeteoSwiss/pyrad/blob/master/src/pyrad_proc/pyrad"
10+
1011

1112
def funcpath_to_docpath(funcpath):
12-
funcpath = funcpath.split('pyrad')[-1]
13+
funcpath = funcpath.split("pyrad")[-1]
1314
return DOC_PATH + funcpath
1415

16+
1517
def parameters_to_dict(params):
1618
dic = {}
1719
params = params.split("\n")
@@ -39,96 +41,125 @@ def parameters_to_dict(params):
3941

4042
def dict_to_restructured_text(yaml_data):
4143
rst_output = []
42-
rst_output.append('List of pyrad products separated by dataset type')
43-
rst_output.append('=================================================\n')
44+
rst_output.append("List of pyrad products separated by dataset type")
45+
rst_output.append("=================================================\n")
4446

4547
for datasettype, value in yaml_data.items():
4648
rst_output.append(f"{datasettype}")
4749
rst_output.append("-----------------------------")
4850

49-
if 'all_products' in yaml_data[datasettype]:
51+
if "all_products" in yaml_data[datasettype]:
5052
rst_output.append(".. note::")
51-
rst_output.append(" Supports all products of type " + \
52-
f"{yaml_data[datasettype]['all_products']}")
53-
rst_output.append('')
53+
rst_output.append(
54+
" Supports all products of type "
55+
+ f"{yaml_data[datasettype]['all_products']}"
56+
)
57+
rst_output.append("")
5458

5559
for product, prodinfo in yaml_data[datasettype].items():
5660
if type(prodinfo) == str:
5761
continue
58-
if 'description' not in prodinfo:
62+
if "description" not in prodinfo:
5963
continue
6064
rst_output.append(f"{product}")
6165
rst_output.append('""""""""""""""""""""""""""""""')
62-
rst_output.append('description')
63-
rst_output.append(' ' + prodinfo['description'] + f'\n `[Source] <{prodinfo["link"]}>`_' )
64-
params = parameters_to_dict(prodinfo['parameters'])
66+
rst_output.append("description")
67+
rst_output.append(
68+
" " + prodinfo["description"] + f'\n `[Source] <{prodinfo["link"]}>`_'
69+
)
70+
params = parameters_to_dict(prodinfo["parameters"])
6571
if len(params):
66-
rst_output.append('parameters')
67-
72+
rst_output.append("parameters")
73+
6874
for param, paraminfo in params.items():
6975
try:
70-
rst_output.append(f' {param.split()[0]} : *{' '.join(param.split()[1:])}*')
76+
rst_output.append(
77+
f" {param.split()[0]} : *{" ".join(param.split()[1:])}*"
78+
)
7179
except IndexError:
72-
rst_output.append(f' {param}')
73-
rst_output.append(' ' + paraminfo)
74-
rst_output.append('')
80+
rst_output.append(f" {param}")
81+
rst_output.append(" " + paraminfo)
82+
rst_output.append("")
7583
# rst_output.append(f"\n\n{value2['parameters']}\n\n")
76-
return '\n'.join(rst_output)
84+
return "\n".join(rst_output)
7785

7886

7987
def process_file(filepath):
80-
with open(filepath, 'r') as f:
88+
with open(filepath, "r") as f:
8189
content = f.readlines()
8290
all_products = {}
8391
started = False
8492
product = None
85-
for i,line in enumerate(content):
86-
if 'def generate' in line:
87-
function = line.split('def')[1].split('(')[0].strip()
93+
for i, line in enumerate(content):
94+
if "def generate" in line:
95+
if started:
96+
# clear buffer
97+
try:
98+
all_products[function][product]["description"] = " ".join(
99+
descr.replace("\n", "").split()
100+
)
101+
except (KeyError, UnboundLocalError, TypeError):
102+
pass
103+
104+
function = line.split("def")[1].split("(")[0].strip()
88105
all_products[function] = {}
89106
reading_title = False
90107
reading_params = False
91-
if 'Accepted product types:' in line:
108+
if "Accepted product types:" in line:
92109
started = True
93110
reading_params = False
94111
if started:
95-
if 'All the products of the' in line:
96-
all_products_type = line.split('All the products of the ')[1].split()[0]
97-
all_products[function]['all_products'] = all_products_type
112+
if "All the products of the" in line:
113+
all_products_type = line.split("All the products of the ")[1].split()[0]
114+
all_products[function]["all_products"] = all_products_type
98115

99116
match = re.findall("^'[A-Z0-9_]*'\\s*:\\d*", line.strip())
100-
if 'Parameters' in line: # End of block with product list
117+
if "Parameters" in line: # End of block with product list
101118
reading_params = False
119+
reading_title = False
102120
if len(match):
103121
reading_params = False
104122
reading_title = True
105-
if product in all_products[function]:
106-
all_products[function][product]['description'] = " ".join(
107-
descr.replace('\n', '').split())
108-
109-
product = match[0].replace("'", "").split(':')[0].strip()
110-
descr = line.split(':')[1].strip()
123+
# write description for last product
124+
try:
125+
if product in all_products[function]:
126+
all_products[function][product]["description"] = " ".join(
127+
descr.replace("\n", "").split()
128+
)
129+
except TypeError:
130+
pass
131+
132+
product = match[0].replace("'", "").split(":")[0].strip()
133+
descr = line.split(":")[1].strip()
111134
all_products[function][product] = {}
112-
all_products[function][product]['parameters'] = ''
135+
all_products[function][product]["parameters"] = ""
113136
continue
114-
if 'User defined parameters' in line:
115-
all_products[function][product]['description'] = " ".join(
116-
descr.replace('\n', '').split())
137+
if "User defined parameters" in line:
138+
all_products[function][product]["description"] = " ".join(
139+
descr.replace("\n", "").split()
140+
)
117141
reading_params = True
118142
reading_title = False
119143
continue
120144
if reading_title:
121145
descr += line
122146
if reading_params and product:
123-
all_products[function][product]['parameters'] += " " + \
124-
line
125-
if ('prdcfg["type"]' in line or "prdcfg['type']" in line) and '==' in line:
147+
all_products[function][product]["parameters"] += " " + line
148+
if ('prdcfg["type"]' in line or "prdcfg['type']" in line) and "==" in line:
126149
for product in all_products[function].keys():
127150
if product in line:
128-
all_products[function][product]['link'] = (funcpath_to_docpath(filepath) +
129-
f'#L{i+1}')
151+
all_products[function][product]["link"] = (
152+
funcpath_to_docpath(filepath) + f"#L{i+1}"
153+
)
154+
155+
if reading_title:
156+
all_products[function][product]["description"] = " ".join(
157+
descr.replace("\n", "").split()
158+
)
159+
130160
return all_products
131161

162+
132163
products = {}
133164
for root, _, files in os.walk(prodpath):
134165
for file in files:
@@ -138,14 +169,14 @@ def process_file(filepath):
138169

139170
# Match function names to product types
140171
mapping_func_to_prodname = {}
141-
file_path = Path(prodpath, 'product_aux.py')
142-
with open(file_path, 'r') as f:
172+
file_path = Path(prodpath, "product_aux.py")
173+
with open(file_path, "r") as f:
143174
content = f.readlines()
144175
for line in content:
145176
match = re.findall("^'[A-Z0-9_]*'\\s*:\\d*", line.strip())
146177
if len(match):
147-
product = match[0].replace("'", "").split(':')[0]
148-
function = line.split(':')[1].strip()
178+
product = match[0].replace("'", "").split(":")[0]
179+
function = line.split(":")[1].strip()
149180
mapping_func_to_prodname[function] = product
150181

151182
for k in mapping_func_to_prodname:
@@ -155,6 +186,6 @@ def process_file(filepath):
155186

156187
# Convert dict data to reStructuredText format
157188
rst_content = dict_to_restructured_text(products)
158-
fname = Path(OUT_DIRECTORY, 'list_products.rst')
159-
with open(fname, 'w') as f:
189+
fname = Path(OUT_DIRECTORY, "list_products.rst")
190+
with open(fname, "w") as f:
160191
f.write(rst_content)

0 commit comments

Comments
 (0)