Add pre-commit integration with black, isort and flake8

This commit is contained in:
Ceda EI 2024-04-02 00:01:27 +05:30
parent 5c61863534
commit 55d12fdf59
7 changed files with 41 additions and 6 deletions

2
.flake8 Normal file
View File

@ -0,0 +1,2 @@
[flake8]
max-line-length = 88

18
.pre-commit-config.yaml Normal file
View File

@ -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

View File

@ -1,5 +1,6 @@
from typing import Optional
import sys import sys
from typing import Optional
import click import click
from rich.console import Console from rich.console import Console
@ -39,7 +40,9 @@ def _login_out(ctx: click.Context, login: bool):
variables_to_prompt = [] variables_to_prompt = []
for module in modules.values(): 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 = {} responses = {}
for var in variables_to_prompt: for var in variables_to_prompt:
@ -54,6 +57,7 @@ def _login_out(ctx: click.Context, login: bool):
else: else:
module.logout(responses) module.logout(responses)
@inni.command() @inni.command()
@click.pass_context @click.pass_context
def login(ctx: click.Context): def login(ctx: click.Context):

View File

@ -1,6 +1,6 @@
import os import os
from pathlib import Path
import tomllib import tomllib
from pathlib import Path
def default_config() -> Path: def default_config() -> Path:
@ -24,7 +24,7 @@ def read_config(path: str):
defaults = { defaults = {
"login": [], "login": [],
"logout": [], "logout": [],
"prompts": {} "prompts": {},
} }
if "inni" not in content: if "inni" not in content:

View File

@ -1,8 +1,10 @@
from inni.template import template_to_vars from inni.template import template_to_vars
class BaseModule: class BaseModule:
login_template_keys = () login_template_keys = ()
logout_template_keys = () logout_template_keys = ()
def __init__(self, config: dict): def __init__(self, config: dict):
self.config = config self.config = config
self.setUp() self.setUp()
@ -14,8 +16,14 @@ class BaseModule:
""" """
Returns the list of variables that the module expects Returns the list of variables that the module expects
""" """
def keys_to_vars(keys): 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 { return {
"login": keys_to_vars(self.login_template_keys), "login": keys_to_vars(self.login_template_keys),
"logout": keys_to_vars(self.logout_template_keys), "logout": keys_to_vars(self.logout_template_keys),

View File

@ -2,6 +2,7 @@ from jinja2 import Environment, FunctionLoader, meta
ENV = Environment(loader=FunctionLoader(lambda i: i)) ENV = Environment(loader=FunctionLoader(lambda i: i))
def template_to_vars(source: str) -> set[str]: def template_to_vars(source: str) -> set[str]:
""" """
Returns the variables present in the template 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): def render_template(source: str, **kwargs):
template = ENV.get_template(source) template = ENV.get_template(source)
return template.render(**kwargs) return template.render(**kwargs)

View File

@ -21,3 +21,6 @@ ipdb = "^0.13.13"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"
[tool.isort]
profile = "black"