Skip to content

Commit

Permalink
chore: Add ruff lint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 16, 2024
1 parent 6ea2f15 commit 65c8c6d
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 134 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ci:
autoupdate_schedule: quarterly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
hooks:
- id: ruff
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ recursive-include tests *.jsonl
recursive-include tests *.py
recursive-include tests *.yaml
recursive-include tests *.gz
exclude .pre-commit-config.yaml
exclude .readthedocs.yaml
16 changes: 9 additions & 7 deletions ocdskit/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _package(key, items, uri, publisher, published_date, version, extensions=Non

def package_records(records, uri='', publisher=None, published_date='', version=DEFAULT_VERSION, extensions=None):
"""
Wraps records in a record package.
Wrap records in a record package.
:param list records: a list of records
:param str uri: the record package's ``uri``
Expand All @@ -56,7 +56,7 @@ def package_records(records, uri='', publisher=None, published_date='', version=

def package_releases(releases, uri='', publisher=None, published_date='', version=DEFAULT_VERSION, extensions=None):
"""
Wraps releases in a release package.
Wrap releases in a release package.
:param list releases: a list of releases
:param str uri: the release package's ``uri``
Expand All @@ -70,9 +70,9 @@ def package_releases(releases, uri='', publisher=None, published_date='', versio

def combine_record_packages(packages, uri='', publisher=None, published_date='', version=DEFAULT_VERSION):
"""
Collects the packages and records from the record packages into one record package.
Collect the packages and records from the record packages into one record package.
Warns ``~ocdskit.exceptions.MissingRecordsWarning`` if the "records" field is missing from a record package.
Warn ``~ocdskit.exceptions.MissingRecordsWarning`` if the "records" field is missing from a record package.
:param packages: an iterable of record packages
:param str uri: the record package's ``uri``
Expand All @@ -92,6 +92,7 @@ def combine_record_packages(packages, uri='', publisher=None, published_date='',
warnings.warn(
f'item {i} has no "records" field (check that it is a record package)',
category=MissingRecordsWarning,
stacklevel=2,
)
if 'packages' in package:
output['packages'].update(dict.fromkeys(package['packages']))
Expand All @@ -108,9 +109,9 @@ def combine_record_packages(packages, uri='', publisher=None, published_date='',

def combine_release_packages(packages, uri='', publisher=None, published_date='', version=DEFAULT_VERSION):
"""
Collects the releases from the release packages into one release package.
Collect the releases from the release packages into one release package.
Warns ``~ocdskit.exceptions.MissingReleasesWarning`` if the "releases" field is missing from a release package.
Warn ``~ocdskit.exceptions.MissingReleasesWarning`` if the "releases" field is missing from a release package.
:param packages: an iterable of release packages
:param str uri: the release package's ``uri``
Expand All @@ -129,6 +130,7 @@ def combine_release_packages(packages, uri='', publisher=None, published_date=''
warnings.warn(
f'item {i} has no "releases" field (check that it is a release package)',
category=MissingReleasesWarning,
stacklevel=2,
)

if publisher:
Expand Down Expand Up @@ -157,7 +159,7 @@ def merge(
convert_exceptions_to_warnings: bool = False,
):
"""
Merges release packages and individual releases.
Merge release packages and individual releases.
By default, yields compiled releases. If ``return_versioned_release`` is ``True``, yields versioned releases. If
``return_package`` is ``True``, wraps the compiled releases (and versioned releases if ``return_versioned_release``
Expand Down
42 changes: 11 additions & 31 deletions ocdskit/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,38 @@ class BaseCommand(ABC):
kwargs = {} # noqa: RUF012

def __init__(self, subparsers):
"""
Initializes the subparser and adds arguments.
"""
"""Initialize the subparser and add arguments."""
self.subparser = subparsers.add_parser(self.name, description=self.help, **self.kwargs)
self.add_base_arguments()
self.add_arguments()
self.args = None

def add_base_arguments(self): # noqa: B027 # noop
"""
Adds default arguments to all commands.
"""
"""Add default arguments to all commands."""

def add_arguments(self): # noqa: B027 # noop
"""
Adds arguments specific to this command.
"""
"""Add arguments specific to this command."""

def add_argument(self, *args, **kwargs):
"""
Adds an argument to the subparser.
"""
"""Add an argument to the subparser."""
self.subparser.add_argument(*args, **kwargs)

@abstractmethod
def handle(self):
"""
Runs the command.
"""
"""Run the command."""

def prefix(self):
"""
Returns the path to the items to process within each input.
"""
"""Return the path to the items to process within each input."""
return ''

def items(self, **kwargs):
"""
Yields the items in the input.
"""
"""Yield the items in the input."""
file = StandardInputReader(self.args.encoding)
yield from ijson.items(file, self.prefix(), multiple_values=True, **kwargs)

def print(self, data, *, streaming=False):
"""
Prints JSON data.
Print JSON data.
:param bool streaming: whether to stream output using ``json.JSONEncoder().iterencode()`` (it is only more
memory efficient if ``data`` contains iterators)
Expand Down Expand Up @@ -100,19 +86,15 @@ def prefix(self):
return self.args.root_path

def items(self, **kwargs):
"""
Yields the items in the input. If an item is an array, yields each entry of the array.
"""
"""Yield the items in the input. If an item is an array, yield each entry of the array."""
for item in super().items(**kwargs):
if isinstance(item, list):
yield from item
else:
yield item

def add_package_arguments(self, infix, prefix='', version='1.1'):
"""
Adds arguments for setting package metadata to the subparser.
"""
"""Add arguments for setting package metadata to the subparser."""
template = f"{prefix}set the {infix} package's {{}} to this value"

self.add_argument('--uri', type=str, default='', help=template.format('uri'))
Expand All @@ -126,9 +108,7 @@ def add_package_arguments(self, infix, prefix='', version='1.1'):
help=f"{prefix}set the {infix} package's required metadata to dummy values")

def parse_package_arguments(self):
"""
Returns package metadata as a dictionary to be used as keyword arguments.
"""
"""Return package metadata as a dictionary to be used as keyword arguments."""
kwargs = {
'uri': self.args.uri,
'publisher': {},
Expand Down
3 changes: 1 addition & 2 deletions ocdskit/commands/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ def handle(self):
raise CommandError(message) from e

for data in self.items(map_type=OrderedDict):
data = upgrade_method(data)
self.print(data)
self.print(upgrade_method(data))
22 changes: 11 additions & 11 deletions ocdskit/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class OCDSKitError(Exception):
"""Base class for exceptions from within this package"""
"""Base class for exceptions from within this package."""


class CommandError(OCDSKitError):
"""Errors from within this package's CLI"""
"""Errors from within this package's CLI."""


class InconsistentVersionError(OCDSKitError):
"""Raised if the versions are inconsistent across packages to compile"""
"""Raised if the versions are inconsistent across packages to compile."""

def __init__(self, message, earlier_version=None, current_version=None):
self.earlier_version = earlier_version
Expand All @@ -16,32 +16,32 @@ def __init__(self, message, earlier_version=None, current_version=None):


class MissingColumnError(OCDSKitError):
"""Raised if the column by which to order is missing"""
"""Raised if the column by which to order is missing."""


class UnknownFormatError(OCDSKitError):
"""Raised if the format of a file can't be determined"""
"""Raised if the format of a file can't be determined."""


class UnknownVersionError(OCDSKitError):
"""Raised if the OCDS version is not recognized"""
"""Raised if the OCDS version is not recognized."""


class MissingOcidKeyError(OCDSKitError, KeyError):
"""Raised if a release to be merged is missing an ``ocid`` field"""
"""Raised if a release to be merged is missing an ``ocid`` field."""


class OCDSKitWarning(UserWarning):
"""Base class for warnings from within this package"""
"""Base class for warnings from within this package."""


class MissingRecordsWarning(OCDSKitWarning):
"""Used when the "records" field is missing from a record package when combining packages"""
"""Used when the "records" field is missing from a record package when combining packages."""


class MissingReleasesWarning(OCDSKitWarning):
"""Used when the "releases" field is missing from a release package when combining packages"""
"""Used when the "releases" field is missing from a release package when combining packages."""


class MergeErrorWarning(OCDSKitWarning):
"""Used when downgrading an OCDS Merge exception to a warning"""
"""Used when downgrading an OCDS Merge exception to a warning."""
2 changes: 1 addition & 1 deletion ocdskit/mapping_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def mapping_sheet(schema, *, order_by=None, infer_required=False, extension_field=None, inherit_extension=True,
include_codelist=False, include_deprecated=True, include_definitions=False, base_uri=None):
"""
Returns information about all field paths in a JSON Schema, as columns and rows.
Return information about all field paths in a JSON Schema, as columns and rows.
If ``include_definitions=False``, this function resolves ``$ref`` properties.
Expand Down
26 changes: 12 additions & 14 deletions ocdskit/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __exit__(self, type_, value, traceback):

def add(self, data, *, ignore_version: bool = False):
"""
Adds release packages and/or individual releases to be merged.
Add release packages and/or individual releases to be merged.
:param data: an iterable of release packages and individual releases
:param ignore_version: do not raise an error if the versions are inconsistent across items to merge
Expand Down Expand Up @@ -127,7 +127,7 @@ def output_package(
convert_exceptions_to_warnings: bool = False,
):
"""
Yields a record package.
Yield a record package.
:param merger: a merger
:param return_versioned_release: whether to include a versioned release in each record
Expand Down Expand Up @@ -164,7 +164,7 @@ def output_records(
convert_exceptions_to_warnings: bool = False,
):
"""
Yields records, ordered by OCID.
Yield records, ordered by OCID.
:param merger: a merger
:param return_versioned_release: whether to include a versioned release in the record
Expand Down Expand Up @@ -201,7 +201,7 @@ def output_records(
record['versionedRelease'] = merger.create_versioned_release(releases)
except InconsistentTypeError as e:
if convert_exceptions_to_warnings:
warnings.warn(str(e), category=MergeErrorWarning)
warnings.warn(str(e), category=MergeErrorWarning, stacklevel=2)
else:
raise

Expand All @@ -215,7 +215,7 @@ def output_releases(
convert_exceptions_to_warnings: bool = False,
):
"""
Yields compiled releases or versioned releases, ordered by OCID.
Yield compiled releases or versioned releases, ordered by OCID.
:param merger: a merger
:param return_versioned_release: whether to yield versioned releases instead of compiled releases
Expand All @@ -235,7 +235,7 @@ def output_releases(
yield merger.create_compiled_release(releases)
except InconsistentTypeError as e:
if convert_exceptions_to_warnings:
warnings.warn(MergeErrorWarning(str(e)))
warnings.warn(MergeErrorWarning(str(e)), stacklevel=2)
else:
raise

Expand All @@ -249,7 +249,9 @@ def output_releases(
class AbstractBackend(ABC):
def add_release(self, release, package_uri):
"""
Adds a release to the backend. (The release might be added to an internal buffer.)
Add a release to the backend.
(The release might be added to an internal buffer.)
:raises MissingOcidKeyError: if the release is missing an ``ocid`` field
"""
Expand All @@ -265,20 +267,16 @@ def _add_release(self, ocid, package_uri, release):
@abstractmethod
def get_releases_by_ocid(self):
"""
Yields an OCIDs and an iterable of tuples of ``(ocid, package_uri, release)``.
Yield an OCIDs and an iterable of tuples of ``(ocid, package_uri, release)``.
OCIDs are yielded in alphabetical order. The iterable is in any order.
"""

def flush(self): # noqa: B027 # noop
"""
Flushes the internal buffer of releases. This may be a no-op on some backends.
"""
"""Flushes the internal buffer of releases. This may be a no-op on some backends."""

def close(self): # noqa: B027 # noop
"""
Tidies up any resources used by the backend. This may be a no-op on some backends.
"""
"""Tidies up any resources used by the backend. This may be a no-op on some backends."""


class PythonBackend(AbstractBackend):
Expand Down
Loading

0 comments on commit 65c8c6d

Please sign in to comment.