-
-
Notifications
You must be signed in to change notification settings - Fork 819
Add ability to customize automatic update channels for add-ons #17597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba90559
Create options for updating add-ons by channel
seanbudd f7b039b
add docs
seanbudd aa10ee4
remove misleading comment
seanbudd 5d1c1d4
address review feedback
seanbudd 110336e
Apply suggestions from code review
seanbudd 7756b8d
address review feedback
seanbudd 481039f
Merge remote-tracking branch 'origin/master' into updateChannelAddons
seanbudd b538505
unit test fix
seanbudd 54a155d
Merge remote-tracking branch 'origin/master' into updateChannelAddons
seanbudd 06c77e3
update config testing
seanbudd df73a06
safer loading of config
seanbudd 6631a79
Pre-commit auto-fix
pre-commit-ci[bot] 0c9755a
address review comments
seanbudd 9eac07a
address review comments
seanbudd 5066e0c
remove config upgrade
seanbudd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # A part of NonVisual Desktop Access (NVDA) | ||
| # Copyright (C) 2025 NV Access Limited | ||
| # This file is covered by the GNU General Public License. | ||
| # See the file COPYING for more details. | ||
|
|
||
| from dataclasses import dataclass, replace | ||
| import json | ||
| import os | ||
| from typing import Any | ||
|
|
||
| from logHandler import log | ||
| import NVDAState | ||
|
|
||
| from .models.channel import UpdateChannel | ||
|
|
||
|
|
||
| @dataclass | ||
| class _AddonSettings: | ||
| """Settings for the Add-on Store management of an add-on. | ||
|
|
||
| All options must have a default value. | ||
| """ | ||
|
|
||
| updateChannel: UpdateChannel = UpdateChannel.DEFAULT | ||
| """Preferred update channels for the add-on.""" | ||
|
|
||
| # TODO: migrate enabled/disabled/blocked state tracking | ||
| # from addonHandler.AddonState/AddonStateCategory to here. | ||
| # The set based state tracking could be replaced by maintaining state data on each add-on. | ||
| # | ||
| # blocked: bool = False | ||
| # """Whether the add-on is blocked from being running due to incompatibility.""" | ||
| # | ||
| # disabled: bool = False | ||
| # """Whether the add-on is disabled.""" | ||
|
|
||
|
|
||
| class _AddonStoreSettings: | ||
| """Settings for the Add-on Store.""" | ||
|
|
||
| _CACHE_FILENAME: str = "_cachedSettings.json" | ||
|
|
||
| _showWarning: bool | ||
| """Show warning when opening Add-on Store.""" | ||
|
|
||
| _addonSettings: dict[str, _AddonSettings] | ||
| """Settings related to the management of add-ons""" | ||
|
|
||
| def __init__(self): | ||
| self._storeSettingsFile = os.path.join( | ||
| NVDAState.WritePaths.addonStoreDir, | ||
| self._CACHE_FILENAME, | ||
| ) | ||
| self._showWarning = True | ||
| self._addonSettings = {} | ||
| self.load() | ||
|
|
||
| def load(self): | ||
| try: | ||
| with open(self._storeSettingsFile, "r", encoding="utf-8") as storeSettingsFile: | ||
| settingsDict: dict[str, Any] = json.load(storeSettingsFile) | ||
| except FileNotFoundError: | ||
| return | ||
| except (json.JSONDecodeError, UnicodeDecodeError): | ||
| log.exception("Invalid add-on store settings") | ||
| if NVDAState.shouldWriteToDisk(): | ||
| os.remove(self._storeSettingsFile) | ||
|
SaschaCowley marked this conversation as resolved.
seanbudd marked this conversation as resolved.
|
||
| return | ||
| else: | ||
| self._loadFromSettingsDict(settingsDict) | ||
|
|
||
| def _loadFromSettingsDict(self, settingsDict: dict[str, Any]): | ||
| try: | ||
| if not isinstance(settingsDict["addonSettings"], dict): | ||
| raise ValueError("addonSettings must be a dict") | ||
|
|
||
| if not isinstance(settingsDict["showWarning"], bool): | ||
| raise ValueError("showWarning must be a bool") | ||
|
|
||
| except (KeyError, ValueError): | ||
| log.exception(f"Invalid add-on store cache:\n{settingsDict}") | ||
| if NVDAState.shouldWriteToDisk(): | ||
| os.remove(self._storeSettingsFile) | ||
|
SaschaCowley marked this conversation as resolved.
seanbudd marked this conversation as resolved.
|
||
| return | ||
|
|
||
| self._showWarning = settingsDict["showWarning"] | ||
| for addonId, settings in settingsDict["addonSettings"].items(): | ||
| try: | ||
| updateChannel = UpdateChannel(settings["updateChannel"]) | ||
| except ValueError: | ||
| log.exception(f"Invalid add-on settings for {addonId}:\n{settings}. Ignoring settings") | ||
| continue | ||
| else: | ||
| self._addonSettings[addonId] = _AddonSettings( | ||
| updateChannel=updateChannel, | ||
| ) | ||
|
|
||
| def save(self): | ||
| if not NVDAState.shouldWriteToDisk(): | ||
| log.error("Shouldn't write to disk, not saving add-on store settings") | ||
| return | ||
|
seanbudd marked this conversation as resolved.
|
||
| settingsDict = { | ||
| "showWarning": self._showWarning, | ||
| "addonSettings": { | ||
| addonId: { | ||
| "updateChannel": addonSettings.updateChannel.value, | ||
| } | ||
| for addonId, addonSettings in self._addonSettings.items() | ||
| }, | ||
| } | ||
| with open(self._storeSettingsFile, "w", encoding="utf-8") as storeSettingsFile: | ||
| json.dump(settingsDict, storeSettingsFile, ensure_ascii=False) | ||
|
SaschaCowley marked this conversation as resolved.
|
||
|
|
||
| def setAddonSettings(self, addonId: str, **kwargs): | ||
| """Set settings for an add-on. | ||
|
|
||
| Keyword arguments the same as _AddonSettings: | ||
| - updateChannel: Update channel for the add-on. | ||
| """ | ||
| if addonId not in self._addonSettings: | ||
| self._addonSettings[addonId] = _AddonSettings(**kwargs) | ||
| else: | ||
| self._addonSettings[addonId] = replace(self._addonSettings[addonId], **kwargs) | ||
| self.save() | ||
|
SaschaCowley marked this conversation as resolved.
|
||
|
|
||
| def getAddonSettings(self, addonId: str) -> _AddonSettings: | ||
| """Get settings for an add-on. | ||
|
|
||
| Returns default settings if the add-on has no stored settings. | ||
| """ | ||
| return self._addonSettings.get(addonId, _AddonSettings()) | ||
|
|
||
| @property | ||
| def showWarning(self) -> bool: | ||
| return self._showWarning | ||
|
|
||
| @showWarning.setter | ||
| def showWarning(self, showWarning: bool): | ||
| self._showWarning = showWarning | ||
| self.save() | ||
|
SaschaCowley marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.