Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved config #704

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions autohooks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def is_empty(self) -> bool:
"""
return not bool(self._config_dict)

def has_key(self, key: str) -> bool:
"""
Returns True if the key is in the config.
"""
return key in self._config_dict


def _gather_mode(mode_string: Optional[str]) -> Mode:
"""
Expand Down Expand Up @@ -134,6 +140,20 @@ def from_dict(config_dict: Dict[str, Any]) -> "AutohooksConfig":
)
return AutohooksConfig(settings=settings, config=config)

@staticmethod
def from_string(content: str) -> "AutohooksConfig":
"""
Load an AutohooksConfig from a string

Args:
content: The content of the config

Returns:
A new AutohooksConfig
"""
config_dict = tomlkit.loads(content)
return AutohooksConfig.from_dict(config_dict)

@staticmethod
def from_toml(toml_file: Path) -> "AutohooksConfig":
"""
Expand All @@ -145,8 +165,7 @@ def from_toml(toml_file: Path) -> "AutohooksConfig":
Returns:
A new AutohooksConfig
"""
config_dict = tomlkit.loads(toml_file.read_text())
return AutohooksConfig.from_dict(config_dict)
return AutohooksConfig.from_string(toml_file.read_text())


def load_config_from_pyproject_toml(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ def test_load_from_non_existing_toml_file(self):

self.assertEqual(len(config.get_pre_commit_script_names()), 0)

def test_load_from_string(self):
config = AutohooksConfig.from_string(
"""
[tool.autohooks]
pre-commit = ["foo", "bar"]
"""
)

self.assertTrue(config.has_autohooks_config())

self.assertListEqual(
config.get_pre_commit_script_names(), ["foo", "bar"]
)

def test_load_from_empty_string(self):
config = AutohooksConfig.from_string("")

self.assertFalse(config.has_autohooks_config())

self.assertEqual(config.get_pre_commit_script_names(), [])

def test_empty_config(self):
config = AutohooksConfig()

Expand Down Expand Up @@ -225,6 +246,12 @@ def test_config_point_syntax(self):
self.assertFalse(bar_config.is_empty())
self.assertEqual(bar_config.get_value("lorem"), "ipsum")

def test_has_key(self):
config = Config()
self.assertFalse(config.has_key("foo"))
config = Config({"foo": "bar"})
self.assertTrue(config.has_key("foo"))


if __name__ == "__main__":
unittest.main()
Loading