def stop(config): """ Stop upyhome on the device. """ click.secho('Stop upyhome', fg=COLOR_INFO) _conf = Config(config) shell = Shell(_conf, stop=True) shell.end()
def restart(config): """ Monitor upyhome on the device. """ click.secho('Restart upyhome', fg=COLOR_INFO) _conf = Config(config) shell = Shell(_conf) click.secho('Soft reset', fg=COLOR_ACTION) shell.soft_reset()
def test(config): """ Test """ click.secho('Start upyhome', fg=COLOR_INFO) click.get_current_context() _conf = Config(config) shell = Shell(_conf) if not shell.is_upyhone(): click.secho('Upyhonme not detected, nothing to do', fg=COLOR_ERROR) else: click.secho('Upyhonme not detected, nothing to do', fg=COLOR_ACTION) shell.end()
def view(config, file): """ Display a file in the device. """ click.secho('Open file', fg=COLOR_INFO) _conf = Config(config) shell = Shell(_conf, stop=True) had_stopped = False if shell.is_upyhone(): click.secho('Upyhonme is running, try to stop it', fg=COLOR_ACTION) if shell.upyhone_exec('stop'): had_stopped = True else: click.secho('Upyhonme not stopped', fg=COLOR_ERROR) res = shell.tail(file) content = res.split('\n') p1 = re.compile('^[ ]*#.*$') p2 = re.compile('^.*""".*$') print_sep() com1 = com2 = False for line in content: m1 = p1.match(line) m2 = p2.match(line) if m1: com1 = True elif m2: com2 = not com2 click.secho(line, fg=COLOR_COMMENT if com1 or com2 or m2 else COLOR_SRC) com1 = False print_sep() if had_stopped: click.secho('Restart upyHome', fg=COLOR_ACTION) if shell.upyhone_exec('start'): click.secho('Upyhonme is started', fg=COLOR_RESULT) shell.end()
def monitor(config): """ Monitor upyhome on the device. """ click.secho('Restart upyhome', fg=COLOR_INFO) _conf = Config(config) shell = Shell(_conf) shell.upyhone_exec('mute', data=False) click.secho("Start monitoring, press CTRL-C to quit", fg=COLOR_ADVERT) p = re.compile('^#(.*)=\\[(.*)\\]') print_sep() while True: try: data = shell.pyboard.serial.readline().decode('utf8') m = p.match(data) if m: grps = m.groups() if(len(grps) == 2): click.secho('Topic:', fg=COLOR_SRC, nl=False) click.secho('%06s'%(grps[0]), fg=COLOR_COMMENT, nl=False) click.secho(' -> ', fg=COLOR_INFO, nl=False) click.secho(grps[1], fg=COLOR_ACTION) except KeyboardInterrupt: break print_sep() shell.end()
def start(config): """ Start upyhome on the device. """ click.secho('Start upyhome', fg=COLOR_INFO) _conf = Config(config) shell = Shell(_conf) if shell.is_upyhone(): click.secho('Upyhonme detected... start', fg=COLOR_ACTION) if shell.upyhone_exec('start'): click.secho('Upyhonme is started', fg=COLOR_RESULT) else: click.secho('Upyhonme not detected, nothing to do', fg=COLOR_ADVERT) shell.end()
def exec(config, function, topic): """ Execute a task on the device. """ click.secho('Execute a command', fg='magenta') _conf = Config(config) shell = Shell(_conf) had_stopped = False click.secho('Execute %s for topic %s'%(function, topic if topic is not None else "all"), fg='green') res = shell.upyhone_exec(func=function, comp=topic) click.secho('Result: %s'%(re), fg='yellow') if had_stopped: click.secho('Restart upyHome', fg='yellow') if shell.upyhone_exec('start'): click.secho('Upyhonme is started', fg='green') shell.end()
def files(config): """ List devices files. """ click.secho('List files', fg='magenta') _conf = Config(config) check_port(_conf) shell = Shell(_conf, stop=True) files = shell.list_files(pretty_print=True) print_sep() click.secho(tabulate(files, ['file', 'date'], 'github'), fg='bright_cyan') print_sep() if stop: click.secho('Restart upyHome', fg='yellow') if shell.upyhone_exec('start', skip=True): click.secho('Upyhonme is started', fg='green') shell.end()
def sync(config, skip): """ Upload the upyhome files. """ click.secho('Sync files', fg='magenta') _conf = Config(config) check_port(_conf) shell = Shell(_conf, stop=True) error = shell.sync_time() if error: click.secho('Failed to sync time, abort!', fg='red') return click.secho('Sync time done! checking file system', fg='green') shell.mkdirs() click.secho('Compute file operations', fg=COLOR_ACTION) files = shell.sync_files(_conf) if skip: files = list(filter(lambda file: file[0] not in ['config.json', 'webrepl_cfg.py'] , files)) print_sep() click.secho(tabulate(files, ['file', 'operatiom', 'date'], 'github'), fg='bright_cyan') print_sep() if click.confirm('Continue'): for file in files: if(file[1] == 'update' or file[1] == 'add'): click.secho('Copy file %s'%(file[0]), fg=COLOR_ACTION) shell.copy_file(file[0]) elif(file[1] == 'delete'): click.secho('Delete file %s'%(file[0]), fg=COLOR_ACTION) shell.rm_file(file[0]) if not skip: click.secho('Compile config', fg=COLOR_ACTION) conf_files = _conf.write_json_conf() multiple = len(conf_files) > 1 selected_conf = None if multiple: selected_conf = ask_sync(conf_files) else: selected_conf = conf_files[0] shell.copy_file(selected_conf+'.json', src_dir=_conf.get_generated_path(), dest_name="config.json") #repl repl = _conf.get_repl_config() if repl is not None: click.secho('Copy REPL config', fg=COLOR_ACTION) data = 'PASS = "******"\n'%(repl) shell.copy_data(data, 'webrepl_cfg.py') if click.confirm('Soft reset'): shell.soft_reset() elif stop: click.secho('Restart upyHome', fg=COLOR_ADVERT) if shell.upyhone_exec('start'): click.secho('Upyhonme is started', fg=COLOR_INFO) else: shell.end() click.secho('Sync done!', fg=COLOR_INFO)