pre-commit-html 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,21 @@
1
1
  """Module for formatting pre-commit results into an HTML file."""
2
2
 
3
- from pathlib import Path
3
+ import os
4
4
  import subprocess
5
- from jinja2 import Template, Environment, FileSystemLoader # noqa: F401
5
+ from pathlib import Path
6
+
7
+ from jinja2 import Environment, FileSystemLoader, Template # noqa: F401
6
8
 
7
- env = Environment(
8
- loader=FileSystemLoader(Path(__file__).parent.resolve().joinpath("site/templates"))
9
+ from pre_commit_html.utils import generate_editor_links
10
+
11
+ env = Environment( # noqa:S701
12
+ loader=FileSystemLoader(
13
+ Path(
14
+ __file__,
15
+ )
16
+ .parent.resolve()
17
+ .joinpath("site/templates")
18
+ ),
9
19
  )
10
20
 
11
21
  render_template = env.get_template
@@ -13,17 +23,23 @@ render_template = env.get_template
13
23
  path_result_content = Path("result_pre_commit.html")
14
24
 
15
25
 
16
- class PreCommitParser:
26
+ class PreCommitToHTML:
17
27
  """Class to parse and format pre-commit results."""
18
28
 
19
- @classmethod
20
- def run_pre_commit(cls) -> str:
29
+ def __init__(self, ide: str = "VS Code") -> None:
30
+ """Initialize the PreCommitToHTML class."""
31
+ self.ide = ide
32
+ self.pre_commit_html()
33
+
34
+ def run_pre_commit(self) -> str:
21
35
  """Run the pre-commit command and capture its output.
22
36
 
23
37
  Returns:
24
38
  str: The output of the pre-commit command.
39
+
25
40
  Raises:
26
41
  subprocess.CalledProcessError: If the pre-commit command fails.
42
+
27
43
  """
28
44
  try:
29
45
  result = subprocess.run(
@@ -35,14 +51,13 @@ class PreCommitParser:
35
51
  except subprocess.CalledProcessError as e:
36
52
  return f"Erro ao executar pre-commit: {e.stderr}"
37
53
 
38
- @classmethod
39
- def pre_commit_html(cls) -> None:
54
+ def pre_commit_html(self) -> None:
40
55
  """Format the pre-commit output into an HTML file.
41
56
 
42
57
  This method runs the pre-commit command, processes its output, and writes the formatted
43
58
  results into an HTML file.
44
59
  """
45
- content = cls.run_pre_commit()
60
+ content = self.run_pre_commit()
46
61
 
47
62
  content_splitlines = content.splitlines()
48
63
 
@@ -58,19 +73,37 @@ class PreCommitParser:
58
73
 
59
74
  if len(h3_file.split(":")) == 4:
60
75
  path_code_file = h3_file.split(":")[0]
76
+ path_file_link = path_code_file
77
+
61
78
  line_code = h3_file.split(":")[1]
62
79
  column_code = h3_file.split(":")[2]
63
80
  message = h3_file.split(":")[3]
64
81
 
82
+ try:
83
+ workdir = Path.cwd().resolve()
84
+ path_code_file = h3_file.split(":")[0]
85
+
86
+ path_file_link = generate_editor_links(
87
+ workdir.joinpath(str(path_code_file)), int(line_code), int(column_code)
88
+ )[self.ide]
89
+ except Exception as e:
90
+ print( # noqa:T201
91
+ f"""
92
+ ====================================
93
+ Error to generate link to file editor
94
+ File: {path_code_file}
95
+ Exception: {e}
96
+ ====================================
97
+ """
98
+ )
99
+
65
100
  ruff_ref = message.split(" ")[1]
66
101
 
67
102
  code_error.append(
68
- "".join(
69
- (
70
- f'<h3>File: <a href="./{path_code_file}:{line_code}:',
71
- f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
72
- )
73
- )
103
+ "".join((
104
+ f'<h3>File: <a href="{path_file_link}',
105
+ f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
106
+ ))
74
107
  )
75
108
  code_error.append(
76
109
  f'<p>Error: <a href="https://docs.astral.sh/ruff/rules/#{ruff_ref}">{ruff_ref}</a>{message}</p>'
@@ -79,14 +112,10 @@ class PreCommitParser:
79
112
  elif "\\" in line and ":" in line and len(code_part) > 0:
80
113
  h3_file = line.replace("\\", "/")
81
114
 
82
- code_part_html = render_template("code_part.jinja").render(
83
- code_part=code_part
84
- )
115
+ code_part_html = render_template("code_part.jinja").render(code_part=code_part)
85
116
 
86
117
  code_error.append(code_part_html)
87
- to_html = render_template("code_error.jinja").render(
88
- code_error=code_error
89
- )
118
+ to_html = render_template("code_error.jinja").render(code_error=code_error)
90
119
 
91
120
  html_content.append(to_html)
92
121
 
@@ -101,12 +130,10 @@ class PreCommitParser:
101
130
  ruff_ref = message.split(" ")[1]
102
131
 
103
132
  code_error.append(
104
- "".join(
105
- (
106
- f'<h3>File: <a href="./{path_code_file}:{line_code}:',
107
- f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
108
- )
109
- )
133
+ "".join((
134
+ f'<h3>File: <a href="./{path_code_file}:{line_code}:',
135
+ f'{column_code}">{path_code_file}:{line_code}:{column_code}</a></h3>',
136
+ ))
110
137
  )
111
138
  code_error.append(
112
139
  f'<p>Error: <a href="https://docs.astral.sh/ruff/rules/#{ruff_ref}">{ruff_ref}</a>{message}</p>'
@@ -117,16 +144,11 @@ class PreCommitParser:
117
144
  code_part.append(line)
118
145
 
119
146
  if path_result_content.exists():
120
- path_result_content.unlink()
147
+ os.remove(str(path_result_content))
121
148
 
122
149
  path_result_content.touch()
123
150
 
124
- html_content = render_template("html_content.jinja").render(
125
- content=html_content
126
- )
151
+ html_content = render_template("html_content.jinja").render(content=html_content)
127
152
 
128
153
  with open(path_result_content, "w", encoding="utf-8") as f:
129
154
  f.write(html_content)
130
-
131
-
132
- PreCommitParser.pre_commit_html()
@@ -1,15 +1,6 @@
1
- """This module contains the main function to run the pre-commit formatter."""
2
-
3
- from . import PreCommitParser
4
-
5
-
6
- def main() -> None:
7
- """Main function to run the pre-commit formatter.
8
-
9
- This function calls the pre_commit_html method from the PreCommitParser class.
10
- """
11
- PreCommitParser.pre_commit_html()
1
+ """Module containing the main function to run the pre-commit formatter."""
12
2
 
3
+ from pre_commit_html.main import main
13
4
 
14
5
  if __name__ == "__main__":
15
6
  SystemExit(main())
@@ -0,0 +1,18 @@
1
+ """Main module for the pre-commit-html package."""
2
+
3
+ import argparse
4
+ import sys
5
+ from typing import Sequence
6
+
7
+ from pre_commit_html import PreCommitToHTML
8
+
9
+
10
+ def main(argv: Sequence[str] | None = None) -> None:
11
+ """Run the pre-commit formatter."""
12
+ argv = argv if argv is not None else sys.argv[1:]
13
+ parser = argparse.ArgumentParser(prog="pre-commit-html")
14
+
15
+ parser.add_argument("-i", "--IDE", type=str, help="The IDE to open the file in.", default="VS Code")
16
+
17
+ args = parser.parse_args(argv)
18
+ PreCommitToHTML(ide=args.IDE)
@@ -0,0 +1,52 @@
1
+ """Utility functions for generating file editor links.
2
+
3
+ This module provides functionality for constructing URLs that open files in various
4
+ editors. It supports multiple editor URL schemes.
5
+ """
6
+
7
+ import pathlib
8
+ from urllib.parse import quote
9
+
10
+
11
+ def generate_editor_links(path: str, line: int = None, column: int = None) -> dict[str, str]:
12
+ """Generate multiple editor links for opening files.
13
+
14
+ Args:
15
+ path (str): The file path.
16
+ line (int, optional): The line number in the file.
17
+ column (int, optional): The column number in the file.
18
+
19
+ Returns:
20
+ dict[str, str]: A dictionary containing editor-specific URL schemes.
21
+
22
+ """
23
+ file_path = pathlib.Path(path).resolve()
24
+ file_uri = file_path.as_uri() # file:// format
25
+ escaped_path = quote(str(file_path)) # For editors like VS Code, JetBrains, Sublime, Atom, etc.
26
+
27
+ # Formatted line and column strings
28
+ line_str = f":{line}" if line else ""
29
+ column_str = f":{column}" if column else ""
30
+
31
+ # Editor-specific URL formats
32
+ links = {
33
+ "file://": f"{file_uri}{f'#L{line}' if line else ''}{f'C{column}' if column else ''}",
34
+ "VS Code": f"vscode://file/{escaped_path}{line_str}{column_str}",
35
+ "Nova (macOS)": (
36
+ f"nova://open?path={escaped_path}{f'&line={line}' if line else ''}{f'&column={column}' if column else ''}"
37
+ ),
38
+ "Sublime Text": (
39
+ f"subl://open?url=file://{escaped_path}"
40
+ f"{f'&line={line}' if line else ''}"
41
+ f"{f'&column={column}' if column else ''}"
42
+ ),
43
+ "Atom": (
44
+ f"atom://core/open/file?filename={escaped_path}"
45
+ f"{f'&line={line}' if line else ''}"
46
+ f"{f'&column={column}' if column else ''}"
47
+ ),
48
+ "BBEdit (macOS)": f"bbedit://open?path={escaped_path}{f'&line={line}' if line else ''}",
49
+ "Vim/Nvim": f"vim://open?url=file://{escaped_path}{f'&line={line}' if line else ''}",
50
+ }
51
+
52
+ return links
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pre_commit_html
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Format results from pre-commit cmd to HTML file
5
5
  License: MIT
6
6
  Author: Robotz213
@@ -33,7 +33,7 @@ This project is designed to run pre-commit hooks and format the results into an
33
33
 
34
34
  ## How It Works
35
35
 
36
- 1. **Run Pre-Commit Hooks**: The `PreCommitParser` class runs the pre-commit hooks using the `pre-commit run --all-files` command.
36
+ 1. **Run Pre-Commit Hooks**: The `PreCommitToHTML` class runs the pre-commit hooks using the `pre-commit run --all-files` command.
37
37
  2. **Parse Output**: The output from the pre-commit hooks is parsed to extract relevant information such as file paths, line numbers, column numbers, and error messages.
38
38
  3. **Generate HTML**: The parsed information is then formatted into an HTML file using Jinja2 templates. The HTML file includes links to the relevant lines in the source code and references to the documentation for the errors.
39
39
 
@@ -58,7 +58,7 @@ This will generate an HTML file named `result_pre_commit.html` in the project di
58
58
  ## Project Structure
59
59
 
60
60
  - `pre_commit_html/__main__.py`: Contains the main function to run the pre-commit formatter.
61
- - `pre_commit_html/__init__.py`: Contains the `PreCommitParser` class which handles running the pre-commit hooks and formatting the output.
61
+ - `pre_commit_html/__init__.py`: Contains the `PreCommitToHTML` class which handles running the pre-commit hooks and formatting the output.
62
62
  - `site/templates/`: Contains the Jinja2 templates used to generate the HTML file.
63
63
 
64
64
  ## Templates
@@ -0,0 +1,13 @@
1
+ pre_commit_html/__init__.py,sha256=Jqu0BfEfeVI69waWiCRmN-fM4nubQFXJbVEnvngTdcI,5233
2
+ pre_commit_html/__main__.py,sha256=3xoQ6wvPiHkruP_D36-H3BHkkVq4601cMGP89eLWV5c,165
3
+ pre_commit_html/main.py,sha256=G9ShTj1bNFGzPt7v5y2MuB5lxXDa4IyWa4Zx23yFBS0,538
4
+ pre_commit_html/site/nothing,sha256=FgEEp-DgpF7jCB5mTtiWk_6yQcNz6q68-hqvGHulRbA,15
5
+ pre_commit_html/site/templates/code_error.jinja,sha256=Nca684pgieEMbgrHFEl7acf26-e5LpGQIYFMDfyHoZ0,52
6
+ pre_commit_html/site/templates/code_part.jinja,sha256=ZmTKj9Ev1_yDoUQJlZ1RQZb1-VYDOSkN2sNvwbnfng0,111
7
+ pre_commit_html/site/templates/html_content.jinja,sha256=9dmoVLZ-BmYEf27j_52F6md23WS_5JshkTqO9VpRvX0,1695
8
+ pre_commit_html/utils.py,sha256=4L0RHabgXxTFARKJR8EArB7SW00m8zCgcyHwIhEvt-8,1968
9
+ pre_commit_html-0.1.4.dist-info/LICENSE,sha256=3V9deIJXT--GYubOVNXj1madYOvijNEIn03Yh6ChvPI,1066
10
+ pre_commit_html-0.1.4.dist-info/METADATA,sha256=MjOucQucaGvrr14v09vVZEZM40nD_UcXMH9L2PVSK8k,2926
11
+ pre_commit_html-0.1.4.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
12
+ pre_commit_html-0.1.4.dist-info/entry_points.txt,sha256=QDAlUvwqJE8dDBmurDHauYiMar21Pu_0_-Ew9EjK75w,65
13
+ pre_commit_html-0.1.4.dist-info/RECORD,,
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ pre-commit-html=pre_commit_html.__main__:main
3
+
@@ -1,11 +0,0 @@
1
- pre_commit_html/__init__.py,sha256=W30OYPhTtrsoGH_xbIFRGO8IpDKcL5OEdobZLqYXYrM,4364
2
- pre_commit_html/__main__.py,sha256=ME8XDYKL00PsCgA96rYrGqzujMCu5FiUPVZo5VyiZ6w,367
3
- pre_commit_html/site/nothing,sha256=FgEEp-DgpF7jCB5mTtiWk_6yQcNz6q68-hqvGHulRbA,15
4
- pre_commit_html/site/templates/code_error.jinja,sha256=Nca684pgieEMbgrHFEl7acf26-e5LpGQIYFMDfyHoZ0,52
5
- pre_commit_html/site/templates/code_part.jinja,sha256=ZmTKj9Ev1_yDoUQJlZ1RQZb1-VYDOSkN2sNvwbnfng0,111
6
- pre_commit_html/site/templates/html_content.jinja,sha256=9dmoVLZ-BmYEf27j_52F6md23WS_5JshkTqO9VpRvX0,1695
7
- pre_commit_html-0.1.2.dist-info/LICENSE,sha256=3V9deIJXT--GYubOVNXj1madYOvijNEIn03Yh6ChvPI,1066
8
- pre_commit_html-0.1.2.dist-info/METADATA,sha256=SRNwXpJjTDfIWzwLcFsu_AcjfQW4YXIVZuTaGtnAVcw,2926
9
- pre_commit_html-0.1.2.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
10
- pre_commit_html-0.1.2.dist-info/entry_points.txt,sha256=qosHLsMCjOyjqzpzxw7GKkNQ5RzBxXAh6sFjINUHpMM,60
11
- pre_commit_html-0.1.2.dist-info/RECORD,,
@@ -1,3 +0,0 @@
1
- [console_scripts]
2
- pre_commit_html=pre_commit_html:__main__
3
-