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