Skip to content

Commit

Permalink
Compare and strip strings a bit nicer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauritsvanrees committed Sep 8, 2015
1 parent 5d2341a commit b9fe932
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Products/GenericSetup/events.py
Expand Up @@ -50,5 +50,6 @@ def handleProfileImportedEvent(event):

version = event.tool.getVersionForProfile(profile_id)
if version and version != 'unknown':
profile_id = profile_id[len('profile-'):]
prefix = 'profile-'
profile_id = profile_id[len(prefix):]
event.tool.setLastVersionForProfile(profile_id, version)
10 changes: 6 additions & 4 deletions Products/GenericSetup/registry.py
Expand Up @@ -644,10 +644,12 @@ def __init__(self):
def getProfileInfo(self, profile_id, for_=None):
""" See IProfileRegistry.
"""
if profile_id.startswith("profile-"):
profile_id = profile_id[len('profile-'):]
elif profile_id.startswith("snapshot-"):
profile_id = profile_id[len('snapshot-'):]
prefixes = ('profile-', 'snapshot-')
for prefix in prefixes:
if profile_id.startswith(prefix):
profile_id = profile_id[len(prefix):]
break

result = self._registered.get(profile_id)
if result is None:
raise KeyError(profile_id)
Expand Down
20 changes: 12 additions & 8 deletions Products/GenericSetup/tool.py
Expand Up @@ -807,17 +807,19 @@ def manage_deleteExportSteps(self, ids, request=None):
def getLastVersionForProfile(self, profile_id):
"""Return the last upgraded version for the specified profile.
"""
if profile_id.startswith("profile-"):
profile_id = profile_id[len('profile-'):]
prefix = 'profile-'
if profile_id.startswith(prefix):
profile_id = profile_id[len(prefix):]
version = self._profile_upgrade_versions.get(profile_id, 'unknown')
return version

security.declareProtected(ManagePortal, 'setLastVersionForProfile')
def setLastVersionForProfile(self, profile_id, version):
"""Set the last upgraded version for the specified profile.
"""
if profile_id.startswith("profile-"):
profile_id = profile_id[len('profile-'):]
prefix = 'profile-'
if profile_id.startswith(prefix):
profile_id = profile_id[len(prefix):]
if isinstance(version, basestring):
version = tuple(version.split('.'))
prof_versions = self._profile_upgrade_versions.copy()
Expand Down Expand Up @@ -1003,14 +1005,16 @@ def _getImportContext(self, context_id, should_purge=None, archive=None):
encoding = self.getEncoding()

if context_id is not None:
if context_id.startswith('snapshot-'):
context_id = context_id[len('snapshot-'):]
prefix = 'snapshot-'
if context_id.startswith(prefix):
context_id = context_id[len(prefix):]
if should_purge is None:
should_purge = True
return SnapshotImportContext(self, context_id, should_purge,
encoding)
if context_id.startswith('profile-'):
context_id = context_id[len('profile-'):]
prefix = 'profile-'
if context_id.startswith(prefix):
context_id = context_id[len(prefix):]
info = _profile_registry.getProfileInfo(context_id)
if info.get('product'):
path = os.path.join(_getProductPath(info['product']),
Expand Down
10 changes: 6 additions & 4 deletions Products/GenericSetup/upgrade.py
Expand Up @@ -81,8 +81,9 @@ def getUpgradeStepsForProfile(self, profile_id):
None if there are no steps registered for a profile matching
that id.
"""
if profile_id.startswith("profile-"):
profile_id = profile_id[len('profile-'):]
prefix = 'profile-'
if profile_id.startswith(prefix):
profile_id = profile_id[len(prefix):]
profile_steps = self._registry.get(profile_id)
if profile_steps is None:
self._registry[profile_id] = OOBTree()
Expand All @@ -93,8 +94,9 @@ def getUpgradeStep(self, profile_id, step_id):
"""Returns the specified upgrade step for the specified
profile, or None if it doesn't exist.
"""
if profile_id.startswith("profile-"):
profile_id = profile_id[len('profile-'):]
prefix = 'profile-'
if profile_id.startswith(prefix):
profile_id = profile_id[len(prefix):]
profile_steps = self._registry.get(profile_id)
if profile_steps is not None:
step = profile_steps.get(step_id, None)
Expand Down

0 comments on commit b9fe932

Please sign in to comment.