(
cls, scriptdir: ScriptDirectory, path: Union[str, os.PathLike[str]]
)
| 1010 | |
| 1011 | @classmethod |
| 1012 | def _from_path( |
| 1013 | cls, scriptdir: ScriptDirectory, path: Union[str, os.PathLike[str]] |
| 1014 | ) -> Optional[Script]: |
| 1015 | |
| 1016 | path = Path(path) |
| 1017 | dir_, filename = path.parent, path.name |
| 1018 | |
| 1019 | if scriptdir.sourceless: |
| 1020 | py_match = _sourceless_rev_file.match(filename) |
| 1021 | else: |
| 1022 | py_match = _only_source_rev_file.match(filename) |
| 1023 | |
| 1024 | if not py_match: |
| 1025 | return None |
| 1026 | |
| 1027 | py_filename = py_match.group(1) |
| 1028 | |
| 1029 | if scriptdir.sourceless: |
| 1030 | is_c = py_match.group(2) == "c" |
| 1031 | is_o = py_match.group(2) == "o" |
| 1032 | else: |
| 1033 | is_c = is_o = False |
| 1034 | |
| 1035 | if is_o or is_c: |
| 1036 | py_exists = (dir_ / py_filename).exists() |
| 1037 | pyc_exists = (dir_ / (py_filename + "c")).exists() |
| 1038 | |
| 1039 | # prefer .py over .pyc because we'd like to get the |
| 1040 | # source encoding; prefer .pyc over .pyo because we'd like to |
| 1041 | # have the docstrings which a -OO file would not have |
| 1042 | if py_exists or is_o and pyc_exists: |
| 1043 | return None |
| 1044 | |
| 1045 | module = util.load_python_file(dir_, filename) |
| 1046 | |
| 1047 | if not hasattr(module, "revision"): |
| 1048 | # attempt to get the revision id from the script name, |
| 1049 | # this for legacy only |
| 1050 | m = _legacy_rev.match(filename) |
| 1051 | if not m: |
| 1052 | raise util.CommandError( |
| 1053 | "Could not determine revision id from " |
| 1054 | f"filename {filename}. " |
| 1055 | "Be sure the 'revision' variable is " |
| 1056 | "declared inside the script (please see 'Upgrading " |
| 1057 | "from Alembic 0.1 to 0.2' in the documentation)." |
| 1058 | ) |
| 1059 | else: |
| 1060 | revision = m.group(1) |
| 1061 | else: |
| 1062 | revision = module.revision |
| 1063 | return Script(module, revision, dir_ / filename) |
no test coverage detected