-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from olzhasar/config
Configuring via pyproject.toml
- Loading branch information
Showing
12 changed files
with
342 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,3 +139,6 @@ cython_debug/ | |
|
||
# vale | ||
vale_styles | ||
|
||
# tmp dir | ||
tests/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from argparse import Namespace | ||
from dataclasses import dataclass, field | ||
from pathlib import Path | ||
from typing import List, Mapping, Optional | ||
|
||
from .constants import DEFAULT_DELAY | ||
|
||
try: | ||
import tomlib | ||
except ImportError: | ||
from pip._vendor import tomli as tomlib | ||
|
||
|
||
CONFIG_SECTION_NAME = "pytest-watcher" | ||
CLI_FIELDS = {"now", "delay", "runner", "patterns", "ignore_patterns"} | ||
CONFIG_FIELDS = CLI_FIELDS | {"runner_args"} | ||
|
||
|
||
@dataclass | ||
class Config: | ||
path: Path | ||
now: bool = False | ||
delay: float = DEFAULT_DELAY | ||
runner: str = "pytest" | ||
runner_args: List[str] = field(default_factory=list) | ||
patterns: List[str] = field(default_factory=list) | ||
ignore_patterns: List[str] = field(default_factory=list) | ||
|
||
@classmethod | ||
def create( | ||
cls, namespace: Namespace, extra_args: Optional[List[str]] = None | ||
) -> "Config": | ||
instance = cls(path=namespace.path) | ||
|
||
config_path = find_config(namespace.path) | ||
if config_path: | ||
parsed = parse_config(config_path) | ||
instance._update_from_mapping(parsed) | ||
|
||
instance._update_from_namespace(namespace, extra_args or []) | ||
return instance | ||
|
||
def _update_from_mapping(self, data: Mapping): | ||
for key, val in data.items(): | ||
setattr(self, key, val) | ||
|
||
def _update_from_namespace( | ||
self, namespace: Namespace, runner_args: Optional[List[str]] | ||
): | ||
self.path = namespace.path | ||
|
||
for f in CLI_FIELDS: | ||
val = getattr(namespace, f) | ||
if val: | ||
setattr(self, f, val) | ||
|
||
if runner_args: | ||
self.runner_args = runner_args | ||
|
||
|
||
def find_config(cwd: Path) -> Optional[Path]: | ||
filename = "pyproject.toml" | ||
|
||
for path in (cwd, *cwd.parents): | ||
config_path = path.joinpath(filename) | ||
|
||
if config_path.exists(): | ||
return config_path | ||
|
||
return None | ||
|
||
|
||
def parse_config(path: Path) -> Mapping: | ||
with open(path, "rb") as f: | ||
try: | ||
data = tomlib.load(f) | ||
except Exception as exc: | ||
raise SystemExit(f"Error parsing pyproject.toml\n{exc}") | ||
|
||
try: | ||
data = data["tool"][CONFIG_SECTION_NAME] | ||
except KeyError: | ||
return {} | ||
|
||
for key in data.keys(): | ||
if key not in CONFIG_FIELDS: | ||
raise SystemExit( | ||
f"Error parsing pyproject.toml.\nUnrecognized option: {key}" | ||
) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
VERSION = "0.3.3" | ||
|
||
DEFAULT_DELAY = 0.2 | ||
LOOP_DELAY = 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.