##// END OF EJS Templates
better readme schain cli
better readme schain cli

File last commit:

r941:06ac8db2550c
r942:64095ee7098f
Show More
cli.py
98 lines | 3.5 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
from multiprocessing import cpu_count
José Chávez
schaincli scafolding reduced
r941 from schaincli import templates
José Chávez
cli dentro del build de schain
r939 from schainpy import controller_api
José Chávez
nextcommand y command
r936 import os
import sys
import glob
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()
@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
nextcommand y command
r936 @click.argument('nextcommand', default=None, required=False, type=click.Path(exists=True, resolve_path=True))
def main(command, nextcommand, version, xml):
José Chávez
primer generate basic
r934 """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY"""
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':
if nextcommand is None:
currentfiles = glob.glob('./*.py')
numberfiles = len(currentfiles)
print currentfiles
if numberfiles > 1:
click.echo('\x1b[6;37;41m[ERROR] - There is more than one file to run\x1b[0m')
elif numberfiles == 1:
subprocess.call(['python ' + currentfiles[0]], shell=True)
else:
click.echo('\x1b[6;37;41m[ERROR] - There is no file to run.\x1b[0m')
else:
subprocess.call(['python ' + nextcommand], shell=True)
José Chávez
primer generate basic
r934 else:
José Chávez
nextcommand y command
r936 click.echo('\x1b[6;37;41m[ERROR] - Command is not defined.\x1b[0m')
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)
scriptname = inputs['name'] + ".py"
script = open(scriptname, 'w')
try:
script.write(current)
José Chávez
nextcommand y command
r936 click.echo('\x1b[6;37;42m[SUCCESS] Script {file} generated\x1b[0m'.format(file=scriptname))
José Chávez
multiprocess added to cli
r935 except Exception as e:
José Chávez
nextcommand y command
r936 click.echo('\x1b[6;37;41m[ERROR] I cannot create the file. Do you have writing permissions?\x1b[0m')
José Chávez
multiprocess added to cli
r935
José Chávez
primer generate basic
r934
def test():
José Chávez
cli dentro del build de schain
r939 print templates.basic.format(name='hola', desc='desc', path='path', startDate='0', endDate='0')
José Chávez
primer generate basic
r934 click.echo('testing')
José Chávez
cli dentro del build de schain
r939
def runFromXML(filename):
controller = controller_api.ControllerThread()
if not controller.readXml(filename):
return
plotterObj = controller.useExternalPlotter()
controller.start()
plotterObj.start()
print "Finishing all processes ..."
controller.join(5)
print "End of script"
return