Skip to content

Commit

Permalink
New verb update_config
Browse files Browse the repository at this point in the history
Update an existing config file to the latest version
  • Loading branch information
claudiodsf committed May 3, 2024
1 parent ac7f90e commit 2b4d93b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Copyright (c) 2021-2024 Claudio Satriano <[email protected]>

## unreleased

Note: you might want to run `requake update_config` to update your config file
to the latest version.

- Verb `plot_slip` renamed to `plot_cumulative`. This new verb has new options
to plot cumulative slip, cumulative moment, and cumulative number of events,
and to make the plot logarithmic.
Expand All @@ -13,6 +16,8 @@ Copyright (c) 2021-2024 Claudio Satriano <[email protected]>
- Additional models to convert magnitude to slip. Currently supported models
are: Nadeau and Johnson (1998), Beeler et al. (2001), Eshelby (1957).
Model selection is done using the `mag_to_slip_model` config parameter.
- New verb `update_config`: update an existing config file to the latest
version

## v0.5 - 2024-04-23

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ The different running modes are specified as "verbs" (positional arguments).
Currently supported verbs are:

sample_config write sample config file to current directory and exit
update_config update an existing config file to the latest version
read_catalog read an event catalog from web services or from a file
print_catalog print the event catalog to screen
scan_catalog scan an existing catalog for earthquake pairs
Expand Down
1 change: 1 addition & 0 deletions docs/running.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Currently supported verbs are:
.. code-block:: text
sample_config write sample config file to current directory and exit
update_config update an existing config file to the latest version
read_catalog read an event catalog from web services or from a file
print_catalog print the event catalog to screen
scan_catalog scan an existing catalog for earthquake pairs
Expand Down
10 changes: 10 additions & 0 deletions requake/config/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def parse_arguments(progname='requake'):
help='write sample config file to current directory and exit'
)
# ---
# --- update_config
updateconfig = subparser.add_parser(
'update_config',
help='update an existing config file to the latest version'
)
updateconfig.add_argument(
'config_file', default='requake.conf', nargs='?',
help='config file to be updated (default: %(default)s)'
)
# ---
# --- read catalog
readcatalog = subparser.add_parser(
'read_catalog',
Expand Down
5 changes: 4 additions & 1 deletion requake/config/rq_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .config import Config
from .utils import (
parse_configspec, read_config, validate_config, write_sample_config,
write_ok
update_config_file, write_ok
)
# pylint: disable=global-statement,import-outside-toplevel

Expand Down Expand Up @@ -203,6 +203,9 @@ def configure(args):
if args.action == 'sample_config':
write_sample_config(configspec, 'requake')
sys.exit(0)
if args.action == 'update_config':
update_config_file(args.configfile, configspec)
sys.exit(0)
config_obj = read_config(args.configfile, configspec)
# Set to None all the 'None' strings
for key, value in config_obj.dict().items():
Expand Down
49 changes: 49 additions & 0 deletions requake/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import os
import sys
import locale
import shutil
from datetime import datetime
from .configobj import ConfigObj
from .configobj.validate import Validator
locale.setlocale(locale.LC_ALL, '')
Expand Down Expand Up @@ -131,6 +133,53 @@ def validate_config(config_obj):
err_exit('No configuration value present!')


def update_config_file(config_file, configspec):
"""
Update a configuration file to the latest version.
:param config_file: Configuration file.
:type config_file: str
:param configspec: Configuration specification file.
:type configspec: str
"""
config_obj = read_config(config_file, configspec)
val = Validator()
config_obj.validate(val)
mod_time = datetime.fromtimestamp(os.path.getmtime(config_file))
mod_time_str = mod_time.strftime('%Y%m%d_%H%M%S')
config_file_old = f'{config_file}.{mod_time_str}'
ans = input(
f'Ok to update {config_file}? [y/N]\n'
f'(Old file will be saved as {config_file_old}) '
)
if ans not in ['y', 'Y']:
sys.exit(0)
config_new = ConfigObj(configspec=configspec, default_encoding='utf8')
config_new = read_config(None, configspec)
config_new.validate(val)
config_new.defaults = []
config_new.comments = configspec.comments
config_new.initial_comment = config_obj.initial_comment
config_new.final_comment = configspec.final_comment
for k, v in config_obj.items():
if k not in config_new:
continue
# Fix for force_list(default=None)
if v == ['None', ]:
v = None
config_new[k] = v
migrate_options = {
# 'old_option': 'new_option'
}
for old_opt, new_opt in migrate_options.items():
if old_opt in config_obj and config_obj[old_opt] != 'None':
config_new[new_opt] = config_obj[old_opt]
shutil.copyfile(config_file, config_file_old)
with open(config_file, 'wb') as fp:
config_new.write(fp)
print(f'{config_file}: updated')


def manage_uncaught_exception(exception):
"""
Manage an uncaught exception.
Expand Down

0 comments on commit 2b4d93b

Please sign in to comment.