Skip to content

Commit

Permalink
Added better-late-than-never update migration code
Browse files Browse the repository at this point in the history
This should have been in 1.2.0 for the two people updating from 1.1.0,
when I renamed `MAX_RECENT_CLIPS` to `MAX_CLIPS_VISIBLE_IN_MENU`.

The real highlight of this commit are the three shiny new
`ConfigParseBetter` methods. Don't touch them.

I made the methods with a `replace` parameter, so this code should have
literally no effect on anyone (unless you're updating from 1.1.0 like a
psycho).
  • Loading branch information
thisismy-github committed Nov 12, 2023
1 parent 7a14d43 commit cb17300
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
75 changes: 75 additions & 0 deletions configparsebetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,81 @@ def renameSection(self, section, newSection):
parser.remove_section(section.name)


def moveSetting(self, oldKey, newSection, newKey=None, oldSection=None, replace=True):
''' Moves an option from `oldSection.oldKey` to `newSection.newKey`,
overwriting `newSection.newKey` if it already exists and `replace`
is True. If `newKey` is not provided, `newSection.oldKey` is used.
If `oldSection` is not provided, the current section is used.
Returns the value of `newSection.newKey` after the move.
If `oldSection.oldKey` does not exist, None is returned. '''
old_section = self.getSection(oldSection)
new_section = self.getSection(newSection)
old_section_dict: dict = self.__parser._sections[old_section.name]
new_section_dict: dict = self.__parser._sections[new_section.name]

# replace the new setting if necessary & get value to return later
newKey = newKey or oldKey
already_exists = newKey in new_section_dict
if replace or not already_exists:
try: value = self.__dict__[old_section.name].__dict__[oldKey]
except KeyError: return None
new_section_dict[newKey] = old_section_dict[oldKey]
else: # `already_exists` must be True if we got this far
value = self.__dict__[new_section.name].__dict__[newKey]
del old_section_dict[oldKey]

try: # replace `old` with `new` within our order
index = KEY_ORDER.index(old_section.name + oldKey)
KEY_ORDER.pop(index)
if not already_exists:
KEY_ORDER.insert(index, new_section.name + newKey)
except:
pass
return value


def renameSetting(self, old, new, replace=True, section=None):
''' Renames an option from `section.old` to `section.new`, overwriting
the `new` key if it already exists and `replace` is True. Returns
the value of `section.new` after the rename. If `section.old`
does not exist, None is returned. '''
section = self.getSection(section)
section_name = section.name
section_dict = self.__parser._sections[section_name]

# replace the new setting if necessary & get value to return later
already_exists = new in section_dict
if replace or not already_exists:
try: value = self.__dict__[section_name].__dict__[old]
except KeyError: return None
section_dict[new] = section_dict[old]
else: # `already_exists` must be True if we got this far
value = self.__dict__[section_name].__dict__[new]
del section_dict[old]

try: # replace `old` with `new` within our order
index = KEY_ORDER.index(section_name + old)
KEY_ORDER.pop(index)
if not already_exists:
KEY_ORDER.insert(index, section_name + new)
except:
pass
return value


def deleteSetting(self, key, section=None):
section = self.getSection(section)
section_name = section.name
section_dict = self.__parser._sections[section_name]
del section_dict[key]

try: # remove from our order
order_key = section_name + key
KEY_ORDER.pop(KEY_ORDER.index(order_key))
except:
pass


def setFilepath(self, filepath, appdata=False):
self.__filepath = self.createConfigPath(filepath, appdata)

Expand Down
31 changes: 23 additions & 8 deletions update.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
RESOURCE_FOLDER = None
BIN_FOLDER = None
show_message = None
cfg = None

HYPERLINK = None
logger = logging.getLogger('update.py')
Expand Down Expand Up @@ -207,7 +208,8 @@ def download_update(latest_version: str, download_url: str, download_path: str,
deleted.append(f'"{folder_name}/{filename}"')
elif os.path.getsize(path) != int(expected_size):
edited.append(f'"{folder_name}/{filename}"')
except: pass
except:
pass
ignored = edited + deleted # we handle both edits and deletes the same way, but this may change
logger.info(f'Ignoring edited resources: {ignored}')

Expand All @@ -224,7 +226,8 @@ def download_update(latest_version: str, download_url: str, download_path: str,
subprocess.Popen(updater_cmd)
return True

except InsufficientSpaceError: pass
except InsufficientSpaceError:
pass
except:
logger.error(f'(!) Could not download latest version. New naming format? Missing updater? {format_exc()}')

Expand Down Expand Up @@ -270,13 +273,25 @@ def validate_update(update_report_path: str) -> None:
except: logger.warning('Failed to delete update report after validation.')

logger.info('Update validated.')
#update_migration(version_change.split(' -> ')[0])
update_migration(version_change.split(' -> ')[0])
msg = f'Update from {version_change} successful.'
show_message('Update successful', msg, 0x00040040) # i-symbol + stay on top


# NOTE: Not needed yet, thankfully.
#def update_migration(old_version: str) -> None:
# ''' Handles additional work required to migrate
# `old_version` to the latest version, if any. '''
# older_than = lambda v: old_version != v and get_later_version(old_version, v) == v
def update_migration(old_version: str) -> None:
''' Handles additional work required to migrate
`old_version` to the latest version, if any. '''
older_than = lambda v: old_version != v and get_later_version(old_version, v) == v
if older_than('1.3.0'):
try:
import irs
cfg.load('MAX_RECENT_CLIPS', 10, section=' --- Tray Menu Recent Clips --- ')
irs.TRAY_RECENT_CLIP_COUNT = cfg.moveSetting(
oldKey='MAX_RECENT_CLIPS',
oldSection=' --- Tray Menu Recent Clips --- ',
newKey='MAX_CLIPS_VISIBLE_IN_MENU',
newSection=' --- General --- ',
replace=False
)
except:
pass

0 comments on commit cb17300

Please sign in to comment.