|
1 | 1 | from pathlib import Path |
2 | 2 |
|
| 3 | +import pandas as pd |
3 | 4 | from loguru import logger |
4 | 5 |
|
| 6 | +from lens.descriptor import DataDescriptor, load_data_descriptor |
5 | 7 | from lens.util.hostname import check_valid_lens_hostname, get_hostname |
6 | 8 | from lens.util.paths import check_file_structure_correct, check_metadata_exists, check_metadata_version_match |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class Lens: |
10 | 12 | def __init__(self) -> None: |
11 | | - pass |
| 13 | + self.started = False |
| 14 | + self.descriptor: DataDescriptor |
12 | 15 |
|
13 | 16 | def startup(self, data_root: Path) -> None: |
| 17 | + self._startup_check_hostname() |
| 18 | + self._startup_check_metadata(data_root) |
| 19 | + self._startup_check_files(data_root) |
| 20 | + logger.info("Lens startup succeeded.") |
| 21 | + self.started = True |
| 22 | + self.descriptor = load_data_descriptor(data_root) |
| 23 | + |
| 24 | + def load_trades_for_day(self, coin: str, date: str) -> pd.DataFrame: |
| 25 | + self._ensure_started() |
| 26 | + if not self._check_coin_available(coin): |
| 27 | + raise RuntimeError("Coin is not available in this dataset.") |
| 28 | + for file in self.descriptor.data_paths[coin].trade_paths: |
| 29 | + if date + ".csv" == file.name: |
| 30 | + return pd.read_csv(file, sep="|") |
| 31 | + raise RuntimeError("Failed to find requested date.") |
| 32 | + |
| 33 | + def load_depth_for_day(self, coin: str, date: str) -> pd.DataFrame: |
| 34 | + self._ensure_started() |
| 35 | + if not self._check_coin_available(coin): |
| 36 | + raise RuntimeError("Coin is not available in this dataset.") |
| 37 | + for file in self.descriptor.data_paths[coin].depth_paths: |
| 38 | + if date + ".csv" == file.name: |
| 39 | + return pd.read_csv(file, sep="|") |
| 40 | + raise RuntimeError("Failed to find requested date.") |
| 41 | + |
| 42 | + def _check_coin_available(self, coin: str) -> bool: |
| 43 | + return coin in self.descriptor.data_paths.keys() |
| 44 | + |
| 45 | + def _ensure_started(self) -> None: |
| 46 | + if not self.started: |
| 47 | + raise RuntimeError("This operation requires lens to be started before calling.") |
| 48 | + |
| 49 | + def _startup_check_hostname(self) -> None: |
14 | 50 | if not check_valid_lens_hostname(get_hostname()): |
15 | 51 | raise RuntimeError("Cannot start lens on invalid host.") |
16 | 52 | logger.info("Host check succeeded.") |
| 53 | + |
| 54 | + def _startup_check_metadata(self, data_root: Path) -> None: |
17 | 55 | if not check_metadata_exists(data_root): |
18 | 56 | raise RuntimeError("Failed to find metadata.json file in the data directory.") |
19 | 57 | logger.info("Metadata found.") |
20 | 58 | if not check_metadata_version_match(data_root): |
21 | 59 | raise RuntimeError("Metadata version does not match current version!") |
22 | 60 | logger.info("Metadata versions match.") |
| 61 | + |
| 62 | + def _startup_check_files(self, data_root: Path) -> None: |
23 | 63 | if not check_file_structure_correct(data_root): |
24 | 64 | raise RuntimeError("File structure does not match expected directory structure.") |
25 | 65 | logger.info("File structure is correct.") |
26 | | - logger.info("Lens startup succeeded.") |
| 66 | + |
| 67 | + def _force_startup_unsafe(self, data_root: Path) -> None: |
| 68 | + self.started = True |
| 69 | + self.descriptor = load_data_descriptor(data_root) |
0 commit comments