Debian Codegraph @

tasks/format.py

raw symbols SCIP
1 # -*- coding: ascii -*-
2 #
3 # Copyright 2018 - 2025
4 # Andr\xe9 Malo or his licensors, as applicable
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 """
18 Run code formatters
19 ~~~~~~~~~~~~~~~~~~~
20
21 """
22
23 import os as _os
24
25 import invoke as _invoke
26
27 from . import _features
28 from ._inv import tasks as _tasks
29
30 _FORMATTERS = []
31
32
33 @_tasks.optional(_FORMATTERS, _features.isort)
34 @_invoke.task()
35 def isort(ctx, diff=False):
36 """
37 Format python code using isort formatter
38
39 Parameters:
40 diff (bool):
41 Just emit a diff instead of changing files inline? Default: false
42 """
43 cmd = [ctx.which("isort")]
44 if diff:
45 cmd += ctx.s("--diff --color")
46 cmd += ctx.s("--settings-path pyproject.toml")
47 cmd += ["tasks"]
48 if ctx.get("package"):
49 cmd += [ctx.package]
50 if _os.path.exists(ctx.shell.native("tests")):
51 cmd += ["tests"]
52
53 with ctx.shell.root_dir():
54 ctx.run(ctx.c(cmd), echo=True)
55
56
57 @_tasks.optional(_FORMATTERS, _features.black)
58 @_invoke.task()
59 def black(ctx, diff=False):
60 """
61 Format python code using Black formatter
62
63 Parameters:
64 diff (bool):
65 Just emit a diff instead of changing files inline? Default: false
66 """
67 cmd = [ctx.which("black")]
68 if diff:
69 cmd += ctx.s("--diff --color")
70 cmd += ctx.s("--config pyproject.toml .")
71
72 with ctx.shell.root_dir():
73 ctx.run(ctx.c(cmd), echo=True)
74
75
76 @_tasks.optional(_FORMATTERS, _features.terraform)
77 @_invoke.task(name="tf")
78 def terraform(ctx):
79 """Format terraform code"""
80 base_cmd = [ctx.which("terraform"), "-chdir=" + ctx.paths.terraform]
81 with ctx.shell.root_dir():
82 ctx.run(ctx.c(base_cmd + ctx.s("fmt -recursive")), echo=True)
83
84
85 @_tasks.optional(_FORMATTERS)
86 @_invoke.task(name="all", default=True)
87 def all_(ctx):
88 """Run all formatters"""
89 _tasks.execute(ctx, *["format." + formatter for formatter in _FORMATTERS])