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