|
4 | 4 |
|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
7 | | -from attrs import frozen |
8 | | - |
| 7 | +from pandas_openscm.db.backends import DATA_BACKENDS, INDEX_BACKENDS |
9 | 8 | from pandas_openscm.db.csv import CSVDataBackend, CSVIndexBackend |
10 | 9 | from pandas_openscm.db.feather import FeatherDataBackend, FeatherIndexBackend |
11 | 10 | from pandas_openscm.db.in_memory import InMemoryDataBackend, InMemoryIndexBackend |
12 | 11 | from pandas_openscm.db.interfaces import OpenSCMDBDataBackend, OpenSCMDBIndexBackend |
13 | 12 | from pandas_openscm.db.netcdf import netCDFDataBackend, netCDFIndexBackend |
14 | 13 | from pandas_openscm.db.openscm_db import AlreadyInDBError, EmptyDBError, OpenSCMDB |
15 | 14 |
|
16 | | - |
17 | | -@frozen |
18 | | -class DataBackendOptions: |
19 | | - """A collection of data back-end options""" |
20 | | - |
21 | | - options: tuple[ # type hint doesn't work properly, but ok |
22 | | - tuple[str, type[OpenSCMDBDataBackend]], ... |
23 | | - ] |
24 | | - """ |
25 | | - Options |
26 | | -
|
27 | | - The first element of each option is the option's short name. |
28 | | - The second element is the class that matches that option. |
29 | | - """ |
30 | | - |
31 | | - def get_instance(self, option: str) -> OpenSCMDBDataBackend: |
32 | | - """ |
33 | | - Get an instance of one of the options |
34 | | -
|
35 | | - Parameters |
36 | | - ---------- |
37 | | - option |
38 | | - Option for which to get a data back-end instance |
39 | | -
|
40 | | - Returns |
41 | | - ------- |
42 | | - : |
43 | | - Initialised instance |
44 | | -
|
45 | | - Raises |
46 | | - ------ |
47 | | - KeyError |
48 | | - The option is not supported |
49 | | - """ |
50 | | - for short_name, option_cls in self.options: |
51 | | - if short_name == option: |
52 | | - return option_cls() |
53 | | - |
54 | | - msg = ( |
55 | | - f"{option=} is not supported. " |
56 | | - f"Available options: {tuple(v[1] for v in self.options)}" |
57 | | - ) |
58 | | - raise KeyError(msg) |
59 | | - |
60 | | - |
61 | | -DATA_BACKENDS = DataBackendOptions( |
62 | | - ( # type: ignore # using class with protocol doesn't work properly |
63 | | - ("csv", CSVDataBackend), |
64 | | - ("feather", FeatherDataBackend), |
65 | | - ("in_memory", InMemoryDataBackend), |
66 | | - ("netCDF", netCDFDataBackend), |
67 | | - # Other options to consider: |
68 | | - # |
69 | | - # - pretty netCDF, where we try and save the data with dimensions where possible |
70 | | - # |
71 | | - # - HDF5: https://pandas.pydata.org/docs/user_guide/io.html#hdf5-pytables |
72 | | - # - sqllite |
73 | | - ) |
74 | | -) |
75 | | -"""Inbuilt data back-ends""" |
76 | | - |
77 | | - |
78 | | -@frozen |
79 | | -class IndexBackendOptions: |
80 | | - """A collection of index back-end options""" |
81 | | - |
82 | | - options: tuple[tuple[str, type[OpenSCMDBIndexBackend]], ...] |
83 | | - """ |
84 | | - Options |
85 | | -
|
86 | | - The first element of each option is the option's short name. |
87 | | - The second element is the class that matches that option. |
88 | | - """ |
89 | | - |
90 | | - def get_instance(self, option: str) -> OpenSCMDBIndexBackend: |
91 | | - """ |
92 | | - Get an instance of one of the options |
93 | | -
|
94 | | - Parameters |
95 | | - ---------- |
96 | | - option |
97 | | - Option for which to get a index back-end instance |
98 | | -
|
99 | | - Returns |
100 | | - ------- |
101 | | - : |
102 | | - Initialised instance |
103 | | -
|
104 | | - Raises |
105 | | - ------ |
106 | | - KeyError |
107 | | - The option is not supported |
108 | | - """ |
109 | | - for short_name, option_cls in self.options: |
110 | | - if short_name == option: |
111 | | - return option_cls() |
112 | | - |
113 | | - msg = ( |
114 | | - f"{option=} is not supported. " |
115 | | - f"Available options: {tuple(v[1] for v in self.options)}" |
116 | | - ) |
117 | | - raise KeyError(msg) |
118 | | - |
119 | | - |
120 | | -INDEX_BACKENDS = IndexBackendOptions( |
121 | | - ( # type: ignore # using class with protocol doesn't work properly |
122 | | - ("csv", CSVIndexBackend), |
123 | | - ("feather", FeatherIndexBackend), |
124 | | - ("in_memory", InMemoryIndexBackend), |
125 | | - ("netCDF", netCDFIndexBackend), |
126 | | - # Other options to consider: |
127 | | - # |
128 | | - # - HDF5: https://pandas.pydata.org/docs/user_guide/io.html#hdf5-pytables |
129 | | - # - sqllite |
130 | | - ) |
131 | | -) |
132 | | -"""Inbuilt index back-ends""" |
133 | | - |
134 | | - |
135 | 15 | __all__ = [ |
136 | 16 | "DATA_BACKENDS", |
137 | 17 | "INDEX_BACKENDS", |
|
0 commit comments