summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--patternutils/commands/patterncfg.py83
-rw-r--r--patternutils/commands/pmatch.py24
-rw-r--r--patternutils/commands/targs.py18
-rw-r--r--patternutils/commands/tmv.py22
-rw-r--r--patternutils/config.py2
-rw-r--r--patternutils/match.py2
-rw-r--r--patternutils/template.py2
-rw-r--r--patternutils/utils.py8
8 files changed, 91 insertions, 70 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py
index 6844efc..2d1fd4f 100644
--- a/patternutils/commands/patterncfg.py
+++ b/patternutils/commands/patterncfg.py
@@ -49,7 +49,7 @@ def list() -> None:
fg="red",
err=True,
)
- raise SystemExit
+ raise SystemExit(1)
@patterncfg.command(name="add", help="add new template file")
@@ -87,19 +87,23 @@ def remove(command: Optional[config.CommandNames], name: str) -> None:
files = config.find_templates(name, command)
if not files:
- click.secho("No files found!", fg="red", err=True)
- raise SystemExit
- elif len(files) != 1:
click.secho(
- "Multiple files found! Please specify the command from which to remove the file.",
+ f"No matching template files found for '{name}'.", fg="red", err=True
+ )
+
+ raise SystemExit(1)
+
+ if len(files) > 1:
+ click.secho(
+ "Multiple files found! Please specify the command to remove the correct file.",
fg="red",
err=True,
)
for f in files:
click.secho(f, fg="red", err=True)
- raise SystemExit
- else:
- os.remove(files[0])
+ raise SystemExit(1)
+
+ os.remove(files[0])
@patterncfg.command(name="view", help="view template file")
@@ -121,41 +125,43 @@ def view(name: str, command: config.CommandNames, color: bool) -> None:
files = config.find_templates(name, command)
if not files:
- click.secho("No files found!", fg="red", err=True)
- raise SystemExit
- elif len(files) != 1:
click.secho(
- "Multiple files found! Please specify the command from which to view the file.",
+ f"No matching template files found for '{name}'.", fg="red", err=True
+ )
+ raise SystemExit(1)
+
+ if len(files) > 1:
+ click.secho(
+ "Multiple files found! Please specify the command to view the correct file.",
fg="red",
err=True,
)
for f in files:
click.secho(f, fg="red", err=True)
- raise SystemExit
- else:
- filename = files[0]
+ raise SystemExit(1)
+
+ filename = files[0]
try:
with open(filename, encoding="utf-8") as fd:
text = fd.read()
except FileNotFoundError:
- click.secho("File not found!", fg="red", err=True)
- raise SystemExit
- else:
- if color and has_pygments:
- if filename.endswith("jinja2"):
- lexer = DjangoLexer
- else:
- lexer = Python3Lexer
- text = highlight(text, lexer(), TerminalFormatter())
- elif color and not has_pygments:
+ click.secho(f"File '{filename}' not found!", fg="red", err=True)
+ raise SystemExit(1)
+
+ if color:
+ if not has_pygments:
click.secho(
- "Pygments is not installed!. Cannot colorize output.",
+ "Pygments is not installed! Cannot colorize output.",
fg="red",
err=True,
)
- raise SystemExit
- click.echo(text[:-1])
+ raise SystemExit(1)
+
+ lexer = DjangoLexer if filename.endswith("jinja2") else Python3Lexer
+ text = highlight(text, lexer(), TerminalFormatter())
+
+ click.echo(text[:-1])
@patterncfg.command(name="edit", help="edit a template file")
@@ -171,18 +177,19 @@ def edit(name: str, command: config.CommandNames) -> None:
files = config.find_templates(name, command)
if not files:
- click.secho("No files found!", fg="red", err=True)
- raise SystemExit
- elif len(files) != 1:
click.secho(
- "Multiple files found! Please specify the command from which to edit the file.",
+ f"No matching template files found for '{name}'.", fg="red", err=True
+ )
+ raise SystemExit(1)
+
+ if len(files) > 1:
+ click.secho(
+ "Multiple files found! Please specify the command to edit the correct file.",
fg="red",
err=True,
)
- for f in files:
- click.secho(f, fg="red", err=True)
- raise SystemExit
- else:
- filename = files[0]
+ for file in files:
+ click.secho(file, fg="red", err=True)
+ raise SystemExit(1)
- click.edit(filename=filename)
+ click.edit(filename=files[0])
diff --git a/patternutils/commands/pmatch.py b/patternutils/commands/pmatch.py
index 44e55b4..166d941 100644
--- a/patternutils/commands/pmatch.py
+++ b/patternutils/commands/pmatch.py
@@ -23,7 +23,7 @@ def apply_regex(
try:
match = mat.apply(subject, regex_pattern)
except ValueError:
- click.secho(f"{subject} did not match", fg="red", err=True)
+ click.secho(f"'{subject}' did not match!", fg="red", err=True)
yield None
else:
match["_path"] = f.path
@@ -103,11 +103,17 @@ def pmatch(
try:
regex_pattern, _ = config.load_template(regex_pattern, "pmatch")
except FileNotFoundError:
- click.secho(f"Regex {regex_pattern} not found!", fg="red", err=True)
- raise SystemExit
+ click.secho(
+ f"Regex pattern '{regex_pattern}' not found!", fg="red", err=True
+ )
+ raise SystemExit(1)
except RuntimeError:
- click.secho("Multiple files found!", fg="red", err=True)
- raise SystemExit
+ click.secho(
+ f"Multiple template files found for '{regex_pattern}'!",
+ fg="red",
+ err=True,
+ )
+ raise SystemExit(1)
try:
regex_pattern_c = re.compile(regex_pattern)
@@ -115,7 +121,7 @@ def pmatch(
if verbose:
click.secho("Regular expression could not be compiled!", fg="red", err=True)
click.secho(e.msg, fg="red", err=True)
- raise SystemExit
+ raise SystemExit(1)
# Monkey patch walk function to include commandline options
walk_function = functools.partial(
@@ -123,13 +129,13 @@ def pmatch(
)
new_walk = itertools.chain.from_iterable(map(walk_function, directory))
- has_invalid_matches = False
matches = []
+ has_invalid_matches = False
for match in apply_regex(new_walk, regex_pattern_c, match_full_path):
if match is None:
has_invalid_matches = True
if stream and abort_on_no_match:
- raise SystemExit
+ raise SystemExit(1)
else:
if stream:
click.echo(utils.json_dumps(match, human_readable))
@@ -138,7 +144,7 @@ def pmatch(
if not stream:
if has_invalid_matches and abort_on_no_match:
- raise SystemExit
+ raise SystemExit(1)
click.echo(utils.json_dumps(matches, human_readable))
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py
index a1a159b..c0bfeeb 100644
--- a/patternutils/commands/targs.py
+++ b/patternutils/commands/targs.py
@@ -104,17 +104,21 @@ def targs(
try:
command, template_engine = config.load_template(command, "targs")
except FileNotFoundError:
- click.secho(f"Command {command} not found!", fg="red", err=True)
- raise SystemExit
+ click.secho(f"Command template '{command}' not found!", fg="red", err=True)
+ raise SystemExit(1)
except RuntimeError:
- click.secho("Multiple files found!", fg="red", err=True)
- raise SystemExit
+ click.secho(
+ f"Multiple template files found for '{command}'!",
+ fg="red",
+ err=True,
+ )
+ raise SystemExit(1)
try:
render = template.get_render_function(command, engine=template_engine)
except RuntimeError:
- click.secho("jinja2 not installed!", fg="red", err=True)
- raise SystemExit
+ click.secho("Jinja2 not installed!", fg="red", err=True)
+ raise SystemExit(1)
commands = [render(r) for r in data]
@@ -125,7 +129,7 @@ def targs(
except RuntimeError:
click.secho("Your edit is not parseable!", fg="red", err=True)
- raise SystemExit
+ raise SystemExit(1)
stdout = subprocess.DEVNULL if redirect_stdout else None
stderr = subprocess.DEVNULL if redirect_stderr else None
diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py
index 0ddbbd2..e9c4721 100644
--- a/patternutils/commands/tmv.py
+++ b/patternutils/commands/tmv.py
@@ -112,17 +112,21 @@ def tmv(
output_pattern, "tmv"
)
except FileNotFoundError:
- click.secho(f"Template {output_pattern} not found!", fg="red", err=True)
- raise SystemExit
+ click.secho(f"Template '{output_pattern}' not found!", fg="red", err=True)
+ raise SystemExit(1)
except RuntimeError:
- click.secho("Multiple files found!", fg="red", err=True)
- raise SystemExit
+ click.secho(
+ f"Multiple template files found for '{output_pattern}'!",
+ fg="red",
+ err=True,
+ )
+ raise SystemExit(1)
try:
render = template.get_render_function(output_pattern, engine=template_engine)
except RuntimeError:
- click.secho("jinja2 not installed!", fg="red", err=True)
- raise SystemExit
+ click.secho("Jinja2 not installed!", fg="red", err=True)
+ raise SystemExit(1)
new_filenames = dict((entry[key], render(**entry)) for entry in data)
@@ -132,19 +136,19 @@ def tmv(
new_filenames = temporary_filenames
except RuntimeError:
click.secho("Your edit is not parseable!", fg="red", err=True)
- raise SystemExit
+ raise SystemExit(1)
has_existing = False
non_existing = {}
for old, new in new_filenames.items():
if os.path.exists(new):
- click.secho(f"{new} does exist!", fg="red", err=True)
+ click.secho(f"File '{new}' already exists!", fg="red", err=True)
has_existing = True
else:
non_existing[old] = new
if has_existing and abort_on_path_exist:
- raise SystemExit
+ raise SystemExit(1)
files_to_move = new_filenames.items() if force else non_existing.items()
diff --git a/patternutils/config.py b/patternutils/config.py
index 0da5639..2ede863 100644
--- a/patternutils/config.py
+++ b/patternutils/config.py
@@ -66,7 +66,7 @@ def find_templates(
# CONFIG_PATH/COMMAND_NAME/TEMPLATE_NAME.python
extension = "(jinja2|python)"
else:
- raise ValueError(f"{command_name} is not known!")
+ raise ValueError(f"Unknown command '{command_name}'.")
regex = re.compile(
os.path.join(CONFIG_PATH, f"{path_sub_regex}", f"{template_name}.{extension}")
diff --git a/patternutils/match.py b/patternutils/match.py
index fc6a805..6120594 100644
--- a/patternutils/match.py
+++ b/patternutils/match.py
@@ -7,4 +7,4 @@ def apply(subject: str, regex_pattern: Pattern[str]) -> dict[str, str]:
groupdict["_subject"] = subject
return groupdict
else:
- raise ValueError("subject doesn't match regex pattern")
+ raise ValueError(f"Subject does not match the regex pattern: {subject}")
diff --git a/patternutils/template.py b/patternutils/template.py
index a05be83..d918e15 100644
--- a/patternutils/template.py
+++ b/patternutils/template.py
@@ -52,6 +52,6 @@ def get_render_function(
env_template = jinja_env.from_string(template)
return cast(Callable[..., str], env_template.render)
else:
- raise RuntimeError("jinja2 not installed!")
+ raise RuntimeError("Jinja2 is required but not installed!")
else:
return template.format
diff --git a/patternutils/utils.py b/patternutils/utils.py
index 88cad88..fed3046 100644
--- a/patternutils/utils.py
+++ b/patternutils/utils.py
@@ -34,7 +34,7 @@ def edit(data: dict[str, str] | list[str]) -> Optional[dict[str, str]]:
return None
except click.UsageError:
click.secho("The editor could not be launced!", fg="red", err=True)
- raise SystemExit
+ raise SystemExit(1)
def json_dumps(data: Any, human_readable: bool = False) -> str:
@@ -49,9 +49,9 @@ def json_dumps(data: Any, human_readable: bool = False) -> str:
def json_loads(data: Any) -> Any:
try:
return json.loads(data)
- except json.JSONDecodeError:
- click.secho("JSON not valid!", fg="red", err=True)
- raise SystemExit
+ except json.JSONDecodeError as e:
+ click.secho(f"Invalid JSON: {e}!", fg="red", err=True)
+ raise SystemExit(1)
def walk(