summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--patternutils/commands/patterncfg.py6
-rw-r--r--patternutils/commands/targs.py20
-rw-r--r--patternutils/commands/tln.py5
-rw-r--r--patternutils/commands/tmv.py2
-rw-r--r--patternutils/match.py3
-rw-r--r--patternutils/utils.py7
6 files changed, 17 insertions, 26 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py
index fa30e01..0115ed2 100644
--- a/patternutils/commands/patterncfg.py
+++ b/patternutils/commands/patterncfg.py
@@ -7,9 +7,9 @@ import click
from .. import config
try:
- from pygments import highlight
- from pygments.formatters import TerminalFormatter
- from pygments.lexers import DjangoLexer, Python3Lexer
+ from pygments import highlight # type: ignore
+ from pygments.formatters import TerminalFormatter # type: ignore
+ from pygments.lexers import DjangoLexer, Python3Lexer # type: ignore
except ImportError:
has_pygments = False
else:
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py
index 9fd0831..bcd2e8e 100644
--- a/patternutils/commands/targs.py
+++ b/patternutils/commands/targs.py
@@ -12,12 +12,7 @@ from .. import config, template, utils
def run_command(data: Dict[str, str], run: Callable[..., None], shell: bool,) -> None:
- if not shell:
- prepared_command = shlex.split(c) # type: Union[str, List[str]]
- else:
- prepared_command = c
-
- run(prepared_command)
+ run(shlex.split(command) if not shell else command)
@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")})
@@ -85,7 +80,7 @@ def targs(
if not stream:
data = json.load(read_from) # type: ignore
else:
- data = [json.loads(line) for line in read_from]
+ data = [json.loads(line) for line in read_from] # type: ignore
except json.JSONDecodeError:
click.secho("JSON not valid!", fg="red", err=True)
raise SystemExit
@@ -106,17 +101,15 @@ def targs(
click.secho("jinja2 not installed!", fg="red", err=True)
raise SystemExit
- commands = [render(r) for r in data]
+ commands = {r: render(r) for r in data}
if editor:
try:
commands = utils.edit(commands)
- except RuntimeErro:
+ except RuntimeError:
click.secho("Your edit is not parseable!", fg="red", err=True)
raise SystemExit
- max_procs = utils.max_procs() if max_procs == 0 else max_procs
-
stdout = subprocess.DEVNULL if redirect_stdout else None
stderr = subprocess.DEVNULL if redirect_stderr else None
patched_subprocess = functools.partial(
@@ -125,12 +118,15 @@ def targs(
patched_run_command = functools.partial(
run_command, run=patched_subprocess, shell=shell,
)
+
+ max_procs = utils.max_procs() if max_procs == 0 else max_procs
+
if max_procs == 1:
for r in commands:
patched_run_command(r)
else:
with multiprocessing.Pool(max_procs) as p:
- p.map(patched_run_command, commands)
+ p.map(patched_run_command, commands.values())
if __name__ == "__main__":
diff --git a/patternutils/commands/tln.py b/patternutils/commands/tln.py
index 295cda9..8f2bd2e 100644
--- a/patternutils/commands/tln.py
+++ b/patternutils/commands/tln.py
@@ -8,8 +8,7 @@ from .. import config, template, utils
def link_file(old_filename: str, new_filename: str, symbolic: bool) -> None:
- dirs = os.path.dirname(new_filename)
- if dirs:
+ if (dirs := os.path.dirname(new_filename)) :
os.makedirs(dirs, exist_ok=True)
if symbolic:
@@ -107,7 +106,7 @@ def tln(
if not stream:
data = json.load(read_from) # type: ignore
else:
- data = [json.loads(line) for line in read_from]
+ data = [json.loads(line) for line in read_from] # type: ignore
except json.JSONDecodeError:
click.secho("JSON not valid!", fg="red", err=True)
raise SystemExit
diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py
index 3e92342..4e93ff4 100644
--- a/patternutils/commands/tmv.py
+++ b/patternutils/commands/tmv.py
@@ -103,7 +103,7 @@ def tmv(
if not stream:
data = json.load(read_from) # type: ignore
else:
- data = [json.loads(line) for line in read_from]
+ data = [json.loads(line) for line in read_from] # type: ignore
except json.JSONDecodeError:
click.secho("JSON not valid!", fg="red", err=True)
raise SystemExit
diff --git a/patternutils/match.py b/patternutils/match.py
index 80f9553..1387001 100644
--- a/patternutils/match.py
+++ b/patternutils/match.py
@@ -2,8 +2,7 @@ from typing import Dict, Pattern
def apply(subject: str, regex_pattern: Pattern[str]) -> Dict[str, str]:
- match = regex_pattern.search(subject)
- if match is not None:
+ if (match := regex_pattern.search(subject)) is not None:
groupdict = match.groupdict()
groupdict["_subject"] = subject
return groupdict
diff --git a/patternutils/utils.py b/patternutils/utils.py
index a2decea..7e2e9e9 100644
--- a/patternutils/utils.py
+++ b/patternutils/utils.py
@@ -13,12 +13,9 @@ def max_procs() -> int:
def edit(data: Dict[str, str]) -> Dict[str, str]:
longest = len(max(data, key=len))
- temporary = [
- "{}\t{}\n".format(key.ljust(longest), value) for key, value in data.items()
- ]
- edited = click.edit("".join(temporary))
+ temporary = [f"{key.ljust(longext)}\t{value}\n" for key, value in data.items()]
- if edited is not None:
+ if (edited := click.edit("".join(temporary))) is not None:
new_data = dict()
for line in edited.split("\n"):
if line and "\t" in line: