##// END OF EJS Templates
errores mejorados
José Chávez -
r945:f9b2d290f79c
parent child
Show More
@@ -1,180 +1,183
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('trash', 'w')
8 sys.stdout = open('trash', 'w')
9 from multiprocessing import cpu_count
9 from multiprocessing import cpu_count
10 from schaincli import templates
10 from schaincli import templates
11 from schainpy import controller_api
11 from schainpy import controller_api
12 from schainpy.model import Operation, ProcessingUnit
12 from schainpy.model import Operation, ProcessingUnit
13 from schainpy.utils import log
13 from schainpy.utils import log
14 from importlib import import_module
14 from importlib import import_module
15 from pydoc import locate
15 from pydoc import locate
16 from fuzzywuzzy import process
16 from fuzzywuzzy import process
17 sys.stdout = save_stdout
17 sys.stdout = save_stdout
18
18
19
19
20 def print_version(ctx, param, value):
20 def print_version(ctx, param, value):
21 if not value or ctx.resilient_parsing:
21 if not value or ctx.resilient_parsing:
22 return
22 return
23 click.echo(schainpy.__version__)
23 click.echo(schainpy.__version__)
24 ctx.exit()
24 ctx.exit()
25
25
26
26
27 cliLogger = log.makelogger('schain cli')
27 cliLogger = log.makelogger('schain cli')
28 PREFIX = 'experiment'
28 PREFIX = 'experiment'
29
29
30
30
31 @click.command()
31 @click.command()
32 @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
32 @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
33 @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True))
33 @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True))
34 @click.argument('command', default='run', required=True)
34 @click.argument('command', default='run', required=True)
35 @click.argument('nextcommand', default=None, required=False, type=str)
35 @click.argument('nextcommand', default=None, required=False, type=str)
36 def main(command, nextcommand, version, xml):
36 def main(command, nextcommand, version, xml):
37 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY"""
37 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY"""
38 if xml is not None:
38 if xml is not None:
39 runFromXML(xml)
39 runFromXML(xml)
40 elif command == 'generate':
40 elif command == 'generate':
41 generate()
41 generate()
42 elif command == 'test':
42 elif command == 'test':
43 test()
43 test()
44 elif command == 'run':
44 elif command == 'run':
45 runschain(nextcommand)
45 runschain(nextcommand)
46 elif command == 'search':
46 elif command == 'search':
47 search(nextcommand)
47 search(nextcommand)
48 else:
48 else:
49 log.error('Command {} is not defined'.format(command))
49 log.error('Command {} is not defined'.format(command))
50
50
51 def check_module(possible, instance):
51 def check_module(possible, instance):
52 def check(x):
52 def check(x):
53 try:
53 try:
54 instancia = locate('schainpy.model.{}'.format(x))
54 instancia = locate('schainpy.model.{}'.format(x))
55 return isinstance(instancia(), instance)
55 return isinstance(instancia(), instance)
56 except Exception as e:
56 except Exception as e:
57 return False
57 return False
58 clean = clean_modules(possible)
58 clean = clean_modules(possible)
59 return [x for x in clean if check(x)]
59 return [x for x in clean if check(x)]
60
60
61
61
62 def clean_modules(module):
62 def clean_modules(module):
63 noEndsUnder = [x for x in module if not x.endswith('__')]
63 noEndsUnder = [x for x in module if not x.endswith('__')]
64 noStartUnder = [x for x in noEndsUnder if not x.startswith('__')]
64 noStartUnder = [x for x in noEndsUnder if not x.startswith('__')]
65 noFullUpper = [x for x in noStartUnder if not x.isupper()]
65 noFullUpper = [x for x in noStartUnder if not x.isupper()]
66 return noFullUpper
66 return noFullUpper
67
67
68
68
69 def search(nextcommand):
69 def search(nextcommand):
70
71 if nextcommand is None:
70 if nextcommand is None:
72 log.error('There is no Operation/ProcessingUnit to search')
71 log.error('There is no Operation/ProcessingUnit to search')
73 elif nextcommand == 'procs':
72 elif nextcommand == 'procs':
74 module = dir(import_module('schainpy.model'))
73 module = dir(import_module('schainpy.model'))
75 procs = check_module(module, ProcessingUnit)
74 procs = check_module(module, ProcessingUnit)
76 try:
75 try:
77 procs.remove('ProcessingUnit')
76 procs.remove('ProcessingUnit')
78 except Exception as e:
77 except Exception as e:
79 pass
78 pass
80 log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
79 log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
81
80
82 elif nextcommand == 'operations':
81 elif nextcommand == 'operations':
83 module = dir(import_module('schainpy.model'))
82 module = dir(import_module('schainpy.model'))
84 noProcs = [x for x in module if not x.endswith('Proc')]
83 noProcs = [x for x in module if not x.endswith('Proc')]
85 operations = check_module(noProcs, Operation)
84 operations = check_module(noProcs, Operation)
86 try:
85 try:
87 operations.remove('Operation')
86 operations.remove('Operation')
88 except Exception as e:
87 except Exception as e:
89 pass
88 pass
90 log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations)))
89 log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations)))
91 else:
90 else:
92 try:
91 try:
93 module = locate('schainpy.model.{}'.format(nextcommand))
92 module = locate('schainpy.model.{}'.format(nextcommand))
94 log.warning('Use this feature with caution. It may not return all the allowed arguments')
95 args = module().getAllowedArgs()
93 args = module().getAllowedArgs()
94 log.warning('Use this feature with caution. It may not return all the allowed arguments')
96 try:
95 try:
97 args.remove('self')
96 args.remove('self')
98 except Exception as e:
97 except Exception as e:
99 pass
98 pass
100 try:
99 try:
101 args.remove('dataOut')
100 args.remove('dataOut')
102 except Exception as e:
101 except Exception as e:
103 pass
102 pass
103 if len(args) == 0:
104 log.success('{} has no arguments'.format(nextcommand))
105 else:
104 log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args)))
106 log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args)))
105 except Exception as e:
107 except Exception as e:
106 log.error('Module {} does not exists'.format(nextcommand))
108 log.error('Module {} does not exists'.format(nextcommand))
107 allModules = dir(import_module('schainpy.model'))
109 allModules = dir(import_module('schainpy.model'))
108 module = check_module(allModules, Operation)
110 module = check_module(allModules, Operation)
109 module.extend(check_module(allModules, ProcessingUnit))
111 module.extend(check_module(allModules, ProcessingUnit))
110 similar = process.extractOne(nextcommand, module)[0]
112 similar = process.extractOne(nextcommand, module)[0]
113 log.success('Searching {} instead'.format(similar))
111 search(similar)
114 search(similar)
112
115
113
116
114 def runschain(nextcommand):
117 def runschain(nextcommand):
115 if nextcommand is None:
118 if nextcommand is None:
116 currentfiles = glob.glob('./{}_*.py'.format(PREFIX))
119 currentfiles = glob.glob('./{}_*.py'.format(PREFIX))
117 numberfiles = len(currentfiles)
120 numberfiles = len(currentfiles)
118 if numberfiles > 1:
121 if numberfiles > 1:
119 log.error('There is more than one file to run')
122 log.error('There is more than one file to run')
120 elif numberfiles == 1:
123 elif numberfiles == 1:
121 subprocess.call(['python ' + currentfiles[0]], shell=True)
124 subprocess.call(['python ' + currentfiles[0]], shell=True)
122 else:
125 else:
123 log.error('There is no file to run')
126 log.error('There is no file to run')
124 else:
127 else:
125 try:
128 try:
126 subprocess.call(['python ' + nextcommand], shell=True)
129 subprocess.call(['python ' + nextcommand], shell=True)
127 except Exception as e:
130 except Exception as e:
128 log.error("I cannot run the file. Does it exists?")
131 log.error("I cannot run the file. Does it exists?")
129
132
130
133
131 def basicInputs():
134 def basicInputs():
132 inputs = {}
135 inputs = {}
133 inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str)
136 inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str)
134 inputs['name'] = click.prompt('Name of the project', default="project", type=str)
137 inputs['name'] = click.prompt('Name of the project', default="project", type=str)
135 inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True))
138 inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True))
136 inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str)
139 inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str)
137 inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str)
140 inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str)
138 inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str)
141 inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str)
139 inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str)
142 inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str)
140 inputs['figpath'] = inputs['path'] + '/figs'
143 inputs['figpath'] = inputs['path'] + '/figs'
141 return inputs
144 return inputs
142
145
143
146
144 def generate():
147 def generate():
145 inputs = basicInputs()
148 inputs = basicInputs()
146 inputs['multiprocess'] = click.confirm('Is this a multiprocess script?')
149 inputs['multiprocess'] = click.confirm('Is this a multiprocess script?')
147 if inputs['multiprocess']:
150 if inputs['multiprocess']:
148 inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int)
151 inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int)
149 current = templates.multiprocess.format(**inputs)
152 current = templates.multiprocess.format(**inputs)
150 else:
153 else:
151 current = templates.basic.format(**inputs)
154 current = templates.basic.format(**inputs)
152 scriptname = '{}_{}.py'.format(PREFIX, inputs['name'])
155 scriptname = '{}_{}.py'.format(PREFIX, inputs['name'])
153 script = open(scriptname, 'w')
156 script = open(scriptname, 'w')
154 try:
157 try:
155 script.write(current)
158 script.write(current)
156 log.success('Script {} generated'.format(scriptname))
159 log.success('Script {} generated'.format(scriptname))
157 except Exception as e:
160 except Exception as e:
158 log.error('I cannot create the file. Do you have writing permissions?')
161 log.error('I cannot create the file. Do you have writing permissions?')
159
162
160
163
161 def test():
164 def test():
162 log.warning('testing')
165 log.warning('testing')
163
166
164
167
165 def runFromXML(filename):
168 def runFromXML(filename):
166 controller = controller_api.ControllerThread()
169 controller = controller_api.ControllerThread()
167 if not controller.readXml(filename):
170 if not controller.readXml(filename):
168 return
171 return
169
172
170 plotterObj = controller.useExternalPlotter()
173 plotterObj = controller.useExternalPlotter()
171
174
172 controller.start()
175 controller.start()
173 plotterObj.start()
176 plotterObj.start()
174
177
175 cliLogger("Finishing all processes")
178 cliLogger("Finishing all processes")
176
179
177 controller.join(5)
180 controller.join(5)
178
181
179 cliLogger("End of script")
182 cliLogger("End of script")
180 return
183 return
General Comments 0
You need to be logged in to leave comments. Login now