##// END OF EJS Templates
hora y fecha en el primer bloque hdf5 >> consola
hora y fecha en el primer bloque hdf5 >> consola

File last commit:

r1330:959603ec6724
r1400:005589ae0527
Show More
cli.py
238 lines | 7.2 KiB | text/x-python | PythonLexer
José Chávez
primer generate basic
r934 import click
José Chávez
multiprocess added to cli
r935 import subprocess
José Chávez
nextcommand y command
r936 import os
import sys
import glob
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 import schainpy
José Chávez
log to file
r1047 from schainpy.controller import Project
José Chávez
1.0
r944 from schainpy.model import Operation, ProcessingUnit
José Chávez
added log helper
r943 from schainpy.utils import log
José Chávez
1.0
r944 from importlib import import_module
from pydoc import locate
from fuzzywuzzy import process
Juan C. Espinoza
Fix CLI
r1180 from schainpy.cli import templates
Fix CLI
r1253 import inspect
try:
from queue import Queue
except:
from Queue import Queue
José Chávez
1.0
r944
José Chávez
primer generate basic
r934
Juan C. Espinoza
Fix CLI
r1180 def getProcs():
modules = dir(schainpy.model)
Fix CLI
r1253 procs = check_module(modules, 'processing')
Juan C. Espinoza
Fix CLI
r1180 try:
procs.remove('ProcessingUnit')
except Exception as e:
pass
return procs
def getOperations():
module = dir(schainpy.model)
noProcs = [x for x in module if not x.endswith('Proc')]
Fix CLI
r1253 operations = check_module(noProcs, 'operation')
Juan C. Espinoza
Fix CLI
r1180 try:
operations.remove('Operation')
Fix CLI
r1253 operations.remove('Figure')
operations.remove('Plot')
Juan C. Espinoza
Fix CLI
r1180 except Exception as e:
pass
return operations
def getArgs(op):
module = locate('schainpy.model.{}'.format(op))
Juan C. Espinoza
Fix CLI and remove imports warnings
r1284 try:
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 obj = module(1, 2, 3, Queue())
Juan C. Espinoza
Fix CLI and remove imports warnings
r1284 except:
obj = module()
if hasattr(obj, '__attrs__'):
args = obj.__attrs__
Fix CLI
r1253 else:
Juan C. Espinoza
Fix CLI and remove imports warnings
r1284 if hasattr(obj, 'myrun'):
args = inspect.getfullargspec(obj.myrun).args
else:
args = inspect.getfullargspec(obj.run).args
Juan C. Espinoza
Fix CLI
r1180 try:
args.remove('self')
except Exception as e:
pass
try:
args.remove('dataOut')
except Exception as e:
pass
return args
Fix CLI
r1253 def getDoc(obj):
module = locate('schainpy.model.{}'.format(obj))
try:
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 obj = module(1, 2, 3, Queue())
Fix CLI
r1253 except:
obj = module()
return obj.__doc__
Juan C. Espinoza
Fix CLI
r1180 def getAll():
Fix CLI
r1253 modules = getOperations()
modules.extend(getProcs())
Juan C. Espinoza
Fix CLI
r1180 return modules
José Chávez
primer generate basic
r934 def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo(schainpy.__version__)
ctx.exit()
José Chávez
1.0
r944
PREFIX = 'experiment'
José Chávez
primer generate basic
r934 @click.command()
@click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str)
@click.argument('command', default='run', required=True)
José Chávez
1.0
r944 @click.argument('nextcommand', default=None, required=False, type=str)
José Chávez
schain xml
r1081 def main(command, nextcommand, version):
George Yong
New templates for CLI (Task #1402)
r1182 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY V3.0\n
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 Available commands:\n
George Yong
New templates for CLI (Task #1402)
r1182 xml: runs a schain XML generated file\n
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 run: runs any python script'\n
José Chávez
added a better --help
r946 generate: generates a template schain script\n
Fix CLI
r1253 list: return a list of available procs and operations\n
search: return avilable operations, procs or arguments of the given
operation/proc\n"""
José Chávez
schain xml
r1081 if command == 'xml':
runFromXML(nextcommand)
José Chávez
multiprocess added to cli
r935 elif command == 'generate':
José Chávez
primer generate basic
r934 generate()
elif command == 'test':
test()
José Chávez
nextcommand y command
r936 elif command == 'run':
José Chávez
1.0
r944 runschain(nextcommand)
elif command == 'search':
search(nextcommand)
Fix CLI
r1253 elif command == 'list':
cmdlist(nextcommand)
José Chávez
1.0
r944 else:
log.error('Command {} is not defined'.format(command))
José Chávez
schain xml
r1081
José Chávez
1.0
r944 def check_module(possible, instance):
def check(x):
try:
instancia = locate('schainpy.model.{}'.format(x))
Fix CLI
r1253 ret = instancia.proc_type == instance
return ret
José Chávez
1.0
r944 except Exception as e:
return False
clean = clean_modules(possible)
return [x for x in clean if check(x)]
def clean_modules(module):
noEndsUnder = [x for x in module if not x.endswith('__')]
noStartUnder = [x for x in noEndsUnder if not x.startswith('__')]
noFullUpper = [x for x in noStartUnder if not x.isupper()]
return noFullUpper
Fix CLI
r1253 def cmdlist(nextcommand):
José Chávez
1.0
r944 if nextcommand is None:
Fix CLI
r1253 log.error('Missing argument, available arguments: procs, operations', '')
José Chávez
1.0
r944 elif nextcommand == 'procs':
Juan C. Espinoza
Fix CLI
r1180 procs = getProcs()
José Chávez
schain xml
r1081 log.success(
Fix CLI
r1253 'Current ProcessingUnits are:\n {}'.format('\n '.join(procs)), '')
José Chávez
1.0
r944 elif nextcommand == 'operations':
Juan C. Espinoza
Fix CLI
r1180 operations = getOperations()
Fix CLI
r1253 log.success('Current Operations are:\n {}'.format(
'\n '.join(operations)), '')
else:
log.error('Wrong argument', '')
def search(nextcommand):
if nextcommand is None:
log.error('There is no Operation/ProcessingUnit to search', '')
José Chávez
1.0
r944 else:
Juan C. Espinoza
Fix CLI and remove imports warnings
r1284 try:
Juan C. Espinoza
Fix CLI
r1180 args = getArgs(nextcommand)
Fix CLI
r1253 doc = getDoc(nextcommand)
Juan C. Espinoza
Beta version!
r1330 log.success('{}\n{}\n\nparameters:\n {}'.format(
Juan C. Espinoza
Rewrite controller, remove MPDecorator to units (keep for plots an writers) use of queues for interproc comm instead of zmq, self operations are no longer supported
r1287 nextcommand, doc, ', '.join(args)), ''
)
Block publishing when input queues are full to avoid data loss
r1256 except Exception as e:
log.error('Module `{}` does not exists'.format(nextcommand), '')
allModules = getAll()
similar = [t[0] for t in process.extract(nextcommand, allModules, limit=12) if t[1]>80]
log.success('Possible modules are: {}'.format(', '.join(similar)), '')
José Chávez
1.0
r944
def runschain(nextcommand):
if nextcommand is None:
currentfiles = glob.glob('./{}_*.py'.format(PREFIX))
numberfiles = len(currentfiles)
if numberfiles > 1:
log.error('There is more than one file to run')
elif numberfiles == 1:
subprocess.call(['python ' + currentfiles[0]], shell=True)
José Chávez
nextcommand y command
r936 else:
José Chávez
1.0
r944 log.error('There is no file to run')
José Chávez
primer generate basic
r934 else:
José Chávez
1.0
r944 try:
subprocess.call(['python ' + nextcommand], shell=True)
except Exception as e:
log.error("I cannot run the file. Does it exists?")
José Chávez
primer generate basic
r934
José Chávez
cli dentro del build de schain
r939
José Chávez
multiprocess added to cli
r935 def basicInputs():
José Chávez
primer generate basic
r934 inputs = {}
José Chávez
schain xml
r1081 inputs['name'] = click.prompt(
'Name of the project', default="project", type=str)
George Yong
New templates for CLI (Task #1402)
r1182 inputs['desc'] = click.prompt(
'Enter a description', default="A schain project", type=str)
inputs['multiprocess'] = click.prompt(
'''Select data type:
- Voltage (*.r): [1]
- Spectra (*.pdata): [2]
- Voltage and Spectra (*.r): [3]
-->''', type=int)
José Chávez
schain xml
r1081 inputs['path'] = click.prompt('Data path', default=os.getcwd(
), type=click.Path(exists=True, resolve_path=True))
inputs['startDate'] = click.prompt(
'Start date', default='1970/01/01', type=str)
inputs['endDate'] = click.prompt(
George Yong
New templates for CLI (Task #1402)
r1182 'End date', default='2018/12/31', type=str)
José Chávez
schain xml
r1081 inputs['startHour'] = click.prompt(
'Start hour', default='00:00:00', type=str)
José Chávez
primer generate basic
r934 inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str)
José Chávez
multiprocess added to cli
r935 inputs['figpath'] = inputs['path'] + '/figs'
return inputs
José Chávez
cli dentro del build de schain
r939
José Chávez
multiprocess added to cli
r935 def generate():
inputs = basicInputs()
George Yong
New templates for CLI (Task #1402)
r1182
if inputs['multiprocess'] == 1:
current = templates.voltage.format(**inputs)
elif inputs['multiprocess'] == 2:
current = templates.spectra.format(**inputs)
elif inputs['multiprocess'] == 3:
current = templates.voltagespectra.format(**inputs)
José Chávez
1.0
r944 scriptname = '{}_{}.py'.format(PREFIX, inputs['name'])
José Chávez
multiprocess added to cli
r935 script = open(scriptname, 'w')
try:
script.write(current)
José Chávez
1.0
r944 log.success('Script {} generated'.format(scriptname))
José Chávez
multiprocess added to cli
r935 except Exception as e:
José Chávez
added log helper
r943 log.error('I cannot create the file. Do you have writing permissions?')
José Chávez
multiprocess added to cli
r935
José Chávez
primer generate basic
r934
def test():
José Chávez
added log helper
r943 log.warning('testing')
José Chávez
cli dentro del build de schain
r939
def runFromXML(filename):
José Chávez
log to file
r1047 controller = Project()
José Chávez
cli dentro del build de schain
r939 if not controller.readXml(filename):
return
controller.start()
return