Skip to content

Commit

Permalink
global: add pre-commit with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalEgn committed Aug 6, 2024
1 parent 81cfdd5 commit 0892e12
Show file tree
Hide file tree
Showing 70 changed files with 3,475 additions and 2,780 deletions.
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https:/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: name-tests-test
args: [ --pytest-test-first ]
exclude: '^(?!factories/)'
- repo: https:/astral-sh/ruff-pre-commit
rev: v0.5.6
hooks:
- id: ruff
args: [ --fix, --unsafe-fixes ]
6 changes: 3 additions & 3 deletions inspire_dojson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from __future__ import absolute_import, division, print_function

from . import common # noqa: F401
from .api import marcxml2record, record2marcxml # noqa: F401
from .errors import DoJsonError # noqa: F401
from inspire_dojson import common # noqa: F401
from inspire_dojson.api import marcxml2record, record2marcxml # noqa: F401
from inspire_dojson.errors import DoJsonError # noqa: F401

__version__ = "63.2.22"
48 changes: 28 additions & 20 deletions inspire_dojson/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@
import re
from itertools import chain

from dojson.contrib.marc21.utils import create_record
from inspire_utils.helpers import force_list
from inspire_utils.record import get_value
from lxml.builder import E
from lxml.etree import tostring
from six import iteritems, text_type, unichr
from six.moves import urllib

from dojson.contrib.marc21.utils import create_record

from inspire_dojson.cds import cds2hep_marc
from inspire_dojson.conferences import conferences
from inspire_dojson.data import data
from inspire_dojson.errors import NotSupportedError
from inspire_dojson.experiments import experiments
from inspire_dojson.hep import hep, hep2marc
from inspire_dojson.hepnames import hepnames, hepnames2marc
from inspire_dojson.institutions import institutions
from inspire_dojson.journals import journals
from inspire_dojson.utils import create_record_from_dict, force_single_element
from inspire_utils.helpers import force_list
from inspire_utils.record import get_value

from .cds import cds2hep_marc
from .conferences import conferences
from .data import data
from .errors import NotSupportedError
from .experiments import experiments
from .hep import hep, hep2marc
from .hepnames import hepnames, hepnames2marc
from .institutions import institutions
from .journals import journals

try:
unichr(0x100000)
RE_INVALID_CHARS_FOR_XML = re.compile(
u'[^\U00000009\U0000000A\U0000000D\U00000020-\U0000D7FF\U0000E000-\U0000FFFD\U00010000-\U0010FFFF]+')
u'[^\U00000009\U0000000A\U0000000D\U00000020-\U0000D7FF\U0000E000-\U0000FFFD\U00010000-\U0010FFFF]+'
)
except ValueError: # pragma: no cover
RE_INVALID_CHARS_FOR_XML = re.compile(
u'[^\U00000009\U0000000A\U0000000D\U00000020-\U0000D7FF\U0000E000-\U0000FFFD]+')
u'[^\U00000009\U0000000A\U0000000D\U00000020-\U0000D7FF\U0000E000-\U0000FFFD]+'
)

RECORD = E.record
CONTROLFIELD = E.controlfield
Expand Down Expand Up @@ -107,7 +107,9 @@ def record2marcxml_etree(record):
elif schema_name == 'authors':
marcjson = hepnames2marc.do(record)
else:
raise NotSupportedError(u'JSON -> MARC rules missing for "{}"'.format(schema_name))
raise NotSupportedError(
u'JSON -> MARC rules missing for "{}"'.format(schema_name)
)

record = RECORD()

Expand All @@ -117,15 +119,19 @@ def record2marcxml_etree(record):
value = force_single_element(values)
if not isinstance(value, text_type):
value = text_type(value)
record.append(CONTROLFIELD(_strip_invalid_chars_for_xml(value), {'tag': tag}))
record.append(
CONTROLFIELD(_strip_invalid_chars_for_xml(value), {'tag': tag})
)
else:
for value in force_list(values):
datafield = DATAFIELD({'tag': tag, 'ind1': ind1, 'ind2': ind2})
for code, els in sorted(iteritems(value)):
for el in force_list(els):
if not isinstance(el, text_type):
el = text_type(el)
datafield.append(SUBFIELD(_strip_invalid_chars_for_xml(el), {'code': code}))
datafield.append(
SUBFIELD(_strip_invalid_chars_for_xml(el), {'code': code})
)
record.append(datafield)

return record
Expand Down Expand Up @@ -155,7 +161,9 @@ def cds_marcxml2record(marcxml):


def _get_collections(marcjson):
collections = chain.from_iterable([force_list(el) for el in force_list(get_value(marcjson, '980__.a'))])
collections = chain.from_iterable(
[force_list(el) for el in force_list(get_value(marcjson, '980__.a'))]
)
normalized_collections = [el.lower() for el in collections]

return normalized_collections
Expand Down
4 changes: 2 additions & 2 deletions inspire_dojson/cds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@

from __future__ import absolute_import, division, print_function

from . import rules # noqa: F401
from .model import cds2hep_marc # noqa: F401
from inspire_dojson.cds import rules # noqa: F401
from inspire_dojson.cds.model import cds2hep_marc # noqa: F401
31 changes: 19 additions & 12 deletions inspire_dojson/cds/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,35 @@
from __future__ import absolute_import, division, print_function

from itertools import chain
from inspire_utils.record import get_value

from inspire_utils.helpers import force_list
from inspire_utils.record import get_value

from ..model import FilterOverdo, clean_record
from inspire_dojson.model import FilterOverdo, clean_record


def add_control_number(record, blob):
if '001' not in blob:
return record

collections = (value.lower() for value in chain(force_list(get_value(blob, '980__.a', default=[])),
force_list(get_value(blob, '980__.c', default=[]))))
collections = (
value.lower()
for value in chain(
force_list(get_value(blob, '980__.a', default=[])),
force_list(get_value(blob, '980__.c', default=[])),
)
)
if 'hidden' in collections:
record.setdefault('595__', []).append({
'9': 'CDS',
'a': u'CDS-{}'.format(blob['001'])
})
record.setdefault('595__', []).append(
{'9': 'CDS', 'a': u'CDS-{}'.format(blob['001'])}
)
else:
record.setdefault('035__', []).append({
'9': 'CDS',
'a': blob['001'],
})
record.setdefault('035__', []).append(
{
'9': 'CDS',
'a': blob['001'],
}
)

return record

Expand Down
Loading

0 comments on commit 0892e12

Please sign in to comment.