cli.py
77 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
|
r934 | import click | ||
import schainpy | ||||
|
r935 | import subprocess | ||
from multiprocessing import cpu_count | ||||
|
r934 | from schaincli import templates | ||
|
r936 | import os | ||
import sys | ||||
import glob | ||||
|
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) | ||||
|
r936 | @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True)) | ||
|
r934 | @click.argument('command', default='run', required=True) | ||
|
r936 | @click.argument('nextcommand', default=None, required=False, type=click.Path(exists=True, resolve_path=True)) | ||
def main(command, nextcommand, version, xml): | ||||
|
r934 | """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY""" | ||
|
r935 | if xml is not None: | ||
subprocess.call(['schain --file=' + xml], shell=True) | ||||
elif command == 'generate': | ||||
|
r934 | generate() | ||
elif command == 'test': | ||||
test() | ||||
|
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) | ||||
|
r934 | else: | ||
|
r936 | click.echo('\x1b[6;37;41m[ERROR] - Command is not defined.\x1b[0m') | ||
|
r934 | |||
|
r935 | def basicInputs(): | ||
|
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)) | ||||
|
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) | ||||
|
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) | ||||
|
r935 | inputs['figpath'] = inputs['path'] + '/figs' | ||
return inputs | ||||
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) | ||||
|
r936 | click.echo('\x1b[6;37;42m[SUCCESS] Script {file} generated\x1b[0m'.format(file=scriptname)) | ||
|
r935 | except Exception as e: | ||
|
r936 | click.echo('\x1b[6;37;41m[ERROR] I cannot create the file. Do you have writing permissions?\x1b[0m') | ||
|
r935 | |||
|
r934 | |||
def test(): | ||||
print templates.basic.format(name='hola', desc= 'desc', path='path', startDate='0', endDate='0') | ||||
click.echo('testing') | ||||