def filled_database(): database = Database('sqlite:///:memory:') for i in range(1, 11): entry = DatabaseEntry() database.add(entry) # every fourth entry gets the tag 'foo' if i % 4 == 0: database.tag(entry, 'foo') # every fifth entry gets the tag 'bar' if i % 5 == 0: database.tag(entry, 'bar') database.commit() return database
def filled_database(): database = Database('sqlite:///:memory:') for i in xrange(1, 11): entry = DatabaseEntry() database.add(entry) # every fourth entry gets the tag 'foo' if i % 4 == 0: database.tag(entry, 'foo') # every fifth entry gets the tag 'bar' if i % 5 == 0: database.tag(entry, 'bar') database.commit() return database
def split_function_database(): """ Generates a custom database to test the split_database function """ database = Database('sqlite:///:memory:') for i in range(1, 11): entry = DatabaseEntry() database.add(entry) # every fourth entry gets the instrument 'EIA' if i % 4 == 0: database.edit(entry, instrument='EIA') # every fifth entry gets the instrument 'AIA_3' elif i % 5 == 0: database.edit(entry, instrument='AIA_3') # every other entry gets instrument 'RHESSI' else: database.edit(entry, instrument='RHESSI') # all entries have provider 'xyz' database.edit(entry, provider='xyz') database.commit() return database
class DownloadResultController(ToolController): queries = {} app_ctrl: AppController = RequiredFeature(AppController.__name__) content_ctrl: ContentController = RequiredFeature(ContentController.name) def __init__(self): self._view = QtWidgets.QWidget() self._ui = Ui_DownloadResult() self._ui.setupUi(self._view) self._ui.tabs.clear() self._ui.tabs.tabCloseRequested.connect(self._onRemoveTab) self.database = Database() self.tabs = {} self.queries = {} self.query_id = 0 self.loading = [] self.loaded = { entry.fileid: entry.path for entry in list(self.database) } self._ui.download_button.clicked.connect( lambda evt: self._onDownloadSelected()) self._ui.open_button.clicked.connect( lambda evt: self._onOpenSelected()) def query(self, attrs): # open tool if not already opened self.app_ctrl.openController(self.name) self.query_id += 1 # add pending tab tab = ResultTab(self.query_id) self.tabs[self.query_id] = tab index = self._ui.tabs.addTab(tab, "Query " + str(self.query_id)) self._ui.tabs.setCurrentIndex(index) # start query executeTask(Fido.search, attrs, self._onQueryResult, [self.query_id]) # register events tab.download.connect( lambda f_id, q_id=self.query_id: self.download(q_id, f_id)) tab.open.connect(lambda f_id: self._onOpen(f_id)) def _onQueryResult(self, query, id): if id not in self.tabs: return query_model = self._convertQuery(query) self.tabs[id].loadQuery(query_model) self.tabs[id].setLoading(self.loading) self.tabs[id].setLoaded(self.loaded.keys()) self.queries[id] = query def download(self, q_id, f_id): req = copy.copy(self.queries[q_id]) req._list = [copy.copy(r) for r in req] for resp in req: resp[:] = [item for item in resp if item.fileid == f_id] self._addLoading([f_id]) executeTask(Fido.fetch, [req], self._onDownloadResult, [f_id, req]) def _onDownloadResult(self, paths, f_id, request): path = paths[0] entry = list( tables.entries_from_fido_search_result( request, self.database.default_waveunit))[0] entry.path = path self.database.add(entry) self.database.commit() self._addLoaded({f_id: path}) def _onOpen(self, f_id): viewer = MapViewerController.fromFile(self.loaded[f_id]) self.content_ctrl.addViewerController(viewer) def _onRemoveTab(self, index): tab = self._ui.tabs.widget(index) self._ui.tabs.removeTab(index) self.tabs.pop(tab.q_id) def _onDownloadSelected(self): tab = self._ui.tabs.currentWidget() f_ids = tab.getSelectedFIds() for f_id in f_ids: if f_id in self.loading or f_id in self.loaded: continue self.download(tab.q_id, f_id) def _onOpenSelected(self): tab = self._ui.tabs.currentWidget() f_ids = tab.getSelectedFIds() for f_id in f_ids: if f_id not in self.loaded: continue self._onOpen(f_id) def _convertQuery(self, query): items = [item for response in query for item in response] return [[c[1](item) for c in columns] for item in items] def _addLoading(self, f_ids): self.loading.extend(f_ids) for tab in self.tabs.values(): tab.setLoading(f_ids) def _addLoaded(self, dict): self.loading = [ f_id for f_id in self.loading if f_id not in dict.keys() ] self.loaded.update(dict) for tab in self.tabs.values(): tab.setLoaded(dict.keys()) @property def item_config(self) -> ItemConfig: return ItemConfig().setTitle("Download Results").setOrientation( QtCore.Qt.BottomDockWidgetArea) @property def view(self) -> QtWidgets: return self._view
class DataManagerController(ToolController): content_ctrl: ContentController = RequiredFeature(ContentController.name) def __init__(self): self._view = QtWidgets.QWidget() self._ui = Ui_DataManager() self._ui.setupUi(self._view) self._ui.refresh_button.setIcon(self._view.style().standardIcon( QStyle.SP_BrowserReload)) self._ui.remove_button.setIcon(self._view.style().standardIcon( QStyle.SP_DialogNoButton)) self._ui.add_button.setIcon(self._view.style().standardIcon( QStyle.SP_DialogYesButton)) self.dlg = DataManagerFilterDialog() db = QtSql.QSqlDatabase.addDatabase('QSQLITE') db.setDatabaseName( sunpy.config.get("database", "url").replace("sqlite:///", "")) model = QtSql.QSqlTableModel() model.setTable("data") model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange) model.select() self._ui.data_table.setModel(model) self.initTableHeader(model) self._ui.add_button.clicked.connect(lambda x: self.onAdd()) self._ui.remove_button.clicked.connect(lambda x: self.onRemove()) self._ui.open_button.clicked.connect(lambda x: self.onOpen()) self._ui.refresh_button.clicked.connect(lambda x: self.model.select()) self._ui.filter_button.clicked.connect(lambda x: self.onFilter()) self.sunpy_db = Database() self.model = model def initTableHeader(self, model): header = self._ui.data_table.horizontalHeader() for i in range(model.columnCount()): header.setSectionResizeMode(i, QtWidgets.QHeaderView.ResizeToContents) header.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) header.customContextMenuRequested.connect(self.onHeaderMenu) self._ui.data_table.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self._ui.data_table.customContextMenuRequested.connect( self.onHeaderMenu) self._ui.data_table.hideColumn(0) self._ui.data_table.hideColumn(2) self._ui.data_table.hideColumn(3) self._ui.data_table.hideColumn(4) self._ui.data_table.hideColumn(8) self._ui.data_table.hideColumn(11) self._ui.data_table.hideColumn(12) self._ui.data_table.hideColumn(13) self._ui.data_table.hideColumn(14) def onAdd(self): paths, _ = QtWidgets.QFileDialog.getOpenFileNames( None, filter="FITS files (*.fits; *.fit; *.fts)") for p in paths: self.sunpy_db.add_from_file(p) self.sunpy_db.commit() self.model.select() def onRemove(self): rows = set([i.row() for i in self._ui.data_table.selectedIndexes()]) for r in rows: self.model.removeRow(r) self.model.submitAll() self.model.select() def onOpen(self): rows = set([i.row() for i in self._ui.data_table.selectedIndexes()]) paths = [ self.model.index(row, self.model.fieldIndex("path")).data() for row in rows ] for path in paths: viewer_ctrl = MapViewerController.fromFile(path) self.content_ctrl.addViewerController(viewer_ctrl) def onFilter(self): if self.dlg.exec_(): self.model.setFilter(self.dlg.getFilter()) self.model.select() def onHeaderMenu(self, point): menu = QMenu(self._view) actions = [] for column in range(self.model.columnCount()): label = self.model.headerData(column, QtCore.Qt.Horizontal) action = QtWidgets.QAction(label) action.setCheckable(True) action.setChecked(not self._ui.data_table.isColumnHidden(column)) event = lambda checked, c=column: self._ui.data_table.showColumn( c) if checked else self._ui.data_table.hideColumn(c) action.triggered.connect(event) actions.append(action) menu.addActions(actions) menu.exec_(QCursor.pos()) @property def item_config(self) -> ItemConfig: return ItemConfig().setMenuPath("File/Data Manager").setTitle( "Data Manager") @property def view(self) -> QWidget: self.model.select() return self._view