def __init__(self, **kwargs): self.path = kwargs.get('path', None) self.file = kwargs.get('file', None) if self.path: if os.path.exists(self.path): config_path = self.detect_git_config(self.path) if os.path.exists(config_path): self.config_path = config_path self.config = ConfigFile.from_path(config_path) else: raise GitRepoNotFoundError(self.path) else: raise IOError(self.path) else: self.config = ConfigFile.from_file(self.file)
def __init__(self,**kwargs): self.path = kwargs.get('path', None) self.file = kwargs.get('file', None) if self.path: if os.path.exists(self.path): config_path = self.detect_git_config(self.path) if os.path.exists(config_path): self.config_path = config_path self.config = ConfigFile.from_path(config_path) else: raise GitRepoNotFoundError(self.path) else: raise IOError(self.path) else: self.config = ConfigFile.from_file(self.file)
def test_default_config(self): cf = self.from_file("""[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true """) self.assertEqual( ConfigFile({ ("core", ): { "repositoryformatversion": "0", "filemode": "true", "bare": "false", "logallrefupdates": "true" } }), cf)
def test_comment_after_variable(self): cf = self.from_file("[section]\nbar= foo # a comment\n") self.assertEqual(ConfigFile({("section", ): {"bar": "foo"}}), cf)
def test_comment_after_section(self): cf = self.from_file("[section] # foo\n") self.assertEqual(ConfigFile({("section", ): {}}), cf)
def test_comment_before_section(self): cf = self.from_file("# foo\n[section]\n") self.assertEqual(ConfigFile({("section", ): {}}), cf)
def test_empty_line_before_section(self): cf = self.from_file("\n[section]\n") self.assertEqual(ConfigFile({("section", ): {}}), cf)
def test_from_file_empty(self): cf = self.from_file("") self.assertEqual(ConfigFile(), cf)
def test_eq(self): self.assertEqual(ConfigFile(), ConfigFile())
def test_empty(self): ConfigFile()
def from_file(self, text): # text = bytes(text, 'UTF-8') text = text.encode("utf-8") return ConfigFile.from_file(BytesIO(text))
def test_write_to_file_subsection(self): c = ConfigFile() c.set(("branch", "blie"), "foo", "bar") f = BytesIO() c.write_to_file(f) self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
def test_write_to_file_section(self): c = ConfigFile() c.set(("core", ), "foo", "bar") f = BytesIO() c.write_to_file(f) self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
def test_write_to_file_empty(self): c = ConfigFile() f = BytesIO() c.write_to_file(f) self.assertEqual(b"", f.getvalue())