Coverage for ezdag/path.py: 100.0%

1# Copyright (C) 2023 Patrick Godwin, Cardiff University

2#

3# This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.

4# If a copy of the MPL was not distributed with this file, You can obtain one at

5# <https://mozilla.org/MPL/2.0/>.

6#

7# SPDX-License-Identifier: MPL-2.0

8

9 from __future__ import annotations

10

11 import os

12 from typing import Callable

13

14

15 def is_abs_or_url(path: str) -> bool:

16 """Check whether a path is absolute or URL-based."""

17 if os.path.isabs(path):

18 return True

19 return "://" in str(path)

20

21

22 def normalize(path: str, *, basename: bool | Callable[[str], bool] = False) -> str:

23 """Selectively return the path's basename based on a condition."""

24 if (callable(basename) and basename(path)) or basename is True:

25 return os.path.basename(path)

26 return path