Skip to content

Commit

Permalink
Support writing to pyproject.toml
Browse files Browse the repository at this point in the history
Many Python projects support a pyproject.toml file, so its
important to support it as a special output file.

If pyproject.toml is passed, precli-init will update the file
with a tool.precli section and its configuration options.

This change also prompts to overwrite existing files. In addition
the enabled vs. disabled rules is now part of a list rather than
option on each rule section. This makes it easier to customize
since most users would prefer to maintain a list rather than
updating each section.

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Oct 26, 2024
1 parent cc3266f commit a4df545
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions precli/cli/init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Copyright 2024 Secure Sauce LLC
# SPDX-License-Identifier: BUSL-1.1
import argparse
import pathlib
import sys
from argparse import Namespace

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
import tomli_w

from precli.core import loader
Expand All @@ -24,26 +29,32 @@ def setup_arg_parser() -> Namespace:
help="output the config to given file",
)

args = parser.parse_args()

return args
return parser.parse_args()


def get_config() -> dict:
parsers = loader.load_extension(group="precli.parsers")
rules = [r for p in parsers.values() for r in p.rules.values()]

config = {"rule": {}}
enabled = []
disabled = []

for rule in rules:
config["rule"][rule.id] = {
"enabled": rule.config.enabled,
"level": rule.config.level,
}
if rule.config.enabled:
enabled.append(rule.id)
else:
disabled.append(rule.id)
if rule.config.parameters:
for parameter, value in rule.config.parameters.items():
config["rule"][rule.id][parameter] = value

config["enabled"] = enabled
config["disabled"] = disabled

return config


Expand All @@ -56,9 +67,34 @@ def main():

# Write to the given file
try:
# TODO: check if file already exists and prompt to overwrite
with open(args.output, "wb") as f:
tomli_w.dump(config, f)
path = pathlib.Path(args.output)

# Check if the file already exists and prompt for overwrite
if path.exists():
overwrite = input(
f"The file '{args.output}' already exists. Overwrite? (y/N): "
)
if overwrite.lower() != "y":
print("Operation cancelled.")
return 1

# Check if the target file is pyproject.toml and prepare the structure
if path.name == "pyproject.toml":
if path.exists():
with open(path, "rb") as f:
doc = tomllib.load(f)
doc.setdefault("tool", {}).setdefault("precli", {}).update(
config
)
else:
doc = {"tool": {"precli": config}}
else:
doc = config

# Write the configuration to the specified file
with open(path, "wb") as f:
tomli_w.dump(doc, f)

except OSError:
print(f"Error writing to file: {args.output}")
return 1
Expand Down

0 comments on commit a4df545

Please sign in to comment.