cape.convert1to2: Utilities to modify CAPE 1.x scripts to 2.0

This module provides utilities to modify programs that used the CAPE 1.x API to ones that will work with CAPE 2.0 and beyond. The primary change between the two versions of CAPE is that many modules have been renamed in order to simplify the structure of the package.

The upgrade from CAPE 1.2 to 2.0 does not actually entail a major change in the underlying function and code of the package, but programs (e.g. what CAPE calls “hooks”) may fail without changing many import statements.

cape.convert1to2.rewrite_imports(infile: str, modnames: dict, outfile: str | None = None)

Rewrite imports for a collection of module name changes

Call:
>>> rewrite_imports(infile, modnames, outfile=None)
Inputs:
infile: str

Name of file to read

modnames: dict

Key is old module name and value is new module name

outfile: {None} | str

Name of file to write (defaults to infile)

cape.convert1to2.sub_imports(content: str, modnames: dict) → str

Rewrite imports for a collection of module name changes

Call:
>>> new_content = sub_imports(content, modnames)
Inputs:
content: str

Original contents before renaming

modnames: dict

Key is old module name and value is new module name

Outputs:
new_content: str

Revised content with rewritten imports

cape.convert1to2.sub_imports_1to2(content: str) → str

Rewrite imports for CAPE upgrade from 1 to 2

Call:
>>> new_content = sub_imports_1to2(content)
Inputs:
content: str

Original contents before renaming

Outputs:
new_content: str

Revised content with rewritten imports

cape.convert1to2.sub_modname(content: str, mod1: str, mod2: str) → str

Replace imports of mod1 with imports of mod2

There are some imports that will not get updated properly when mod1 and mod2 are not of the same depth. For example, updating cape.cntl -> cape.cfdx.cntl will work for most imports, but it won’t proper change

from ..cntl import Cntl

to

from ..cfdx.cntl import Cntl

Such cases should be limited to CAPE-internal functions, so this limitation should not affect external users’ hook functions or other scripts and modules.

Call:
>>> new_content = sub_modname(content, mod1, mod2)
Inputs:
content: str

Original contents before renaming

mod1: str

Name of module to be replaced, e.g. "cape.attdb"

mod2: str

New name of module, e.g. "cape.dkit"

Outputs:
new_content: str

Revised content with rewritten imports

cape.convert1to2.upgrade1to2()

Recursively update imports in Python files found w/i current dir

Call:
>>> upgrade1to2()