|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Terms and Conditions of Use |
| 4 | +# |
| 5 | +# Unless otherwise noted, computer program source code of this |
| 6 | +# distribution # is covered under Crown Copyright, Government of |
| 7 | +# Canada, and is distributed under the MIT License. |
| 8 | +# |
| 9 | +# The Canada wordmark and related graphics associated with this |
| 10 | +# distribution are protected under trademark law and copyright law. |
| 11 | +# No permission is granted to use them outside the parameters of |
| 12 | +# the Government of Canada's corporate identity program. For |
| 13 | +# more information, see |
| 14 | +# http://www.tbs-sct.gc.ca/fip-pcim/index-eng.asp |
| 15 | +# |
| 16 | +# Copyright title to all 3rd party software distributed with this |
| 17 | +# software is held by the respective copyright holders as noted in |
| 18 | +# those files. Users are asked to read the 3rd Party Licenses |
| 19 | +# referenced with those assets. |
| 20 | +# |
| 21 | +# Copyright (c) 2026 Tom Kralidis |
| 22 | +# |
| 23 | +# Permission is hereby granted, free of charge, to any person |
| 24 | +# obtaining a copy of this software and associated documentation |
| 25 | +# files (the "Software"), to deal in the Software without |
| 26 | +# restriction, including without limitation the rights to use, |
| 27 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 28 | +# copies of the Software, and to permit persons to whom the |
| 29 | +# Software is furnished to do so, subject to the following |
| 30 | +# conditions: |
| 31 | +# |
| 32 | +# The above copyright notice and this permission notice shall be |
| 33 | +# included in all copies or substantial portions of the Software. |
| 34 | +# |
| 35 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 36 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 37 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 38 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 39 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 40 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 41 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 42 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 43 | +# |
| 44 | +# ================================================================= |
| 45 | + |
| 46 | +import click |
| 47 | +import logging |
| 48 | + |
| 49 | +import yaml |
| 50 | + |
| 51 | +from pygeometa import cli_options |
| 52 | +from pygeometa.core import read_mcf |
| 53 | + |
| 54 | +LOGGER = logging.getLogger(__name__) |
| 55 | + |
| 56 | + |
| 57 | +def migrate_from_1_0_to_2_0(mcf_dict: dict) -> dict: |
| 58 | + """ |
| 59 | + migrate MCF from 1.0 |
| 60 | +
|
| 61 | + :param mcf_dict: `dict` of MCF |
| 62 | +
|
| 63 | + :returns: `dict` of MCF migrated from 1.0 |
| 64 | + """ |
| 65 | + |
| 66 | + mcf_dict['mcf']['version'] = LATEST_MCF |
| 67 | + |
| 68 | + mcf_dict['metadata']['dates'] = { |
| 69 | + 'creation': mcf_dict['metadata'].pop('datestamp', None) |
| 70 | + } |
| 71 | + |
| 72 | + return mcf_dict |
| 73 | + |
| 74 | + |
| 75 | +LATEST_MCF = 2.0 |
| 76 | + |
| 77 | +MIGRATIONS = { |
| 78 | + '1.0': migrate_from_1_0_to_2_0 |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +def migrate(mcf: dict) -> dict: |
| 83 | + """ |
| 84 | + migrate MCF document |
| 85 | +
|
| 86 | + :param mcf_dict: `dict` of MCF |
| 87 | +
|
| 88 | + :returns: `dict` of MCF migrated from 1.0 |
| 89 | + """ |
| 90 | + |
| 91 | + mcf_dict = read_mcf(mcf, skip_version_fail=True) |
| 92 | + |
| 93 | + mcf_version = str(mcf_dict['mcf']['version']) |
| 94 | + LOGGER.info(f'Migrating MCF from version {mcf_version}') |
| 95 | + |
| 96 | + if mcf_version == str(LATEST_MCF): |
| 97 | + LOGGER.info('MCF is already at the latest version') |
| 98 | + return mcf_dict |
| 99 | + |
| 100 | + if mcf_version not in MIGRATIONS.keys(): |
| 101 | + msg = 'Unsupported version for migration' |
| 102 | + raise RuntimeError(msg) |
| 103 | + |
| 104 | + return MIGRATIONS[mcf_version](mcf_dict) |
| 105 | + |
| 106 | + |
| 107 | +@click.command('migrate') |
| 108 | +@click.pass_context |
| 109 | +@cli_options.ARGUMENT_MCF |
| 110 | +@cli_options.OPTION_OUTPUT |
| 111 | +@cli_options.OPTION_VERBOSITY |
| 112 | +def migrate_(ctx, mcf, output, verbosity): |
| 113 | + """migrate an MCF document""" |
| 114 | + |
| 115 | + try: |
| 116 | + content = migrate(mcf) |
| 117 | + except RuntimeError as err: |
| 118 | + raise click.ClickException(err) |
| 119 | + |
| 120 | + if output is None: |
| 121 | + click.echo(yaml.dump(content)) |
| 122 | + else: |
| 123 | + output.write(yaml.dump(content, indent=4)) |
0 commit comments