##// END OF EJS Templates
Fix CLI
Juan C. Espinoza -
r1180:2858555d5123
parent child
Show More
@@ -5,7 +5,7 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('/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
@@ -13,11 +13,49 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.utils import paramsFinder
16 from schainpy.cli import templates
17 import templates
18 sys.stdout = save_stdout
17 sys.stdout = save_stdout
19
18
20
19
20 def getProcs():
21 modules = dir(schainpy.model)
22 procs = check_module(modules, ProcessingUnit)
23 try:
24 procs.remove('ProcessingUnit')
25 except Exception as e:
26 pass
27 return procs
28
29 def getOperations():
30 module = dir(schainpy.model)
31 noProcs = [x for x in module if not x.endswith('Proc')]
32 operations = check_module(noProcs, Operation)
33 try:
34 operations.remove('Operation')
35 except Exception as e:
36 pass
37 return operations
38
39 def getArgs(op):
40 module = locate('schainpy.model.{}'.format(op))
41 args = module().getAllowedArgs()
42 try:
43 args.remove('self')
44 except Exception as e:
45 pass
46 try:
47 args.remove('dataOut')
48 except Exception as e:
49 pass
50 return args
51
52 def getAll():
53 allModules = dir(schainpy.model)
54 modules = check_module(allModules, Operation)
55 modules.extend(check_module(allModules, ProcessingUnit))
56 return modules
57
58
21 def print_version(ctx, param, value):
59 def print_version(ctx, param, value):
22 if not value or ctx.resilient_parsing:
60 if not value or ctx.resilient_parsing:
23 return
61 return
@@ -25,10 +63,8 def print_version(ctx, param, value):
25 ctx.exit()
63 ctx.exit()
26
64
27
65
28 cliLogger = log.makelogger('schain cli')
29 PREFIX = 'experiment'
66 PREFIX = 'experiment'
30
67
31
32 @click.command()
68 @click.command()
33 @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
69 @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
34 @click.argument('command', default='run', required=True)
70 @click.argument('command', default='run', required=True)
@@ -74,33 +110,29 def clean_modules(module):
74
110
75 def search(nextcommand):
111 def search(nextcommand):
76 if nextcommand is None:
112 if nextcommand is None:
77 log.error('There is no Operation/ProcessingUnit to search')
113 log.error('There is no Operation/ProcessingUnit to search', '')
78 elif nextcommand == 'procs':
114 elif nextcommand == 'procs':
79 procs = paramsFinder.getProcs()
115 procs = getProcs()
80 log.success(
116 log.success(
81 'Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
117 'Current ProcessingUnits are:\n{}'.format('\n'.join(procs)), '')
82
118
83 elif nextcommand == 'operations':
119 elif nextcommand == 'operations':
84 operations = paramsFinder.getOperations()
120 operations = getOperations()
85 log.success('Current Operations are:\n\033[1m{}\033[0m'.format(
121 log.success('Current Operations are:\n{}'.format(
86 '\n'.join(operations)))
122 '\n'.join(operations)), '')
87 else:
123 else:
88 try:
124 try:
89 args = paramsFinder.getArgs(nextcommand)
125 args = getArgs(nextcommand)
90 log.warning(
91 'Use this feature with caution. It may not return all the allowed arguments')
92 if len(args) == 0:
126 if len(args) == 0:
93 log.success('{} has no arguments'.format(nextcommand))
127 log.success('`{}` has no arguments'.format(nextcommand), '')
94 else:
128 else:
95 log.success('Showing {} arguments:\n\033[1m{}\033[0m'.format(
129 log.success('`{}` arguments: {}'.format(
96 nextcommand, '\n'.join(args)))
130 nextcommand, ', '.join(args)), '')
97 except Exception as e:
131 except Exception as e:
98 log.error('Module {} does not exists'.format(nextcommand))
132 log.error('Module `{}` does not exists'.format(nextcommand), '')
99 allModules = paramsFinder.getAll()
133 allModules = getAll()
100 similar = process.extractOne(nextcommand, allModules)[0]
134 similar = [t[0] for t in process.extract(nextcommand, allModules, limit=12) if t[1]>80]
101 log.success('Showing {} instead'.format(similar))
135 log.success('Possible modules are: {}'.format(', '.join(similar)), '')
102 search(similar)
103
104
136
105 def runschain(nextcommand):
137 def runschain(nextcommand):
106 if nextcommand is None:
138 if nextcommand is None:
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now