Example #1
0
    def __init__(self):
        if not Kentauros.initialised:
            Kentauros.cli = CLIArgs()
            Kentauros.debug = get_env_debug() or self.cli.get_debug()
            Kentauros.verby = __smaller_int__(get_env_verby(), self.cli.get_verby())
            Kentauros.conf = ktr_get_conf()
            Kentauros.packages = OrderedDict()

            Kentauros.initialised = True
Example #2
0
def ktr_conf_from_file(conf_type: KtrConfType, file_path: str, errmsg: str=None) -> KtrConf:
    """
    This factory method is used to create a :py:class:`KtrConf` instance with values that are read
    from an `ini`-style configuration file and store the results in the instance's attributes.

    It also stores the :py:class:`ConfigParser` object and file path, in
    case they are needed later.

    Arguments:
        KtrConfType conf_type:  type of configuration
        str file_path:          path to configuration file
        str errmsg:             error message that will be printed in case the file is not found

    Returns:
        KtrConf:                resulting configuration object
    """

    if not os.path.exists(file_path):
        if get_env_debug():
            print(LOG_PREFIX1 + errmsg, flush=True)
        return None

    config = ConfigParser()

    successful = config.read(file_path)
    if not successful:
        if errmsg:
            print(LOG_PREFIX1 + errmsg, flush=True)
            return None

    if "main" not in config.sections():
        print(LOG_PREFIX1 + "Configuration file invalid (no 'main' section).", flush=True)
        print(LOG_PREFIX2 + file_path, flush=True)
        return None

    basedir = None

    try:
        basedir = config.get("main", "basedir")
    except NoOptionError:
        return None
    finally:
        if basedir == "":
            basedir = None

    result = KtrConf(conf_type=conf_type, basedir=basedir)

    if not result.validate():
        print(LOG_PREFIX1 + "Something went wrong during configuration verification.", flush=True)
        return None
    else:
        return result
Example #3
0
    def from_file(self, file_path: str, errmsg: str=None):
        """
        This method is used to read values from an `ini`-style configuration file and store the
        results in the instance's attributes.

        It also stores the :py:class:`ConfigParser` object and file path, in case they were needed
        along the line.

        Arguments:
            str file_path:  path to configuration file
            str errmsg:     error message that will be printed in case the file is not found

        Returns:
            KtrConf:        returns instance itself or *None* if file is not found.
        """

        if not os.path.exists(file_path):
            if get_env_debug():
                print(LOG_PREFIX1 + errmsg, flush=True)
            return None

        self.file = file_path
        self.conf = ConfigParser()

        successful = self.conf.read(self.file)
        if not successful:
            if errmsg:
                print(LOG_PREFIX1 + errmsg, flush=True)
            return None

        if "main" not in self.conf.sections():
            return None

        try:
            self.basedir = os.path.abspath(__replace_home__(self.conf.get("main", "basedir")))
        except NoOptionError:
            self.basedir = None
        finally:
            if self.basedir == "":
                self.basedir = None

        if not self.validate():
            print(LOG_PREFIX1 + "Not all necessary configuration options have been set.",
                  flush=True)
            print(LOG_PREFIX2 + self.file, flush=True)
            return None
        else:
            return self