def __init__(self, name, description, settings_packs, x, y, toolbox, project, logger): """ Args: name (str): item name description (str): item description settings_packs (list): dicts mapping database URLs to _SettingsPack objects x (float): initial X coordinate of item icon y (float): initial Y coordinate of item icon toolbox (ToolboxUI): a ToolboxUI instance project (SpineToolboxProject): the project this item belongs to logger (LoggerInterface): a logger instance """ super().__init__(name, description, x, y, project, logger) self._toolbox = toolbox self._settings_packs = dict() self._workers = dict() if settings_packs is None: settings_packs = list() for pack in settings_packs: serialized_url = pack["database_url"] url = deserialize_path(serialized_url, self._project.project_dir) url = _normalize_url(url) settings_pack = _SettingsPack.from_dict(pack, url) settings_pack.notifications.changed_due_to_settings_state.connect( self._report_notifications) self._settings_packs[url] = settings_pack self._activated = False self._project.db_mngr.session_committed.connect( self._update_settings_after_db_commit) for url, pack in self._settings_packs.items(): if pack.state != SettingsState.OK: self._start_worker(url)
def __init__( self, toolbox, project, logger, name, description, settings_packs, x, y, cancel_on_export_error=None, cancel_on_error=None, ): """ Args: toolbox (ToolboxUI): a ToolboxUI instance project (SpineToolboxProject): the project this item belongs to logger (LoggerInterface): a logger instance name (str): item name description (str): item description settings_packs (list): dicts mapping database URLs to _SettingsPack objects x (float): initial X coordinate of item icon y (float): initial Y coordinate of item icon cancel_on_export_error (bool, options): legacy ``cancel_on_error`` option cancel_on_error (bool, options): True if execution should fail on all export errors, False to ignore certain error cases; optional to provide backwards compatibility """ super().__init__(name, description, x, y, project, logger) self._toolbox = toolbox if cancel_on_error is not None: self._cancel_on_error = cancel_on_error else: self._cancel_on_error = cancel_on_export_error if cancel_on_export_error is not None else True self._settings_packs = dict() self._export_list_items = dict() self._workers = dict() if settings_packs is None: settings_packs = list() for pack in settings_packs: serialized_url = pack["database_url"] url = deserialize_path(serialized_url, self._project.project_dir) url = _normalize_url(url) try: settings_pack = SettingsPack.from_dict(pack, url, logger) except gdx.GdxExportException as error: logger.msg_error.emit( f"Failed to fully restore Exporter settings: {error}") settings_pack = SettingsPack("") settings_pack.notifications.changed_due_to_settings_state.connect( self._report_notifications) self._settings_packs[url] = settings_pack for url, pack in self._settings_packs.items(): if pack.state not in (SettingsState.OK, SettingsState.INDEXING_PROBLEM): self._start_worker(url) elif pack.last_database_commit != _latest_database_commit_time_stamp( url): self._start_worker(url, update_settings=True)
def test_deserialize_path_with_absolute_path(self): with TemporaryDirectory() as project_dir: serialized = { "type": "path", "relative": False, "path": str(Path(gettempdir(), "file.fat").as_posix()) } deserialized = deserialize_path(serialized, project_dir) self.assertEqual(deserialized, str(Path(gettempdir(), "file.fat")))
def test_deserialize_path_with_relative_path(self): project_dir = gettempdir() serialized = { "type": "path", "relative": True, "path": "subdir/file.fat" } deserialized = deserialize_path(serialized, project_dir) self.assertEqual(deserialized, str(Path(project_dir, "subdir", "file.fat")))
def from_dict(cls, item_dict, name, project_dir, app_settings, specifications, logger): """See base class.""" if item_dict["url"]["dialect"] == "sqlite": item_dict["url"]["database"] = deserialize_path( item_dict["url"]["database"], project_dir) url = convert_to_sqlalchemy_url(item_dict["url"], name, logger, log_errors=True) return cls(name, url, logger)
def test_deserialize_path_with_relative_file_url(self): project_dir = gettempdir() serialized = { "type": "file_url", "relative": True, "path": "subdir/database.sqlite", "scheme": "sqlite" } deserialized = deserialize_path(serialized, project_dir) expected = "sqlite:///" + str( Path(project_dir, "subdir", "database.sqlite")) self.assertEqual(deserialized, expected)
def test_deserialize_path_with_absolute_file_url(self): with TemporaryDirectory() as project_dir: path = str(Path(gettempdir(), "database.sqlite").as_posix()) serialized = { "type": "file_url", "relative": False, "path": path, "scheme": "sqlite" } deserialized = deserialize_path(serialized, project_dir) expected = "sqlite:///" + str(Path(gettempdir(), "database.sqlite")) self.assertEqual(deserialized, expected)
def deserialize_mappings(mappings, project_path): """Returns mapping settings as dict with absolute paths as keys. Args: mappings (list): List where each element contains two dictionaries (path dict and mapping dict) project_path (str): Path to project directory Returns: dict: Dictionary with absolute paths as keys and mapping settings as values """ abs_path_mappings = {} for source, mapping in mappings: abs_path_mappings[deserialize_path(source, project_path)] = mapping return abs_path_mappings
def from_dict(cls, item_dict, name, project_dir, app_settings, specifications, logger): """See base class.""" references = item_dict["references"] file_references = [ deserialize_path(r, project_dir) for r in references ] data_dir = pathlib.Path(project_dir, ".spinetoolbox", "items", item_dict["short name"]) data_files = list() with os.scandir(data_dir) as scan_iterator: for entry in scan_iterator: if entry.is_file(): data_files.append(entry.path) return cls(name, file_references, data_files, logger)
def deserialize_checked_states(serialized, project_path): """Reverse operation for serialize_checked_states above. Returns absolute file paths with their check state as boolean. Args: serialized (list): List of serialized paths with a boolean value project_path (str): Absolute project directory path Returns: dict: Dictionary with paths as keys and boolean check states as value """ if not serialized: return dict() deserialized = dict() for serialized_label, checked in serialized: label = deserialize_path(serialized_label, project_path) deserialized[label] = checked return deserialized
def __init__(self, name, description, x, y, toolbox, project, logger, references=None): """Data Connection class. Args: name (str): Object name description (str): Object description x (float): Initial X coordinate of item icon y (float): Initial Y coordinate of item icon toolbox (ToolboxUI): QMainWindow instance project (SpineToolboxProject): the project this item belongs to logger (LoggerInterface): a logger instance references (list): a list of file paths """ super().__init__(name, description, x, y, project, logger) self._toolbox = toolbox self.reference_model = QStandardItemModel() # References to files self.data_model = QStandardItemModel( ) # Paths of project internal files. These are found in DC data directory self.datapackage_icon = QIcon(QPixmap(":/icons/datapkg.png")) self.data_dir_watcher = QFileSystemWatcher(self) if os.path.isdir(self.data_dir): self.data_dir_watcher.addPath(self.data_dir) # Populate references model if references is None: references = list() # Convert relative paths to absolute absolute_refs = [ deserialize_path(r, self._project.project_dir) for r in references ] self.references = absolute_refs self.populate_reference_list(self.references) # Populate data (files) model data_files = self.data_files() self.populate_data_list(data_files) self.spine_datapackage_form = None self.data_dir_watcher.directoryChanged.connect(self.refresh)
def __init__(self, name, description, x, y, toolbox, project, logger, url=None): """Data Store class. Args: name (str): Object name description (str): Object description x (float): Initial X coordinate of item icon y (float): Initial Y coordinate of item icon toolbox (ToolboxUI): QMainWindow instance project (SpineToolboxProject): the project this item belongs to logger (LoggerInterface): a logger instance url (str or dict): SQLAlchemy url """ super().__init__(name, description, x, y, project, logger) if url is None: url = dict() if url and not isinstance(url["database"], str): url["database"] = deserialize_path(url["database"], self._project.project_dir) self._toolbox = toolbox self._url = self.parse_url(url) self._sa_url = None self.ds_view = None self._for_spine_model_checkbox_state = Qt.Unchecked # Make logs directory for this Data Store self.logs_dir = os.path.join(self.data_dir, "logs") try: create_dir(self.logs_dir) except OSError: self._logger.msg_error.emit( f"[OSError] Creating directory {self.logs_dir} failed. Check permissions." )
def test_deserialize_path_with_non_file_url(self): project_dir = gettempdir() serialized = {"type": "url", "path": "http://www.spine-model.org/"} deserialized = deserialize_path(serialized, project_dir) self.assertEqual(deserialized, "http://www.spine-model.org/")