Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move linting to ruff #29

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,55 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
- id: check-toml
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
args: [--fix=lf]
- id: name-tests-test
args: ["--pytest-test-first"]
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/pygrep-hooks
adamltyson marked this conversation as resolved.
Show resolved Hide resolved
rev: v1.10.0
hooks:
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
- id: mypy
additional_dependencies:
- attrs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is attrs eally necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope 😞

- types-setuptools
- pandas-stubs
adamltyson marked this conversation as resolved.
Show resolved Hide resolved
- types-attrs
- types-PyYAML
- types-requests
- repo: https://github.com/mgedmin/check-manifest
rev: "0.49"
hooks:
- id: check-manifest
args: [--no-build-isolation]
additional_dependencies: [setuptools-scm]
- repo: https://github.com/codespell-project/codespell
# Configuration for codespell is in pyproject.toml
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies:
# tomli dependency can be removed when we drop support for Python 3.10
- tomli
60 changes: 22 additions & 38 deletions fancylog/fancylog.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
fancylog
"""fancylog
===============

Wrapper around the standard logging module, with additional information.

"""

import contextlib
import logging
import os
import sys
Expand Down Expand Up @@ -38,8 +38,7 @@ def start_logging(
log_to_console=True,
timestamp=True,
):
"""
Prepares the log file, and then begins logging.
"""Prepares the log file, and then begins logging.

:param output_dir: Directory to save the log file
:param package: What python package are we logging?
Expand All @@ -64,12 +63,8 @@ def start_logging(
:param log_to_console: Print logs to the console or not: Default: True
:return: Path to the logging file#
"""

output_dir = str(output_dir)
if verbose:
print_log_level = "DEBUG"
else:
print_log_level = "INFO"
print_log_level = "DEBUG" if verbose else "INFO"

if log_to_file:
if filename is None:
Expand Down Expand Up @@ -120,18 +115,17 @@ def __init__(
write_variables=True,
log_header=None,
):
self.file = open(file, "w", encoding="utf-8")
self.program = program
if write_header:
self.write_log_header(output_dir, log_header)
if write_git:
self.write_git_info(self.program.__name__)
if write_cli_args:
self.write_command_line_arguments()
if write_variables and variable_objects:
self.write_variables(variable_objects)

self.file.close()
with open(file, "w", encoding="utf-8") as self.file:
if write_header:
self.write_log_header(output_dir, log_header)
if write_git:
self.write_git_info(self.program.__name__)
if write_cli_args:
self.write_command_line_arguments()
if write_variables and variable_objects:
self.write_variables(variable_objects)

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

self.file.write("Commit hash: {} \n".format(git_info.head.hash))
self.file.write(f"Commit hash: {git_info.head.hash} \n")
self.file.write(f"Commit message: {git_info.head.message} \n")
self.file.write(f"Commit date & time: {git_info.head.datetime} \n")
self.file.write(
"Commit message: {} \n".format(git_info.head.message)
f"Commit author: {git_info.head.committer_name} \n"
)
self.file.write(
"Commit date & time: {} \n".format(git_info.head.datetime)
)
self.file.write(
"Commit author: {} \n".format(git_info.head.committer_name)
)
self.file.write(
"Commit author email: {}".format(git_info.head.committer_email)
f"Commit author email: {git_info.head.committer_email}"
)

except GitPythonError:
Expand Down Expand Up @@ -202,10 +192,8 @@ def write_log_header(self, output_dir, log_header):
)
self.file.write("Output directory: " + output_dir + "\n")
self.file.write("Current directory: " + os.getcwd() + "\n")
try:
with contextlib.suppress(AttributeError):
self.file.write(f"Version: {self.program.__version__}")
except AttributeError:
pass

def write_separated_section_header(
self,
Expand Down Expand Up @@ -238,8 +226,7 @@ def initialise_logger(
file_level="DEBUG",
log_to_console=True,
):
"""
Sets up (possibly multiprocessing aware) logging.
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
:param print_level: What level of logging to print to console.
Default: 'INFO'
Expand Down Expand Up @@ -279,8 +266,7 @@ def setup_logging(
multiprocessing_aware=True,
log_to_console=True,
):
"""
Sets up (possibly multiprocessing aware) logging.
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
:param print_level: What level of logging to print to console.
Default: 'INFO'
Expand All @@ -291,7 +277,6 @@ def setup_logging(
Default: True

"""

initialise_logger(
filename,
print_level=print_level,
Expand Down Expand Up @@ -320,8 +305,7 @@ def setup_logging(


def disable_logging():
"""
Prevents any more logging. Saves remembering that logging.disable() with
"""Prevents any more logging. Saves remembering that logging.disable() with
no argument doesn't work.
:return:
"""
Expand Down
30 changes: 10 additions & 20 deletions fancylog/tools/git.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
git
"""git
===============

Wrappers around gitpython to return information about the git repository
Expand All @@ -9,25 +8,21 @@


class GitPythonError(Exception):
"""
Exception if gitpython cannot be found (Typical in production
"""Exception if gitpython cannot be found (Typical in production
environments).
"""

pass


class GitEnvironmentError(Exception):
"""
Exception if gitpython fails (Typical in production environments).
"""
"""Exception if gitpython fails (Typical in production environments)."""

pass


class GitHead:
"""
Class to parse a repo.head.commit object from gitpython, and return
"""Class to parse a repo.head.commit object from gitpython, and return
more informative properties
"""

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


class GitInfo:
"""
Class to parse a repo object from gitpython, and return more informative
"""Class to parse a repo object from gitpython, and return more informative
properties
"""

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


def get_git_info(repo_path):
"""
Returns a class with useful information about the git repository.
"""Returns a class with useful information about the git repository.
(if there is one). Will only work with "dev" installs (otherwise
gitpython is not installed)
:return:
"""

try:
import git

except ImportError:
raise GitPythonError
return None
except ImportError as exc:
raise GitPythonError from exc

try:
repo = git.Repo(repo_path)
return GitInfo(repo)

except git.InvalidGitRepositoryError:
raise GitEnvironmentError
return None
except git.InvalidGitRepositoryError as exc:
raise GitEnvironmentError from exc
58 changes: 31 additions & 27 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,6 @@ exclude = ["tests*"]
[tool.pytest.ini_options]
addopts = "--cov=fancylog"

[tool.black]
target-version = ['py38', 'py39', 'py310', 'py311']
skip-string-normalization = false
line-length = 79
exclude = '''
(
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| examples
)/
)
'''

[tool.isort]
profile = "black"
line_length = 79

[tool.setuptools_scm]

[tool.check-manifest]
Expand All @@ -98,7 +72,37 @@ ignore = [

[tool.ruff]
line-length = 79
exclude = ["__init__.py","build",".eggs"]
exclude = ["__init__.py", "build", ".eggs"]
fix = true

[tool.ruff.lint]
# See https://docs.astral.sh/ruff/rules/
ignore = [
"D203", # one blank line before class
"D213", # multi-line-summary second line
]
select = [
"E", # pycodestyle errors
"F", # Pyflakes
"UP", # pyupgrade
"I", # isort
"B", # flake8 bugbear
"SIM", # flake8 simplify
"C90", # McCabe complexity
# "D", # pydocstyle
]
per-file-ignores = { "tests/*" = [
adamltyson marked this conversation as resolved.
Show resolved Hide resolved
"D100", # missing docstring in public module
"D205", # missing blank line between summary and description
"D103", # missing docstring in public function
], "examples/*" = [
"D400", # first line should end with a period.
"D415", # first line should end with a period, question mark...
"D205", # missing blank line between summary and description
] }

[tool.ruff.format]
docstring-code-format = true # Also format code in docstrings


[tool.tox]
Expand Down
Loading