browser: decouple dataset controller from dataset dock

This commit is contained in:
Sebastien Bourdeauducq 2023-05-31 21:57:54 +08:00
parent c6b29b30fb
commit c8dc2cbf09
2 changed files with 32 additions and 27 deletions

View File

@ -23,8 +23,30 @@ class Model(DictSyncTreeSepModel):
return short_format(v[1]) return short_format(v[1])
class DatasetCtl:
def __init__(self, master_host, master_port):
self.master_host = master_host
self.master_port = master_port
async def set(self, key, value, persist=None):
logger.info("Uploading dataset '%s' to master...", key)
try:
remote = RPCClient()
await remote.connect_rpc(self.master_host, self.master_port,
"master_dataset_db")
try:
await remote.set(key, value, persist)
finally:
remote.close_rpc()
except:
logger.error("Failed uploading dataset '%s'",
key, exc_info=True)
else:
logger.info("Finished uploading dataset '%s'", key)
class DatasetsDock(QtWidgets.QDockWidget): class DatasetsDock(QtWidgets.QDockWidget):
def __init__(self, dataset_sub, master_host, master_port): def __init__(self, dataset_sub, dataset_ctl):
QtWidgets.QDockWidget.__init__(self, "Datasets") QtWidgets.QDockWidget.__init__(self, "Datasets")
self.setObjectName("Datasets") self.setObjectName("Datasets")
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable | self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
@ -64,8 +86,7 @@ class DatasetsDock(QtWidgets.QDockWidget):
self.set_model(Model(dict())) self.set_model(Model(dict()))
dataset_sub.add_setmodel_callback(self.set_model) dataset_sub.add_setmodel_callback(self.set_model)
self.master_host = master_host self.dataset_ctl = dataset_ctl
self.master_port = master_port
def _search_datasets(self): def _search_datasets(self):
if hasattr(self, "table_model_filter"): if hasattr(self, "table_model_filter"):
@ -82,22 +103,6 @@ class DatasetsDock(QtWidgets.QDockWidget):
self.table_model_filter.setSourceModel(self.table_model) self.table_model_filter.setSourceModel(self.table_model)
self.table.setModel(self.table_model_filter) self.table.setModel(self.table_model_filter)
async def _upload_dataset(self, name, value,):
logger.info("Uploading dataset '%s' to master...", name)
try:
remote = RPCClient()
await remote.connect_rpc(self.master_host, self.master_port,
"master_dataset_db")
try:
await remote.set(name, value)
finally:
remote.close_rpc()
except:
logger.error("Failed uploading dataset '%s'",
name, exc_info=True)
else:
logger.info("Finished uploading dataset '%s'", name)
def upload_clicked(self): def upload_clicked(self):
idx = self.table.selectedIndexes() idx = self.table.selectedIndexes()
if idx: if idx:
@ -105,7 +110,7 @@ class DatasetsDock(QtWidgets.QDockWidget):
key = self.table_model.index_to_key(idx) key = self.table_model.index_to_key(idx)
if key is not None: if key is not None:
persist, value = self.table_model.backing_store[key] persist, value = self.table_model.backing_store[key]
asyncio.ensure_future(self._upload_dataset(key, value)) asyncio.ensure_future(self.dataset_ctl.set(key, value))
def save_state(self): def save_state(self):
return bytes(self.table.header().saveState()) return bytes(self.table.header().saveState())

View File

@ -48,8 +48,8 @@ def get_argparser():
class Browser(QtWidgets.QMainWindow): class Browser(QtWidgets.QMainWindow):
def __init__(self, smgr, dataset_sub, browse_root, def __init__(self, smgr, dataset_sub, dataset_ctl, browse_root,
master_host, master_port, *, loop=None): *, loop=None):
QtWidgets.QMainWindow.__init__(self) QtWidgets.QMainWindow.__init__(self)
smgr.register(self) smgr.register(self)
@ -81,12 +81,11 @@ class Browser(QtWidgets.QMainWindow):
self.files.dataset_changed.connect( self.files.dataset_changed.connect(
self.experiments.dataset_changed) self.experiments.dataset_changed)
self.applets = applets.AppletsDock(self, dataset_sub, loop=loop) self.applets = applets.AppletsDock(self, dataset_sub, dataset_ctl, loop=loop)
smgr.register(self.applets) smgr.register(self.applets)
atexit_register_coroutine(self.applets.stop, loop=loop) atexit_register_coroutine(self.applets.stop, loop=loop)
self.datasets = datasets.DatasetsDock( self.datasets = datasets.DatasetsDock(dataset_sub, dataset_ctl)
dataset_sub, master_host, master_port)
smgr.register(self.datasets) smgr.register(self.datasets)
self.files.metadata_changed.connect(self.datasets.metadata_changed) self.files.metadata_changed.connect(self.datasets.metadata_changed)
@ -151,8 +150,9 @@ def main():
smgr = state.StateManager(args.db_file) smgr = state.StateManager(args.db_file)
browser = Browser(smgr, dataset_sub, args.browse_root, dataset_ctl = datasets.DatasetCtl(args.server, args.port)
args.server, args.port, loop=loop) browser = Browser(smgr, dataset_sub, dataset_ctl, args.browse_root,
loop=loop)
widget_log_handler.callback = browser.log.model.append widget_log_handler.callback = browser.log.model.append
if os.name == "nt": if os.name == "nt":