Config: expose config rules parsing
Expose Config.parse_bool() and Config.parse_int() to parse text according to git-config rules.
This commit is contained in:
@@ -223,6 +223,25 @@ class Config(object):
|
|||||||
err = C.git_config_add_file_ondisk(self._config, to_str(path), level, force)
|
err = C.git_config_add_file_ondisk(self._config, to_str(path), level, force)
|
||||||
check_error(err)
|
check_error(err)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Methods to parse a string according to the git-config rules
|
||||||
|
#
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_bool(text):
|
||||||
|
res = ffi.new('int *')
|
||||||
|
err = C.git_config_parse_bool(res, to_str(text))
|
||||||
|
|
||||||
|
return res[0] != 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_int(text):
|
||||||
|
res = ffi.new('int64_t *')
|
||||||
|
err = C.git_config_parse_int64(res, to_str(text))
|
||||||
|
check_error(err)
|
||||||
|
|
||||||
|
return res[0]
|
||||||
|
|
||||||
#
|
#
|
||||||
# Static methods to get specialized version of the config
|
# Static methods to get specialized version of the config
|
||||||
#
|
#
|
||||||
|
@@ -179,5 +179,12 @@ class ConfigTest(utils.RepoTestCase):
|
|||||||
self.assertTrue('core.bare' in lst)
|
self.assertTrue('core.bare' in lst)
|
||||||
self.assertTrue(lst['core.bare'])
|
self.assertTrue(lst['core.bare'])
|
||||||
|
|
||||||
|
def test_parsing(self):
|
||||||
|
self.assertTrue(Config.parse_bool("on"))
|
||||||
|
self.assertTrue(Config.parse_bool("1"))
|
||||||
|
|
||||||
|
self.assertEqual(5, Config.parse_int("5"))
|
||||||
|
self.assertEqual(1024, Config.parse_int("1k"))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user