From e4f3912f4f0d8230aab07c4ffdaa127c0ba5579b Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 28 Apr 2020 04:15:21 +0100 Subject: [PATCH] Cleanup of defaults --- FlatCAMApp.py | 147 ++++++++++---------------------------------------- defaults.py | 79 ++++++++++++--------------- 2 files changed, 63 insertions(+), 163 deletions(-) diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 4bac9544..61c0f913 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -390,7 +390,7 @@ class App(QtCore.QObject): f.close() # Write factory_defaults.FlatConfig file to disk - FlatCAMDefaults.save_factory_defaults_file(os.path.join(self.data_path, "factory_defaults.FlatConfig")) + FlatCAMDefaults.save_factory_defaults(os.path.join(self.data_path, "factory_defaults.FlatConfig")) # create a recent files json file if there is none try: @@ -432,7 +432,7 @@ class App(QtCore.QObject): self.fcDefaults = FlatCAMDefaults() current_defaults_path = os.path.join(self.data_path, "current_defaults.FlatConfig") if user_defaults: - self.fcDefaults.load_defaults(filename=current_defaults_path) + self.fcDefaults.load(filename=current_defaults_path) self.defaults = self.fcDefaults.defaults if self.defaults["global_gray_icons"] is False: @@ -3229,7 +3229,7 @@ class App(QtCore.QObject): return # Load in the defaults from the chosen file - self.fcDefaults.load_defaults(filename=filename) + self.fcDefaults.load(filename=filename) self.on_preferences_edited() self.inform.emit('[success] %s: %s' % (_("Imported Defaults from"), filename)) @@ -3245,66 +3245,35 @@ class App(QtCore.QObject): defaults_file_content = None - self.date = str(datetime.today()).rpartition('.')[0] - self.date = ''.join(c for c in self.date if c not in ':-') - self.date = self.date.replace(' ', '_') - + # Show file chooser + date = str(datetime.today()).rpartition('.')[0] + date = ''.join(c for c in date if c not in ':-') + date = date.replace(' ', '_') filter__ = "Config File .FlatConfig (*.FlatConfig);;All Files (*.*)" try: filename, _f = FCFileSaveDialog.get_saved_filename( caption=_("Export FlatCAM Preferences"), - directory=self.data_path + '/preferences_' + self.date, + directory=self.data_path + '/preferences_' + date, filter=filter__ ) except TypeError: filename, _f = FCFileSaveDialog.get_saved_filename(caption=_("Export FlatCAM Preferences"), filter=filter__) - filename = str(filename) - defaults_from_file = {} - if filename == "": self.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled.")) return - else: - try: - f = open(filename, 'w') - defaults_file_content = f.read() - f.close() - except PermissionError: - self.inform.emit('[WARNING] %s' % - _("Permission denied, saving not possible.\n" - "Most likely another app is holding the file open and not accessible.")) - return - except IOError: - App.log.debug('Creating a new preferences file ...') - f = open(filename, 'w') - json.dump({}, f) - f.close() - except Exception: - e = sys.exc_info()[0] - App.log.error("Could not load defaults file.") - App.log.error(str(e)) - self.inform.emit('[ERROR_NOTCL] %s' % _("Could not load preferences file.")) - return - try: - defaults_from_file = json.loads(defaults_file_content) - except Exception: - App.log.warning("Trying to read an empty Preferences file. Continue.") + # Update options + self.defaults_read_form() + self.propagate_defaults() - # Update options - self.defaults_read_form() - defaults_from_file.update(self.defaults) - self.propagate_defaults(silent=True) + # Save update options + try: + self.fcDefaults.write(filename=filename) + except Exception: + self.inform.emit('[ERROR_NOTCL] %s %s' % (_("Failed to write defaults to file."), str(filename))) + return - # Save update options - try: - f = open(filename, "w") - json.dump(defaults_from_file, f, default=to_dict, indent=2, sort_keys=True) - f.close() - except Exception: - self.inform.emit('[ERROR_NOTCL] %s %s' % (_("Failed to write defaults to file."), str(filename))) - return if self.defaults["global_open_style"] is False: self.file_opened.emit("preferences", filename) self.file_saved.emit("preferences", filename) @@ -4231,35 +4200,9 @@ class App(QtCore.QObject): :return: None """ - self.save_defaults() - # def on_app_exit(self): - # self.report_usage("on_app_exit()") - # - # if self.collection.get_list(): - # msgbox = QtWidgets.QMessageBox() - # # msgbox.setText("Save changes ...") - # msgbox.setText("There are files/objects opened in FlatCAM. " - # "\n" - # "Do you want to Save the project?") - # msgbox.setWindowTitle("Save changes") - # msgbox.setWindowIcon(QtGui.QIcon(self.resource_location + '/save_as.png')) - # msgbox.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No | - # QtWidgets.QMessageBox.Cancel) - # msgbox.setDefaultButton(QtWidgets.QMessageBox.Yes) - # - # response = msgbox.exec_() - # - # if response == QtWidgets.QMessageBox.Yes: - # self.on_file_saveprojectas(thread=False) - # elif response == QtWidgets.QMessageBox.Cancel: - # return - # self.save_defaults() - # else: - # self.save_defaults() - # log.debug("Application defaults saved ... Exit event.") - # QtWidgets.qApp.quit() + def save_defaults(self, silent=False, data_path=None, first_time=False): """ @@ -4279,43 +4222,16 @@ class App(QtCore.QObject): if data_path is None: data_path = self.data_path - # Read options from file - try: - f = open(data_path + "/current_defaults.FlatConfig") - defaults_file_content = f.read() - f.close() - except Exception: - e = sys.exc_info()[0] - App.log.error("Could not load defaults file.") - App.log.error(str(e)) - self.inform.emit('[ERROR_NOTCL] %s' % _("Could not load defaults file.")) - return - - try: - defaults = json.loads(defaults_file_content) - except Exception: - e = sys.exc_info()[0] - App.log.error("Failed to parse defaults file.") - App.log.error(str(e)) - self.inform.emit('[ERROR_NOTCL] %s' % _("Failed to parse defaults file.")) - return - - # Update options - self.defaults_read_form() - defaults.update(self.defaults) - self.propagate_defaults(silent=True) + self.propagate_defaults() if first_time is False: self.save_toolbar_view() - # Save update options - filename = data_path + "/current_defaults.FlatConfig" + # Save the options to disk try: - f = open(filename, "w") - json.dump(defaults, f, default=to_dict, indent=2, sort_keys=True) - f.close() + self.fcDefaults.write(filename=os.path.join(data_path, "current_defaults.FlatConfig")) except Exception as e: - log.debug("App.save_defaults() --> %s" % str(e)) + log.error("save_defaults() --> Failed to write defaults to file %s" % str(e)) self.inform.emit('[ERROR_NOTCL] %s %s' % (_("Failed to write defaults to file."), str(filename))) return @@ -10917,18 +10833,13 @@ class App(QtCore.QObject): App.log.debug(" **************** Finished PROJECT loading... **************** ") - def propagate_defaults(self, silent=False): + def propagate_defaults(self): """ This method is used to set default values in classes. It's an alternative to project options but allows the use of values invisible to the user. - - :param silent: No messages - :return: None """ - - if silent is False: - self.log.debug("propagate_defaults()") + self.log.debug("propagate_defaults()") # Which objects to update the given parameters. routes = { @@ -10947,11 +10858,8 @@ class App(QtCore.QObject): if param in routes[param].defaults: try: routes[param].defaults[param] = self.defaults[param] - if silent is False: - self.log.debug(" " + param + " OK") except KeyError: - if silent is False: - self.log.debug("FlatCAMApp.propagate_defaults() --> ERROR: " + param + " not in defaults.") + self.log.error("FlatCAMApp.propagate_defaults() --> ERROR: " + param + " not in defaults.") else: # Try extracting the name: # classname_param here is param in the object @@ -10959,8 +10867,9 @@ class App(QtCore.QObject): p = param[len(routes[param].__name__) + 1:] if p in routes[param].defaults: routes[param].defaults[p] = self.defaults[param] - if silent is False: - self.log.debug(" " + param + " OK!") + + + def plot_all(self, fit_view=True, use_thread=True): """ diff --git a/defaults.py b/defaults.py index 1d087ce0..01b48179 100644 --- a/defaults.py +++ b/defaults.py @@ -6,19 +6,13 @@ from FlatCAMCommon import LoudDict from camlib import to_dict import simplejson import logging -# FlatCAM Translation import gettext import FlatCAMTranslation as fcTranslate import builtins fcTranslate.apply_language('strings') if '_' not in builtins.__dict__: _ = gettext.gettext - -# TODO: -# * on_import_preferences -# * on_export_preferences - -log = logging.getLogger('base') +log = logging.getLogger('FlatCAMDefaults') class FlatCAMDefaults: @@ -667,6 +661,27 @@ class FlatCAMDefaults: "document_tab_size": 80, } + @classmethod + def save_factory_defaults(cls, file_path: str): + """Writes the factory defaults to a file at the given path, overwriting any existing file.""" + # Delete any existing factory defaults file + if os.path.isfile(file_path): + os.chmod(file_path, stat.S_IRWXO | stat.S_IWRITE | stat.S_IWGRP) + os.remove(file_path) + + try: + # recreate a new factory defaults file and save the factory defaults data into it + f_f_def_s = open(file_path, "w") + simplejson.dump(cls.factory_defaults, f_f_def_s, default=to_dict, indent=2, sort_keys=True) + f_f_def_s.close() + + # and then make the factory_defaults.FlatConfig file read_only + # so it can't be modified after creation. + os.chmod(file_path, stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH) + log.debug("FlatCAM factory defaults written to: %s" % file_path) + except Exception as e: + log.error("save_factory_defaults() -> %s" % str(e)) + def __init__(self): self.defaults = LoudDict() self.defaults.update(self.factory_defaults) @@ -674,13 +689,13 @@ class FlatCAMDefaults: self.current_defaults.update(self.factory_defaults) self.old_defaults_found = False - def load_defaults(self, filename: str): - """ - Loads the application's default settings from current_defaults.FlatConfig into - ``self.defaults``. + def write(self, filename: str): + """Saves the defaults to a file on disk""" + with open(filename, "w") as file: + simplejson.dump(self.defaults, file, default=to_dict, indent=2, sort_keys=True) - :return: None - """ + def load(self, filename: str): + """Loads the defaults from a file on disk, performing migration if required.""" # Read in the file try: @@ -708,9 +723,9 @@ class FlatCAMDefaults: return # Perform migration if necessary - if self.is_old_defaults(defaults): + if self.__is_old_defaults(defaults): self.old_defaults_found = True - defaults = self.migrate_old_defaults(defaults=defaults) + defaults = self.__migrate_old_defaults(defaults=defaults) else: self.old_defaults_found = False @@ -720,16 +735,12 @@ class FlatCAMDefaults: log.debug("FlatCAM defaults loaded from: %s" % filename) - def is_old_defaults(self, defaults: dict) -> bool: - """ - Takes a defaults dict and determines whether or not migration is necessary. - """ + def __is_old_defaults(self, defaults: dict) -> bool: + """Takes a defaults dict and determines whether or not migration is necessary.""" return 'version' not in defaults or defaults['version'] != self.factory_defaults['version'] - def migrate_old_defaults(self, defaults: dict) -> dict: - """ - Performs migration on the passed-in defaults dictionary, and returns the migrated dict - """ + def __migrate_old_defaults(self, defaults: dict) -> dict: + """Performs migration on the passed-in defaults dictionary, and returns the migrated dict""" migrated = {} for k, v in defaults.items(): if k in self.factory_defaults and k != 'version': @@ -751,26 +762,6 @@ class FlatCAMDefaults: migrated[k] = v return migrated - @classmethod - def save_factory_defaults_file(cls, file_path: str): - """ - Writes the factory defaults to a file at the given path, overwriting any existing file. - Sets the file to be read only. - """ - # Delete any existing factory defaults file - if os.path.isfile(file_path): - os.chmod(file_path, stat.S_IRWXO | stat.S_IWRITE | stat.S_IWGRP) - os.remove(file_path) - try: - # recreate a new factory defaults file and save the factory defaults data into it - f_f_def_s = open(file_path, "w") - simplejson.dump(cls.factory_defaults, f_f_def_s, default=to_dict, indent=2, sort_keys=True) - f_f_def_s.close() - # and then make the factory_defaults.FlatConfig file read_only - # so it can't be modified after creation. - os.chmod(file_path, stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH) - log.debug("FlatCAM factory defaults written to: %s" % file_path) - except Exception as e: - log.error("save_factory_defaults_file() -> %s" % str(e)) +