Skip to content

Commit

Permalink
f-strings housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Mar 23, 2024
1 parent 1e6f58f commit 6a8f4d4
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 382 deletions.
16 changes: 7 additions & 9 deletions woudc_data_registry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2021 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -88,20 +88,18 @@
msg = 'WDR_DB_NAME e is not set!'
LOGGER.error(msg)
raise EnvironmentError(msg)
WDR_DATABASE_URL = '{}:///{}'.format(WDR_DB_TYPE, WDR_DB_NAME)
WDR_DATABASE_URL = f'{WDR_DB_TYPE}:///{WDR_DB_NAME}'
else:
if None in [WDR_DB_USERNAME, WDR_DB_PASSWORD, WDR_SEARCH_TYPE,
WDR_SEARCH_URL, WDR_WAF_BASEDIR, WDR_WAF_BASEURL]:
msg = 'System environment variables are not set!'
LOGGER.error(msg)
raise EnvironmentError(msg)

WDR_DATABASE_URL = '{}://{}:{}@{}:{}/{}'.format(WDR_DB_TYPE,
WDR_DB_USERNAME,
WDR_DB_PASSWORD,
WDR_DB_HOST,
WDR_DB_PORT,
WDR_DB_NAME)
auth = f'{WDR_DB_USERNAME}:{WDR_DB_PASSWORD}'
host_port_name = f'{WDR_DB_HOST}:{WDR_DB_PORT}/{WDR_DB_NAME}'

WDR_DATABASE_URL = f'{WDR_DB_TYPE}://{auth}@{host_port_name}'

if None in [WDR_ERROR_CONFIG, WDR_EXTRA_CONFIG]:
msg = 'Central configuration environment variables are not set!'
Expand All @@ -113,6 +111,6 @@
with open(WDR_EXTRA_CONFIG) as extra_config_file:
EXTRAS = yaml.safe_load(extra_config_file)
except Exception as err:
msg = 'Failed to read extra configurations file due to: {}'.format(err)
msg = f'Failed to read extra configurations file: {err}'
LOGGER.error(msg)
raise EnvironmentError(msg)
19 changes: 9 additions & 10 deletions woudc_data_registry/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -113,7 +113,7 @@ def orchestrate(source, working_dir, metadata_only=False,
run_report = RunReport(working_dir)

for file_to_process, contributor in run_:
click.echo('Processing filename: {}'.format(file_to_process))
click.echo(f'Processing filename: {file_to_process}')

LOGGER.info('Detecting file')
if not is_text_file(file_to_process):
Expand Down Expand Up @@ -171,31 +171,31 @@ def orchestrate(source, working_dir, metadata_only=False,
passed.append(file_to_process)

except UnicodeDecodeError as err:
LOGGER.error('Unknown file format: {}'.format(err))
LOGGER.error(f'Unknown file format: {err}')

click.echo('Not ingested')
failed.append(file_to_process)

op_report.write_failing_file(file_to_process, contributor)
run_report.write_failing_file(file_to_process, contributor)
except NonStandardDataError as err:
LOGGER.error('Invalid Extended CSV: {}'.format(err.errors))
LOGGER.error(f'Invalid Extended CSV: {err.errors}')

click.echo('Not ingested')
failed.append(file_to_process)

op_report.write_failing_file(file_to_process, contributor)
run_report.write_failing_file(file_to_process, contributor)
except MetadataValidationError as err:
LOGGER.error('Invalid Extended CSV: {}'.format(err.errors))
LOGGER.error(f'Invalid Extended CSV: {err.errors}')

click.echo('Not ingested')
failed.append(file_to_process)

op_report.write_failing_file(file_to_process, contributor)
run_report.write_failing_file(file_to_process, contributor)
except Exception as err:
click.echo('Processing failed: {}'.format(err))
click.echo(f'Processing failed: {err}')
failed.append(file_to_process)

op_report.write_failing_file(file_to_process, contributor)
Expand All @@ -205,12 +205,11 @@ def orchestrate(source, working_dir, metadata_only=False,

for name in files_to_process:
if name in passed:
click.echo('Pass: {}'.format(name))
click.echo(f'Pass: {name}')
elif name in failed:
click.echo('Fail: {}'.format(name))
click.echo(f'Fail: {name}')

click.echo('({}/{} files passed)'
.format(len(passed), len(files_to_process)))
click.echo(f'({len(passed)}/{len(files_to_process)} files passed)')


@click.group()
Expand Down
6 changes: 3 additions & 3 deletions woudc_data_registry/dataset_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -80,7 +80,7 @@ def get_validator(dataset, reporter):
elif dataset in DATASETS:
return DatasetValidator(reporter)
else:
raise ValueError('Invalid dataset {}'.format(dataset))
raise ValueError(f'Invalid dataset {dataset}')


class DatasetValidator(object):
Expand Down Expand Up @@ -628,7 +628,7 @@ def check_time_series(self, extcsv):
level = extcsv.extcsv['CONTENT']['Level']
data_table = 'N14_VALUES' if level == 1.0 else 'C_PROFILE'

LOGGER.debug('Assessing order of #{}.Date column'.format(data_table))
LOGGER.debug(f'Assessing order of #{data_table}.Date column')
success = True

data_table_valueline = extcsv.line_num(data_table) + 2
Expand Down
12 changes: 6 additions & 6 deletions woudc_data_registry/epicentre/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -74,7 +74,7 @@ def list_(ctx):
"""List all contributors"""

for c in get_metadata(Contributor):
click.echo('{} {}'.format(c.contributor_id.ljust(24), c.name))
click.echo(f'{c.contributor_id.ljust(24)} {c.name}')


@click.command('show')
Expand Down Expand Up @@ -128,7 +128,7 @@ def add(ctx, name, acronym, country, project, wmo_region,

result = add_metadata(Contributor, contributor_,
save_to_registry, save_to_index)
click.echo('Contributor {} added'.format(result.contributor_id))
click.echo(f'Contributor {result.contributor_id} added')


@click.command('update')
Expand Down Expand Up @@ -179,7 +179,7 @@ def update(ctx, identifier, name, acronym, country, project,

update_metadata(Contributor, identifier, contributor_,
save_to_registry, save_to_index)
click.echo('Contributor {} updated'.format(identifier))
click.echo(f'Contributor {identifier} updated')


@click.command('delete')
Expand All @@ -192,13 +192,13 @@ def delete(ctx, identifier):
click.echo('Contributor not found')
return

q = 'Are you sure you want to delete contributor {}?'.format(identifier)
q = f'Are you sure you want to delete contributor {identifier}?'

if click.confirm(q): # noqa
delete_metadata(Contributor, identifier,
save_to_registry, save_to_index)

click.echo('Contributor {} deleted'.format(identifier))
click.echo(f'Contributor {identifier} deleted')


contributor.add_command(list_)
Expand Down
12 changes: 6 additions & 6 deletions woudc_data_registry/epicentre/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -92,7 +92,7 @@ def list_(ctx):
"""List all deployments"""

for c in get_metadata(Deployment):
click.echo('{} @ {}'.format(c.contributor_id.ljust(20), c.station_id))
click.echo(f'{c.contributor_id.ljust(20)} @ {c.station_id}')


@click.command('show')
Expand Down Expand Up @@ -137,7 +137,7 @@ def add(ctx, station, contributor, start_date, end_date):

result = add_metadata(Deployment, deployment_,
save_to_registry, save_to_index)
click.echo('Deployment {} added'.format(result.deployment_id))
click.echo(f'Deployment {result.deployment_id} added')


@click.command('update')
Expand Down Expand Up @@ -170,7 +170,7 @@ def update(ctx, identifier, station, contributor, start_date, end_date):

update_metadata(Deployment, identifier, deployment_,
save_to_registry, save_to_index)
click.echo('Deployment {} updated'.format(identifier))
click.echo(f'Deployment {identifier} updated')


@click.command('delete')
Expand All @@ -183,13 +183,13 @@ def delete(ctx, identifier):
click.echo('Contributor not found')
return

q = 'Are you sure you want to delete deployment {}?'.format(identifier)
q = f'Are you sure you want to delete deployment {identifier}?'

if click.confirm(q): # noqa
delete_metadata(Deployment, identifier,
save_to_registry, save_to_index)

click.echo('Deployment {} deleted'.format(identifier))
click.echo(f'Deployment {identifier} deleted')


deployment.add_command(list_)
Expand Down
15 changes: 7 additions & 8 deletions woudc_data_registry/epicentre/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -110,9 +110,8 @@ def list_(ctx):

for c in get_metadata(Instrument):
descriptor = ' '.join([c.name, c.model, c.serial])
station = '{}{}'.format(c.station.station_type, c.station_id)
click.echo('{} - {}, {}'.format(descriptor.ljust(30), station,
c.dataset_id))
station = f'{c.station.station_type}{c.station_id}'
click.echo(f'{descriptor.ljust(30)} - {station}, {c.dataset_id}')


@click.command('show')
Expand Down Expand Up @@ -167,7 +166,7 @@ def add(ctx, station, dataset, contributor, name, model, serial, geometry):

result = add_metadata(Instrument, instrument_,
save_to_registry, save_to_index)
click.echo('Instrument {} added'.format(result.instrument_id))
click.echo(f'Instrument {result.instrument_id} added')


@click.command('update')
Expand Down Expand Up @@ -226,7 +225,7 @@ def update(ctx, identifier, station, dataset,

update_metadata(Instrument, identifier, instrument_,
save_to_registry, save_to_index)
click.echo('Instrument {} updated'.format(identifier))
click.echo(f'Instrument {identifier} updated')


@click.command('delete')
Expand All @@ -239,12 +238,12 @@ def delete(ctx, identifier):
click.echo('Instrument not found')
return

q = 'Are you sure you want to delete instrument {}?'.format(identifier)
q = f'Are you sure you want to delete instrument {identifier}?'

if click.confirm(q): # noqa
delete_metadata(Instrument, identifier,
save_to_registry, save_to_index)
click.echo('Instrument {} deleted'.format(identifier))
click.echo(f'Instrument {identifier} deleted')


instrument.add_command(list_)
Expand Down
23 changes: 10 additions & 13 deletions woudc_data_registry/epicentre/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2019 Government of Canada
# Copyright (c) 2024 Government of Canada
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -65,12 +65,12 @@ def get_metadata(entity, identifier=None):
:returns: `list` of all matching objects
"""

LOGGER.debug('Querying metadata objects {}'.format(entity))
LOGGER.debug(f'Querying metadata objects {entity}')
prop = getattr(entity, entity.id_field)
if identifier is None:
res = REGISTRY.session.query(entity).order_by(prop)
else:
LOGGER.debug('Quering identifier {}'.format(identifier))
LOGGER.debug(f'Quering identifier {identifier}')
res = REGISTRY.session.query(entity).filter(
prop == identifier).all()

Expand All @@ -83,7 +83,7 @@ def get_metadata(entity, identifier=None):
term = 'results'
else:
term = 'result'
LOGGER.debug('Found {} {}'.format(count, term))
LOGGER.debug(f'Found {count} {term}')

return res

Expand All @@ -106,7 +106,7 @@ def add_metadata(entity, dict_, save_to_registry=True, save_to_index=True):
Country.name_en == dict_['country_id'])

if results.count() == 0:
msg = 'Invalid country: {}'.format(dict_['country_id'])
msg = f"Invalid country: {dict_['country']}"
LOGGER.error(msg)
raise ValueError(msg)

Expand All @@ -118,7 +118,7 @@ def add_metadata(entity, dict_, save_to_registry=True, save_to_index=True):
Contributor.contributor_id == dict_['contributor_id'])

if results.count() == 0:
msg = 'Invalid contributor: {}'.format(dict_['contributor_id'])
msg = f"Invalid contributor: {dict_['contributor_id']}"
LOGGER.error(msg)
raise ValueError(msg)

Expand Down Expand Up @@ -160,12 +160,11 @@ def update_metadata(entity, identifier, dict_,
records = get_metadata(entity, identifier)

if len(records) == 0:
msg = 'identifier {} not found'.format(identifier)
msg = f'identifier {identifier} not found'
LOGGER.warning(msg)
raise ValueError(msg)
else:
LOGGER.debug('Updating metadata entity {}, identifier {}'
.format(entity, identifier))
LOGGER.debug(f'Updating metadata entity {entity}, identifier {identifier}') # noqa
obj = records[0]

if 'station_name' in dict_ and 'station_id' in dict_:
Expand All @@ -188,8 +187,7 @@ def update_metadata(entity, identifier, dict_,
try:
obj.generate_ids()
except Exception as err:
LOGGER.warning('Unable to generate IDS due to: {}'
.format(str(err)))
LOGGER.warning(f'Unable to generate IDS: {err}')

if save_to_index and getattr(obj, entity.id_field) != identifier:
SEARCH_INDEX.unindex(entity, identifier)
Expand All @@ -215,8 +213,7 @@ def delete_metadata(entity, identifier,
:returns: `bool` of whether the operation was successful.
"""

LOGGER.debug('Updating metadata entity {}, identifier {}'.format(
entity, identifier))
LOGGER.debug(f'Updating metadata entity {entity}, identifier {identifier}') # noqa

prop = getattr(entity, entity.id_field)
REGISTRY.session.query(entity).filter(prop == identifier).delete()
Expand Down
Loading

0 comments on commit 6a8f4d4

Please sign in to comment.