def test_empty_config_value(self): cr = GitConfigParser(fixture_path('git_config_with_empty_value'), read_only=True) assert cr.get_value('core', 'filemode'), "Should read keys with values" with self.assertRaises(cp.NoOptionError): cr.get_value('color', 'ui')
def test_empty_config_value(self): cr = GitConfigParser(fixture_path('git_config_with_empty_value'), read_only=True) assert cr.get_value('core', 'filemode'), "Should read keys with values" with self.assertRaises(cp.NoOptionError): cr.get_value('color', 'ui')
def _parse_gitmodules(dspath): gitmodule_path = opj(dspath, ".gitmodules") parser = GitConfigParser(gitmodule_path) mods = {} for sec in parser.sections(): modpath = parser.get_value(sec, 'path', default=0) if not modpath or not sec.startswith('submodule '): continue modpath = normpath(opj(dspath, modpath)) modprops = {'gitmodule_{}'.format(opt): parser.get_value(sec, opt) for opt in parser.options(sec) if not (opt.startswith('__') or opt == 'path')} modprops['gitmodule_name'] = sec[11:-1] mods[modpath] = modprops return mods
def test_base(self): path_repo = fixture_path("git_config") path_global = fixture_path("git_config_global") r_config = GitConfigParser([path_repo, path_global], read_only=True) assert r_config.read_only num_sections = 0 num_options = 0 # test reader methods assert r_config._is_initialized is False for section in r_config.sections(): num_sections += 1 for option in r_config.options(section): num_options += 1 val = r_config.get(section, option) val_typed = r_config.get_value(section, option) assert isinstance(val_typed, ( bool, int, float, ) + string_types) assert val assert "\n" not in option assert "\n" not in val # writing must fail with self.assertRaises(IOError): r_config.set(section, option, None) with self.assertRaises(IOError): r_config.remove_option(section, option) # END for each option with self.assertRaises(IOError): r_config.remove_section(section) # END for each section assert num_sections and num_options assert r_config._is_initialized is True # get value which doesnt exist, with default default = "my default value" assert r_config.get_value("doesnt", "exist", default) == default # it raises if there is no default though with self.assertRaises(cp.NoSectionError): r_config.get_value("doesnt", "exist")
def migrate(conf: GitConfigParser, repo: Repo, console: Console): if "bossman" not in conf.sections(): conf.add_section("bossman") console.print("Initializing git configuration [yellow]{}[/]...".format( conf._file_or_files)) conf_version = packaging.version.parse( conf.get_value("bossman", "version", "0.0.0")) for version, migration in MIGRATIONS.items(): if conf_version <= packaging.version.parse(version): migration(conf, repo, console)
def test_base(self): path_repo = fixture_path("git_config") path_global = fixture_path("git_config_global") r_config = GitConfigParser([path_repo, path_global], read_only=True) assert r_config.read_only num_sections = 0 num_options = 0 # test reader methods assert r_config._is_initialized is False for section in r_config.sections(): num_sections += 1 for option in r_config.options(section): num_options += 1 val = r_config.get(section, option) val_typed = r_config.get_value(section, option) assert isinstance(val_typed, (bool, int, float, ) + string_types) assert val assert "\n" not in option assert "\n" not in val # writing must fail with self.assertRaises(IOError): r_config.set(section, option, None) with self.assertRaises(IOError): r_config.remove_option(section, option) # END for each option with self.assertRaises(IOError): r_config.remove_section(section) # END for each section assert num_sections and num_options assert r_config._is_initialized is True # get value which doesnt exist, with default default = "my default value" assert r_config.get_value("doesnt", "exist", default) == default # it raises if there is no default though with self.assertRaises(cp.NoSectionError): r_config.get_value("doesnt", "exist")
def test_single_to_multiple(self): file_obj = self._to_memcache(fixture_path('git_config_multiple')) with GitConfigParser(file_obj, read_only=False) as cw: cw.add_value('section1', 'other_option1', 'other_value1a') cw.write() file_obj.seek(0) cr = GitConfigParser(file_obj, read_only=True) self.assertEqual(cr.get_value('section1', 'option1'), 'value1b') self.assertEqual(cr.get_values('section1', 'option1'), ['value1a', 'value1b']) self.assertEqual(cr.get_value('section1', 'other_option1'), 'other_value1a') self.assertEqual(cr.get_values('section1', 'other_option1'), ['other_value1', 'other_value1a']) self.assertEqual(cr.items('section1'), [('option1', 'value1b'), ('other_option1', 'other_value1a')]) self.assertEqual( cr.items_all('section1'), [('option1', ['value1a', 'value1b']), ('other_option1', ['other_value1', 'other_value1a'])])
def get_user_email(cfg: git.GitConfigParser) -> str: ''' Returns the user email specified in the .gitconfig file Parameters: cfg (git.GitConfigparser): A config parser object pointing at the user's .gitconfig file Returns: email (str): The user's email address ''' try: return cfg.get_value('user', 'email') except configparser.NoSectionError or configparser.NoOptionError: print('WARNING: Git user email not set.') return pyip.inputEmail(prompt='Please enter your email: ')
def get_user_name(cfg: git.GitConfigParser) -> str: ''' Returns the user name specified in the .gitconfig file Parameters: cfg (git.GitConfigParser): A config parser object pointing at the user's .gitconfig file Returns: name (str): The user's name ''' try: return cfg.get_value('user', 'name') except configparser.NoSectionError or configparser.NoOptionError: print('WARNING: Git user name not set.') return pyip.inputStr(prompt='Please enter your name: ')
def test_read_write(self): # writer must create the exact same file as the one read before for filename in ("git_config", "git_config_global"): file_obj = self._to_memcache(fixture_path(filename)) w_config = GitConfigParser(file_obj, read_only=False) w_config.read() # enforce reading assert w_config._sections w_config.write() # enforce writing # we stripped lines when reading, so the results differ assert file_obj.getvalue() self.assertEqual( file_obj.getvalue(), self._to_memcache(fixture_path(filename)).getvalue()) # creating an additional config writer must fail due to exclusive access self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only=False) # should still have a lock and be able to make changes assert w_config._lock._has_lock() # changes should be written right away sname = "my_section" oname = "mykey" val = "myvalue" w_config.add_section(sname) assert w_config.has_section(sname) w_config.set(sname, oname, val) assert w_config.has_option(sname, oname) assert w_config.get(sname, oname) == val sname_new = "new_section" oname_new = "new_key" ival = 10 w_config.set_value(sname_new, oname_new, ival) assert w_config.get_value(sname_new, oname_new) == ival file_obj.seek(0) r_config = GitConfigParser(file_obj, read_only=True) assert r_config.has_section(sname) assert r_config.has_option(sname, oname) assert r_config.get(sname, oname) == val w_config.release()
def test_read_write(self): # writer must create the exact same file as the one read before for filename in ("git_config", "git_config_global"): file_obj = self._to_memcache(fixture_path(filename)) w_config = GitConfigParser(file_obj, read_only=False) w_config.read() # enforce reading assert w_config._sections w_config.write() # enforce writing # we stripped lines when reading, so the results differ assert file_obj.getvalue() self.assertEqual(file_obj.getvalue(), self._to_memcache(fixture_path(filename)).getvalue()) # creating an additional config writer must fail due to exclusive access self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only=False) # should still have a lock and be able to make changes assert w_config._lock._has_lock() # changes should be written right away sname = "my_section" oname = "mykey" val = "myvalue" w_config.add_section(sname) assert w_config.has_section(sname) w_config.set(sname, oname, val) assert w_config.has_option(sname, oname) assert w_config.get(sname, oname) == val sname_new = "new_section" oname_new = "new_key" ival = 10 w_config.set_value(sname_new, oname_new, ival) assert w_config.get_value(sname_new, oname_new) == ival file_obj.seek(0) r_config = GitConfigParser(file_obj, read_only=True) assert r_config.has_section(sname) assert r_config.has_option(sname, oname) assert r_config.get(sname, oname) == val w_config.release()
def _parse_gitmodules(dspath): gitmodule_path = opj(dspath, ".gitmodules") parser = GitConfigParser(gitmodule_path) mods = {} for sec in parser.sections(): try: modpath = parser.get(sec, 'path') except Exception: lgr.debug("Failed to get '%s.path', skipping section", sec) continue if not modpath or not sec.startswith('submodule '): continue modpath = normpath(opj(dspath, modpath)) modprops = {'gitmodule_{}'.format(opt): parser.get_value(sec, opt) for opt in parser.options(sec) if not (opt.startswith('__') or opt == 'path')} modprops['gitmodule_name'] = sec[11:-1] mods[modpath] = modprops # make sure we let go of any resources held be the parser # we cannot rely on __del__ parser.release() return mods
def _parse_gitmodules(dspath): gitmodule_path = opj(dspath, ".gitmodules") parser = GitConfigParser(gitmodule_path) mods = {} for sec in parser.sections(): try: modpath = parser.get(sec, 'path') except Exception: lgr.debug("Failed to get '%s.path', skipping section", sec) continue if not modpath or not sec.startswith('submodule '): continue modpath = normpath(opj(dspath, modpath)) modprops = {'gitmodule_{}'.format(opt): parser.get_value(sec, opt) for opt in parser.options(sec) if not (opt.startswith('__') or opt == 'path')} modprops['gitmodule_name'] = sec[11:-1] mods[modpath] = modprops # make sure we let go of any resources held be the parser # we cannot rely on __del__ parser.release() return mods
def _git_user(): config = GitConfigParser() return config.get_value('user', 'name', '---')
def _git_email(): config = GitConfigParser() return config.get_value('user', 'email', '---')
def config(self,item): """ Get a variable from the git config file under the meta section """ config = GitConfigParser(environ['HOME']+"/.gitconfig") return config.get_value('meta', item)
def required(conf: GitConfigParser): parse = packaging.version.parse conf_version = parse(conf.get_value("bossman", "version", "0.0.0")) return any(conf_version <= parse(version) for version in MIGRATIONS)