From c279edd52e3fc2ce4253f01cb75aa1379d71f399 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Sat, 30 Mar 2024 12:47:39 +0100 Subject: [PATCH] No need to register hooks, since they are executed within same method --- picard/config.py | 28 ++++++++++++++-------------- picard/config_upgrade.py | 9 ++------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/picard/config.py b/picard/config.py index e15efd9277..5061714b15 100644 --- a/picard/config.py +++ b/picard/config.py @@ -246,7 +246,6 @@ def __initialize(self): if 'version' not in self.application or not self.application['version']: TextOption('application', 'version', '0.0.0dev0') self._version = Version.from_string(self.application['version']) - self._upgrade_hooks = dict() @classmethod def from_app(cls, parent): @@ -284,18 +283,13 @@ def from_file(cls, parent, filename): this.__initialize() return this - def register_upgrade_hook(self, to_version, func): - """Register a function to upgrade from one config version to another""" - assert to_version <= PICARD_VERSION, "%r > %r !!!" % (to_version, PICARD_VERSION) - self._upgrade_hooks[to_version] = func - - def run_upgrade_hooks(self): - """Executes registered functions to upgrade config version to the latest""" + def run_upgrade_hooks(self, hooks): + """Executes passed hooks to upgrade config version to the latest""" if self._version == Version(0, 0, 0, 'dev', 0): # This is a freshly created config self._write_version(PICARD_VERSION) return - if not self._upgrade_hooks: + if not hooks: return if self._version >= PICARD_VERSION: if self._version > PICARD_VERSION: @@ -305,8 +299,14 @@ def run_upgrade_hooks(self): PICARD_VERSION.to_string() )) return - for version in sorted(self._upgrade_hooks): - hook = self._upgrade_hooks[version] + for version in sorted(hooks): + hook = hooks[version] + if version > PICARD_VERSION: + raise ConfigUpgradeError( + "Upgrade hook %s has version %s > Picard version %s" + % (hook.__name__, version, PICARD_VERSION) + ) + if self._version < version: try: if hook.__doc__: @@ -324,13 +324,13 @@ def run_upgrade_hooks(self): hook.__name__, )) from e else: - del self._upgrade_hooks[version] + del hooks[version] self._write_version(version) else: # hook is not applicable, mark as done - del self._upgrade_hooks[version] + del hooks[version] - if not self._upgrade_hooks: + if not hooks: # all hooks were executed, ensure config is marked with latest version self._write_version(PICARD_VERSION) diff --git a/picard/config_upgrade.py b/picard/config_upgrade.py index 002df9770f..08c67f3ace 100644 --- a/picard/config_upgrade.py +++ b/picard/config_upgrade.py @@ -562,12 +562,7 @@ def is_upgrade_hook(f): ) # Build a dict with version as key and function as value - hooks = { + config.run_upgrade_hooks({ Version.from_string(name[len(UPGRADE_FUNCTION_PREFIX):]): hook for name, hook in getmembers(sys.modules[__name__], predicate=is_upgrade_hook) - } - - for to_version, hook in hooks.items(): - config.register_upgrade_hook(to_version, hook) - - config.run_upgrade_hooks() + })