Skip to content

Commit 8c5d4e7

Browse files
committed
Propagate implications
1 parent d1ceaf9 commit 8c5d4e7

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/pandas_openscm/db/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_instance(self, option: str) -> OpenSCMDBIndexBackend:
148148
)
149149
raise KeyError(msg)
150150

151-
def guess_backend(self, index_file_name: str) -> OpenSCMDBDataBackend:
151+
def guess_backend(self, index_file_name: str) -> OpenSCMDBIndexBackend:
152152
"""
153153
Guess backend from a file name
154154

src/pandas_openscm/db/openscm_db.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ def from_gzipped_tar_archive(
338338
):
339339
backend_data = DATA_BACKENDS.guess_backend(member.name)
340340

341+
if backend_data is None: # pragma: noqa
342+
# Should be impossible to get here
343+
raise TypeError(backend_data)
344+
345+
if backend_index is None: # pragma: noqa
346+
# Should be impossible to get here
347+
raise TypeError(backend_index)
348+
341349
res = cls(backend_data=backend_data, backend_index=backend_index, db_dir=db_dir)
342350

343351
return res
@@ -700,6 +708,7 @@ def save( # noqa: PLR0913
700708
file_map_start=file_map_db,
701709
data_to_write=data,
702710
get_new_data_file_path=self.get_new_data_file_path,
711+
db_dir=self.db_dir,
703712
)
704713

705714
# As needed, re-write files without deleting the old files
@@ -782,7 +791,7 @@ def to_gzipped_tar_archive(self, out_file: Path, mode: str = "w:gz") -> Path:
782791
783792
This is the same as `out_file`, but is returned for convenience.
784793
"""
785-
with tarfile.open(out_file, mode) as tar:
794+
with tarfile.open(out_file, mode=mode) as tar:
786795
tar.add(self.db_dir, arcname="db")
787796

788797
return out_file

src/pandas_openscm/db/rewriting.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from attrs import define
1818

1919
from pandas_openscm.db.interfaces import OpenSCMDBDataBackend
20+
from pandas_openscm.db.path_handling import DBPath
2021
from pandas_openscm.index_manipulation import (
2122
unify_index_levels_check_index_types,
2223
update_index_from_candidates,
@@ -180,7 +181,8 @@ def make_move_plan(
180181
index_start: pd.DataFrame,
181182
file_map_start: pd.Series[Path], # type: ignore # pandas confused about ability to support Path
182183
data_to_write: pd.DataFrame,
183-
get_new_data_file_path: Callable[[int], Path],
184+
get_new_data_file_path: Callable[[int], DBPath],
185+
db_dir: Path,
184186
) -> MovePlan:
185187
"""
186188
Make a plan for moving data around to make room for new data
@@ -196,6 +198,12 @@ def make_move_plan(
196198
data_to_write
197199
Data that is going to be written in the database
198200
201+
get_new_data_file_path
202+
Callable which, given an integer, returns the path info for the new data file
203+
204+
db_dir
205+
Database directory
206+
199207
Returns
200208
-------
201209
:
@@ -229,7 +237,7 @@ def make_move_plan(
229237
# (would be even more efficient to just update the file IDs,
230238
# but that would create a coupling I can't get my head around right now).
231239
delete_file_ids = full_overwrite.index[full_overwrite]
232-
delete_paths = file_map_start.loc[delete_file_ids]
240+
delete_paths = (db_dir / v for v in file_map_start.loc[delete_file_ids])
233241
moved_index = index_start[~index_start["file_id"].isin(delete_file_ids)]
234242
file_map_out = file_map_start.loc[moved_index["file_id"].unique()]
235243

@@ -246,7 +254,7 @@ def make_move_plan(
246254
full_overwrite_file_ids = full_overwrite.index[full_overwrite]
247255
partial_overwrite_file_ids = partial_overwrite.index[partial_overwrite]
248256
file_ids_to_delete = np.union1d(full_overwrite_file_ids, partial_overwrite_file_ids)
249-
delete_paths = file_map_start.loc[file_ids_to_delete]
257+
delete_paths = (db_dir / v for v in file_map_start.loc[file_ids_to_delete])
250258

251259
file_id_map = {}
252260
max_file_id_start = file_map_start.index.max()
@@ -259,12 +267,13 @@ def make_move_plan(
259267
):
260268
new_file_id = max_file_id_start + 1 + increment
261269

262-
file_map_out.loc[new_file_id] = get_new_data_file_path(new_file_id)
270+
new_db_path = get_new_data_file_path(new_file_id)
271+
file_map_out.loc[new_file_id] = new_db_path.rel_db
263272

264273
rewrite_actions_l.append(
265274
ReWriteAction(
266-
from_file=file_map_start.loc[file_id_old],
267-
to_file=file_map_out.loc[new_file_id],
275+
from_file=db_dir / file_map_start.loc[file_id_old],
276+
to_file=new_db_path.abs,
268277
locator=fiddf.index.droplevel("file_id"),
269278
)
270279
)

0 commit comments

Comments
 (0)