def test_write_an_inverse_bool_entry(self): meta_config = { 'a_bool': { 'entry': 'an.example', 'type': 'inverse-bool', 'default': False } } self._tested.set_meta_config(meta_config) config = IniConfig() self._tested.write(dict(), config) self.assert_equal( 'the-default-value', config.get_or_default_value('an', 'example', 'the-default-value')) self._tested.write(dict(a_bool=False), config) self.assert_equal( 'yes', config.get_or_default_value('an', 'example', 'unexpected-default-value')) self._tested.write(dict(a_bool=True), config) self.assert_equal( 'no', config.get_or_default_value('an', 'example', 'unexpected-default-value'))
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_write_nodes(self): meta_config = { 'a_bool': { 'entry': 'an.example', 'type': 'inverse-bool', 'default': False }, 'a_node': { 'type': 'node', 'an_int': { 'entry': 'an.int_entry', 'type': 'int', 'default': 0 }, 'another_node': { 'type': 'node', 'a_string': { 'entry': 'a.string', 'type': 'string', 'default': 'default string-value' }, } } } self._tested.set_meta_config(meta_config) config = IniConfig() self._tested.write(dict(), config) self.assert_equal( 'the-default-value', config.get_or_default_value('an', 'example', 'the-default-value')) self._tested.write( dict(a_bool=False, a_node=dict(another_node=dict(a_string='some_value'))), config) self.assert_equal( 'yes', config.get_or_default_value('an', 'example', 'unexpected-default-value')) self.assert_equal( 'the-default-value', config.get_or_default_value('an', 'int_entry', 'the-default-value')) self.assert_equal( 'some_value', config.get_or_default_value('a', 'string', 'unexpected-default-value'))
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 test_write_an_int_entry(self): meta_config = { 'an_int': { 'entry': 'an.example', 'type': 'int', 'default': 42 } } self._tested.set_meta_config(meta_config) config = IniConfig() self._tested.write(dict(), config) self.assert_equal( 'the-default-value', config.get_or_default_value('an', 'example', 'the-default-value')) self._tested.write(dict(an_int=45), config) self.assert_equal( '45', config.get_or_default_value('an', 'example', 'unexpected-default-value'))
def test_write_a_string_entry_which_is_not_set(self): meta_config = { 'sample_text': { 'entry': 'an.example', 'type': 'string', 'default': 'something' } } self._tested.set_meta_config(meta_config) config = IniConfig() self._tested.write(dict(), config) self.assert_equal( 'the-default-value', config.get_or_default_value('an', 'example', 'the-default-value'))
def _custom_version(self, cfg: IniConfig) -> str: return cfg.get_or_default_value('core', 'custom_version', INITIAL_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))
def __load_int_item(self, meta_config_entry: typing.Dict, config: IniConfig): section, option = self._get_section_option(meta_config_entry) value = config.get_or_default_value(section, option, meta_config_entry['default']) return int(value)