blob: 76df73f2655197c21d276cd38eaa7f2389cf44c7 (
plain) (
blame)
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
|
from __future__ import annotations
import logging
import shutil
from abc import ABC
from pathlib import Path
from sec_certs.serialization.json import ComplexSerializableType, get_class_fullname, only_backed
logger = logging.getLogger(__name__)
class JSONPathDataset(ComplexSerializableType, ABC):
_json_path: Path | None
def __init__(self, json_path: str | Path | None = None):
super().__init__()
self.json_path = Path(json_path) if json_path is not None else None
@property
def is_backed(self) -> bool:
"""
Returns whether the dataset is backed by a JSON file.
"""
return self.json_path is not None
@property
def json_path(self) -> Path | None:
return self._json_path
@json_path.setter
def json_path(self, new_path: str | Path | None) -> None:
if new_path is None:
self._json_path = None
return
new_path = Path(new_path)
if new_path.is_dir():
raise ValueError(f"Json path of {get_class_fullname(self)} cannot be a directory.")
self._json_path = new_path
@only_backed()
def move_dataset(self, new_json_path: str | Path) -> None:
logger.info(f"Moving {get_class_fullname(self)} dataset to {new_json_path}.")
new_json_path = Path(new_json_path)
new_json_path.parent.mkdir(parents=True, exist_ok=True)
if self.json_path and self.json_path.exists():
shutil.move(self.json_path, new_json_path)
self.json_path = new_json_path
else:
self.json_path = new_json_path
self.to_json()
@classmethod
def from_json(cls, input_path: str | Path, is_compressed: bool = False):
dset = super().from_json(input_path, is_compressed)
dset.json_path = Path(input_path)
return dset
|