Skip to content

Commit

Permalink
No need to register hooks, since they are executed within same method
Browse files Browse the repository at this point in the history
  • Loading branch information
zas committed Mar 30, 2024
1 parent 83d90a2 commit c279edd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
28 changes: 14 additions & 14 deletions picard/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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__:
Expand All @@ -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)

Expand Down
9 changes: 2 additions & 7 deletions picard/config_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit c279edd

Please sign in to comment.