Skip to content

Commit ee00e87

Browse files
authored
Merge pull request #50 from pharmaverse/ruff-check
Add ruff linter rules and fix linting issues
2 parents 591f2af + 5e70929 commit ee00e87

File tree

8 files changed

+272
-212
lines changed

8 files changed

+272
-212
lines changed

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ dev = [
6969
minversion = "6.0"
7070
testpaths = ["tests"]
7171
addopts = "-v --cov=pkglite --cov-report html"
72+
73+
[tool.ruff.lint]
74+
select = [
75+
# pycodestyle
76+
"E",
77+
# Pyflakes
78+
"F",
79+
# pyupgrade
80+
"UP",
81+
# flake8-bugbear
82+
"B",
83+
# flake8-simplify
84+
"SIM",
85+
# isort
86+
"I",
87+
]

src/pkglite/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
from .pack import pack
33
from .unpack import unpack
44
from .use import use_pkglite
5+
6+
__all__ = [
7+
"classify_file",
8+
"is_text_file",
9+
"pack",
10+
"unpack",
11+
"use_pkglite",
12+
]

src/pkglite/classify.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from typing import FrozenSet
32

43

54
def is_text_file(path: str, n: int | None = None) -> bool:
@@ -16,8 +15,8 @@ def is_text_file(path: str, n: int | None = None) -> bool:
1615
Returns:
1716
True if the file is text, False if binary.
1817
"""
19-
ALLOW: FrozenSet[int] = frozenset([9, 10, 13] + list(range(32, 256)))
20-
BLOCK: FrozenSet[int] = frozenset(list(range(0, 7)) + list(range(14, 32)))
18+
ALLOW: frozenset[int] = frozenset([9, 10, 13] + list(range(32, 256)))
19+
BLOCK: frozenset[int] = frozenset(list(range(0, 7)) + list(range(14, 32)))
2120

2221
with open(path, "rb") as file:
2322
bytecode = bytes(file.read(n or os.path.getsize(path)))

src/pkglite/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
@app.callback()
1414
def callback():
1515
"""
16-
pkglite - Plain text representations for packages written in any programming language
16+
pkglite - Plain text representations for packages written in any
17+
programming language.
1718
"""
1819

1920

src/pkglite/pack.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def load_ignore_matcher(directory: str) -> PathMatcher:
3535
if not os.path.exists(ignore_path):
3636
return lambda path: False
3737

38-
with open(ignore_path, "r", encoding="utf-8") as f:
38+
with open(ignore_path, encoding="utf-8") as f:
3939
patterns = f.readlines()
4040

4141
spec = PathSpec.from_lines("gitwildmatch", patterns)
@@ -102,7 +102,7 @@ def read_text_content(file_path: str) -> str:
102102
Returns:
103103
Formatted text content.
104104
"""
105-
with open(file_path, "r", encoding="utf-8") as f:
105+
with open(file_path, encoding="utf-8") as f:
106106
return "".join(" " + line for line in f)
107107

108108

@@ -212,29 +212,31 @@ def pack(
212212
package_name = get_package_name(directory)
213213

214214
all_files = [
215-
(os.path.join(root, file), root, file)
215+
os.path.join(root, file)
216216
for root, _, files in os.walk(directory)
217217
for file in files
218218
if not ignore_matcher(os.path.join(root, file))
219219
]
220220

221221
if not quiet:
222222
print_action("Packing", package_name)
223-
for file_path, root, file in all_files:
223+
for file_path in all_files:
224224
rel_path = os.path.relpath(file_path, directory)
225225
print_sub_action("Reading", rel_path, path_type="source")
226226
if content := process_single_file(
227227
file_path, directory, package_name, ignore_matcher
228228
):
229229
out.write(content)
230230
else:
231-
for file_path, _, _ in all_files:
231+
for file_path in all_files:
232232
if content := process_single_file(
233233
file_path, directory, package_name, ignore_matcher
234234
):
235235
out.write(content)
236236

237237
if not quiet:
238238
print_success(
239-
f"Packed {format_count(len(abs_dirs))} packages into {format_path(abs_output, path_type='target')}"
239+
"Packed "
240+
f"{format_count(len(abs_dirs))} packages into "
241+
f"{format_path(abs_output, path_type='target')}"
240242
)

src/pkglite/unpack.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,16 @@ def finalize_current_entry(
149149
content_lines: list[str] = []
150150
state = ParserState.EXPECTING_PACKAGE
151151

152-
with open(input_file, "r", encoding="utf-8") as f:
152+
with open(input_file, encoding="utf-8") as f:
153153
for line in f:
154154
line = line.rstrip()
155155

156156
if state == ParserState.EXPECTING_PACKAGE:
157157
# Handle transition from current package to new package
158-
if current_file:
159-
if file_data := finalize_current_entry(current_file, content_lines):
160-
files.append(file_data)
158+
if current_file and (
159+
file_data := finalize_current_entry(current_file, content_lines)
160+
):
161+
files.append(file_data)
161162

162163
new_state, new_file = handle_package_state(line)
163164
if new_file:
@@ -217,8 +218,10 @@ def write_binary_file(file_path: Path, content: str) -> None:
217218
binary_content = binascii.unhexlify(content)
218219
file_path.parent.mkdir(parents=True, exist_ok=True)
219220
file_path.write_bytes(binary_content)
220-
except binascii.Error:
221-
raise ValueError(f"Invalid hexadecimal content for binary file: {file_path}")
221+
except binascii.Error as err:
222+
raise ValueError(
223+
f"Invalid hexadecimal content for binary file: {file_path}"
224+
) from err
222225

223226

224227
def write_file(file_data: FileData, output_directory: Path) -> None:
@@ -274,6 +277,8 @@ def unpack(
274277

275278
if not quiet:
276279
print_success(
277-
f"Unpacked {format_count(len(packages))} packages from "
278-
f"{format_path(str(input_path), path_type='source')} into {format_path(str(output_path), path_type='target')}"
280+
"Unpacked "
281+
f"{format_count(len(packages))} packages from "
282+
f"{format_path(str(input_path), path_type='source')} into "
283+
f"{format_path(str(output_path), path_type='target')}"
279284
)

src/pkglite/use.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import shutil
44
from collections.abc import Sequence
55
from pathlib import Path
6-
from typing import Tuple
76

87
import pkglite.templates
98

@@ -12,7 +11,7 @@
1211

1312
def process_directory(
1413
template: Path, directory: str | Path, force: bool, quiet: bool
15-
) -> Tuple[str, bool]:
14+
) -> tuple[str, bool]:
1615
"""
1716
Process a single directory and create/overwrite `.pkgliteignore` file.
1817
@@ -23,15 +22,18 @@ def process_directory(
2322
quiet: If True, suppress output messages.
2423
2524
Returns:
26-
A tuple containing the path to the `.pkgliteignore` file and whether it was created/overwritten.
25+
A tuple containing the `.pkgliteignore` path and whether it was
26+
created or overwritten.
2727
"""
2828
dir_path = Path(os.path.abspath(os.path.expanduser(str(directory))))
2929
ignore_path = str(dir_path / ".pkgliteignore")
3030

3131
if os.path.exists(ignore_path) and not force:
3232
if not quiet:
3333
print_warning(
34-
f"Skipping: {format_path('.pkgliteignore', path_type='target')} already exists in {format_path(str(dir_path), path_type='target')}"
34+
"Skipping: "
35+
f"{format_path('.pkgliteignore', path_type='target')} already exists "
36+
f"in {format_path(str(dir_path), path_type='target')}"
3537
)
3638
return ignore_path, False
3739

@@ -41,7 +43,9 @@ def process_directory(
4143
if not quiet:
4244
action = "Overwrote" if os.path.exists(ignore_path) and force else "Created"
4345
print_success(
44-
f"{action} {format_path('.pkgliteignore', path_type='target')} in {format_path(str(dir_path), path_type='target')}"
46+
f"{action} "
47+
f"{format_path('.pkgliteignore', path_type='target')} in "
48+
f"{format_path(str(dir_path), path_type='target')}"
4549
)
4650
return ignore_path, True
4751

0 commit comments

Comments
 (0)