Hide keyboard shortcuts

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/>. 

21 

22from __future__ import annotations 

23 

24import contextlib 

25import posixpath 

26import logging 

27import os 

28 

29from pathlib import Path, PurePath, PurePosixPath 

30 

31__all__ = ('os2posix', 'posix2os', 'NoTransaction') 

32 

33from typing import ( 

34 Any, 

35 Callable, 

36 Iterator, 

37 Union, 

38) 

39 

40# Determine if the path separator for the OS looks like POSIX 

41IS_POSIX = os.sep == posixpath.sep 

42 

43# Root path for this operating system 

44OS_ROOT_PATH = Path().resolve().root 

45 

46log = logging.getLogger(__name__) 

47 

48 

49def os2posix(ospath: str) -> str: 

50 """Convert a local path description to a POSIX path description. 

51 

52 Parameters 

53 ---------- 

54 ospath : `str` 

55 Path using the local path separator. 

56 

57 Returns 

58 ------- 

59 posix : `str` 

60 Path using POSIX path separator 

61 """ 

62 if IS_POSIX: 

63 return ospath 

64 

65 posix = PurePath(ospath).as_posix() 

66 

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 

72 

73 return posix 

74 

75 

76def posix2os(posix: Union[PurePath, str]) -> str: 

77 """Convert a POSIX path description to a local path description. 

78 

79 Parameters 

80 ---------- 

81 posix : `str`, `PurePath` 

82 Path using the POSIX path separator. 

83 

84 Returns 

85 ------- 

86 ospath : `str` 

87 Path using OS path separator 

88 """ 

89 if IS_POSIX: 

90 return str(posix) 

91 

92 posixPath = PurePosixPath(posix) 

93 paths = list(posixPath.parts) 

94 

95 # Have to convert the root directory after splitting 

96 if paths[0] == posixPath.root: 

97 paths[0] = OS_ROOT_PATH 

98 

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("") 

103 

104 return os.path.join(*paths) 

105 

106 

107class NoTransaction: 

108 """A simple emulation of the `DatastoreTransaction` class. 

109 

110 Does nothing. 

111 """ 

112 

113 def __init__(self) -> None: 

114 return 

115 

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