diff --git a/configparsebetter.py b/configparsebetter.py index 6d70caa..61d81ac 100644 --- a/configparsebetter.py +++ b/configparsebetter.py @@ -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) diff --git a/update.py b/update.py index 556472b..7cd1456 100644 --- a/update.py +++ b/update.py @@ -21,6 +21,7 @@ RESOURCE_FOLDER = None BIN_FOLDER = None show_message = None +cfg = None HYPERLINK = None logger = logging.getLogger('update.py') @@ -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}') @@ -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()}') @@ -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