Skip to content

Commit

Permalink
Configure ruff as linter and formatter.
Browse files Browse the repository at this point in the history
Co-authored-by: Arfima Dev <dev@arfima.com>
Signed-off-by: Xavier Barrachina Civera <xbarrachina@arfima.com>
  • Loading branch information
xbarra and devarfima committed Aug 12, 2024
1 parent 66fab71 commit ab001ed
Show file tree
Hide file tree
Showing 32 changed files with 1,059 additions and 451 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

55 changes: 16 additions & 39 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ repos:
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-json
Expand All @@ -50,18 +49,14 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
args:
['--no-error-on-unmatched-pattern', '--ignore-unknown']

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.41.0
hooks:
- id: markdownlint
args: ["--fix"]
# yamllint disable rule:comments-indentation
## Commenting until issue #94 is fixed
# - repo: https://github.com/igorshubovych/markdownlint-cli
# rev: v0.41.0
# hooks:
# - id: markdownlint
# args: ["--fix"]
# yamllint enable rule:comments-indentation

- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.1
Expand All @@ -79,38 +74,25 @@ repos:
hooks:
- id: shellcheck

- repo: https://github.com/pycqa/pydocstyle.git
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: ["tomli"]

- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
rev: v1.7.0.14
rev: v1.7.1.15
hooks:
- id: actionlint

- repo: https://github.com/pycqa/flake8
rev: "7.0.0"
hooks:
- id: flake8
additional_dependencies:
- pep8-naming

- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
args: [ "-d", "{rules: {line-length: {max: 120}}, ignore-from-file: [.gitignore],}", ]
args:
["-d", "{rules: {line-length: {max: 120}},
ignore-from-file: [.gitignore],}"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.5
rev: v0.5.7
hooks:
- id: ruff
files: ^(scripts|tests|custom_components)/.+\.py$
args: [--fix, --exit-non-zero-on-fix]
args: [--fix, --exit-non-zero-on-fix, --config=pyproject.toml]
- id: ruff-format
files: ^(scripts|tests|custom_components)/.+\.py$

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.0"
Expand All @@ -120,22 +102,17 @@ repos:
args: ["--show-error-codes", "--install-types", "--non-interactive"]
additional_dependencies: ["pytest", "types-requests"]

# yamllint disable rule:comments-indentation
# Check for misspellings in documentation files
# - repo: https://github.com/codespell-project/codespell
# rev: v2.2.2
# hooks:
# - id: codespell

# To embrace black styles, even in docs
# - repo: https://github.com/asottile/blacken-docs
# rev: v1.13.0
# hooks:
# - id: blacken-docs
# additional_dependencies: [black]

# Automatically upgrade Python syntax for newer versions
# - repo: https://github.com/asottile/pyupgrade
# rev: v3.15.0
# hooks:
# - id: pyupgrade
# args: ['--py37-plus']
# yamllint enable rule:comments-indentation
4 changes: 0 additions & 4 deletions .prettierignore

This file was deleted.

120 changes: 78 additions & 42 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ requires = [
"wheel",
"pdm"
]

build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
Expand All @@ -93,13 +94,9 @@ test = [
"sphinx-pyproject"
]
lint = [
"isort",
"black",
"black[jupyter]",
"flake8",
"Flake8-pyproject",
"mypy",
"pre-commit",
"ruff",
]

[tool.pdm.scripts]
Expand All @@ -108,9 +105,6 @@ release = "scripts/release-versioning.sh"
test = "pytest"
docs = { shell = "cd docs && mkdocs serve", help = "Start the dev server for doc preview" }
lint = "pre-commit run --all-files"
isort = { cmd = "isort --check ." }
black = { cmd = "black --check ." }
flake8 = { cmd = "flake8 src" }
mypy = { cmd = "mypy --install-types --non-interactive src" }
all = {composite = ["pytest", "mypy", "isort", "black", "flake8"]}

Expand All @@ -129,44 +123,86 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
warn_unreachable = true
ignore_missing_imports = true

[tool.isort]
profile = "black"
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
line_length = 120

[tool.coverage.run]
source = "src"
omit = "tests/*"
relative_files = true

[tool.yapf]
blank_line_before_nested_class_or_def = true
column_limit = 88

[tool.black]
line-length = 120
exclude = '''
/(
\.git
| \.tox
| \venv
| \.venv
| \*.env
| \build
| \dist
)/
'''

[tool.flake8]
max-line-length = "120"
extend-ignore = [
"E501",
"E203", # Allow spacing before colon (to favor Black).
]

[project.scripts]
os_climate_hazard = "hazard.cli:cli"

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

[tool.ruff.lint]
extend-fixable = [
# Instead of trailing-whitespace
"W291", "W293"
]

extend-select = [
# Instead of pydocstyle
"D",
#Instead of flake8
"E", "F", "B",
# Instead of pep8-naming
"N",
# Instead of flake8-debugger or debug-statements
"T10",
]

ignore = [
"E203",
"E501",
# Avoid incompatible rules
"D203",
"D213",

# Ignore this rules so that precommit passes. Uncomment to start fixing them
"B006", "B008", "B904", "B012", "B024", "B020",
"D",
"F401", "F811",
]

[tool.ruff.lint.extend-per-file-ignores]
# Ignore `D` rules everywhere except for the `src/` directory.
"!src/**.py" = ["D"]

[tool.ruff.lint.pycodestyle]
max-line-length = 120

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
docstring-code-format = true
docstring-code-line-length = "dynamic"
2 changes: 0 additions & 2 deletions src/hazard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def days_tas_above_indicator(
inventory_format: Optional[str] = "osc",
extra_xarray_store: Optional[bool] = False,
):

hazard_services.days_tas_above_indicator(
gcm_list,
scenario_list,
Expand Down Expand Up @@ -47,7 +46,6 @@ def degree_days_indicator(
extra_xarray_store: Optional[bool] = False,
inventory_format: Optional[str] = "osc",
):

hazard_services.degree_days_indicator(
gcm_list,
scenario_list,
Expand Down
39 changes: 27 additions & 12 deletions src/hazard/docs_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def __init__(
{bucket}/hazard/inventory.json
Args:
----
fs (Optional[AbstractFileSystem], optional): AbstractFileSystem. Defaults to None in which case S3FileSystem will be created. # noqa: E501
local_path (Optional[str], optional): local path where to save the inventory. only used if `fs` is a LocalFileSystem.
"""
if fs is None:
access_key = os.environ.get(self.__access_key, None)
Expand All @@ -51,7 +53,9 @@ def __init__(
self._root = f"s3://{str(PurePosixPath(bucket, prefix))}"
elif type(self._fs) == LocalFileSystem: # noqa: E721 # use isinstance?
if local_path is None:
raise ValueError("if using a local filesystem, please provide a value for `local_path`")
raise ValueError(
"if using a local filesystem, please provide a value for `local_path`"
)
self._root = str(PurePosixPath(local_path))
else:
self._root = str(PurePosixPath(bucket, prefix))
Expand Down Expand Up @@ -88,13 +92,14 @@ def write_new_empty_inventory(self):

def write_inventory_stac(self, resources: Iterable[HazardResource]):
"""Write a hazard models inventory as STAC."""

if self._fs == s3fs.S3FileSystem:
path_root = self._root
else:
path_root = "."

items = HazardResources(resources=list(resources)).to_stac_items(path_root=path_root, items_as_dicts=True)
items = HazardResources(resources=list(resources)).to_stac_items(
path_root=path_root, items_as_dicts=True
)
for it in items:
with self._fs.open(self._full_path_stac_item(id=it["id"]), "w") as f:
f.write(json.dumps(it, indent=4))
Expand All @@ -110,7 +115,6 @@ def write_inventory_stac(self, resources: Iterable[HazardResource]):
f.write(json_str)

def stac_catalog(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:

return {
"stac_version": "1.0.0",
"id": "osc-hazard-indicators-catalog",
Expand All @@ -125,7 +129,6 @@ def stac_catalog(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:
}

def stac_collection(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:

return {
"stac_version": "1.0.0",
"type": "Collection",
Expand All @@ -136,23 +139,35 @@ def stac_collection(self, items: List[Dict[str, Any]]) -> Dict[str, Any]:
"license": "CC-BY-4.0",
"extent": {
"spatial": {"bbox": [[-180, -90, 180, 90]]},
"temporal": {"interval": [["1950-01-01T00:00:00Z", "2100-12-31T23:59:59Z"]]},
"temporal": {
"interval": [["1950-01-01T00:00:00Z", "2100-12-31T23:59:59Z"]]
},
},
"providers": [{"name": "UKRI", "roles": ["producer"], "url": "https://www.ukri.org/"}],
"providers": [
{"name": "UKRI", "roles": ["producer"], "url": "https://www.ukri.org/"}
],
"links": [
{"rel": "self", "type": "application/json", "href": "./collection.json"},
{
"rel": "self",
"type": "application/json",
"href": "./collection.json",
},
{"rel": "root", "type": "application/json", "href": "./catalog.json"},
]
+ [{"rel": "item", "href": f"./{x['id']}.json"} for x in items],
}

def update_inventory(self, resources: Iterable[HazardResource], remove_existing: bool = False):
def update_inventory(
self, resources: Iterable[HazardResource], remove_existing: bool = False
):
"""Add the hazard models provided to the inventory. If a model with the same key
(hazard type and id) exists, replace."""

(hazard type and id) exists, replace.
"""
# if format == stac, we do a round trip, stac -> osc -> stac.
path = self._full_path_inventory()
combined = {} if remove_existing else dict((i.key(), i) for i in self.read_inventory())
combined = (
{} if remove_existing else dict((i.key(), i) for i in self.read_inventory())
)
for resource in resources:
combined[resource.key()] = resource
models = HazardResources(resources=list(combined.values()))
Expand Down
4 changes: 3 additions & 1 deletion src/hazard/indicator_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def inventory(self) -> Iterable[HazardResource]:
...

@abstractmethod
def run_single(self, item: T, source: Any, target: ReadWriteDataArray, client: Client):
def run_single(
self, item: T, source: Any, target: ReadWriteDataArray, client: Client
):
"""Run a single item of the batch."""
...
Loading

0 comments on commit ab001ed

Please sign in to comment.