|
| 1 | +""" |
| 2 | +Application |
| 3 | +""" |
| 4 | + |
| 5 | +from os import environ |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from reflex import App |
| 9 | +from twisted.logger import Logger |
| 10 | + |
| 11 | +from transmissions.ext.click import readConfig |
| 12 | +from transmissions.ext.logger import startLogging |
| 13 | +from transmissions.run._search import searchIndexFactoryFromConfig # FIXME: private |
| 14 | +from transmissions.run._store import storeFactoryFromConfig # FIXME: private |
| 15 | +from transmissions.search import TransmissionsIndex |
| 16 | +from transmissions.store import TXDataStore |
| 17 | + |
| 18 | + |
| 19 | +log = Logger() |
| 20 | + |
| 21 | + |
| 22 | +class StoreFactory: |
| 23 | + """ |
| 24 | + Factory for data store. |
| 25 | + """ |
| 26 | + |
| 27 | + _store: TXDataStore | None = None |
| 28 | + |
| 29 | + async def store(self) -> TXDataStore: |
| 30 | + """ |
| 31 | + Get and cache the data store. |
| 32 | + """ |
| 33 | + if self._store is None: |
| 34 | + log.info("Initializing data store...") |
| 35 | + storeFactory = storeFactoryFromConfig(configuration, create=False) |
| 36 | + self._store = await storeFactory() |
| 37 | + |
| 38 | + return self._store |
| 39 | + |
| 40 | + |
| 41 | +class SearchIndexFactory: |
| 42 | + """ |
| 43 | + Factory for search index. |
| 44 | + """ |
| 45 | + |
| 46 | + _index: TransmissionsIndex | None = None |
| 47 | + |
| 48 | + async def index(self, store: TXDataStore) -> TransmissionsIndex: |
| 49 | + """ |
| 50 | + Get and cache the search index. |
| 51 | + """ |
| 52 | + if self._index is None: |
| 53 | + log.info("Initializing search index...") |
| 54 | + searchIndexFactory = searchIndexFactoryFromConfig(configuration) |
| 55 | + self._index = await searchIndexFactory(store) |
| 56 | + |
| 57 | + return self._index |
| 58 | + |
| 59 | + |
| 60 | +startLogging() |
| 61 | + |
| 62 | +defaultConfigPath = Path("~/.rtx.toml") # FIXME: Not DRY; see _command.py |
| 63 | +fileName = environ.get("CONFIG", defaultConfigPath) |
| 64 | + |
| 65 | +log.info("Reading configuration file: {file}", file=fileName) |
| 66 | +configuration = readConfig(Path(fileName)) |
| 67 | + |
| 68 | +storeFactory = StoreFactory() |
| 69 | +searchIndexFactory = SearchIndexFactory() |
| 70 | + |
| 71 | +app = App() |
0 commit comments