Skip to content

migration

Functions:

Name Description
migrate

Perform migration of configuration based on the API version.

replace_line_in_file

Replace a line in a file based on a given pattern.

migrate

migrate(cfg: DcspyConfigYaml) -> DcspyConfigYaml

Perform migration of configuration based on the API version.

If the api_ver key does not exist, it is set to 2.3.3.

Parameters:

Name Type Description Default

cfg

DcspyConfigYaml

Configuration dictionary

required

Returns:

Type Description
DcspyConfigYaml

Full-migrated dictionary

Source code in src/dcspy/migration.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def migrate(cfg: DcspyConfigYaml) -> DcspyConfigYaml:
    """
    Perform migration of configuration based on the API version.

    If the api_ver key does not exist, it is set to 2.3.3.

    :param cfg: Configuration dictionary
    :return: Full-migrated dictionary
    """
    LOG.debug(f'Starting configuration:\n{pformat(cfg)}')
    src_ver = cfg.get('api_ver', '2.3.3')  # do not touch this api_ver!
    LOG.debug(f'Current API version: {src_ver}')
    for migration_func in _filter_api_ver_func(str(src_ver)):
        migration_func(cfg)
        LOG.debug(f'Migration done: {migration_func.__name__}')
    cfg['api_ver'] = __version__
    cfg_with_defaults = {key: cfg.get(key, value) for key, value in defaults_cfg.items()}
    if 'UNKNOWN' in str(cfg_with_defaults['dcsbios']):
        cfg_with_defaults['dcsbios'] = defaults_cfg['dcsbios']
    final_cfg = {**cfg_with_defaults, **cfg}
    LOG.debug(f'Final configuration:\n{pformat(final_cfg)}')
    return final_cfg

replace_line_in_file

replace_line_in_file(
    filename: str,
    dir_path: Path,
    pattern: Pattern,
    new_text: str,
) -> None

Replace a line in a file based on a given pattern.

Parameters:

Name Type Description Default

filename

str

The name of the file to replace the line in.

required

dir_path

Path

The directory path where the file is located.

required

pattern

Pattern

The regular expression pattern to search for in the file.

required

new_text

str

The text to replace the line matching the pattern with.

required
Source code in src/dcspy/migration.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def replace_line_in_file(filename: str, dir_path: Path, pattern: re.Pattern, new_text: str) -> None:
    """
    Replace a line in a file based on a given pattern.

    :param filename: The name of the file to replace the line in.
    :param dir_path: The directory path where the file is located.
    :param pattern: The regular expression pattern to search for in the file.
    :param new_text: The text to replace the line matching the pattern with.
    """
    yaml_filename = dir_path / filename
    with suppress(FileNotFoundError):
        with open(yaml_filename) as yaml_file:
            file_content = yaml_file.read()
        LOG.debug(yaml_filename)
        updated_content = re.sub(pattern, new_text, file_content)
        with open(yaml_filename, 'w') as yaml_file:
            yaml_file.write(updated_content)