8
8
import urllib .parse as urlparse
9
9
from pathlib import Path
10
10
11
- from terracotta .drivers .base import Driver
11
+ from terracotta .drivers .base_classes import MetaStore
12
+ from terracotta .drivers .terracotta_driver import TerracottaDriver
13
+ from terracotta .drivers .geotiff_raster_store import GeoTiffRasterStore
12
14
13
15
URLOrPathType = Union [str , Path ]
14
16
15
17
16
- def load_driver (provider : str ) -> Type [Driver ]:
18
+ def load_driver (provider : str ) -> Type [MetaStore ]:
17
19
if provider == 'sqlite-remote' :
18
- from terracotta .drivers .sqlite_remote import RemoteSQLiteDriver
19
- return RemoteSQLiteDriver
20
+ from terracotta .drivers .sqlite_remote_meta_store import RemoteSQLiteMetaStore
21
+ return RemoteSQLiteMetaStore
20
22
21
23
if provider == 'mysql' :
22
- from terracotta .drivers .mysql import MySQLDriver
23
- return MySQLDriver
24
+ from terracotta .drivers .mysql_meta_store import MySQLMetaStore
25
+ return MySQLMetaStore
24
26
25
27
if provider == 'sqlite' :
26
- from terracotta .drivers .sqlite import SQLiteDriver
27
- return SQLiteDriver
28
+ from terracotta .drivers .sqlite_meta_store import SQLiteMetaStore
29
+ return SQLiteMetaStore
28
30
29
31
raise ValueError (f'Unknown database provider { provider } ' )
30
32
31
33
32
- def auto_detect_provider (url_or_path : Union [ str , Path ] ) -> str :
33
- parsed_path = urlparse .urlparse (str ( url_or_path ) )
34
+ def auto_detect_provider (url_or_path : str ) -> str :
35
+ parsed_path = urlparse .urlparse (url_or_path )
34
36
35
37
scheme = parsed_path .scheme
36
38
if scheme == 's3' :
@@ -42,10 +44,10 @@ def auto_detect_provider(url_or_path: Union[str, Path]) -> str:
42
44
return 'sqlite'
43
45
44
46
45
- _DRIVER_CACHE : Dict [Tuple [URLOrPathType , str , int ], Driver ] = {}
47
+ _DRIVER_CACHE : Dict [Tuple [URLOrPathType , str , int ], TerracottaDriver ] = {}
46
48
47
49
48
- def get_driver (url_or_path : URLOrPathType , provider : str = None ) -> Driver :
50
+ def get_driver (url_or_path : URLOrPathType , provider : str = None ) -> TerracottaDriver :
49
51
"""Retrieve Terracotta driver instance for the given path.
50
52
51
53
This function always returns the same instance for identical inputs.
@@ -66,25 +68,37 @@ def get_driver(url_or_path: URLOrPathType, provider: str = None) -> Driver:
66
68
67
69
>>> import terracotta as tc
68
70
>>> tc.get_driver('tc.sqlite')
69
- SQLiteDriver('/home/terracotta/tc.sqlite')
71
+ TerracottaDriver(
72
+ meta_store=SQLiteDriver('/home/terracotta/tc.sqlite'),
73
+ raster_store=GeoTiffRasterStore()
74
+ )
70
75
>>> tc.get_driver('mysql://root@localhost/tc')
71
- MySQLDriver('mysql://root@localhost:3306/tc')
76
+ TerracottaDriver(
77
+ meta_store=MySQLDriver('mysql+pymysql://localhost:3306/tc'),
78
+ raster_store=GeoTiffRasterStore()
79
+ )
72
80
>>> # pass provider if path is given in a non-standard way
73
81
>>> tc.get_driver('root@localhost/tc', provider='mysql')
74
- MySQLDriver('mysql://root@localhost:3306/tc')
82
+ TerracottaDriver(
83
+ meta_store=MySQLDriver('mysql+pymysql://localhost:3306/tc'),
84
+ raster_store=GeoTiffRasterStore()
85
+ )
75
86
76
87
"""
88
+ url_or_path = str (url_or_path )
89
+
77
90
if provider is None : # try and auto-detect
78
91
provider = auto_detect_provider (url_or_path )
79
92
80
- if isinstance (url_or_path , Path ):
81
- url_or_path = str (url_or_path )
82
-
83
93
DriverClass = load_driver (provider )
84
94
normalized_path = DriverClass ._normalize_path (url_or_path )
85
95
cache_key = (normalized_path , provider , os .getpid ())
86
96
87
97
if cache_key not in _DRIVER_CACHE :
88
- _DRIVER_CACHE [cache_key ] = DriverClass (url_or_path )
98
+ driver = TerracottaDriver (
99
+ meta_store = DriverClass (url_or_path ),
100
+ raster_store = GeoTiffRasterStore ()
101
+ )
102
+ _DRIVER_CACHE [cache_key ] = driver
89
103
90
104
return _DRIVER_CACHE [cache_key ]
0 commit comments