Coverage for python/lsst/daf/butler/core/_butlerUri/utils.py : 39%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24import contextlib
25import posixpath
26import logging
27import os
29from pathlib import Path, PurePath, PurePosixPath
31__all__ = ('os2posix', 'posix2os', 'NoTransaction')
33from typing import (
34 Any,
35 Callable,
36 Iterator,
37 Union,
38)
40# Determine if the path separator for the OS looks like POSIX
41IS_POSIX = os.sep == posixpath.sep
43# Root path for this operating system
44OS_ROOT_PATH = Path().resolve().root
46log = logging.getLogger(__name__)
49def os2posix(ospath: str) -> str:
50 """Convert a local path description to a POSIX path description.
52 Parameters
53 ----------
54 ospath : `str`
55 Path using the local path separator.
57 Returns
58 -------
59 posix : `str`
60 Path using POSIX path separator
61 """
62 if IS_POSIX:
63 return ospath
65 posix = PurePath(ospath).as_posix()
67 # PurePath strips trailing "/" from paths such that you can no
68 # longer tell if a path is meant to be referring to a directory
69 # Try to fix this.
70 if ospath.endswith(os.sep) and not posix.endswith(posixpath.sep):
71 posix += posixpath.sep
73 return posix
76def posix2os(posix: Union[PurePath, str]) -> str:
77 """Convert a POSIX path description to a local path description.
79 Parameters
80 ----------
81 posix : `str`, `PurePath`
82 Path using the POSIX path separator.
84 Returns
85 -------
86 ospath : `str`
87 Path using OS path separator
88 """
89 if IS_POSIX:
90 return str(posix)
92 posixPath = PurePosixPath(posix)
93 paths = list(posixPath.parts)
95 # Have to convert the root directory after splitting
96 if paths[0] == posixPath.root:
97 paths[0] = OS_ROOT_PATH
99 # Trailing "/" is stripped so we need to add back an empty path
100 # for consistency
101 if str(posix).endswith(posixpath.sep):
102 paths.append("")
104 return os.path.join(*paths)
107class NoTransaction:
108 """A simple emulation of the `DatastoreTransaction` class.
110 Does nothing.
111 """
113 def __init__(self) -> None:
114 return
116 @contextlib.contextmanager
117 def undoWith(self, name: str, undoFunc: Callable, *args: Any, **kwargs: Any) -> Iterator[None]:
118 """No-op context manager to replace `DatastoreTransaction`
119 """
120 yield None