Skip to content

Commit 5a09511

Browse files
authored
Merge pull request #38 from SciCatProject/copier-update
Copier update
2 parents f228982 + 728f06a commit 5a09511

15 files changed

+89
-70
lines changed

.copier-answers.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
2-
_commit: d91bf92
2+
_commit: 1c41c93
33
_src_path: gh:scipp/copier_template
44
description: A daemon that creates a raw dataset using scicat interface whenever a
55
new file is written by a file-writer.

.pre-commit-config.yaml

+8-22
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,21 @@ repos:
1313
- id: trailing-whitespace
1414
args: [ --markdown-linebreak-ext=md ]
1515
exclude: '\.svg'
16-
- repo: https://github.com/pycqa/isort
17-
rev: 5.12.0
18-
hooks:
19-
- id: isort
20-
name: isort (python)
21-
- repo: https://github.com/psf/black-pre-commit-mirror
22-
rev: 23.11.0
23-
hooks:
24-
- id: black
2516
- repo: https://github.com/kynan/nbstripout
2617
rev: 0.6.0
2718
hooks:
2819
- id: nbstripout
2920
types: [ "jupyter" ]
3021
args: [ "--drop-empty-cells",
3122
"--extra-keys 'metadata.language_info.version cell.metadata.jp-MarkdownHeadingCollapsed cell.metadata.pycharm'" ]
32-
- repo: https://github.com/pycqa/flake8
33-
rev: 6.1.0
34-
hooks:
35-
- id: flake8
36-
types: ["python"]
37-
additional_dependencies: ["flake8-bugbear==23.9.16"]
38-
args: ["--max-line-length=88", "--extend-ignore=E203"]
39-
- repo: https://github.com/pycqa/bandit
40-
rev: 1.7.5
41-
hooks:
42-
- id: bandit
43-
additional_dependencies: ["bandit[toml]"]
44-
args: ["-c", "pyproject.toml"]
23+
- repo: https://github.com/astral-sh/ruff-pre-commit
24+
rev: v0.4.3
25+
hooks:
26+
- id: ruff
27+
args: [ --fix ]
28+
types_or: [ python, pyi, jupyter ]
29+
- id: ruff-format
30+
types_or: [ python, pyi ]
4531
- repo: https://github.com/codespell-project/codespell
4632
rev: v2.2.6
4733
hooks:

pyproject.toml

+35-9
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,43 @@ filterwarnings = [
6767
"error",
6868
]
6969

70-
[tool.bandit]
71-
# Excluding tests because bandit doesn't like `assert`.
72-
exclude_dirs = ["docs/conf.py", "tests"]
70+
[tool.ruff]
71+
line-length = 88
72+
extend-include = ["*.ipynb"]
73+
extend-exclude = [
74+
".*", "__pycache__", "build", "dist", "install",
75+
]
76+
77+
[tool.ruff.lint]
78+
# See https://docs.astral.sh/ruff/rules/
79+
select = ["B", "C4", "DTZ", "E", "F", "G", "I", "PERF", "PGH", "PT", "PYI", "RUF", "S", "T20", "UP", "W"]
80+
ignore = [
81+
# Conflict with ruff format, see
82+
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
83+
"COM812", "COM819", "D206", "D300", "E111", "E114", "E117", "ISC001", "ISC002", "Q000", "Q001", "Q002", "Q003", "W191",
84+
]
85+
fixable = ["I001", "B010"]
86+
isort.known-first-party = ["scicat-filewriter-ingest"]
87+
pydocstyle.convention = "numpy"
7388

74-
[tool.black]
75-
skip-string-normalization = true
89+
[tool.ruff.lint.per-file-ignores]
90+
# those files have an increased risk of relying on import order
91+
"__init__.py" = ["I"]
92+
"tests/*" = [
93+
"S101", # asserts are fine in tests
94+
"B018", # 'useless expressions' are ok because some tests just check for exceptions
95+
]
96+
"*.ipynb" = [
97+
"E501", # longer lines are sometimes more readable
98+
"F403", # *-imports used with domain types
99+
"F405", # linter may fail to find names because of *-imports
100+
"I", # we don't collect imports at the top
101+
"S101", # asserts are used for demonstration and are safe in notebooks
102+
"T201", # printing is ok for demonstration purposes
103+
]
76104

77-
[tool.isort]
78-
skip_gitignore = true
79-
profile = "black"
80-
known_first_party = ["scicat-filewriter-ingest"]
105+
[tool.ruff.format]
106+
quote-style = "preserve"
81107

82108
[tool.mypy]
83109
strict = true

requirements/make_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
from argparse import ArgumentParser
33
from pathlib import Path
4-
from typing import List
54

65
import tomli
76

@@ -20,7 +19,7 @@
2019
"""
2120

2221

23-
def write_dependencies(dependency_name: str, dependencies: List[str]) -> None:
22+
def write_dependencies(dependency_name: str, dependencies: list[str]) -> None:
2423
path = Path(f"{dependency_name}.in")
2524
if path.exists():
2625
sections = path.read_text().split(CUSTOM_AUTO_SEPARATOR)

src/background-ingestor.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
33
import json
44
import logging
5+
import pathlib
56
from collections.abc import Generator
67
from contextlib import contextmanager
78

@@ -56,17 +57,16 @@ def main() -> None:
5657
logger.info("Nexus file to be ingested : ")
5758
logger.info(nexus_file)
5859

59-
done_writing_message_file = (
60+
done_writing_message_file = pathlib.Path(
6061
arg_namespace.arg_namespace.done_writing_message_file
6162
)
6263
logger.info("Done writing message file linked to nexus file : ")
6364
logger.info(done_writing_message_file)
6465

6566
# open and read done writing message input file
66-
with open(done_writing_message_file, 'r') as f:
67-
done_writing_message = json.load(f)
67+
done_writing_message = json.load(done_writing_message_file.open())
68+
logger.info(done_writing_message)
6869

69-
print(done_writing_message)
7070
# open nexus file
7171
# nxs = snx.File(nexus_file)
7272

src/scicat_configuration.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
33
import argparse
4+
from collections.abc import Mapping
45
from dataclasses import dataclass
5-
from typing import Mapping, Optional
66

77

88
def build_main_arg_parser() -> argparse.ArgumentParser:
@@ -152,8 +152,8 @@ class RunOptions:
152152
log_message_prefix: str
153153
log_level: str
154154
check_by_job_id: bool
155-
system_log_facility: Optional[str] = None
156-
pyscicat: Optional[str] = None
155+
system_log_facility: str | None = None
156+
pyscicat: str | None = None
157157
graylog: bool = False
158158

159159

@@ -220,10 +220,10 @@ def build_scicat_config(input_args: argparse.Namespace) -> ScicatConfig:
220220
):
221221
config_dict = json.loads(config_file_path.read_text())
222222
else:
223-
config_dict = dict()
223+
config_dict = {}
224224

225225
# Overwrite deep-copied options with command line arguments
226-
run_option_dict: dict = copy.deepcopy(config_dict.setdefault("options", dict()))
226+
run_option_dict: dict = copy.deepcopy(config_dict.setdefault("options", {}))
227227
for arg_name, arg_value in vars(input_args).items():
228228
if arg_value is not None:
229229
run_option_dict[arg_name] = arg_value
@@ -236,6 +236,6 @@ def build_scicat_config(input_args: argparse.Namespace) -> ScicatConfig:
236236
return ScicatConfig(
237237
original_dict=MappingProxyType(config_dict),
238238
run_options=RunOptions(**run_option_dict),
239-
kafka_options=kafkaOptions(**config_dict.setdefault("kafka", dict())),
240-
graylog_options=GraylogOptions(**config_dict.setdefault("graylog", dict())),
239+
kafka_options=kafkaOptions(**config_dict.setdefault("kafka", {})),
240+
graylog_options=GraylogOptions(**config_dict.setdefault("graylog", {})),
241241
)

src/scicat_ingestor.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# SPDX-License-Identifier: BSD-3-Clause
2-
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
2+
# Copyright (c) 2024 Scicatproject contributors (https://github.com/ScicatProject)
3+
# ruff: noqa: E402, F401
4+
5+
import importlib.metadata
6+
7+
try:
8+
__version__ = importlib.metadata.version(__package__ or __name__)
9+
except importlib.metadata.PackageNotFoundError:
10+
__version__ = "0.0.0"
11+
12+
del importlib
13+
314
import logging
415
from collections.abc import Generator
516
from contextlib import contextmanager

src/scicat_kafka.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
from collections.abc import Generator
55

66
from confluent_kafka import Consumer
7+
from scicat_configuration import kafkaOptions
78
from streaming_data_types import deserialise_wrdn
89
from streaming_data_types.finished_writing_wrdn import (
910
FILE_IDENTIFIER as WRDN_FILE_IDENTIFIER,
1011
)
1112
from streaming_data_types.finished_writing_wrdn import WritingFinished
1213

13-
from scicat_configuration import kafkaOptions
14-
1514

1615
def collect_consumer_options(options: kafkaOptions) -> dict:
1716
"""Build a Kafka consumer and configure it according to the ``options``."""
@@ -55,7 +54,7 @@ def build_consumer(kafka_options: kafkaOptions, logger: logging.Logger) -> Consu
5554
return None
5655

5756
kafka_topics = collect_kafka_topics(kafka_options)
58-
logger.info(f"Subscribing to the following Kafka topics: {kafka_topics}")
57+
logger.info("Subscribing to the following Kafka topics: %s", kafka_topics)
5958
consumer.subscribe(kafka_topics)
6059
return Consumer(consumer_options)
6160

@@ -66,7 +65,8 @@ def validate_consumer(consumer: Consumer, logger: logging.Logger) -> bool:
6665
except Exception as err:
6766
logger.error(
6867
"Kafka consumer could not be instantiated. "
69-
f"Error message from kafka thread: \n{err}"
68+
"Error message from kafka thread: \n%s",
69+
err,
7070
)
7171
return False
7272
else:

src/scicat_logging.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
3+
import datetime
34
import logging
45
import logging.handlers
5-
from datetime import datetime
66

77
import graypy
8-
98
from scicat_configuration import ScicatConfig
109

1110

@@ -24,7 +23,9 @@ def build_logger(config: ScicatConfig) -> logging.Logger:
2423
if run_options.file_log:
2524
file_name_components = [run_options.log_filepath_prefix]
2625
if run_options.file_log_timestamp:
27-
file_name_components.append(datetime.now().strftime('%Y%m%d%H%M%S%f'))
26+
file_name_components.append(
27+
datetime.datetime.now(datetime.UTC).strftime('%Y%m%d%H%M%S%f')
28+
)
2829
file_name_components.append('.log')
2930

3031
file_name = '_'.join(file_name_components)

src/scicat_metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
3+
from collections.abc import Callable
34
from importlib.metadata import entry_points
4-
from typing import Callable
55

66

77
def load_metadata_extractors(extractor_name: str) -> Callable:

tests/_scicat_ingestor.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
from time import sleep
1010

1111
# Run the main function in a subprocess
12-
process = subprocess.Popen(
13-
[
14-
"scicat_ingestor",
15-
*(sys.argv[1:] or ["--verbose", "-c", "resources/config.sample.json"]),
16-
]
12+
command = (
13+
"scicat_ingestor",
14+
*(sys.argv[1:] or ["--verbose", "-c", "resources/config.sample.json"]),
1715
)
16+
process = subprocess.Popen(command) # noqa: S603
1817

1918
# Send a SIGINT signal to the process after 5 seconds
2019
sleep(5)

tests/test_logging.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import pathlib
22

33
import pytest
4-
54
from scicat_configuration import GraylogOptions, RunOptions, ScicatConfig, kafkaOptions
65

76

8-
@pytest.fixture
7+
@pytest.fixture()
98
def scicat_config(tmp_path: pathlib.Path) -> ScicatConfig:
109
return ScicatConfig(
11-
original_dict=dict(),
10+
original_dict={},
1211
run_options=RunOptions(
1312
config_file='test',
1413
verbose=True,

tests/test_metadata_extractor.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import pytest
2-
32
from scicat_metadata import load_metadata_extractors
43

54

65
@pytest.mark.parametrize(
7-
["extractor_name", "expected_result"], [("max", 5), ("min", 1), ("mean", 3)]
6+
("extractor_name", "expected_result"), [("max", 5), ("min", 1), ("mean", 3)]
87
)
98
def test_metadata_extractor(extractor_name: str, expected_result: int):
109
"""Test if the metadata extractor can be loaded."""

tests/test_scicat_configuration.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import argparse
44

55
import pytest
6-
76
from scicat_configuration import ScicatConfig
87

98

10-
@pytest.fixture
9+
@pytest.fixture()
1110
def main_arg_parser() -> argparse.ArgumentParser:
1211
"""Return the namespace of the main argument parser."""
1312
from scicat_configuration import build_main_arg_parser
@@ -34,7 +33,7 @@ def test_scicat_arg_parser_configuration_matches(
3433
# Parse the configuration file
3534
assert config_path.exists()
3635
config_from_file: dict = json.loads(config_path.read_text())
37-
main_options: dict = config_from_file.get('options', dict())
36+
main_options: dict = config_from_file.get('options', {})
3837

3938
# Check if all keys matches
4039
all_keys = set(config_from_args.keys()).union(main_options.keys())
@@ -52,7 +51,7 @@ def test_build_scicat_config_default(main_arg_parser: argparse.ArgumentParser) -
5251
assert scicat_config.run_options.config_file == 'config.20240405.json'
5352

5453

55-
@pytest.fixture
54+
@pytest.fixture()
5655
def scicat_config(main_arg_parser: argparse.ArgumentParser) -> ScicatConfig:
5756
from scicat_configuration import build_scicat_config
5857

tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ commands = scicat_ingestor --help # Minimal test of the script
1111

1212
[testenv:nightly]
1313
deps = -r requirements/nightly.txt
14-
commands = pytest
14+
commands = pytest {posargs}
1515

1616
[testenv:unpinned]
1717
description = Test with unpinned dependencies, as a user would install now.
1818
deps =
1919
-r requirements/basetest.txt
2020
scicat-filewriter-ingest
21-
commands = pytest
21+
commands = pytest {posargs}
2222

2323
[testenv:docs]
2424
description = invoke sphinx-build to build the HTML docs

0 commit comments

Comments
 (0)