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