Skip to content

Commit

Permalink
Merge pull request #111 from equinor/refactor/replace-black-flake8-wi…
Browse files Browse the repository at this point in the history
…th-ruff

Refactor/replace black flake8 with ruff
  • Loading branch information
sean-sinclair authored Dec 18, 2024
2 parents dd1ba35 + 97a849e commit 6c9c667
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 117 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/format_and_lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check formatting and linting

on:
pull_request:
push: { branches: [main] }

jobs:
ruff-check:
name: Run ruff lint and format checks
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'

- name: Installing dependencies
run: pip install ruff

- name: Run ruff lint
run: ruff check .

- name: Run ruff format
run: ruff format . --check

16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: local
hooks:
- id: lint
name: Ruff Lint
description: Linting using ruff
entry: bash -c 'ruff check .'
language: system
stages: ["pre-commit", "pre-push"]

- id: format
name: Ruff Format
description: Formatting using ruff
entry: bash -c 'ruff format . --check'
language: system
stages: ["pre-commit", "pre-push"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Documentation Status](https://readthedocs.org/projects/fmu-sumo-uploader/badge/?version=latest)](https://fmu-sumo-uploader.readthedocs.io/en/latest/?badge=latest)
[![SCM Compliance](https://scm-compliance-api.radix.equinor.com/repos/equinor/fmu-sumo-uploader/badge)](https://scm-compliance-api.radix.equinor.com/repos/equinor/fmu-sumo-uploader/badge)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)


## Documentation and guidelines
[fmu-sumo-uploader documentation](https://fmu-sumo-uploader.readthedocs.io/en/latest/)
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

39 changes: 32 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ build-backend = "setuptools.build_meta"
[tool.setuptools_scm]
version_file = "src/fmu/sumo/uploader/_version.py"

[tool.isort]
profile = "black"

[tool.black]
line-length = 79

[project]
name = "fmu-sumo-uploader"
requires-python = ">=3.9"
Expand All @@ -25,7 +19,7 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["black", "flake8", "pytest"]
dev = ["ruff", "pytest", "pre-commit"]
test = ["pytest", "pytest-timeout", "fmu-sumo"]

docs = [
Expand All @@ -50,3 +44,34 @@ sumo_upload = "fmu.sumo.uploader.scripts.sumo_upload:main"
[project.entry-points.ert]
fmu_sumo_uploader_jobs = "fmu.sumo.uploader.hook_implementations.jobs"
sumo_upload = "fmu.sumo.uploader.scripts.sumo_upload"

[tool.ruff]
exclude = [
".env",
".git",
".github",
".venv",
"venv",
]

line-length = 79

[tool.ruff.lint]
ignore = [
"E501",
"PD901",
]

extend-select = [
"C4", # Flake8-comprehensions
"I", # isort
"SIM", # Flake8-simplify
"TC", # Flake8-type-checking
"TID", # Flake8-tidy-imports
"N", # pep8-naming
"PD", # Pandas
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]

3 changes: 2 additions & 1 deletion src/fmu/sumo/uploader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
except ImportError:
__version__ = "0.0.0"

import sumo.wrapper

from fmu.sumo.uploader.caseondisk import CaseOnDisk
from fmu.sumo.uploader.caseonjob import CaseOnJob
import sumo.wrapper

SumoConnection = sumo.wrapper.SumoClient

Expand Down
14 changes: 7 additions & 7 deletions src/fmu/sumo/uploader/_fileondisk.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"""
The FileOnDisk class objectifies a file as it appears
on the disk. A file in this context refers to a data/metadata
pair (technically two files).
The FileOnDisk class objectifies a file as it appears
on the disk. A file in this context refers to a data/metadata
pair (technically two files).
"""

import os
import hashlib
import base64
import hashlib
import os

import yaml

from fmu.sumo.uploader._sumofile import SumoFile, _path_to_yaml_path
from fmu.sumo.uploader._logger import get_uploader_logger

from fmu.sumo.uploader._sumofile import SumoFile, _path_to_yaml_path

# pylint: disable=C0103 # allow non-snake case variable names

Expand Down
13 changes: 6 additions & 7 deletions src/fmu/sumo/uploader/_fileonjob.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"""
The FileOnDisk class objectifies a file as it appears
on the disk. A file in this context refers to a data/metadata
pair (technically two files).
The FileOnDisk class objectifies a file as it appears
on the disk. A file in this context refers to a data/metadata
pair (technically two files).
"""

import hashlib
import base64
import hashlib

from fmu.sumo.uploader._sumofile import SumoFile
from fmu.sumo.uploader._logger import get_uploader_logger

from fmu.sumo.uploader._sumofile import SumoFile

# pylint: disable=C0103 # allow non-snake case variable names

logger = get_uploader_logger()


class FileOnJob(SumoFile):
def __init__(self, byte_string: str, metadata):
"""
Expand All @@ -41,4 +41,3 @@ def __init__(self, byte_string: str, metadata):
self.metadata["file"]["checksum_md5"] = digester.hexdigest()

self.metadata["file"]["absolute_path"] = ""

19 changes: 12 additions & 7 deletions src/fmu/sumo/uploader/_logger.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import logging
import sys


# def getLogger(module_name="subscript"):
def get_uploader_logger():
# pylint: disable=invalid-name
"""Provides a unified logger for fmu-sumo-uploader.
Code is copied from
Code is copied from
https://github.com/equinor/subscript/blob/main/src/subscript/__init__.py
Logging output is split by logging levels (split between WARNING and ERROR)
to stdout and stderr, each log occurs in only one of the streams.
to stdout and stderr, each log occurs in only one of the streams.
Returns:
A logger object
"""
logger = logging.getLogger("fmu.sumo.uploader")
logger.propagate = False # Avoids duplicate logging
logger = logging.getLogger("fmu.sumo.uploader")
logger.propagate = False # Avoids duplicate logging

if not len(logger.handlers):
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")

formatter = logging.Formatter(
"%(asctime)s:%(levelname)s:%(name)s:%(message)s"
)

stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(lambda record: record.levelno < logging.ERROR)
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.addFilter(lambda record: record.levelno >= logging.ERROR)
stderr_handler.addFilter(
lambda record: record.levelno >= logging.ERROR
)
stderr_handler.setFormatter(formatter)
logger.addHandler(stderr_handler)

Expand Down
45 changes: 20 additions & 25 deletions src/fmu/sumo/uploader/_sumocase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
"""

import warnings
import time
import datetime
import statistics
import time
import warnings


from fmu.sumo.uploader._upload_files import upload_files
from fmu.sumo.uploader._logger import get_uploader_logger

from fmu.sumo.uploader._upload_files import upload_files

# pylint: disable=C0103 # allow non-snake case variable names

Expand Down Expand Up @@ -82,7 +80,7 @@ def upload(self, threads=4):
ok_uploads = []
failed_uploads = []
rejected_uploads = []
files_to_upload = [f for f in self.files]
files_to_upload = list(self.files)

_t0 = time.perf_counter()

Expand All @@ -102,23 +100,20 @@ def upload(self, threads=4):
rejected_uploads += upload_results.get("rejected_uploads")
failed_uploads = upload_results.get("failed_uploads")

if rejected_uploads:
if any(
[
res.get("metadata_upload_response_status_code") in [404]
for res in rejected_uploads
]
):
warnings.warn("Case is not registered on Sumo")
logger.info(
"Case was not found on Sumo. If you are in the FMU context "
"something may have gone wrong with the case registration "
"or you have not specified that the case shall be uploaded."
"A warning will be issued, and the script will stop. "
"If you are NOT in the FMU context, you can specify that "
"this script also registers the case by passing "
"register=True. This should not be done in the FMU context."
)
if rejected_uploads and any(
res.get("metadata_upload_response_status_code") in [404]
for res in rejected_uploads
):
warnings.warn("Case is not registered on Sumo")
logger.info(
"Case was not found on Sumo. If you are in the FMU context "
"something may have gone wrong with the case registration "
"or you have not specified that the case shall be uploaded."
"A warning will be issued, and the script will stop. "
"If you are NOT in the FMU context, you can specify that "
"this script also registers the case by passing "
"register=True. This should not be done in the FMU context."
)

_dt = time.perf_counter() - _t0

Expand Down Expand Up @@ -218,7 +213,7 @@ def _get_log_msg(sumo_parent_id, status):
status.get("blob_upload_response_status_code")
),
"response_text": (
(status.get("blob_upload_response_status_text"))
status.get("blob_upload_response_status_text")
),
},
}
Expand Down Expand Up @@ -268,7 +263,7 @@ def _sanitize_datetimes(data):
return data.isoformat()

if isinstance(data, dict):
for key in data.keys():
for key in data:
data[key] = _sanitize_datetimes(data[key])

elif isinstance(data, list):
Expand Down
13 changes: 7 additions & 6 deletions src/fmu/sumo/uploader/_sumofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"""

import os
import subprocess
import sys
import time
import subprocess
import warnings

import httpx
from azure.storage.blob import BlobClient, ContentSettings
from fmu.sumo.uploader._logger import get_uploader_logger

from fmu.sumo.uploader._logger import get_uploader_logger

# pylint: disable=C0103 # allow non-snake case variable names

Expand Down Expand Up @@ -88,7 +89,7 @@ def _upload_byte_string(self, blob_url):
content_settings = ContentSettings(
content_type="application/octet-stream"
)
response = blobclient.upload_blob(
blobclient.upload_blob(
self.byte_string,
blob_type="BlockBlob",
length=len(self.byte_string),
Expand Down Expand Up @@ -135,9 +136,9 @@ def upload_to_sumo(self, sumo_parent_id, sumoclient, sumo_mode):
{
"status": "rejected",
"metadata_upload_response_status_code": 500,
"metadata_upload_response_text": "File upload cannot be attempted; this is a seismic data object but it does not have a value for data.vertical_domain."
}
)
"metadata_upload_response_text": "File upload cannot be attempted; this is a seismic data object but it does not have a value for data.vertical_domain.",
}
)
return result

try:
Expand Down
6 changes: 4 additions & 2 deletions src/fmu/sumo/uploader/_upload_files.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""
The function that uploads files.
The function that uploads files.
"""

import json
from concurrent.futures import ThreadPoolExecutor
from copy import deepcopy
import json

import yaml

from fmu.dataio._utils import read_parameters_txt
from fmu.dataio.dataio import ExportData
from fmu.sumo.uploader._fileonjob import FileOnJob
Expand Down
Loading

0 comments on commit 6c9c667

Please sign in to comment.