def get_save_filename(parent, title, filename, filt, pickertag=None, pickertype=None): pickertype = get_pickertype(pickertag, pickertype) if pickertype == "fs": dirname, filename = os.path.split(filename) filename = getSaveFileName(parent, dirname, filt, title=title, default_filename=filename, show_save_action=True) elif pickertype in ["qt", "default"]: filename = get_save_filename_qt(parent, title, os.path.expanduser(filename), filt) else: raise FatalUserError( "Unknown file picker type '{}'.".format(pickertype)) logging.debug("Selected '%s'", filename) if filename == "": filename = None return filename
def get_open_filename(parent, title, dirname, filt, pickertag=None, pickertype=None): pickertype = get_pickertype(pickertag, pickertype) if pickertype == "fs": # fs filepicker takes file filters as a list if not isinstance(filt, list): filt = filt.split(';;') filename = getOpenFileName(parent, dirname, filt, title="Import Flight Track") elif pickertype in ["qt", "default"]: # qt filepicker takes file filters separated by ';;' filename = get_open_filename_qt(parent, title, os.path.expanduser(dirname), filt) else: raise FatalUserError(f"Unknown file picker type '{pickertype}'.") logging.debug("Selected '%s'", filename) if filename == "": filename = None return filename
def modify_config_file(data, path=constants.MSUI_SETTINGS): """ modifies a config file Args: data: data to be modified/written path: path of config file Note: sole purpose of the path argument is to be able to test with example config files """ path = path.replace("\\", "/") dir_name, file_name = fs.path.split(path) json_file_data = {} with fs.open_fs(dir_name) as _fs: if _fs.exists(file_name): try: file_content = _fs.readtext(file_name) json_file_data = json.loads( file_content, object_pairs_hook=dict_raise_on_duplicates_empty) json_file_data_copy = copy.deepcopy(json_file_data) for key in data: if key not in json_file_data: json_file_data_copy[key] = config_loader(dataset=key, default=True) modified_data = merge_dict(json_file_data_copy, data) logging.debug("Merged default and user settings") _fs.writetext(file_name, json.dumps(modified_data, indent=4)) read_config_file() except json.JSONDecodeError as e: logging.error(f"Error while loading json file {e}") error_message = f"Unexpected error while loading config\n{e}" raise FatalUserError(error_message) except ValueError as e: logging.error(f"Error while loading json file {e}") error_message = f"Invalid keys detected in config\n{e}" raise FatalUserError(error_message) else: error_message = f"MSS config File '{path}' not found" raise FileNotFoundError(error_message)
def get_existing_directory(parent, title, defaultdir, pickertag=None, pickertype=None): pickertype = get_pickertype(pickertag, pickertype) if pickertype == "fs": dirname = getExistingDirectory(parent, title=title, fs_url=defaultdir)[0] elif pickertype in ["qt", "default"]: dirname = get_existing_directory_qt(parent, title, defaultdir) else: raise FatalUserError(f"Unknown file picker type '{pickertype}'.") logging.debug("Selected '%s'", dirname) if dirname == "": dirname = None return dirname
def read_config_file(path=constants.MSUI_SETTINGS): """ reads a config file and updates global user_options Args: path: path of config file Note: sole purpose of the path argument is to be able to test with example config files """ path = path.replace("\\", "/") dir_name, file_name = fs.path.split(path) json_file_data = {} with fs.open_fs(dir_name) as _fs: if _fs.exists(file_name): file_content = _fs.readtext(file_name) try: json_file_data = json.loads( file_content, object_pairs_hook=dict_raise_on_duplicates_empty) except json.JSONDecodeError as e: logging.error(f"Error while loading json file {e}") error_message = f"Unexpected error while loading config\n{e}" raise FatalUserError(error_message) except ValueError as e: logging.error(f"Error while loading json file {e}") error_message = f"Invalid keys detected in config\n{e}" raise FatalUserError(error_message) else: error_message = f"MSS config File '{path}' not found" raise FileNotFoundError(error_message) global user_options if json_file_data: user_options = merge_dict(copy.deepcopy(default_options), json_file_data) logging.debug("Merged default and user settings") else: user_options = copy.deepcopy(default_options) logging.debug("No user settings found, using default settings")
def get_open_filenames(parent, title, dirname, filt, pickertag=None, pickertype=None): """ Opens multiple files simultaneously Currently implemented only in kmloverlay_dockwidget.py """ pickertype = get_pickertype(pickertag, pickertype) if pickertype in ["qt", "default"]: filename = get_open_filenames_qt(parent, title, os.path.expanduser(dirname), filt) else: raise FatalUserError(f"Unknown file picker type '{pickertype}'.") logging.debug("Selected '%s'", filename) if filename == "": filename = None return filename
def save_figure(self, *args): picker_default = config_loader(dataset="filepicker_default", default=mss_default.filepicker_default) picker_type = config_loader(dataset="filepicker_matplotlib", default=picker_default) if picker_type in ["default", "qt"]: save_figure_original(self, *args) elif picker_type == "fs": filetypes = self.canvas.get_supported_filetypes_grouped() sorted_filetypes = sorted(six.iteritems(filetypes)) startpath = matplotlib.rcParams.get('savefig.directory', LAST_SAVE_DIRECTORY) startpath = os.path.expanduser(startpath) start = os.path.join(startpath, self.canvas.get_default_filename()) filters = [] for name, exts in sorted_filetypes: exts_list = " ".join(['*.%s' % ext for ext in exts]) filter = '%s (%s)' % (name, exts_list) filters.append(filter) fname, filter = _getSaveFileName(self.parent, title="Choose a filename to save to", filename=start, filters=filters) if fname is not None: if not fname.endswith(filter[1:]): fname = filter.replace('*', fname) if startpath == '': # explicitly missing key or empty str signals to use cwd matplotlib.rcParams['savefig.directory'] = startpath else: # save dir for next time savefig_dir = os.path.dirname(six.text_type(fname)) matplotlib.rcParams['savefig.directory'] = savefig_dir try: _dirname, _name = os.path.split(fname) _fs = open_fs(_dirname) with _fs.open(_name, 'wb') as source: self.canvas.print_figure(source, format=filter.replace('*.', '')) except Exception as e: QtWidgets.QMessageBox.critical(self, "Error saving file", six.text_type(e), QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton) else: raise FatalUserError( "Unknown file picker type '{}'".format(picker_type))
def get_open_filename(parent, title, dirname, filt, pickertag=None, pickertype=None): pickertype = get_pickertype(pickertag, pickertype) if pickertype == "fs": filename = getOpenFileName(parent, dirname, filt, title="Import Flight Track") elif pickertype in ["qt", "default"]: filename = get_open_filename_qt(parent, title, os.path.expanduser(dirname), filt) else: raise FatalUserError( "Unknown file picker type '{}'.".format(pickertype)) logging.debug("Selected '%s'", filename) if filename == "": filename = None return filename