def test_other_functions(self): # Test assumes it self.assert_false(os.path.exists(self.cfg_file)) cfg = IniConfig() cfg.open(self.cfg_file) # testing has self.assert_false(cfg.has('an', 'apple')) cfg.set('an', 'apple', 'is red') self.assert_equal(cfg.get('an', 'apple'), 'is red') self.assert_true(cfg.has('an', 'apple')) cfg.remove('an', 'apple') self.assert_false(cfg.has('an', 'apple')) # cfg must not be empty cfg.set('an', 'apple', 'is red') self.assert_equal(cfg.get('an', 'apple'), 'is red') # testing default return values self.assert_equal(cfg.get_sections(), ['an']) self.assert_equal(cfg.get_options('an'), ['apple']) self.assert_equal(cfg.get_options('death star'), []) self.assert_equal(cfg.get('siths', 'darth vader'), None) cfg.set('an apple', 'is red', '!%@#""' + "\nangry bird") self.assert_equal(cfg.get('an apple', 'is red'), '!%@#""' + "\nangry bird") self.assert_equal(cfg.get_or_default_value('an apple', 'is red', 'green'), '!%@#""' + "\nangry bird") self.assert_equal(cfg.get_or_default_value('an apple', 'is not red', 'green'), 'green')
def test_config_can_be_written_and_read(self): (fd, filename) = tempfile.mkstemp() os.close(fd) cfg = IniConfig() cfg.set('an', 'example', '4222222') cfg.write(filename) cfg = IniConfig() self.assert_is_none(cfg.get('an', 'example')) cfg.open(filename) self.assert_equal('4222222', cfg.get('an', 'example')) os.unlink(filename)
def _get_core_and_custom_version(project_dir: str) -> typing.Tuple[str, str]: cfg = IniConfig() cfg.open(os.path.join(project_dir, Project.CFG_FILE)) core_version = cfg.get('core', 'version') custom_version = cfg.get_or_default_value('core', 'custom_version', INITIAL_VERSION) return core_version, custom_version
def __load_inverse_bool_item(self, meta_config_entry: typing.Dict, config: IniConfig): section, option = self._get_section_option(meta_config_entry) if not config.has(section, option): return meta_config_entry['default'] else: value = config.get(section, option) return not convert_to_bool(value)
def _core_version(self, cfg: IniConfig) -> str: return cfg.get('core', 'version')
class Project: CFG_FILE = 'project.cfg' VERSION = 2 def __init__(self, name: str): self.name = name self._project_dir = f'{app_config().projectdir}/{name}' self.config = IniConfig() self.config.open(f'{self._project_dir}/{self.CFG_FILE}') self.ext = _project_ext_type(self) if _project_ext_type else None @property def project_dir(self) -> str: return self._project_dir @property def etc_dir(self) -> str: return f'{self._project_dir}/etc' @property def src_dir(self) -> str: return f'{self._project_dir}/src' @property def tmp_dir(self) -> str: return f'{self._project_dir}/tmp' @property def branch(self) -> str: return self.config.get('main', 'branch') @property def upstream_branch(self) -> str: return self.config.get('main', 'upstream_branch') @property def remote(self) -> str: return self.config.get('main', 'remote') @property def ticket_id(self) -> str: return self.config.get('main', 'ticket_id') def repo_dir(self, repo: str) -> str: return f'{self.src_dir}/{repo}' @property def repositories(self) -> typing.Iterable[str]: for entry in os.listdir(self.src_dir): if os.path.exists(f'{self.repo_dir(entry)}/.git'): yield entry @property def version(self) -> str: return self.config.get('core', 'version') @property def custom_version(self) -> str: return self.config.get_or_default_value('core', 'custom_version', INITIAL_VERSION) def as_dict(self) -> typing.Dict[str, typing.Any]: return dict(name=self.name, branch=self.branch, upstream=dict(remote=self.remote, branch=self.upstream_branch), ticket_id=self.ticket_id, repositories=list(self.repositories))