##// END OF EJS Templates
added log helper
added log helper

File last commit:

r943:0a4a147186e4
r943:0a4a147186e4
Show More
cli.py
99 lines | 3.4 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
added log helper
r943 from multiprocessing import cpu_count
from schaincli import templates
from schainpy import controller_api
from schainpy.utils import log
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
added log helper
r943 cliLogger = log.makelogger('schain cli')
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
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:
José Chávez
added log helper
r943 log.error('There is more than one file to run')
José Chávez
nextcommand y command
r936 elif numberfiles == 1:
subprocess.call(['python ' + currentfiles[0]], shell=True)
else:
José Chávez
added log helper
r943 log.error('There is no file to run.')
José Chávez
nextcommand y command
r936 else:
subprocess.call(['python ' + nextcommand], shell=True)
José Chávez
primer generate basic
r934 else:
José Chávez
added log helper
r943 log.error('Command is not defined.')
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
added log helper
r943 log.success('Script {file} generated'.format(file=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):
controller = controller_api.ControllerThread()
if not controller.readXml(filename):
return
plotterObj = controller.useExternalPlotter()
controller.start()
plotterObj.start()
José Chávez
added log helper
r943 cliLogger("Finishing all processes ...")
José Chávez
cli dentro del build de schain
r939
controller.join(5)
José Chávez
added log helper
r943 cliLogger("End of script")
José Chávez
cli dentro del build de schain
r939 return