Skip to content

Commit

Permalink
resolve paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Dec 9, 2024
1 parent 6b9bff5 commit 47534b1
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions petab/v2/petab1to2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
from itertools import chain
from pathlib import Path
from urllib.parse import urlparse

from pandas.io.common import get_handle, is_url

Expand Down Expand Up @@ -76,7 +77,7 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
# condition tables, observable tables, SBML files, parameter table:
# no changes - just copy
file = yaml_config[C.PARAMETER_FILE]
_copy_file(get_src_path(file), get_dest_path(file))
_copy_file(get_src_path(file), Path(get_dest_path(file)))

for problem_config in yaml_config[C.PROBLEMS]:
for file in chain(
Expand All @@ -89,7 +90,7 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
problem_config.get(C.MEASUREMENT_FILES, []),
problem_config.get(C.VISUALIZATION_FILES, []),
):
_copy_file(get_src_path(file), get_dest_path(file))
_copy_file(get_src_path(file), Path(get_dest_path(file)))

# TODO: Measurements: preequilibration to experiments/timecourses once
# finalized
Expand Down Expand Up @@ -131,18 +132,23 @@ def _update_yaml(yaml_config: dict) -> dict:
return yaml_config


def _copy_file(src: Path | str, dest: Path | str):
def _copy_file(src: Path | str, dest: Path):
"""Copy file."""
src = str(src)
dest = str(dest)

if src == dest:
return
# src might be a URL - convert to Path if local
src_url = urlparse(src)
if not src_url.scheme:
src = Path(src)
elif src_url.scheme == "file" and not src_url.netloc:
src = Path(src.removeprefix("file:/"))

if is_url(src):
with get_handle(src, mode="r") as src_handle:
with open(dest, "w") as dest_handle:
dest_handle.write(src_handle.handle.read())
return

shutil.copy(str(src), str(dest))
try:
if src.samefile(dest):
return
except FileNotFoundError:
shutil.copy(str(src), str(dest))

0 comments on commit 47534b1

Please sign in to comment.