|
1
|
|
|
# Copyright 2014 Diamond Light Source Ltd. |
|
2
|
|
|
# |
|
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4
|
|
|
# you may not use this file except in compliance with the License. |
|
5
|
|
|
# You may obtain a copy of the License at |
|
6
|
|
|
# |
|
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8
|
|
|
# |
|
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12
|
|
|
# See the License for the specific language governing permissions and |
|
13
|
|
|
# limitations under the License. |
|
14
|
|
|
|
|
15
|
|
|
""" |
|
16
|
|
|
.. module:: docstring_parser |
|
17
|
|
|
:platform: Unix |
|
18
|
|
|
:synopsis: Parser for the docstring to interpret parameters and plugin info. |
|
19
|
|
|
|
|
20
|
|
|
.. moduleauthor:: Nicola Wadeson <[email protected]> |
|
21
|
|
|
|
|
22
|
|
|
""" |
|
23
|
|
|
import sys |
|
24
|
|
|
import re |
|
25
|
|
|
|
|
26
|
|
|
import savu.plugins.loaders.utils.yaml_utils as yu |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def find_synopsis(dclass): |
|
30
|
|
|
""" |
|
31
|
|
|
Finds the parameters list from the docstring |
|
32
|
|
|
""" |
|
33
|
|
|
mod_doc_lines = _get_doc_lines(sys.modules[dclass.__module__].__doc__) |
|
34
|
|
|
return _parse_synopsis(mod_doc_lines) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def _parse_synopsis(mod_doc_lines): |
|
38
|
|
|
syn_regexp = re.compile(r"^:synopsis: \s?(?P<synopsis>.*[^ ])$") |
|
39
|
|
|
synopsis, idx2 = __find_regexp(syn_regexp, mod_doc_lines) |
|
40
|
|
|
synopsis = "" if not synopsis else synopsis[0] + "." |
|
41
|
|
|
return synopsis |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def _get_doc_lines(doc): |
|
45
|
|
|
if not doc: |
|
46
|
|
|
return [""] |
|
47
|
|
|
return [" ".join(l.strip(" .").split()) for l in doc.split("\n")] |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def __find_regexp(regexp, str_list): |
|
51
|
|
|
args = [regexp.findall(s) for s in str_list] |
|
52
|
|
|
index = [i for i in range(len(args)) if args[i]] |
|
53
|
|
|
args = [arg[0] for arg in args if len(arg)] |
|
54
|
|
|
return args, index |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def remove_new_lines(in_string): |
|
58
|
|
|
"""Remove new lines between text""" |
|
59
|
|
|
out_string = in_string |
|
60
|
|
|
if out_string: |
|
61
|
|
|
out_string = in_string.splitlines() |
|
62
|
|
|
out_string = [l.strip() for l in out_string] |
|
63
|
|
|
out_string = " ".join(out_string) |
|
64
|
|
|
return out_string |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
def change_dtype_to_str(doc): |
|
68
|
|
|
""" |
|
69
|
|
|
Not all Savu dtypes are valid yaml syntax, this function |
|
70
|
|
|
will convert them all to strings |
|
71
|
|
|
|
|
72
|
|
|
param doc: the plugin tools docstring |
|
73
|
|
|
:return: Altered yaml docstring with dtype values changed to be strings |
|
74
|
|
|
""" |
|
75
|
|
|
lines = doc.split('\n') |
|
76
|
|
|
doc = "" |
|
77
|
|
|
for i, l in enumerate(lines): |
|
78
|
|
|
split = (l.split('dtype:', 1)) |
|
79
|
|
|
if len(split) == 2: |
|
80
|
|
|
dtype = split[1].lstrip().rstrip() |
|
81
|
|
|
if dtype: |
|
82
|
|
|
if not dtype[0] == "'" and not dtype[0] == '"': |
|
83
|
|
|
l = l.replace(dtype, "'" + dtype + "'") |
|
84
|
|
|
else: |
|
85
|
|
|
print(f"Empty dtype entry for this plugin" |
|
86
|
|
|
f" tools file on line {i}") |
|
87
|
|
|
doc += l + "\n" |
|
88
|
|
|
return doc |
|
89
|
|
|
|
|
90
|
|
|
def load_yaml_doc(doc): |
|
91
|
|
|
"""Load in the yaml format. Call yaml_utils.py |
|
92
|
|
|
|
|
93
|
|
|
Parameters |
|
94
|
|
|
---------- |
|
95
|
|
|
lines : str |
|
96
|
|
|
String of information |
|
97
|
|
|
|
|
98
|
|
|
Returns |
|
99
|
|
|
---------- |
|
100
|
|
|
all_params: OrderedDict |
|
101
|
|
|
Ordered dict of parameters |
|
102
|
|
|
|
|
103
|
|
|
""" |
|
104
|
|
|
all_params = "" |
|
105
|
|
|
try: |
|
106
|
|
|
all_params = yu.read_yaml_from_doc(doc) |
|
107
|
|
|
except Exception as e: |
|
108
|
|
|
print("\nError reading the yaml structure from Yaml Utils.\n %s" % e) |
|
109
|
|
|
return all_params |
|
110
|
|
|
|