From 55d12fdf592f9254403594528d1e44c86c677c07 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 2 Apr 2024 00:01:27 +0530 Subject: [PATCH] Add pre-commit integration with black, isort and flake8 --- .flake8 | 2 ++ .pre-commit-config.yaml | 18 ++++++++++++++++++ inni/cli.py | 8 ++++++-- inni/config.py | 4 ++-- inni/modules/base.py | 10 +++++++++- inni/template.py | 2 +- pyproject.toml | 3 +++ 7 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 .flake8 create mode 100644 .pre-commit-config.yaml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..2bcd70e --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 88 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..014a586 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/psf/black + rev: 24.3.0 + hooks: + - id: black + + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + name: isort (python) + + - repo: https://github.com/pycqa/flake8 + rev: 7.0.0 + hooks: + - id: flake8 diff --git a/inni/cli.py b/inni/cli.py index e29ae7e..b5ef35d 100644 --- a/inni/cli.py +++ b/inni/cli.py @@ -1,5 +1,6 @@ -from typing import Optional import sys +from typing import Optional + import click from rich.console import Console @@ -39,7 +40,9 @@ def _login_out(ctx: click.Context, login: bool): variables_to_prompt = [] for module in modules.values(): - variables_to_prompt += module.template_variables()["login" if login else "logout"] + variables_to_prompt += module.template_variables()[ + "login" if login else "logout" + ] responses = {} for var in variables_to_prompt: @@ -54,6 +57,7 @@ def _login_out(ctx: click.Context, login: bool): else: module.logout(responses) + @inni.command() @click.pass_context def login(ctx: click.Context): diff --git a/inni/config.py b/inni/config.py index 1702186..cdc6291 100644 --- a/inni/config.py +++ b/inni/config.py @@ -1,6 +1,6 @@ import os -from pathlib import Path import tomllib +from pathlib import Path def default_config() -> Path: @@ -24,7 +24,7 @@ def read_config(path: str): defaults = { "login": [], "logout": [], - "prompts": {} + "prompts": {}, } if "inni" not in content: diff --git a/inni/modules/base.py b/inni/modules/base.py index 88b584b..adb8521 100644 --- a/inni/modules/base.py +++ b/inni/modules/base.py @@ -1,8 +1,10 @@ from inni.template import template_to_vars + class BaseModule: login_template_keys = () logout_template_keys = () + def __init__(self, config: dict): self.config = config self.setUp() @@ -14,8 +16,14 @@ class BaseModule: """ Returns the list of variables that the module expects """ + def keys_to_vars(keys): - return tuple(var for key in keys for var in template_to_vars(self.config.get(key, ""))) + return tuple( + var + for key in keys + for var in template_to_vars(self.config.get(key, "")) + ) + return { "login": keys_to_vars(self.login_template_keys), "logout": keys_to_vars(self.logout_template_keys), diff --git a/inni/template.py b/inni/template.py index 5e54821..bdfc968 100644 --- a/inni/template.py +++ b/inni/template.py @@ -2,6 +2,7 @@ from jinja2 import Environment, FunctionLoader, meta ENV = Environment(loader=FunctionLoader(lambda i: i)) + def template_to_vars(source: str) -> set[str]: """ Returns the variables present in the template @@ -12,4 +13,3 @@ def template_to_vars(source: str) -> set[str]: def render_template(source: str, **kwargs): template = ENV.get_template(source) return template.render(**kwargs) - diff --git a/pyproject.toml b/pyproject.toml index 1617cd6..1c95ea0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,3 +21,6 @@ ipdb = "^0.13.13" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" + +[tool.isort] +profile = "black"