Skip to content

Commit

Permalink
Add --config option
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Oct 19, 2024
1 parent 31a49bf commit 579c3cc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
10 changes: 10 additions & 0 deletions gersemi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ def create_argparser():
[default: cache enabled]
""",
)
control_configuration_group.add_argument(
"--config",
dest="configuration_file",
type=pathlib.Path,
default=None,
help=f"""
{control_conf_doc["configuration_file"]}
[default: omitted]
""",
)

parser.add_argument(
dest="sources",
Expand Down
2 changes: 1 addition & 1 deletion gersemi/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
__license__ = "MPL 2.0"
__title__ = "gersemi"
__url__ = "https://github.com/BlankSpruce/gersemi"
__version__ = "0.16.2"
__version__ = "0.17.0-rc1"
38 changes: 24 additions & 14 deletions gersemi/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ class ListExpansion(EnumWithMetadata):
class OutcomeConfiguration:
"""
These arguments control how gersemi formats source code.
Values for these arguments can be stored in .gersemirc file which should be
placed in directory next to the source file or any parent directory with
priority on closest configuration file.
Values for these arguments can be stored in .gersemirc file which can be
placed in directory next to the source file or any parent directory.
The highest priority has file provided through --config, then file closest
to the source file, then file in parent directory etc. until root of file
system is reached.
Arguments from command line can be used to override parts of that stored
configuration or supply them in absence of configuration file.
Precedence: (command line arguments) > (.gersemirc values) > (defaults)
Precedence: (command line arguments) > (configuration file) > (defaults)
"""

line_length: int = field(
Expand Down Expand Up @@ -192,7 +194,7 @@ def summary(self):
class ControlConfiguration:
"""
These arguments control how gersemi operates rather than how it formats source code.
Values for these options are not read from .gersemirc file.
Values for these options are not read from configuration file.
Default values are used when the arguments aren't supplied.
Precedence: (command line arguments) > (defaults)
"""
Expand All @@ -202,7 +204,6 @@ class ControlConfiguration:
metadata=dict(
title="Quiet",
description="Skip printing non-error messages to stderr.",
command_line_only=True,
),
)

Expand All @@ -211,7 +212,6 @@ class ControlConfiguration:
metadata=dict(
title="Colorized diff",
description="If --diff is selected showed diff is colorized.",
command_line_only=True,
),
)

Expand All @@ -226,7 +226,6 @@ class ControlConfiguration:
files in parallel.
"""
),
command_line_only=True,
),
)

Expand All @@ -240,7 +239,21 @@ class ControlConfiguration:
to be formatted to speed up execution.
"""
),
command_line_only=True,
),
)

configuration_file: Optional[Path] = field(
default=None,
metadata=dict(
title="Configuration file",
description=doc(
"""
Path to configuration file. When present this configuration
file will be used for determining configuration for all sources
instead of automatically found configuration files closest to
each of the sources.
"""
),
),
)

Expand Down Expand Up @@ -390,12 +403,9 @@ def override_with_args(configuration, args):


def make_outcome_configuration(
path, args
configuration_file, args
) -> Tuple[OutcomeConfiguration, NotSupportedKeys]:
outcome, not_supported_keys = load_configuration_from_file(
find_closest_dot_gersemirc(path)
)

outcome, not_supported_keys = load_configuration_from_file(configuration_file)
return override_with_args(outcome, args), not_supported_keys


Expand Down
9 changes: 7 additions & 2 deletions gersemi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from gersemi.configuration import (
Configuration,
ControlConfiguration,
find_closest_dot_gersemirc,
make_control_configuration,
make_outcome_configuration,
MaxWorkers,
Expand Down Expand Up @@ -305,7 +306,8 @@ def _inform_about_not_supported_keys(self, item: NotSupportedKeys):

def __call__(self, path: Path) -> Configuration:
outcome_configuration, not_supported_keys = make_outcome_configuration(
path, self.args
configuration_file=find_closest_dot_gersemirc(path),
args=self.args,
)
self._inform_about_not_supported_keys(not_supported_keys)
return Configuration(control=self.control, outcome=outcome_configuration)
Expand All @@ -314,8 +316,11 @@ def __call__(self, path: Path) -> Configuration:
def split_files_by_configuration(
paths: Iterable[Path], args: argparse.Namespace, control: ControlConfiguration
):
result = defaultdict(list)
helper = ConfigurationHelper(args, control)
if control.configuration_file is not None:
return {helper(control.configuration_file): paths}

result = defaultdict(list)
for path in paths:
result[helper(path)].append(path)

Expand Down
32 changes: 32 additions & 0 deletions tests/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,35 @@ def test_disable_formatting(app, testfiles):
assert app("--check", not_formatted) == fail()
assert app("--enable-formatting", "--check", not_formatted) == fail()
assert app("--disable-formatting", "--check", not_formatted) == success()


def test_config_parameter(app, testfiles):
base = testfiles
target = base / "directory_with_formatted_files"
configuration_file_elsewhere = base / "directory_with_not_formatted_files"
configuration_file = configuration_file_elsewhere / ".gersemirc"
config = ["--config", configuration_file_elsewhere / ".gersemirc"]

assert app("--check", target) == success(stderr="")
assert app("-l", 100, "--check", target) == fail()

with create_dot_gersemirc(where=configuration_file_elsewhere, line_length=100):
assert app("--check", target) == success(stderr="")
assert app("-l", 100, "--check", target) == fail()

assert app(*config, "--check", target) == fail()
assert app(*config, "-l", 80, "--check", target) == success(stderr="")

with create_dot_gersemirc(
where=configuration_file_elsewhere, line_length=100, color=True, kambei=314
):
assert app("--check", target) == success(stderr="")
assert app("-l", 100, "--check", target) == fail()

assert app(*config, "--check", target) == fail()
assert app(*config, "-l", 80, "--check", target) == success(
# pylint: disable=line-too-long
stderr=f"""{configuration_file}: these options are supported only through command line: color
{configuration_file}: these options are not supported: kambei
"""
)

0 comments on commit 579c3cc

Please sign in to comment.