Skip to content

Commit a3193f7

Browse files
authored
Move linting to ruff (#29)
* Move linting to ruff * Apply suggestions from code review * Remove some unecessary config
1 parent 7773d00 commit a3193f7

File tree

4 files changed

+65
-90
lines changed

4 files changed

+65
-90
lines changed

.pre-commit-config.yaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
77
rev: v4.5.0
88
hooks:
9+
- id: check-added-large-files
910
- id: check-docstring-first
1011
- id: check-executables-have-shebangs
12+
- id: check-case-conflict
1113
- id: check-merge-conflict
14+
- id: check-symlinks
15+
- id: check-yaml
1216
- id: check-toml
17+
- id: debug-statements
1318
- id: end-of-file-fixer
1419
- id: mixed-line-ending
1520
args: [--fix=lf]
21+
- id: name-tests-test
22+
args: ["--pytest-test-first"]
1623
- id: requirements-txt-fixer
1724
- id: trailing-whitespace
1825
- repo: https://github.com/astral-sh/ruff-pre-commit
1926
rev: v0.3.4
2027
hooks:
2128
- id: ruff
22-
- repo: https://github.com/psf/black
23-
rev: 24.3.0
24-
hooks:
25-
- id: black
29+
- id: ruff-format
2630
- repo: https://github.com/pre-commit/mirrors-mypy
2731
rev: v1.9.0
2832
hooks:
@@ -35,3 +39,11 @@ repos:
3539
- id: check-manifest
3640
args: [--no-build-isolation]
3741
additional_dependencies: [setuptools-scm]
42+
- repo: https://github.com/codespell-project/codespell
43+
# Configuration for codespell is in pyproject.toml
44+
rev: v2.2.6
45+
hooks:
46+
- id: codespell
47+
additional_dependencies:
48+
# tomli dependency can be removed when we drop support for Python 3.10
49+
- tomli

fancylog/fancylog.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""
2-
fancylog
1+
"""fancylog
32
===============
43
54
Wrapper around the standard logging module, with additional information.
65
76
"""
87

8+
import contextlib
99
import logging
1010
import os
1111
import sys
@@ -38,8 +38,7 @@ def start_logging(
3838
log_to_console=True,
3939
timestamp=True,
4040
):
41-
"""
42-
Prepares the log file, and then begins logging.
41+
"""Prepares the log file, and then begins logging.
4342
4443
:param output_dir: Directory to save the log file
4544
:param package: What python package are we logging?
@@ -64,12 +63,8 @@ def start_logging(
6463
:param log_to_console: Print logs to the console or not: Default: True
6564
:return: Path to the logging file#
6665
"""
67-
6866
output_dir = str(output_dir)
69-
if verbose:
70-
print_log_level = "DEBUG"
71-
else:
72-
print_log_level = "INFO"
67+
print_log_level = "DEBUG" if verbose else "INFO"
7368

7469
if log_to_file:
7570
if filename is None:
@@ -120,18 +115,17 @@ def __init__(
120115
write_variables=True,
121116
log_header=None,
122117
):
123-
self.file = open(file, "w", encoding="utf-8")
124118
self.program = program
125-
if write_header:
126-
self.write_log_header(output_dir, log_header)
127-
if write_git:
128-
self.write_git_info(self.program.__name__)
129-
if write_cli_args:
130-
self.write_command_line_arguments()
131-
if write_variables and variable_objects:
132-
self.write_variables(variable_objects)
133119

134-
self.file.close()
120+
with open(file, "w", encoding="utf-8") as self.file:
121+
if write_header:
122+
self.write_log_header(output_dir, log_header)
123+
if write_git:
124+
self.write_git_info(self.program.__name__)
125+
if write_cli_args:
126+
self.write_command_line_arguments()
127+
if write_variables and variable_objects:
128+
self.write_variables(variable_objects)
135129

136130
def write_git_info(self, program_name, header="GIT INFO"):
137131
self.write_separated_section_header(header)
@@ -142,18 +136,14 @@ def write_git_info(self, program_name, header="GIT INFO"):
142136
program_path = os.path.split(program_path)[0]
143137
git_info = get_git_info(program_path)
144138

145-
self.file.write("Commit hash: {} \n".format(git_info.head.hash))
139+
self.file.write(f"Commit hash: {git_info.head.hash} \n")
140+
self.file.write(f"Commit message: {git_info.head.message} \n")
141+
self.file.write(f"Commit date & time: {git_info.head.datetime} \n")
146142
self.file.write(
147-
"Commit message: {} \n".format(git_info.head.message)
143+
f"Commit author: {git_info.head.committer_name} \n"
148144
)
149145
self.file.write(
150-
"Commit date & time: {} \n".format(git_info.head.datetime)
151-
)
152-
self.file.write(
153-
"Commit author: {} \n".format(git_info.head.committer_name)
154-
)
155-
self.file.write(
156-
"Commit author email: {}".format(git_info.head.committer_email)
146+
f"Commit author email: {git_info.head.committer_email}"
157147
)
158148

159149
except GitPythonError:
@@ -202,10 +192,8 @@ def write_log_header(self, output_dir, log_header):
202192
)
203193
self.file.write("Output directory: " + output_dir + "\n")
204194
self.file.write("Current directory: " + os.getcwd() + "\n")
205-
try:
195+
with contextlib.suppress(AttributeError):
206196
self.file.write(f"Version: {self.program.__version__}")
207-
except AttributeError:
208-
pass
209197

210198
def write_separated_section_header(
211199
self,
@@ -238,8 +226,7 @@ def initialise_logger(
238226
file_level="DEBUG",
239227
log_to_console=True,
240228
):
241-
"""
242-
Sets up (possibly multiprocessing aware) logging.
229+
"""Sets up (possibly multiprocessing aware) logging.
243230
:param filename: Where to save the logs to
244231
:param print_level: What level of logging to print to console.
245232
Default: 'INFO'
@@ -279,8 +266,7 @@ def setup_logging(
279266
multiprocessing_aware=True,
280267
log_to_console=True,
281268
):
282-
"""
283-
Sets up (possibly multiprocessing aware) logging.
269+
"""Sets up (possibly multiprocessing aware) logging.
284270
:param filename: Where to save the logs to
285271
:param print_level: What level of logging to print to console.
286272
Default: 'INFO'
@@ -291,7 +277,6 @@ def setup_logging(
291277
Default: True
292278
293279
"""
294-
295280
initialise_logger(
296281
filename,
297282
print_level=print_level,
@@ -320,8 +305,7 @@ def setup_logging(
320305

321306

322307
def disable_logging():
323-
"""
324-
Prevents any more logging. Saves remembering that logging.disable() with
308+
"""Prevents any more logging. Saves remembering that logging.disable() with
325309
no argument doesn't work.
326310
:return:
327311
"""

fancylog/tools/git.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
git
1+
"""git
32
===============
43
54
Wrappers around gitpython to return information about the git repository
@@ -9,25 +8,21 @@
98

109

1110
class GitPythonError(Exception):
12-
"""
13-
Exception if gitpython cannot be found (Typical in production
11+
"""Exception if gitpython cannot be found (Typical in production
1412
environments).
1513
"""
1614

1715
pass
1816

1917

2018
class GitEnvironmentError(Exception):
21-
"""
22-
Exception if gitpython fails (Typical in production environments).
23-
"""
19+
"""Exception if gitpython fails (Typical in production environments)."""
2420

2521
pass
2622

2723

2824
class GitHead:
29-
"""
30-
Class to parse a repo.head.commit object from gitpython, and return
25+
"""Class to parse a repo.head.commit object from gitpython, and return
3126
more informative properties
3227
"""
3328

@@ -42,8 +37,7 @@ def __init__(self, head_commit):
4237

4338

4439
class GitInfo:
45-
"""
46-
Class to parse a repo object from gitpython, and return more informative
40+
"""Class to parse a repo object from gitpython, and return more informative
4741
properties
4842
"""
4943

@@ -52,24 +46,20 @@ def __init__(self, repo):
5246

5347

5448
def get_git_info(repo_path):
55-
"""
56-
Returns a class with useful information about the git repository.
49+
"""Returns a class with useful information about the git repository.
5750
(if there is one). Will only work with "dev" installs (otherwise
5851
gitpython is not installed)
5952
:return:
6053
"""
61-
6254
try:
6355
import git
6456

65-
except ImportError:
66-
raise GitPythonError
67-
return None
57+
except ImportError as exc:
58+
raise GitPythonError from exc
6859

6960
try:
7061
repo = git.Repo(repo_path)
7162
return GitInfo(repo)
7263

73-
except git.InvalidGitRepositoryError:
74-
raise GitEnvironmentError
75-
return None
64+
except git.InvalidGitRepositoryError as exc:
65+
raise GitEnvironmentError from exc

pyproject.toml

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,6 @@ exclude = ["tests*"]
5757
[tool.pytest.ini_options]
5858
addopts = "--cov=fancylog"
5959

60-
[tool.black]
61-
target-version = ['py38', 'py39', 'py310', 'py311']
62-
skip-string-normalization = false
63-
line-length = 79
64-
exclude = '''
65-
(
66-
/(
67-
\.eggs
68-
| \.git
69-
| \.hg
70-
| \.mypy_cache
71-
| \.tox
72-
| \.venv
73-
| _build
74-
| buck-out
75-
| build
76-
| dist
77-
| examples
78-
)/
79-
)
80-
'''
81-
82-
[tool.isort]
83-
profile = "black"
84-
line_length = 79
85-
8660
[tool.setuptools_scm]
8761

8862
[tool.check-manifest]
@@ -93,12 +67,27 @@ ignore = [
9367
"tests/*",
9468
"tests/test_unit/*",
9569
"tests/test_integration/*",
96-
".flake8"
9770
]
9871

9972
[tool.ruff]
10073
line-length = 79
101-
exclude = ["__init__.py","build",".eggs"]
74+
exclude = ["__init__.py", "build", ".eggs"]
75+
fix = true
76+
77+
[tool.ruff.lint]
78+
# See https://docs.astral.sh/ruff/rules/
79+
select = [
80+
"E", # pycodestyle errors
81+
"F", # Pyflakes
82+
"UP", # pyupgrade
83+
"I", # isort
84+
"B", # flake8 bugbear
85+
"SIM", # flake8 simplify
86+
"C90", # McCabe complexity
87+
]
88+
89+
[tool.ruff.format]
90+
docstring-code-format = true # Also format code in docstrings
10291

10392

10493
[tool.tox]

0 commit comments

Comments
 (0)