Skip to content

configparser

Base module for pyRevit config parsing.

Attributes

KEY_VALUE_TRUE = 'True' module-attribute

KEY_VALUE_FALSE = 'False' module-attribute

Classes

PyRevitConfigSectionParser(config_parser, section_name)

Bases: object

Config section parser object. Handle section options.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def __init__(self, config_parser, section_name):
    self._parser = config_parser
    self._section_name = section_name

Attributes

header property

Section header.

subheader property

Section sub-header e.g. Section.SubSection.

Functions

has_option(option_name)

Check if section contains given option.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def has_option(self, option_name):
    """Check if section contains given option."""
    return self._parser.has_option(self._section_name, option_name)
get_option(op_name, default_value=None)

Get option value or return default.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def get_option(self, op_name, default_value=None):
    """Get option value or return default."""
    try:
        return self.__getattr__(op_name)
    except Exception as opt_get_err:
        if default_value is not None:
            return default_value
        else:
            raise opt_get_err
set_option(op_name, value)

Set value of given option.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def set_option(self, op_name, value):
    """Set value of given option."""
    self.__setattr__(op_name, value)
remove_option(option_name)

Remove given option from section.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def remove_option(self, option_name):
    """Remove given option from section."""
    return self._parser.remove_option(self._section_name, option_name)
has_subsection(section_name)

Check if section has any subsections.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def has_subsection(self, section_name):
    """Check if section has any subsections."""
    return True if self.get_subsection(section_name) else False
add_subsection(section_name)

Add subsection to section.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def add_subsection(self, section_name):
    """Add subsection to section."""
    return self._parser.add_section(
        coreutils.make_canonical_name(self._section_name, section_name)
    )
get_subsections()

Get all subsections.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def get_subsections(self):
    """Get all subsections."""
    subsections = []
    for section_name in self._parser.sections():
        if section_name.startswith(self._section_name + '.'):
            subsec = PyRevitConfigSectionParser(self._parser, section_name)
            subsections.append(subsec)
    return subsections
get_subsection(section_name)

Get subsection with given name.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def get_subsection(self, section_name):
    """Get subsection with given name."""
    for subsection in self.get_subsections():
        if subsection.subheader == section_name:
            return subsection

PyRevitConfigParser(cfg_file_path=None)

Bases: object

Config parser object. Handle config sections and io.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def __init__(self, cfg_file_path=None):
    self._cfg_file_path = cfg_file_path
    self._parser = configparser.ConfigParser()
    if self._cfg_file_path:
        try:
            with codecs.open(self._cfg_file_path, 'r', 'utf-8') as cfg_file:
                self._parser.readfp(cfg_file)
        except (OSError, IOError):
            raise PyRevitIOError()
        except Exception as read_err:
            raise PyRevitException(read_err)

Functions

get_config_file_hash()

Get calculated unique hash for this config.

Returns:

Type Description
str

hash of the config.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def get_config_file_hash(self):
    """Get calculated unique hash for this config.

    Returns:
        (str): hash of the config.
    """
    with codecs.open(self._cfg_file_path, 'r', 'utf-8') as cfg_file:
        cfg_hash = coreutils.get_str_hash(cfg_file.read())

    return cfg_hash
has_section(section_name)

Check if config contains given section.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def has_section(self, section_name):
    """Check if config contains given section."""
    try:
        self.get_section(section_name)
        return True
    except Exception:
        return False
add_section(section_name)

Add section with given name to config.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def add_section(self, section_name):
    """Add section with given name to config."""
    self._parser.add_section(section_name)
    return PyRevitConfigSectionParser(self._parser, section_name)
get_section(section_name)

Get section with given name.

Raises:

Type Description
AttributeError

if section is missing

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def get_section(self, section_name):
    """Get section with given name.

    Raises:
        AttributeError: if section is missing
    """
    # check is section with full name is available
    if self._parser.has_section(section_name):
        return PyRevitConfigSectionParser(self._parser, section_name)

    # if not try to match with section_name.subsection
    # if there is a section_name.subsection defined, that should be
    # the sign that the section exists
    # section obj then supports getting all subsections
    for cfg_section_name in self._parser.sections():
        master_section = coreutils.get_canonical_parts(cfg_section_name)[0]
        if section_name == master_section:
            return PyRevitConfigSectionParser(self._parser,
                                              master_section)

    # if no match happened then raise exception
    raise AttributeError('Section does not exist in config file.')
remove_section(section_name)

Remove section from config.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def remove_section(self, section_name):
    """Remove section from config."""
    cfg_section = self.get_section(section_name)
    for cfg_subsection in cfg_section.get_subsections():
        self._parser.remove_section(cfg_subsection.header)
    self._parser.remove_section(cfg_section.header)
reload(cfg_file_path=None)

Reload config from original or given file.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def reload(self, cfg_file_path=None):
    """Reload config from original or given file."""
    try:
        with codecs.open(cfg_file_path \
                or self._cfg_file_path, 'r', 'utf-8') as cfg_file:
            self._parser.readfp(cfg_file)
    except (OSError, IOError):
        raise PyRevitIOError()
save(cfg_file_path=None)

Save config to original or given file.

Source code in pyrevitlib/pyrevit/coreutils/configparser.py
def save(self, cfg_file_path=None):
    """Save config to original or given file."""
    try:
        with codecs.open(cfg_file_path \
                or self._cfg_file_path, 'w', 'utf-8') as cfg_file:
            self._parser.write(cfg_file)
    except (OSError, IOError):
        raise PyRevitIOError()