From d01ffd9373a0cc2f5196804da9e53f4c660e80ec Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Tue, 26 Oct 2021 21:24:11 +0200 Subject: [PATCH] flame: fixing contextmanager for save and load preferences file --- openpype/hosts/flame/api/lib.py | 48 +++++++++++---------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/openpype/hosts/flame/api/lib.py b/openpype/hosts/flame/api/lib.py index a58b67d54a..2b3396c420 100644 --- a/openpype/hosts/flame/api/lib.py +++ b/openpype/hosts/flame/api/lib.py @@ -10,37 +10,15 @@ log = Logger().get_logger(__name__) @contextlib.contextmanager -def load_preferences_file(klass, filepath, attribute): +def io_preferences_file(klass, filepath, write=False): try: - with open(filepath, "r") as prefs_file: - setattr(klass, attribute, pickle.load(prefs_file)) - - yield + flag = "w" if write else "r" + yield open(filepath, flag) except IOError: - klass.log.info("Unable to load preferences from {}".format( + klass.log.info("Unable to work with preferences `{}`".format( filepath)) - finally: - klass.log.info("Preferences loaded from {}".format(filepath)) - - -@contextlib.contextmanager -def save_preferences_file(klass, filepath, attribute): - try: - with open(filepath, "w") as prefs_file: - attr = getattr(klass, attribute) - pickle.dump(attr, prefs_file) - - yield - - except IOError: - klass.log.info("Unable to save preferences to {}".format( - filepath)) - - finally: - klass.log.info("Preferences saved to {}".format(filepath)) - class FlameAppFramework(object): # flameAppFramework class takes care of preferences @@ -170,19 +148,22 @@ class FlameAppFramework(object): (proj_pref_path, user_pref_path, glob_pref_path) = self.get_pref_file_paths() - with load_preferences_file(self, proj_pref_path, "prefs"): + with io_preferences_file(self, proj_pref_path) as prefs_file: + self.prefs = pickle.load(prefs_file) self.log.info( "Project - preferences contents:\n{}".format( pformat(self.prefs) )) - with load_preferences_file(self, user_pref_path, "prefs_user"): + with io_preferences_file(self, user_pref_path) as prefs_file: + self.prefs_user = pickle.load(prefs_file) self.log.info( "User - preferences contents:\n{}".format( pformat(self.prefs_user) )) - with load_preferences_file(self, glob_pref_path, "prefs_global"): + with io_preferences_file(self, glob_pref_path) as prefs_file: + self.prefs_global = pickle.load(prefs_file) self.log.info( "Global - preferences contents:\n{}".format( pformat(self.prefs_global) @@ -204,19 +185,22 @@ class FlameAppFramework(object): (proj_pref_path, user_pref_path, glob_pref_path) = self.get_pref_file_paths() - with save_preferences_file(self, proj_pref_path, "prefs"): + with io_preferences_file(self, proj_pref_path, True) as prefs_file: + pickle.dump(self.prefs, prefs_file) self.log.info( "Project - preferences contents:\n{}".format( pformat(self.prefs) )) - with save_preferences_file(self, user_pref_path, "prefs_user"): + with io_preferences_file(self, user_pref_path, True) as prefs_file: + pickle.dump(self.prefs_user, prefs_file) self.log.info( "User - preferences contents:\n{}".format( pformat(self.prefs_user) )) - with save_preferences_file(self, glob_pref_path, "prefs_global"): + with io_preferences_file(self, glob_pref_path, True) as prefs_file: + pickle.dump(self.prefs_global, prefs_file) self.log.info( "Global - preferences contents:\n{}".format( pformat(self.prefs_global)