@@ -1,208 +1,236 | |||
|
1 | 1 | import click |
|
2 | 2 | import schainpy |
|
3 | 3 | import subprocess |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import glob |
|
7 | 7 | save_stdout = sys.stdout |
|
8 | 8 | sys.stdout = open('/dev/null', 'w') |
|
9 | 9 | from multiprocessing import cpu_count |
|
10 | 10 | from schainpy.controller import Project |
|
11 | 11 | from schainpy.model import Operation, ProcessingUnit |
|
12 | 12 | from schainpy.utils import log |
|
13 | 13 | from importlib import import_module |
|
14 | 14 | from pydoc import locate |
|
15 | 15 | from fuzzywuzzy import process |
|
16 | 16 | from schainpy.cli import templates |
|
17 | import inspect | |
|
18 | try: | |
|
19 | from queue import Queue | |
|
20 | except: | |
|
21 | from Queue import Queue | |
|
17 | 22 | sys.stdout = save_stdout |
|
18 | 23 | |
|
19 | 24 | |
|
20 | 25 | def getProcs(): |
|
21 | 26 | modules = dir(schainpy.model) |
|
22 |
procs = check_module(modules, |
|
|
27 | procs = check_module(modules, 'processing') | |
|
23 | 28 | try: |
|
24 | 29 | procs.remove('ProcessingUnit') |
|
25 | 30 | except Exception as e: |
|
26 | 31 | pass |
|
27 | 32 | return procs |
|
28 | 33 | |
|
29 | 34 | def getOperations(): |
|
30 | 35 | module = dir(schainpy.model) |
|
31 | 36 | noProcs = [x for x in module if not x.endswith('Proc')] |
|
32 |
operations = check_module(noProcs, |
|
|
37 | operations = check_module(noProcs, 'operation') | |
|
33 | 38 | try: |
|
34 | 39 | operations.remove('Operation') |
|
40 | operations.remove('Figure') | |
|
41 | operations.remove('Plot') | |
|
35 | 42 | except Exception as e: |
|
36 | 43 | pass |
|
37 | 44 | return operations |
|
38 | 45 | |
|
39 | 46 | def getArgs(op): |
|
40 | 47 | module = locate('schainpy.model.{}'.format(op)) |
|
41 | args = module().getAllowedArgs() | |
|
48 | ||
|
49 | if hasattr(module, '__attrs'): | |
|
50 | args = module.__attrs__ | |
|
51 | else: | |
|
52 | args = inspect.getargspec(module.run).args | |
|
42 | 53 | try: |
|
43 | 54 | args.remove('self') |
|
44 | 55 | except Exception as e: |
|
45 | 56 | pass |
|
46 | 57 | try: |
|
47 | 58 | args.remove('dataOut') |
|
48 | 59 | except Exception as e: |
|
49 | 60 | pass |
|
50 | 61 | return args |
|
51 | 62 | |
|
63 | def getDoc(obj): | |
|
64 | module = locate('schainpy.model.{}'.format(obj)) | |
|
65 | try: | |
|
66 | obj = module(1,2,3,Queue(),5) | |
|
67 | except: | |
|
68 | obj = module() | |
|
69 | return obj.__doc__ | |
|
70 | ||
|
52 | 71 | def getAll(): |
|
53 | allModules = dir(schainpy.model) | |
|
54 | modules = check_module(allModules, Operation) | |
|
55 | modules.extend(check_module(allModules, ProcessingUnit)) | |
|
72 | modules = getOperations() | |
|
73 | modules.extend(getProcs()) | |
|
56 | 74 | return modules |
|
57 | 75 | |
|
58 | 76 | |
|
59 | 77 | def print_version(ctx, param, value): |
|
60 | 78 | if not value or ctx.resilient_parsing: |
|
61 | 79 | return |
|
62 | 80 | click.echo(schainpy.__version__) |
|
63 | 81 | ctx.exit() |
|
64 | 82 | |
|
65 | 83 | |
|
66 | 84 | PREFIX = 'experiment' |
|
67 | 85 | |
|
68 | 86 | @click.command() |
|
69 | 87 | @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str) |
|
70 | 88 | @click.argument('command', default='run', required=True) |
|
71 | 89 | @click.argument('nextcommand', default=None, required=False, type=str) |
|
72 | 90 | def main(command, nextcommand, version): |
|
73 | 91 | """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY V3.0\n |
|
74 | 92 | Available commands.\n |
|
75 | 93 | xml: runs a schain XML generated file\n |
|
76 | 94 | run: runs any python script starting 'experiment_'\n |
|
77 | 95 | generate: generates a template schain script\n |
|
78 | search: return avilable operations, procs or arguments of the give operation/proc\n""" | |
|
96 | list: return a list of available procs and operations\n | |
|
97 | search: return avilable operations, procs or arguments of the given | |
|
98 | operation/proc\n""" | |
|
79 | 99 | if command == 'xml': |
|
80 | 100 | runFromXML(nextcommand) |
|
81 | 101 | elif command == 'generate': |
|
82 | 102 | generate() |
|
83 | 103 | elif command == 'test': |
|
84 | 104 | test() |
|
85 | 105 | elif command == 'run': |
|
86 | 106 | runschain(nextcommand) |
|
87 | 107 | elif command == 'search': |
|
88 | 108 | search(nextcommand) |
|
109 | elif command == 'list': | |
|
110 | cmdlist(nextcommand) | |
|
89 | 111 | else: |
|
90 | 112 | log.error('Command {} is not defined'.format(command)) |
|
91 | 113 | |
|
92 | 114 | |
|
93 | 115 | def check_module(possible, instance): |
|
94 | 116 | def check(x): |
|
95 | 117 | try: |
|
96 | 118 | instancia = locate('schainpy.model.{}'.format(x)) |
|
97 |
ret |
|
|
119 | ret = instancia.proc_type == instance | |
|
120 | return ret | |
|
98 | 121 | except Exception as e: |
|
99 | 122 | return False |
|
100 | 123 | clean = clean_modules(possible) |
|
101 | 124 | return [x for x in clean if check(x)] |
|
102 | 125 | |
|
103 | 126 | |
|
104 | 127 | def clean_modules(module): |
|
105 | 128 | noEndsUnder = [x for x in module if not x.endswith('__')] |
|
106 | 129 | noStartUnder = [x for x in noEndsUnder if not x.startswith('__')] |
|
107 | 130 | noFullUpper = [x for x in noStartUnder if not x.isupper()] |
|
108 | 131 | return noFullUpper |
|
109 | 132 | |
|
110 | ||
|
111 | def search(nextcommand): | |
|
133 | def cmdlist(nextcommand): | |
|
112 | 134 | if nextcommand is None: |
|
113 | log.error('There is no Operation/ProcessingUnit to search', '') | |
|
135 | log.error('Missing argument, available arguments: procs, operations', '') | |
|
114 | 136 | elif nextcommand == 'procs': |
|
115 | 137 | procs = getProcs() |
|
116 | 138 | log.success( |
|
117 | 139 | 'Current ProcessingUnits are:\n{}'.format('\n'.join(procs)), '') |
|
118 | ||
|
119 | 140 | elif nextcommand == 'operations': |
|
120 | 141 | operations = getOperations() |
|
121 | 142 | log.success('Current Operations are:\n{}'.format( |
|
122 | 143 | '\n'.join(operations)), '') |
|
123 | 144 | else: |
|
145 | log.error('Wrong argument', '') | |
|
146 | ||
|
147 | def search(nextcommand): | |
|
148 | if nextcommand is None: | |
|
149 | log.error('There is no Operation/ProcessingUnit to search', '') | |
|
150 | else: | |
|
124 | 151 | try: |
|
125 | 152 | args = getArgs(nextcommand) |
|
153 | doc = getDoc(nextcommand) | |
|
126 | 154 | if len(args) == 0: |
|
127 |
log.success(' |
|
|
155 | log.success('\n{} has no arguments'.format(nextcommand), '') | |
|
128 | 156 | else: |
|
129 |
log.success(' |
|
|
130 | nextcommand, ', '.join(args)), '') | |
|
157 | log.success('{}\n{}\n\narguments:\n {}'.format( | |
|
158 | nextcommand, doc, ', '.join(args)), '') | |
|
131 | 159 | except Exception as e: |
|
132 | 160 | log.error('Module `{}` does not exists'.format(nextcommand), '') |
|
133 | 161 | allModules = getAll() |
|
134 | 162 | similar = [t[0] for t in process.extract(nextcommand, allModules, limit=12) if t[1]>80] |
|
135 | 163 | log.success('Possible modules are: {}'.format(', '.join(similar)), '') |
|
136 | 164 | |
|
137 | 165 | def runschain(nextcommand): |
|
138 | 166 | if nextcommand is None: |
|
139 | 167 | currentfiles = glob.glob('./{}_*.py'.format(PREFIX)) |
|
140 | 168 | numberfiles = len(currentfiles) |
|
141 | 169 | if numberfiles > 1: |
|
142 | 170 | log.error('There is more than one file to run') |
|
143 | 171 | elif numberfiles == 1: |
|
144 | 172 | subprocess.call(['python ' + currentfiles[0]], shell=True) |
|
145 | 173 | else: |
|
146 | 174 | log.error('There is no file to run') |
|
147 | 175 | else: |
|
148 | 176 | try: |
|
149 | 177 | subprocess.call(['python ' + nextcommand], shell=True) |
|
150 | 178 | except Exception as e: |
|
151 | 179 | log.error("I cannot run the file. Does it exists?") |
|
152 | 180 | |
|
153 | 181 | |
|
154 | 182 | def basicInputs(): |
|
155 | 183 | inputs = {} |
|
156 | 184 | inputs['name'] = click.prompt( |
|
157 | 185 | 'Name of the project', default="project", type=str) |
|
158 | 186 | inputs['desc'] = click.prompt( |
|
159 | 187 | 'Enter a description', default="A schain project", type=str) |
|
160 | 188 | inputs['multiprocess'] = click.prompt( |
|
161 | 189 | '''Select data type: |
|
162 | 190 | |
|
163 | 191 | - Voltage (*.r): [1] |
|
164 | 192 | - Spectra (*.pdata): [2] |
|
165 | 193 | - Voltage and Spectra (*.r): [3] |
|
166 | 194 | |
|
167 | 195 | -->''', type=int) |
|
168 | 196 | inputs['path'] = click.prompt('Data path', default=os.getcwd( |
|
169 | 197 | ), type=click.Path(exists=True, resolve_path=True)) |
|
170 | 198 | inputs['startDate'] = click.prompt( |
|
171 | 199 | 'Start date', default='1970/01/01', type=str) |
|
172 | 200 | inputs['endDate'] = click.prompt( |
|
173 | 201 | 'End date', default='2018/12/31', type=str) |
|
174 | 202 | inputs['startHour'] = click.prompt( |
|
175 | 203 | 'Start hour', default='00:00:00', type=str) |
|
176 | 204 | inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str) |
|
177 | 205 | inputs['figpath'] = inputs['path'] + '/figs' |
|
178 | 206 | return inputs |
|
179 | 207 | |
|
180 | 208 | |
|
181 | 209 | def generate(): |
|
182 | 210 | inputs = basicInputs() |
|
183 | 211 | |
|
184 | 212 | if inputs['multiprocess'] == 1: |
|
185 | 213 | current = templates.voltage.format(**inputs) |
|
186 | 214 | elif inputs['multiprocess'] == 2: |
|
187 | 215 | current = templates.spectra.format(**inputs) |
|
188 | 216 | elif inputs['multiprocess'] == 3: |
|
189 | 217 | current = templates.voltagespectra.format(**inputs) |
|
190 | 218 | scriptname = '{}_{}.py'.format(PREFIX, inputs['name']) |
|
191 | 219 | script = open(scriptname, 'w') |
|
192 | 220 | try: |
|
193 | 221 | script.write(current) |
|
194 | 222 | log.success('Script {} generated'.format(scriptname)) |
|
195 | 223 | except Exception as e: |
|
196 | 224 | log.error('I cannot create the file. Do you have writing permissions?') |
|
197 | 225 | |
|
198 | 226 | |
|
199 | 227 | def test(): |
|
200 | 228 | log.warning('testing') |
|
201 | 229 | |
|
202 | 230 | |
|
203 | 231 | def runFromXML(filename): |
|
204 | 232 | controller = Project() |
|
205 | 233 | if not controller.readXml(filename): |
|
206 | 234 | return |
|
207 | 235 | controller.start() |
|
208 | 236 | return |
General Comments 0
You need to be logged in to leave comments.
Login now