##// END OF EJS Templates
merge from v2.3
merge from v2.3

File last commit:

r1047:20a96cdbf240
r1057:3e3a4edbb591 merge
Show More
cli.py
157 lines | 5.6 KiB | text/x-python | PythonLexer
José Chávez
primer generate basic
r934 import click
import schainpy
José Chávez
multiprocess added to cli
r935 import subprocess
José Chávez
nextcommand y command
r936 import os
import sys
import glob
José Chávez
1.0
r944 save_stdout = sys.stdout
sys.stdout = open('trash', 'w')
José Chávez
added log helper
r943 from multiprocessing import cpu_count
from schaincli import templates
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
José Chávez
algunos cambios en roj
r953 from schainpy.utils import paramsFinder
José Chávez
1.0
r944 sys.stdout = save_stdout
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
José Chávez
added log helper
r943 cliLogger = log.makelogger('schain cli')
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)
José Chávez
nextcommand y command
r936 @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True))
José Chávez
primer generate basic
r934 @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
nextcommand y command
r936 def main(command, nextcommand, version, xml):
José Chávez
added a better --help
r946 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY \n
Available commands.\n
--xml: runs a schain XML generated file\n
run: runs any python script starting 'experiment_'\n
generate: generates a template schain script\n
search: return avilable operations, procs or arguments of the give operation/proc\n"""
José Chávez
multiprocess added to cli
r935 if xml is not None:
José Chávez
cli dentro del build de schain
r939 runFromXML(xml)
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)
else:
log.error('Command {} is not defined'.format(command))
def check_module(possible, instance):
def check(x):
try:
instancia = locate('schainpy.model.{}'.format(x))
return isinstance(instancia(), instance)
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
def search(nextcommand):
if nextcommand is None:
log.error('There is no Operation/ProcessingUnit to search')
elif nextcommand == 'procs':
José Chávez
algunos cambios en roj
r953 procs = paramsFinder.getProcs()
José Chávez
1.0
r944 log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs)))
elif nextcommand == 'operations':
José Chávez
algunos cambios en roj
r953 operations = paramsFinder.getOperations()
José Chávez
1.0
r944 log.success('Current Operations are:\n\033[1m{}\033[0m'.format('\n'.join(operations)))
else:
try:
José Chávez
algunos cambios en roj
r953 args = paramsFinder.getArgs(nextcommand)
José Chávez
errores mejorados
r945 log.warning('Use this feature with caution. It may not return all the allowed arguments')
if len(args) == 0:
log.success('{} has no arguments'.format(nextcommand))
else:
log.success('Showing arguments of {} are:\n\033[1m{}\033[0m'.format(nextcommand, '\n'.join(args)))
José Chávez
1.0
r944 except Exception as e:
log.error('Module {} does not exists'.format(nextcommand))
José Chávez
algunos cambios en roj
r953 allModules = paramsFinder.getAll()
similar = process.extractOne(nextcommand, allModules)[0]
log.success('Showing {} instead'.format(similar))
José Chávez
1.0
r944 search(similar)
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 = {}
inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str)
inputs['name'] = click.prompt('Name of the project', default="project", type=str)
inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True))
José Chávez
multiprocess added to cli
r935 inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str)
inputs['endDate'] = click.prompt('End date', default='2017/12/31', type=str)
José Chávez
primer generate basic
r934 inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str)
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()
inputs['multiprocess'] = click.confirm('Is this a multiprocess script?')
if inputs['multiprocess']:
inputs['nProcess'] = click.prompt('How many process?', default=cpu_count(), type=int)
current = templates.multiprocess.format(**inputs)
else:
current = templates.basic.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