def main(): ''' Script entry point ''' args = create_argparser(DESCRIPTION).parse_args() for index, entry in enumerate(args.ks_cmdline_args): args.ks_cmdline_args[index] = entry.strip("' ") # if KATS_WORKSPACE is defined then we change our current directory to point there workspace = os.environ.get(WORKSPACE_ENV) if not workspace: print('%s environmental variable not defined running from local directory' % (WORKSPACE_ENV)) else: print('workspace set to %s' % (workspace)) os.chdir(workspace) # load log configuration file config = load(args.log) config = json_substitute_env(config) setup_logging(config, args.log_level) # proceed with the shell shell = KalsimShell(**vars(args)) try: shell.initialise() shell.run() except Exception: # pylint:disable=broad-except raise finally: shell.clean_up()
def create_argparser(description): ''' Parse command line arguments Args: description (str): Parser description Returns: argparse.ArgumentParser: Argument parser ''' parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-c', '--config', default=CONFIG_DEFAULT, help='Configuration file') parser.add_argument('-l', '--log', default=LOG_FILE_DEFAULT, help='Log configuration file') parser.add_argument('--log_level', type=int, metavar='LEVEL', default=LOG_LEVEL_DEFAULT, help='Log level') parser.add_argument('--repl', default=REPL_DEFAULT, help='Read Eval Print Loop tool') parser.add_argument('--hide_prompt', action='store_true', help='Do not display prompt') parser.add_argument('--platform', default=PLATFORM_DEFAULT, help='Chip platform') parser.add_argument('--capability_description', default=CAPABILITY_DESCRIPTION_DEFAULT, help='Configuration file with description of capabilities') parser.add_argument('--script', nargs='+', help='''Script to execute after kalsim connect. For script arguments enclose it with "" as --script "script1.py arg1" "script2.py arg1 arg2"''') parser.add_argument('--exit', action='store_true', help='Exit after connection') # command line arguments are described in the OPTIONS dict for entry in sorted(OPTIONS): args = ['--' + entry] kwargs = { 'action': OPTIONS[entry][2], 'default': OPTIONS[entry][1], 'help': OPTIONS[entry][4], } if OPTIONS[entry][2] != 'store_true' and OPTIONS[entry][2] != 'store_false': kwargs['type'] = OPTIONS[entry][0] if OPTIONS[entry][2] == 'store': kwargs['metavar'] = 'VAL' if OPTIONS[entry][3]: kwargs['nargs'] = '+' parser.add_argument(*args, **kwargs) # inject config file defaults if defined args = parser.parse_args() if args.config: filename = get_config_filename(args.config) config = load(filename) parser.set_defaults(**config) return parser
def load_capability_file(filename): ''' Get capability data from capability file The capability file should have the following format .. code-block:: python { "BASIC_PASS_THROUGH": { "id": 1, "messages": { "CAPABILITY_VERSION_REQUEST": 4096, "ENABLE_FADE_OUT": 8192, "DISABLE_FADE_OUT": 8193, "SET_DATA_STREAM_BASED": 8207, "CHANGE_INPUT_DATA_TYPE": 10, "CHANGE_OUTPUT_DATA_TYPE": 11 } }, "SCO_SEND": { "id": 3, "messages": { } }, } Args: filename (str): File name Returns: dict: Capability file contents ''' return load(filename)