Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/has_paths.py: 68%

63 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-03-07 11:54 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6''' 

7Support for relative paths in :py:mod:`~pyrocko.guts`-based configuration 

8files. 

9''' 

10 

11from .guts import Object, String 

12import os.path as op 

13 

14guts_prefix = 'pf' 

15 

16 

17def xjoin(basepath, path): 

18 if path is None and basepath is not None: 

19 return basepath 

20 elif op.isabs(path) or basepath is None: 

21 return path 

22 else: 

23 return op.join(basepath, path) 

24 

25 

26def xrelpath(path, start): 

27 if op.isabs(path): 

28 return path 

29 else: 

30 return op.relpath(path, start) 

31 

32 

33class Path(String): 

34 pass 

35 

36 

37class HasPaths(Object): 

38 path_prefix = Path.T(optional=True) 

39 

40 def __init__(self, *args, **kwargs): 

41 Object.__init__(self, *args, **kwargs) 

42 self._basepath = None 

43 self._parent_path_prefix = None 

44 

45 def ichildren(self): 

46 for (prop, val) in self.T.ipropvals(self): 

47 if isinstance(val, HasPaths): 

48 yield val 

49 

50 elif prop.multivalued and val is not None: 

51 for ele in val: 

52 if isinstance(ele, HasPaths): 

53 yield ele 

54 

55 def effective_path_prefix(self): 

56 return self.path_prefix or self._parent_path_prefix 

57 

58 def set_basepath(self, basepath, parent_path_prefix=None): 

59 self._basepath = basepath 

60 self._parent_path_prefix = parent_path_prefix 

61 for val in self.ichildren(): 

62 val.set_basepath( 

63 basepath, self.effective_path_prefix()) 

64 

65 def set_basepath_from(self, other): 

66 self.set_basepath(other.get_basepath(), other.effective_path_prefix()) 

67 

68 def get_basepath(self): 

69 assert self._basepath is not None 

70 

71 return self._basepath 

72 

73 def change_basepath(self, new_basepath, parent_path_prefix=None): 

74 assert self._basepath is not None 

75 

76 self._parent_path_prefix = parent_path_prefix 

77 if self.path_prefix or not self._parent_path_prefix: 

78 

79 self.path_prefix = op.normpath(xjoin(xrelpath( 

80 self._basepath, new_basepath), self.path_prefix)) 

81 

82 for val in self.ichildren(): 

83 val.change_basepath(new_basepath, self.effective_path_prefix()) 

84 

85 self._basepath = new_basepath 

86 

87 def expand_path(self, path, extra=None): 

88 assert self._basepath is not None 

89 

90 if extra is None: 

91 def extra(path): 

92 return path 

93 

94 path_prefix = self.effective_path_prefix() 

95 

96 if path is None: 

97 return None 

98 

99 elif isinstance(path, str): 

100 return extra( 

101 op.normpath(xjoin(self._basepath, xjoin(path_prefix, path)))) 

102 else: 

103 return [ 

104 extra( 

105 op.normpath(xjoin(self._basepath, xjoin(path_prefix, p)))) 

106 for p in path] 

107 

108 def rel_path(self, path): 

109 return xrelpath(path, self.get_basepath())