Skip to content

Commit

Permalink
chore: lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jul 23, 2024
1 parent 87e9ba2 commit 917631a
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 56 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [v1.0.0](https://github.com/DataShades/ckanext-ingest/releases/tag/v1.0.0) - 2022-02-07

<small>[Compare with first commit](https://github.com/DataShades/ckanext-ingest/compare/5218fb4ae2e6c806e027ff44a5a17bd41377967c...v1.0.0)</small>

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,4 @@ specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include README.rst
include LICENSE
include requirements.txt
recursive-include ckanext/ingest *.html *.json *.js *.less *.css *.mo *.yaml *.yml
recursive-include ckanext/ingest *.html *.json *.js *.less *.css *.mo *.yaml *.yml
3 changes: 0 additions & 3 deletions ckanext/ingest/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def ingest_extract_records(
`iter_records` function that produces an iterable over records.
Args:
source: str|FileStorage - data source for records
strategy: str|None - record extraction strategy. If missing, strategy
Expand Down Expand Up @@ -62,7 +61,6 @@ def ingest_import_records(context: types.Context, data_dict: dict[str, Any]):
`Record.ingest`, potentially creating/updating data.
Args:
source: str|FileStorage - data source for records
strategy: str|None - record extraction strategy. If missing, strategy
Expand All @@ -80,7 +78,6 @@ def ingest_import_records(context: types.Context, data_dict: dict[str, Any]):
take: int - max number of records that will be ingested
"""

tk.check_access("ingest_import_records", context, data_dict)

start = data_dict["skip"]
Expand Down
2 changes: 1 addition & 1 deletion ckanext/ingest/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def into_uploaded_file(value: Any):
"""Try converting value into shared.Storage object"""
"""Try converting value into shared.Storage object."""
if isinstance(value, shared.Storage):
return value

Expand Down
1 change: 1 addition & 0 deletions ckanext/ingest/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def ingest_into_uuid(value: str):
def ingest_munge_name(value: str):
return munge.munge_name(value)


def ingest_strip_prefix(prefix: str):
def validator(value: Any):
if isinstance(value, str) and value.startswith(prefix):
Expand Down
26 changes: 16 additions & 10 deletions ckanext/ingest/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

import contextlib

import ckan.plugins.toolkit as tk
from ckan import common, plugins
from ckan import common
from ckan import plugins as p

from . import config, interfaces, shared

Expand All @@ -12,10 +15,10 @@
@tk.blanket.cli
@tk.blanket.blueprints
@tk.blanket.config_declarations
class IngestPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IConfigurable)
plugins.implements(interfaces.IIngest, inherit=True)
class IngestPlugin(p.SingletonPlugin):
p.implements(p.IConfigurer)
p.implements(p.IConfigurable)
p.implements(interfaces.IIngest, inherit=True)

# IConfigurer

Expand All @@ -31,7 +34,7 @@ def configure(self, config_: common.CKANConfig):
blacklist = config.disabled_strategies()
name_mapping = config.name_mapping()

for plugin in plugins.PluginImplementations(interfaces.IIngest):
for plugin in p.PluginImplementations(interfaces.IIngest):
for name, s in plugin.get_ingest_strategies().items():
final_name = name_mapping.get(f"{s.__module__}:{s.__name__}", name)

Expand All @@ -45,13 +48,16 @@ def configure(self, config_: common.CKANConfig):

# IIngest
def get_ingest_strategies(self) -> dict[str, type[shared.ExtractionStrategy]]:
from .strategy import csv, xlsx, zip
from .strategy import csv, zip

strategies = {
strategies: dict[str, type[shared.ExtractionStrategy]] = {
"ingest:recursive_zip": zip.ZipStrategy,
"ingest:scheming_csv": csv.CsvStrategy,
}
if xlsx.is_installed:
strategies["ingest:xlsx"] = xlsx.XlsxStrategy

with contextlib.suppress(ImportError):
from .strategy.xlsx import XlsxStrategy

strategies["ingest:xlsx"] = XlsxStrategy

return strategies
15 changes: 6 additions & 9 deletions ckanext/ingest/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def must_handle(cls, mime: str | None, source: Storage) -> bool:
return False

def chunks(self, source: Storage, options: StrategyOptions) -> Iterable[Any]:
"""Iterate over separate chunks of data with correpoinding options
suitable for Record creation."""
"""Produce chunks of data."""
return []

def chunk_into_record(self, chunk: Any, options: StrategyOptions) -> Record:
Expand Down Expand Up @@ -187,24 +186,22 @@ def make_storage(
name: str | None = None,
mimetype: str | None = None,
):
"""Transform bytes stream(BytesIO), string or bytes into source object
expected by ExtractionStrategy.
"""Transform incoming stream into standard format.
Transform bytes stream(BytesIO), string or bytes into source object
expected by ExtractionStrategy.
"""
if isinstance(stream, str):
stream = stream.encode()

if isinstance(stream, bytes):
if isinstance(stream, (bytes, bytearray, memoryview)):
stream = BytesIO(stream)

return Storage(stream, name, content_type=mimetype)


def get_extra(options: StrategyOptions | RecordOptions, key: str, default: T) -> T:
"""Safely return an item from `extras` member of strategy or record
options.
"""
"""Safely return an item from strategy `extras`."""
return options.setdefault("extras", {}).get(key, default)


Expand Down
1 change: 0 additions & 1 deletion ckanext/ingest/strategy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

4 changes: 3 additions & 1 deletion ckanext/ingest/strategy/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CsvStrategy(shared.ExtractionStrategy):
record_factory = PackageRecord

def chunks(
self, source: shared.Storage, options: shared.StrategyOptions,
self,
source: shared.Storage,
options: shared.StrategyOptions,
) -> Iterable[dict[str, Any]]:
reader_options: dict[str, Any] = shared.get_extra(options, "reader_options", {})
str_stream = StringIO(source.read().decode())
Expand Down
18 changes: 8 additions & 10 deletions ckanext/ingest/strategy/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
resources to rewrite it and create a proper base XLSX strategy.
"""

from __future__ import annotations

import logging
from typing import IO, Callable, Iterable, TypedDict, cast

from ckanext.ingest import shared

try:
from openpyxl import Workbook, load_workbook
from openpyxl.worksheet.worksheet import Worksheet
from openpyxl import Workbook, load_workbook
from openpyxl.worksheet.worksheet import Worksheet

is_installed = True
except ImportError:
is_installed = False
from ckanext.ingest import shared

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -60,11 +56,13 @@ def chunks(
yield {
"sheet": sheet,
"document": doc,
"locator": lambda name: doc[name] if name in doc else None,
"locator": lambda name: doc.get(name, None), # type: ignore
}

def extract(
self, source: shared.Storage, options: shared.StrategyOptions,
self,
source: shared.Storage,
options: shared.StrategyOptions,
) -> Iterable[shared.Record]:
extras = options.get("extras", {})

Expand Down
2 changes: 1 addition & 1 deletion ckanext/ingest/strategy/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ZipChunk(TypedDict):
handler: shared.ExtractionStrategy
name: str
source: shared.Storage
locator: Callable[[str], IO[bytes]]
locator: Callable[[str], IO[bytes] | None]


class ZipStrategy(shared.ExtractionStrategy):
Expand Down
5 changes: 3 additions & 2 deletions ckanext/ingest/tests/logic/test_action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import mimetypes
import os
from typing import Optional

import pytest

Expand All @@ -14,7 +15,7 @@
def source():
data = os.path.join(os.path.dirname(__file__), "data")

def reader(filename: str, mime: Optional[str] = None):
def reader(filename: str, mime: str | None = None):
if mime is None:
mime, _enc = mimetypes.guess_type(filename)

Expand Down
9 changes: 6 additions & 3 deletions ckanext/ingest/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

_default = object()


@dataclasses.dataclass
class Options:
"""Transformation options taken from `{profile}_options` attribute of field
in ckanext-scheming's schema
"""Transformation options.
This information is taken from `{profile}_options` attribute of field in
ckanext-scheming's schema.
These options define how raw value passed into the Record transforms into
proper value that is suitable for the entity's schema. Workflow is the
Expand Down Expand Up @@ -161,7 +164,7 @@ def _normalize_choice(
choices: list[dict[str, str]],
separator: str,
) -> str | list[str] | None:
"""Transform select label[s] into corresponding value[s]"""
"""Transform select label[s] into corresponding value[s]."""
if not value:
return None

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ignore = [
"PLC1901", # simplify comparison to empty string: violated by SQLAlchemy filters
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"ckanext/ingest/tests*" = ["S", "PL", "ANN"]
"ckanext/ingest/logic/schema.py" = ["PLR0913"]
"ckanext/ingest/logic/*" = [
Expand Down
10 changes: 0 additions & 10 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,3 @@ previous = true
domain = ckanext-ingest
directory = ckanext/ingest/i18n
statistics = true


[tool:pytest]
filterwarnings =
ignore::sqlalchemy.exc.SADeprecationWarning
ignore::sqlalchemy.exc.SAWarning
ignore::DeprecationWarning

addopts = --ckan-ini test.ini
testpaths = ckanext/ingest/tests
2 changes: 1 addition & 1 deletion test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s

0 comments on commit 917631a

Please sign in to comment.