From 2858555d5123c1739fa3bb9faaf6f589ddda82b2 2018-07-17 18:42:27 From: Juan C. Espinoza Date: 2018-07-17 18:42:27 Subject: [PATCH] Fix CLI --- diff --git a/schainpy/cli/cli.py b/schainpy/cli/cli.py index 9480dfc..5c0aa9b 100644 --- a/schainpy/cli/cli.py +++ b/schainpy/cli/cli.py @@ -5,7 +5,7 @@ import os import sys import glob save_stdout = sys.stdout -sys.stdout = open('trash', 'w') +sys.stdout = open('/dev/null', 'w') from multiprocessing import cpu_count from schainpy.controller import Project from schainpy.model import Operation, ProcessingUnit @@ -13,11 +13,49 @@ from schainpy.utils import log from importlib import import_module from pydoc import locate from fuzzywuzzy import process -from schainpy.utils import paramsFinder -import templates +from schainpy.cli import templates sys.stdout = save_stdout +def getProcs(): + modules = dir(schainpy.model) + procs = check_module(modules, ProcessingUnit) + try: + procs.remove('ProcessingUnit') + except Exception as e: + pass + return procs + +def getOperations(): + module = dir(schainpy.model) + noProcs = [x for x in module if not x.endswith('Proc')] + operations = check_module(noProcs, Operation) + try: + operations.remove('Operation') + except Exception as e: + pass + return operations + +def getArgs(op): + module = locate('schainpy.model.{}'.format(op)) + args = module().getAllowedArgs() + try: + args.remove('self') + except Exception as e: + pass + try: + args.remove('dataOut') + except Exception as e: + pass + return args + +def getAll(): + allModules = dir(schainpy.model) + modules = check_module(allModules, Operation) + modules.extend(check_module(allModules, ProcessingUnit)) + return modules + + def print_version(ctx, param, value): if not value or ctx.resilient_parsing: return @@ -25,10 +63,8 @@ def print_version(ctx, param, value): ctx.exit() -cliLogger = log.makelogger('schain cli') PREFIX = 'experiment' - @click.command() @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str) @click.argument('command', default='run', required=True) @@ -74,33 +110,29 @@ def clean_modules(module): def search(nextcommand): if nextcommand is None: - log.error('There is no Operation/ProcessingUnit to search') + log.error('There is no Operation/ProcessingUnit to search', '') elif nextcommand == 'procs': - procs = paramsFinder.getProcs() + procs = getProcs() log.success( - 'Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs))) + 'Current ProcessingUnits are:\n{}'.format('\n'.join(procs)), '') elif nextcommand == 'operations': - operations = paramsFinder.getOperations() - log.success('Current Operations are:\n\033[1m{}\033[0m'.format( - '\n'.join(operations))) + operations = getOperations() + log.success('Current Operations are:\n{}'.format( + '\n'.join(operations)), '') else: try: - args = paramsFinder.getArgs(nextcommand) - log.warning( - 'Use this feature with caution. It may not return all the allowed arguments') + args = getArgs(nextcommand) if len(args) == 0: - log.success('{} has no arguments'.format(nextcommand)) + log.success('`{}` has no arguments'.format(nextcommand), '') else: - log.success('Showing {} arguments:\n\033[1m{}\033[0m'.format( - nextcommand, '\n'.join(args))) + log.success('`{}` arguments: {}'.format( + nextcommand, ', '.join(args)), '') except Exception as e: - log.error('Module {} does not exists'.format(nextcommand)) - allModules = paramsFinder.getAll() - similar = process.extractOne(nextcommand, allModules)[0] - log.success('Showing {} instead'.format(similar)) - search(similar) - + log.error('Module `{}` does not exists'.format(nextcommand), '') + allModules = getAll() + similar = [t[0] for t in process.extract(nextcommand, allModules, limit=12) if t[1]>80] + log.success('Possible modules are: {}'.format(', '.join(similar)), '') def runschain(nextcommand): if nextcommand is None: diff --git a/schainpy/utils/paramsFinder.py b/schainpy/utils/paramsFinder.py deleted file mode 100644 index e0ced54..0000000 --- a/schainpy/utils/paramsFinder.py +++ /dev/null @@ -1,80 +0,0 @@ -import schainpy -from schainpy.model import Operation, ProcessingUnit -from pydoc import locate - -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 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 getProcs(): - module = dir(schainpy.model) - procs = check_module(module, ProcessingUnit) - try: - procs.remove('ProcessingUnit') - except Exception as e: - pass - return procs - -def getOperations(): - module = dir(schainpy.model) - noProcs = [x for x in module if not x.endswith('Proc')] - operations = check_module(noProcs, Operation) - try: - operations.remove('Operation') - except Exception as e: - pass - return operations - -def getArgs(op): - module = locate('schainpy.model.{}'.format(op)) - args = module().getAllowedArgs() - try: - args.remove('self') - except Exception as e: - pass - try: - args.remove('dataOut') - except Exception as e: - pass - return args - -def getAll(): - allModules = dir(schainpy.model) - modules = check_module(allModules, Operation) - modules.extend(check_module(allModules, ProcessingUnit)) - return modules - -def formatArgs(op): - args = getArgs(op) - - argsAsKey = ["\t'{}'".format(x) for x in args] - argsFormatted = ": 'string',\n".join(argsAsKey) - - print(op) - print("parameters = { \n" + argsFormatted + ": 'string',\n }") - print('\n') - - -if __name__ == "__main__": - getAll() - [formatArgs(x) for x in getAll()] - - ''' - parameters = { - 'id': , - 'wintitle': , - } - ''' \ No newline at end of file diff --git a/schainpy/utils/trash b/schainpy/utils/trash deleted file mode 100644 index 384299d..0000000 --- a/schainpy/utils/trash +++ /dev/null @@ -1 +0,0 @@ -You should install "digital_rf_hdf5" module if you want to read USRP data