Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various: better code #62

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 40 additions & 54 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jinja2 = "<3.0.0"
itsdangerous = "<2.0.0"
werkzeug = "<2.0.0"
click = '<8.0.0'
pytest-invenio = ">=1.4.0,<1.4.12"
sentry-sdk = "<1.6.1"

[tool.poetry.dev-dependencies]
## Python packages development dependencies (order matters)
Expand Down
21 changes: 9 additions & 12 deletions rero_ebooks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

"""API for manipulating Ebooks records."""


import contextlib
import copy
from uuid import uuid4

Expand Down Expand Up @@ -124,11 +126,10 @@ def _delete_uri(cls, not_available_record, old_record, url):
for e_res in not_available_e_res:
# check if exists!
res_url = e_res.get('uniform_resource_identifier')
if res_url.startswith(url):
if e_res in old_e_res:
epub_count += 1
old_e_res.remove(e_res)
old_record['__order__'].remove(field)
if res_url.startswith(url) and e_res in old_e_res:
epub_count += 1
old_e_res.remove(e_res)
old_record['__order__'].remove(field)

return old_record, epub_count

Expand All @@ -142,7 +143,7 @@ def remove_uri(cls, data, vendor=None, url=None,
merged_data, epub_count = cls._delete_uri(data, record, url)
record.replace(merged_data, dbcommit=dbcommit, reindex=reindex,
forceindex=reindex)
return record, 'REMOVE URIs: {count}'.format(count=epub_count)
return record, f'REMOVE URIs: {epub_count}'
data['pid'] = pid
return data, 'REMOVE missing'

Expand Down Expand Up @@ -216,9 +217,7 @@ def replace(self, data, dbcommit=False, reindex=False, forceindex=False):
new_data = copy.deepcopy(data)
pid = new_data.get('pid')
if not pid:
raise EbookError.PidMissing(
'missing pid={pid}'.format(pid=self.pid)
)
raise EbookError.PidMissing(f'missing pid={self.pid}')
self.clear()
self = self.update(new_data, dbcommit=dbcommit, reindex=reindex,
forceindex=forceindex)
Expand All @@ -239,10 +238,8 @@ def reindex(self, forceindex=False):

def delete_from_index(self):
"""Delete record from index."""
try:
with contextlib.suppress(NotFoundError):
RecordIndexer().delete(self)
except NotFoundError:
pass

@classmethod
def get_persistent_identifier(cls, pid):
Expand Down
33 changes: 10 additions & 23 deletions rero_ebooks/apiharvester/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ def api_source_config(name, url, classname, code, update):
code=code,
update=update
)
click.echo(
'{msg} ApiHarvestConfig: {name}'.format(
msg=msg,
name=name
)
)
click.echo(f'{msg} ApiHarvestConfig: {name}')


@apiharvester.command('sources')
Expand All @@ -73,8 +68,7 @@ def api_source_config(name, url, classname, code, update):
@with_appcontext
def api_source_config_from_file(configfile, update):
"""Add or update ApiHarvestConfigs from file."""
configs = yaml.load(configfile, Loader=yaml.FullLoader)
if configs:
if configs := yaml.load(configfile, Loader=yaml.FullLoader):
for name, values in sorted(configs.items()):
url = values.get('url', '')
classname = values.get('classname', '')
Expand All @@ -86,18 +80,11 @@ def api_source_config_from_file(configfile, update):
code=code,
update=update
)
click.echo(
'{msg} ApiHarvestConfig: {name}'.format(
msg=msg,
name=name
)
)
click.echo(f'{msg} ApiHarvestConfig: {name}')

else:
click.secho(
'ERROR: no YML config found in: {filename}'.format(
filename=configfile.name
)
f'ERROR: no YML config found in: {configfile.name}'
)


Expand Down Expand Up @@ -131,14 +118,14 @@ def init_oai_sets(configfile, verbose):
def harvest(name, from_date, enqueue, max, verbose):
"""Harvest records from an API repository."""
if name:
click.secho('Harvest api: {name}'.format(name=name), fg='green')
click.secho(f'Harvest api: {name}', fg='green')
if from_date:
from_date = parser.parse(from_date).isoformat()
if enqueue:
async_id = harvest_records.delay(name=name, from_date=from_date,
max=max, verbose=verbose)
if verbose:
click.echo('AsyncResult {id}'.format(id=async_id))
click.echo(f'AsyncResult {async_id}')
else:
harvest_records(name=name, from_date=from_date,
max=max, verbose=verbose)
Expand All @@ -151,7 +138,7 @@ def info():
apis = ApiHarvestConfig.query.all()
for api in apis:
click.echo(api.name)
click.echo('\tlastrun : {lastrun}'.format(lastrun=api.lastrun))
click.echo('\turl : {url}'.format(url=api.url))
click.echo('\tclassname : {classname}'.format(classname=api.classname))
click.echo('\tcode : {code}'.format(code=api.code))
click.echo(f'\tlastrun : {api.lastrun}')
click.echo(f'\turl : {api.url}')
click.echo(f'\tclassname : {api.classname}')
click.echo(f'\tcode : {api.code}')
Loading