1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

 

''' 

Manage separate databases and caches for different user projects. 

 

Squirrel based applications can either use the user's global database which 

lives in Pyrocko's global cache directory (by default under 

``'$HOME/.pyrocko/cache/squirrel'``) or a project specific local database which 

can be conveniently created in the top level directory of a user's project 

under ``'.squirrel'`` or ``'squirrel'``. The choice of database and associated 

cache directory locations is referred to here as the Squirrel environment. This 

module provides functions to create local environments and to look for a usable 

environment in the hierarchy of a user's project directory. 

''' 

 

from __future__ import absolute_import, print_function 

 

import os 

 

from pyrocko.guts import Object, String 

from pyrocko import config 

 

from . import error 

 

guts_prefix = 'pf' 

 

 

g_db_filename = 'nuts.sqlite' 

g_cache_dirname = 'cache' 

 

 

def get_squirrel_path(path=None): 

if path is None: 

path = os.curdir 

 

path = os.path.abspath(path) 

if os.path.isdir(path) and os.path.exists( 

os.path.join(path, g_db_filename)): 

return path 

 

while True: 

for entry in ['squirrel', '.squirrel']: 

candidate = os.path.join(path, entry) 

if os.path.isdir(candidate) \ 

and os.path.exists(os.path.join(candidate, g_db_filename)): 

 

return candidate 

 

path_new = os.path.dirname(path) 

if path_new == path: 

break 

 

path = path_new 

 

return os.path.join(config.config().cache_dir, 'squirrel') 

 

 

def get_environment(path=None): 

''' 

Get default Squirrel environment relevant for a given file system path. 

 

:param path: 

Directory path to use as starting point for detection of the Squirrel 

environment. By default, the current working directory is used as 

starting point. When searching for a usable environment the directory 

``'.squirrel'`` or ``'squirrel'`` in the current (or starting point) 

directory is used if it exists, otherwise the parent directories are 

search upwards for the existence of such a directory. If no such 

directory is found, the user's global Squirrel environment, usually 

``'$HOME/.pyrocko/cache/squirrel'``, is used. 

 

:returns: 

:py:class:`SquirrelEnvironment` object containing the detected database 

and cache directory paths. 

''' 

if path is None: 

path = os.curdir 

 

squirrel_path = get_squirrel_path(path) 

 

return SquirrelEnvironment.make(squirrel_path) 

 

 

def init_environment(path=None): 

''' 

Initialize empty Squirrel environment. 

 

:param path: 

Path to the directory where the new environment's ``'.squirrel'`` 

directory should be created. If set to ``None``, the current working 

directory is used. 

 

If a ``'.squirrel'`` directory already exists at the given location, 

:py:exc:`~pyrocko.squirrel.error.SquirrelError` is raised. 

''' 

if path is None: 

path = os.curdir 

 

squirrel_path = os.path.abspath(os.path.join(path, '.squirrel')) 

try: 

os.mkdir(squirrel_path) 

except OSError: 

raise error.SquirrelError( 

'Cannot create squirrel directory: %s' % squirrel_path) 

 

from .base import Squirrel 

env = SquirrelEnvironment.make(squirrel_path) 

sq = Squirrel(env) 

del sq 

 

 

class SquirrelEnvironment(Object): 

''' 

Configuration object providing paths to database and cache. 

''' 

 

database_path = String.T(optional=True) 

cache_path = String.T(optional=True) 

persistent = String.T(optional=True) 

 

@classmethod 

def make(cls, squirrel_path): 

return cls( 

database_path=os.path.join(squirrel_path, g_db_filename), 

cache_path=os.path.join(squirrel_path, g_cache_dirname)) 

 

 

__all__ = [ 

'get_squirrel_path', 

'get_environment', 

'init_environment', 

'SquirrelEnvironment']