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