Skip to content

Make tasks.py simpler #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# type: ignore
import shutil
import subprocess

import toml
from invoke import Context, task
from rich.console import Console

import monkey_patch_invoke as _ # noqa: F401

console = Console()


def get_pep8_compliant_name(project_name: str) -> str:
return project_name.replace('-', '_')
Expand All @@ -22,14 +18,6 @@ def get_project_path():
return project_name


def execute_command(command: str) -> dict[str, str]:
with subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
stdout, stderr = process.communicate()
if process.returncode != 0:
raise Exception(stderr.decode())
return {'command': command, 'result': stdout.decode()}


@task
def run(context: Context):
context.run(f'python {get_project_path()}/main.py', pty=True)
Expand All @@ -41,23 +29,15 @@ def test(context: Context):


@task
def format_code(context: Context, verbose: bool = False) -> None:
def format_code(context: Context, verbose: bool = False):
commands = [
f'pautoflake {get_project_path()}',
f'ruff --fix {get_project_path()}',
f'yapf --in-place --recursive --parallel {get_project_path()}',
]
results: list[dict[str, str]] = []
with console.status('[bold green] Formatting code...'):
for command in commands:
results.append(execute_command(command))

if verbose:
for result in results:
console.print(f'$ {result["command"]}')
console.print(result['result'])

console.print('[bold green]Success[/bold green]')
for command in commands:
context.run(command, pty=True)


@task
Expand All @@ -69,7 +49,6 @@ def check(context: Context):
@task
def check_code_style(context: Context):
commands = [
f'pautoflake {get_project_path()} --check',
f'ruff {get_project_path()}',
f'yapf --diff --recursive --parallel {get_project_path()}',
]
Expand Down