Beispiel #1
0
    def settings(self, key=None, default=None):
        if self.get(self.atype):
            s = self.store().derive(self.atype)
        elif self.has_defaults():
            s = Store(parent=self.defaults)

        if key is None:
            return s.values()
        return s.get(key, default)
Beispiel #2
0
def run_cli():
    try:

        # load all components
        ext_mgr = ExtensionManager()
        ext_mgr.add_builtin_path()
        ext_mgr.load()

        # prepare local environment xii runs in
        paths.prepare_local_paths()

        # parse arguments
        parser = cli_arg_parser(ext_mgr)
        cli_args = parser.parse_args()

        # load variable store
        store = Store()

        # initialize variables
        init_runtime(store, cli_args)

        # parse definifition file
        defn = util.jinja_read(store.get("runtime/definition"), store)

        # construct component configurations
        definition.prepare_store(defn, store)

        # get command
        cmd = ext_mgr.get_command(cli_args.command)

        if not cmd:
            warn("Invalid command `{}`. Command not unknown.".format(
                cli_args.command))
            return 1

        command_arg_parser = cmd["class"].argument_parser()
        command_args = command_arg_parser.parse_args(cli_args.command_args)

        store.set("command/args", vars(command_args))

        instance = cmd["class"](command_args, cmd["templates"], store)
        prepare_command(instance, ext_mgr)

        return instance.run()
    except Interrupted:
        warn("interrupted... stopping immediately!")
        return 1

    except XiiError as e:
        it = iter(e.error())
        warn(e.error_title() + ": " + next(it))

        for line in it:
            warn(line)

        return 1
Beispiel #3
0
    def settings(self, key=None, default=None):
        """return validated settings

        This function will return the searched key or all settings if
        not `key` is specified.

        Args:
            key: Search key eg. "settingsdict/key1" or "foo" or None
            default: If key is not found in settings default is returned

        Returns:
            Value specified in `default` or None
        """
        if self.store().has_key(self.atype):
            s = self.store().derive(self.atype)
        elif self.has_defaults():
            s = Store(parent=self.defaults)
        else:
            raise error.Bug("Try to get default settings for {} but "
                            "no defaults are defined".format(self.entity()))
        if key is None:
            return s.values()
        return s.get(key, default)
Beispiel #4
0
    def settings(self, key=None, default=None):
        """return validated settings

        This function will return the searched key or all settings if
        not `key` is specified.

        Args:
            key: Search key eg. "settingsdict/key1" or "foo" or None
            default: If key is not found in settings default is returned

        Returns:
            Value specified in `default` or None
        """
        if self.store().has_key(self.atype):
            s = self.store().derive(self.atype)
        elif self.has_defaults():
            s = Store(parent=self.defaults)
        else:
            raise error.Bug("Try to get default settings for {} but "
                            "no defaults are defined".format(self.entity()))
        if key is None:
            return s.values()
        return s.get(key, default)
Beispiel #5
0
def run_cli():
    extension.load_builtin()
    parser = cli_arg_parser()
    try:

        # prepare local environment xii runs in
        paths.prepare_local_paths()

        # parse arguments
        cli_args = parser.parse_args()

        # load variable store
        store = Store()
        store.set("runtime/config", paths.local("config.yml"))

        store.set("runtime/definition", paths.find_definition_file(cli_args.deffile))

        # load defaults / home configuration into variable store
        config = util.yaml_read(store.get("runtime/config"))

        store.set("global", config)

        if cli_args.parallel is False:
            store.set("global/parallel", cli_args.parallel)

        # merge with arguments from commandline
        for define in [d.split("=") for d in cli_args.defines]:
            if len(define) != 2:
                warn("Invalid variable definition detected")
                warn("Use -Dscope/variable=value")
                return 1
            store.set(define[0], util.convert_type(define[1]))

        for envvar in filter(lambda x: x.startswith("XII_"), os.environ):
            print("define {} = {}".format(envvar[4:], os.environ[envvar]))
            store.set(envvar[4:], os.environ[envvar])

        # parse definifition file
        defn = util.jinja_read(store.get("runtime/definition"), store)

        # construct component configurations
        definition.prepare_store(defn, store)

        # run command
        instance = command.Register.get(cli_args.command, cli_args.command_args, store)
        # return exit code

        if not instance:
            warn("Invalid command `{}`. Command not unknown.".format(cli_args.command))
            return 1

        return instance.run()
    except Interrupted:
        warn("interrupted... stopping immediately!")
        return 1

    except XiiError as e:
        it = iter(e.error())
        warn(e.error_title() + ": " + next(it))

        for line in it:
            warn(line)

        if cli_args.debug:
            store.dump()
        return 1