diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index c1713fd7..e9044573 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -59,11 +59,11 @@ jobs: # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages steps: - name: Checkout repository - uses: actions/checkout@v5 + uses: actions/checkout@v6 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3 + uses: github/codeql-action/init@v4 with: languages: ${{ matrix.language }} build-mode: ${{ matrix.build-mode }} @@ -90,6 +90,6 @@ jobs: exit 1 - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 + uses: github/codeql-action/analyze@v4 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index d49d970d..dd9ee099 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -28,7 +28,7 @@ jobs: # based on. - name: Checkout head commits # https://github.com/actions/checkout - uses: actions/checkout@v5 + uses: actions/checkout@v6 #with: #ref: ${{github.head.sha}} #repository: ${{github.event.pull_request.head.repo.full_name}} diff --git a/.github/workflows/push-checks.yml b/.github/workflows/push-checks.yml index 51c9d47b..f6a1b0ef 100644 --- a/.github/workflows/push-checks.yml +++ b/.github/workflows/push-checks.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 with: fetch-depth: 0 - name: Set up Python diff --git a/.github/workflows/submodule-update.yml b/.github/workflows/submodule-update.yml index 5dbba221..e74e1020 100644 --- a/.github/workflows/submodule-update.yml +++ b/.github/workflows/submodule-update.yml @@ -24,7 +24,7 @@ jobs: OWNER: 'EDCD' steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 with: ref: '${{ env.CHECKOUT_BRANCH }}' submodules: true diff --git a/.github/workflows/windows-build.yml b/.github/workflows/windows-build.yml index 1d43c3c7..79cc024f 100644 --- a/.github/workflows/windows-build.yml +++ b/.github/workflows/windows-build.yml @@ -27,19 +27,22 @@ jobs: name: Linux environment build steps runs-on: "ubuntu-latest" steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 with: submodules: true - name: Create .gitversion run: | echo "${{ needs.variables.outputs.short_sha }}" > .gitversion + - name: Sanitize version for filenames + run: echo "SAFE_SEMVER=${{ needs.variables.outputs.sem_ver }}" | tr '/ ' '_' >> $GITHUB_ENV + - name: Make tar archive run: | # For 'tar' we can only specify filename/glob exclusions, not any # directory location tar -c -v -z \ - -f "../EDMarketConnector-release-${{ needs.variables.outputs.sem_ver }}.tar.gz" \ + -f "../EDMarketConnector-release-${SAFE_SEMVER}.tar.gz" \ -C .. \ --exclude=EDMarketConnector-release-*.* \ --exclude=.editorconfig \ @@ -59,18 +62,18 @@ jobs: --exclude=scripts \ --exclude=tests \ EDMarketConnector - mv "../EDMarketConnector-release-${{ needs.variables.outputs.sem_ver }}.tar.gz" . + mv "../EDMarketConnector-release-${SAFE_SEMVER}.tar.gz" . - name: Upload build files - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: name: built-files-linux path: | EDMarketConnector-release-*.tar.gz - windows_build: + windows_build_86: needs: [variables] - name: Build EDMC + name: Build EDMC (x86) runs-on: windows-latest defaults: @@ -78,10 +81,15 @@ jobs: shell: powershell steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 with: submodules: true + - name: Sanitize version for filenames + run: | + $safe = "${{ needs.variables.outputs.sem_ver }}" -replace '[\/ ]', '_' + echo "SAFE_SEMVER=$safe" >> $env:GITHUB_ENV + - name: Make zip archive uses: thedoctor0/zip-release@main with: @@ -94,7 +102,7 @@ jobs: path: 'EDMarketConnector' # 3. The .zip file has to be in the 'root' so that upload-artifact # will process it. Can't use relative paths. - filename: 'EDMarketConnector/EDMarketConnector-release-${{ needs.variables.outputs.sem_ver }}.zip' + filename: ${{ format('EDMarketConnector/EDMarketConnector-release-{0}.zip', env.SAFE_SEMVER) }} # 4. And all the exclusions need to be prefixed with the 'root' # directory. # NB: If this gets too long it can cause zip 'Command Line Error', @@ -113,9 +121,9 @@ jobs: - name: Download winsparkle run: | - Invoke-Webrequest -UseBasicParsing https://github.com/vslavik/winsparkle/releases/download/v0.9.0/WinSparkle-0.9.0.zip -OutFile out.zip + Invoke-Webrequest -UseBasicParsing https://github.com/vslavik/winsparkle/releases/download/v0.9.2/WinSparkle-0.9.2.zip -OutFile out.zip Expand-Archive out.zip - Move-Item 'out\WinSparkle-0.9.0\Release\*' '.\' + Move-Item 'out\WinSparkle-0.9.2\Release\*' '.\' - name: Build EDMC run: | @@ -128,21 +136,102 @@ jobs: - name: Rename Installer run: | - Get-ChildItem -Path . -Filter "EDMarketConnector_Installer_*.exe" | Rename-Item -NewName {"EDMarketConnector_Installer_Unsigned_$($_.Name -replace '^EDMarketConnector_Installer_', '')"} + Get-ChildItem -Path . -Filter "EDMarketConnector_Installer_*.exe" | + Rename-Item -NewName { + $rest = $_.Name -replace '^EDMarketConnector_Installer_', '' + "EDMarketConnector_Installer_Unsigned_${rest}_x86.exe" + } + - name: Upload build files + uses: actions/upload-artifact@v5 + with: + name: built-files-windows-x86 + path: | + EDMarketConnector_Installer_*.exe + EDMarketConnector-release-*.zip + windows_build_64: + needs: [variables] + name: Build EDMC (x64) + runs-on: windows-latest + + defaults: + run: + shell: powershell + + steps: + - uses: actions/checkout@v6 + with: + submodules: true + + - name: Sanitize version for filenames + run: | + $safe = "${{ needs.variables.outputs.sem_ver }}" -replace '[\/ ]', '_' + echo "SAFE_SEMVER=$safe" >> $env:GITHUB_ENV + + - name: Make zip archive + uses: thedoctor0/zip-release@main + with: + type: 'zip' + # We want an in-zip prefix of `EDMarketConnector/` for all files, so + # we specify that: + # 1. We work from the parent directory + directory: '..' + # 2. The path we're using is the 'root' directory + path: 'EDMarketConnector' + # 3. The .zip file has to be in the 'root' so that upload-artifact + # will process it. Can't use relative paths. + filename: ${{ format('EDMarketConnector/EDMarketConnector-release-{0}.zip', env.SAFE_SEMVER) }} + # 4. And all the exclusions need to be prefixed with the 'root' + # directory. + # NB: If this gets too long it can cause zip 'Command Line Error', + # presumably due to a Windows CL length limit. + exclusions: 'EDMarketConnector/EDMarketConnector-release-*.* EDMarketConnector/.editorconfig EDMarketConnector/.flake8 EDMarketConnector/.git* EDMarketConnector/.mypy.ini EDMarketConnector/.pre-commit-config.yaml EDMarketConnector/build.py EDMarketConnector/*.manifest EDMarketConnector/coriolis-data/ EDMarketConnector/img/ EDMarketConnector/pyproject.toml EDMarketConnector/scripts/ EDMarketConnector/tests/' + + - uses: actions/setup-python@v6 + with: + python-version-file: '.python-version' + architecture: "x64" + cache: 'pip' # caching pip dependencies + + - name: Install python tools + run: | + pip install -r requirements-dev.txt + + - name: Download winsparkle + run: | + Invoke-Webrequest -UseBasicParsing https://github.com/vslavik/winsparkle/releases/download/v0.9.2/WinSparkle-0.9.2.zip -OutFile out.zip + Expand-Archive out.zip + Move-Item 'out\WinSparkle-0.9.2\x64\Release\*' '.\' + + - name: Build EDMC + run: | + python build.py + + - name: InnoSetup + uses: nadeemjazmawe/inno-setup-action-cli@v6.0.5 + with: + filepath: './EDMC_Installer_Config.iss' + + - name: Rename Installer + run: | + Get-ChildItem -Path . -Filter "EDMarketConnector_Installer_*.exe" | + Rename-Item -NewName { + $rest = $_.Name -replace '^EDMarketConnector_Installer_', '' + "EDMarketConnector_Installer_Unsigned_${rest}_x64.exe" + } - name: Upload build files - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: - name: built-files-windows + name: built-files-windows-64 path: | EDMarketConnector_Installer_*.exe EDMarketConnector-release-*.zip Merge: runs-on: ubuntu-latest - needs: [ windows_build, linux_build ] + needs: [ windows_build_64, windows_build_86, linux_build ] steps: - name: Merge Artifacts - uses: actions/upload-artifact/merge@v4 + uses: actions/upload-artifact/merge@v5 with: name: Built files pattern: built-files-* @@ -155,7 +244,7 @@ jobs: steps: - name: Download binary - uses: actions/download-artifact@v5 + uses: actions/download-artifact@v6 with: name: Built files path: ./ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 27544fce..d227a688 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: # - id: identity - repo: https://github.com/pre-commit/pre-commit-hooks - rev: 'v4.1.0' + rev: 'v6.0.0' hooks: - id: check-merge-conflict - id: debug-statements @@ -21,7 +21,7 @@ repos: # - id: autopep8 - repo: https://github.com/PyCQA/flake8 - rev: 7.1.2 + rev: 7.3.0 hooks: - id: flake8 @@ -36,7 +36,7 @@ repos: # mypy - static type checking # mypy --follow-imports skip - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.15.0' + rev: 'v1.18.2' hooks: - id: mypy # verbose: true @@ -58,7 +58,7 @@ repos: # safety.exe check -r requirements.txt - repo: https://github.com/Lucas-C/pre-commit-hooks-safety - rev: 'v1.4.0' + rev: 'v1.4.2' hooks: - id: python-safety-dependencies-check entry: safety @@ -77,7 +77,7 @@ repos: always_run: true default_language_version: - python: python3.11 + python: python3.13 default_stages: [ pre-commit, pre-push ] diff --git a/.python-version b/.python-version index 2c073331..24ee5b1b 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.11 +3.13 diff --git a/ChangeLog.md b/ChangeLog.md index 19972006..0cd46d97 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,11 +1,83 @@ This is the master changelog for Elite Dangerous Market Connector. Entries are in reverse chronological order (latest first). --- -* We currently test against, and package with, Python 3.11, 32-bit. - * As a result, we do not support Windows 7, 8, or 8.1. - * Developers can check the contents of the `.python-version` file - in the source (not distributed with the Windows installer) for the - currently used version. +* We currently test against, and package with, Python 3.13, 64-bit. --- +Pre-Release 6.0.0-Beta2 +=== +This is a PUBLIC BETA for EDMC 6.0.0, now available on the Beta update track! + +This build contains everything in Beta1, with a few fixes identified in testing and a new --config option. + +This is a BETA release – BUGS ARE EXPECTED. Please report any issues on GitHub so we can stabilize the final 6.0.0 release. + +**Changes and Enhancements** +* Added a new --config option to EDMC to allow users to specify a different config file. +* Added additional notes about 64-bit builds to the changelog generator. + +**Bug Fixes** +* Fixed a bug where the release hashes.sum would not have content populate. +* Fixed a bug where .ini config files would not be read during file conversion on Linux static builds like Flatpak. +* Fixed a bug where Embarked crew would send erroneous events to Inara. + +Pre-Release 6.0.0-Beta1 +=== +This is a PUBLIC BETA for EDMC 6.0.0, now available on the Beta update track! + +This build represents one of the most significant internal updates to EDMC in recent memory. +It includes a complete overhaul of the configuration system, full x64-bit build support, major plugin system +enhancements (including enable/disable support), new core plugins, and additional updates across the codebase. + +Because of the scale of these changes, existing workflows, plugins, and user configurations may behave differently. +Plugin developers must review the removal list and update their plugins promptly, as several +deprecated APIs and legacy behaviors have been removed. + +This is a BETA release – BUGS ARE EXPECTED. Please report any issues on GitHub so we can stabilize the final 6.0.0 release. + +**Changes and Enhancements** +* Enables building of x64-bit builds. +* Added a new unified config system and migrated both Linux and Windows configuration files to a new config.toml format. This config file is placed by default in EDMC's LocalAppData folder on Windows or the XDG_DATA_HOME/EDMarketConnector path on Linux. +* Added a new EDAstro core plugin to send specific events to EDAstro. +* Added a new plugin Enable/Disable system, in Preferences -> Plugins. +* Added a new logger to the Config module prior to the default logger. +* Added a new --skip-journallock argument to allow EDMC to start even if the journal lock was not acquired. +* Added a new ScrollableNotebook class to enable horizontal scrolling of tabs in a Notebook. +* Updated the Plugins settings window to use the new ScrollableNotebook class. +* Updated a number of GitHub workflow dependencies. +* Updated the default Python version to 3.13. +* Updated EDMC:// protocol handler to use process handles with least-privilege access for improved security and reliability. +* Updated a number of Tkinter TK elements to use the updated TTK equivalents. +* Updated some internal function calls to use non-deprecated alternatives. +* Updated the LastError class to a Python Dataclass. +* Updated a number of dependencies. +* Updated a number of Win32 calls with proper prototyping for x64-bit builds. +* Updated the Windows WinSparkle updater to be more maintainable. +* Updated the Windows DDE Request handler for the EDMC Protocol, specifically callbacks and internal stability. +* Simplified some internal logic calls. +* Simplified the git shorthash function call. + +**Bug Fixes** +* Fixes a bug where protocol handler reset popups would be generated on first runs of EDMC. +* Fixed a bug where "en" was not present in available languages. +* Fixed a bug where the EDMC System Profiler could not be run on Linux systems. +* Fixed a bug where the plugin prefs window was not resizable. +* Fixed a bug where EDDN's queue would not actually start processing on app launch. + +**Key Removals** +* Removed the long-deprecated config 1.0 conversion calls. +* Removed the "_" builtin translation in favor of tr.tl. +* Removed the stringFromNumber, numberFromString, and preferredLanguages functions. +* Removed the _Translations singleton in favor of the more modern classes. +* Removed the nb.Entry, nb.ColoredButton classes. +* Removed legacy queue migration functionality from EDDN. +* Removed the help_open_log_folder function in favor of open_log_folder. +* Removed the legacy config AbstractClass and most Windows/Linux specific config functions in favor of new defaults. + +**Plugin Developers** +* Several deprecated functions have been removed. Please ensure your plugins are updated! +* The new Plugin Disable option relies on plugins respecting plugin_stop(). Ensure that your plugins respect this call! +* EDMC is now installed by default with x64-bit builds. +* EDMC will expect a minimum version of Python 3.13. While we do not currently use code incompatible with some earlier versions, we reserve the right to do so. + Release 5.13.3 === This release contains a bugfix for a race condition that could occur if plugin setup took too long for the journal monitor thread. @@ -1656,7 +1728,7 @@ updated to using Python 3.9.8. - When the new option is not active, the application will minimize to the taskbar as normal. -Bug Fixex +Bug Fixes --- * If a CAPI query failed in such a way that no `requests.Response` object diff --git a/EDMC.py b/EDMC.py index b8e95a55..58264fb0 100755 --- a/EDMC.py +++ b/EDMC.py @@ -132,6 +132,8 @@ def main() -> None: # noqa: C901, CCR001 action='store_true', ) + parser.add_argument('--config', help="Define a custom config to load EDMC from") + parser.add_argument('-a', metavar='FILE', help='write ship loadout to FILE in Companion API json format') parser.add_argument('-e', metavar='FILE', help='write ship loadout to FILE in E:D Shipyard plain text format') parser.add_argument('-l', metavar='FILE', help='write ship locations to FILE in CSV format') @@ -175,6 +177,9 @@ def main() -> None: # noqa: C901, CCR001 sys.exit(EXIT_ARGS) edmclogger.set_channels_loglevel(args.loglevel) + if args.config: + config.reload_from_path(args.config) + logger.debug(f'Startup v{appversion()} : Running on Python v{sys.version}') logger.debug(f'''Platform: {sys.platform} argv[0]: {sys.argv[0]} diff --git a/EDMCLogging.py b/EDMCLogging.py index 59c8e312..a05ad107 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -51,7 +51,7 @@ from threading import get_native_id as thread_native_id from time import gmtime from traceback import print_exc from typing import TYPE_CHECKING, cast -from config import appcmdname, appname, config, trace_on +from config import appcmdname, appname, config, trace_on, config_logger # TODO: Tests: # @@ -193,7 +193,7 @@ class Logger: self.logger_channel_rotating.setFormatter(self.logger_formatter) self.logger.addHandler(self.logger_channel_rotating) - def get_logger(self) -> 'LoggerMixin': + def get_logger(self) -> LoggerMixin: """ Obtain the self.logger of the class instance. @@ -232,7 +232,7 @@ class Logger: logger.trace("Not changing log level because it's TRACE") # type: ignore -def get_plugin_logger(plugin_name: str, loglevel: int = _default_loglevel) -> 'LoggerMixin': +def get_plugin_logger(plugin_name: str, loglevel: int = _default_loglevel) -> LoggerMixin: """ Return a logger suitable for a plugin. @@ -352,7 +352,7 @@ class EDMCContextFilter(logging.Filter): try: args, _, _, value_dict = inspect.getargvalues(frame) if len(args) and args[0] in ('self', 'cls'): - frame_class: 'object' = value_dict[args[0]] + frame_class: object = value_dict[args[0]] if frame_class: # See https://en.wikipedia.org/wiki/Name_mangling#Python for how name mangling works. @@ -450,7 +450,7 @@ class EDMCContextFilter(logging.Filter): # Go up through stack frames until we find the first with a # type(f_locals.self) of logging.Logger. This should be the start # of the frames internal to logging. - frame: 'FrameType' = getframe(0) + frame: FrameType = getframe(0) while frame: if isinstance(frame.f_locals.get('self'), logging.Logger): frame = cast('FrameType', frame.f_back) # Want to start on the next frame below @@ -518,7 +518,7 @@ class EDMCContextFilter(logging.Filter): return module_name -def get_main_logger(sublogger_name: str = '') -> 'LoggerMixin': +def get_main_logger(sublogger_name: str = '') -> LoggerMixin: """Return the correct logger for how the program is being run.""" if not os.getenv("EDMC_NO_UI"): # GUI app being run @@ -535,4 +535,6 @@ if not loglevel: base_logger_name = appcmdname if os.getenv('EDMC_NO_UI') else appname edmclogger = Logger(base_logger_name, loglevel=loglevel) -logger: 'LoggerMixin' = edmclogger.get_logger() +for h in config_logger.handlers[:]: + config_logger.removeHandler(h) +logger: LoggerMixin = edmclogger.get_logger() diff --git a/EDMCSystemProfiler.py b/EDMCSystemProfiler.py index a5d97ce4..123ca5fc 100644 --- a/EDMCSystemProfiler.py +++ b/EDMCSystemProfiler.py @@ -38,7 +38,7 @@ from monitor import monitor from EDMCLogging import get_main_logger -def get_sys_report(active_config: config.AbstractConfig) -> str: +def get_sys_report(active_config: config.Config) -> str: """Gather system information about Elite, the Host Computer, and EDMC.""" # Calculate Requested Information plt = platform.uname() @@ -108,7 +108,7 @@ def copy_sys_report(root: tk.Tk, report: str) -> None: messagebox.showinfo("System Profiler", "System Report copied to Clipboard", parent=root) -def main(active_config: config.AbstractConfig) -> None: +def main(active_config: config.Config) -> None: """Entry Point for the System Profiler.""" # Now Let's Begin root: tk.Tk = tk.Tk() diff --git a/EDMarketConnector.py b/EDMarketConnector.py old mode 100755 new mode 100644 index 2f03980c..4de51eac --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -54,7 +54,7 @@ if __name__ == '__main__': log_file_path.mkdir(exist_ok=True) log_file_path /= f'{appname}.log' - sys.stdout = sys.stderr = open(log_file_path, mode='wt', buffering=1) # Do NOT use WITH here. + sys.stdout = sys.stderr = open(log_file_path, mode='w', buffering=1) # Do NOT use WITH here. # TODO: Test: Make *sure* this redirect is working, else py2exe is going to cause an exit popup @@ -102,6 +102,8 @@ if __name__ == '__main__': # noqa: C901 help="Start the application minimized", action="store_true" ) + + parser.add_argument('--config', help="Define a custom config to load EDMC from") ########################################################################### ########################################################################### @@ -194,10 +196,19 @@ if __name__ == '__main__': # noqa: C901 help='Skips the Time Delta check for processed events', action='store_true' ) + + parser.add_argument( + '--skip-journallock', + help='Allows EDMC to continue even if journal lock could not be acquired', + action='store_true', + ) ########################################################################### args: argparse.Namespace = parser.parse_args() + if args.config: + config.reload_from_path(args.config) + if args.capi_pretend_down: import config as conf_module logger.info('Pretending CAPI is down') @@ -205,7 +216,7 @@ if __name__ == '__main__': # noqa: C901 if args.capi_use_debug_access_token: import config as conf_module - with open(conf_module.config.app_dir_path / 'access_token.txt', 'r') as at: + with open(conf_module.config.app_dir_path / 'access_token.txt') as at: conf_module.capi_debug_access_token = at.readline().strip() level_to_set: int | None = None @@ -281,7 +292,7 @@ if __name__ == '__main__': # noqa: C901 # Get thread and process IDs _, process_id = win32process.GetWindowThreadProcessId(hwnd) # Get the process handle - return win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, process_id) + return win32api.OpenProcess(win32con.PROCESS_QUERY_LIMITED_INFORMATION, False, process_id) except Exception: return None @@ -337,6 +348,12 @@ if __name__ == '__main__': # noqa: C901 # Ref: win32gui.EnumWindows(enumwindowsproc, 0) + def skip_journallock_popup(): + """Create the "skipping Journal Lock" popup.""" + from tkinter import messagebox + lockmsg = "Ignoring failed Journal Lock. Continuing at your own risk.\nConsider also using a debug sender." + messagebox.showwarning(title=appname, message=lockmsg) + def already_running_popup(): """Create the "already running" popup.""" from tkinter import messagebox @@ -352,9 +369,15 @@ if __name__ == '__main__': # noqa: C901 handle_edmc_callback_or_foregrounding() - if locked == JournalLockResult.ALREADY_LOCKED: - # There's a copy already running. + if locked == JournalLockResult.ALREADY_LOCKED and args.skip_journallock: + # There may be a copy already running. We are intentionally ignoring it. + logger.info("Intentionally skipping Journal Lock check. This may have unintended consequences") + # To be sure the user knows, we need a popup + if not args.edmc: + skip_journallock_popup() + if locked == JournalLockResult.ALREADY_LOCKED and not args.skip_journallock: + # There's a copy already running. logger.info("An EDMarketConnector.exe process was already running, exiting.") # To be sure the user knows, we need a popup @@ -453,7 +476,7 @@ class AppWindow: if sys.platform == 'win32' and not bool(config.get_int('no_systray')): from simplesystray import SysTrayIcon - def open_window(systray: 'SysTrayIcon', *args) -> None: + def open_window(systray: SysTrayIcon, *args) -> None: self.w.deiconify() logfile_loc = pathlib.Path(config.app_dir_path / 'logs') @@ -589,7 +612,7 @@ class AppWindow: self.status.grid(columnspan=2, sticky=tk.EW) for child in frame.winfo_children(): - child.grid_configure(padx=self.PADX, pady=( + child.grid_configure(padx=self.PADX, pady=( # type: ignore sys.platform != 'win32' or isinstance(child, tk.Frame)) and 2 or 0) self.menubar = tk.Menu() @@ -746,14 +769,6 @@ class AppWindow: # Start a protocol handler to handle cAPI registration. Requires main loop to be running. self.w.after_idle(lambda: protocol.protocolhandler.start(self.w)) - - # Migration from <= 3.30 - for username in config.get_list('fdev_usernames', default=[]): - config.delete_password(username) - config.delete('fdev_usernames', suppress=True) - config.delete('username', suppress=True) - config.delete('password', suppress=True) - config.delete('logdir', suppress=True) self.w.after_idle(lambda: self.postprefs(False)) # Companion login happens in callback from monitor self.toggle_suit_row(visible=False) if args.start_min: @@ -938,7 +953,7 @@ class AppWindow: self.cooldown() - def export_market_data(self, data: 'CAPIData') -> bool: # noqa: CCR001 + def export_market_data(self, data: CAPIData) -> bool: # noqa: CCR001 """ Export CAPI market data. @@ -1857,7 +1872,7 @@ class AppWindow: h.write(str(companion.session.capi_raw_data).encode(encoding='utf-8')) if sys.platform == 'win32': - def exit_tray(self, systray: 'SysTrayIcon') -> None: + def exit_tray(self, systray: SysTrayIcon) -> None: """Tray icon is shutting down.""" exit_thread = threading.Thread( target=self.onexit, @@ -2034,7 +2049,7 @@ def show_killswitch_poppup(root=None): idx += 1 idx += 1 - ok_button = tk.Button(frame, text="Ok", command=tl.destroy) + ok_button = ttk.Button(frame, text="Ok", command=tl.destroy) ok_button.grid(columnspan=2, sticky=tk.EW) @@ -2044,20 +2059,23 @@ def validate_providers(): station_provider: str = config.get_str("station_provider") if station_provider not in plug.provides('station_url'): logger.error("Station Provider Not Valid. Setting to Default.") + if config.get_str('station_provider'): # Only generate a pop-up if an invalid entry exists, not no value. + reset_providers["Station"] = (station_provider, "EDSM") config.set('station_provider', 'EDSM') - reset_providers["Station"] = (station_provider, "EDSM") shipyard_provider: str = config.get_str("shipyard_provider") if shipyard_provider not in plug.provides('shipyard_url'): logger.error("Shipyard Provider Not Valid. Setting to Default.") + if config.get_str('shipyard_provider'): # Only generate a pop-up if an invalid entry exists, not no value. + reset_providers["Shipyard"] = (shipyard_provider, "EDSY") config.set('shipyard_provider', 'EDSY') - reset_providers["Shipyard"] = (shipyard_provider, "EDSY") system_provider: str = config.get_str("system_provider") if system_provider not in plug.provides('system_url'): logger.error("System Provider Not Valid. Setting to Default.") + if config.get_str('system_provider'): # Only generate a pop-up if an invalid entry exists, not no value. + reset_providers["System"] = (system_provider, "EDSM") config.set('system_provider', 'EDSM') - reset_providers["System"] = (system_provider, "EDSM") if not reset_providers: return diff --git a/L10n/en.template b/L10n/en.template index 559dbb82..f670e39c 100644 --- a/L10n/en.template +++ b/L10n/en.template @@ -498,6 +498,15 @@ /* prefs.py: Catch & Record Profiler Errors; */ "Error in System Profiler" = "Error in System Profiler"; +/* prefs.py: Disabling a Plugin. */ +"Disabling plugin {PLUGIN}. This will cause a restart. Click OK to continue..." = "Disabling plugin {PLUGIN}. This will cause a restart. Click OK to continue..."; + +/* prefs.py: Enable a Disabled Plugin */ +"Enable" = "Enable"; + +/* prefs.py: Disable an Enabled Plugin */ +"Disable" = "Disable"; + /* stats.py: Cmdr stats; In files: stats.py:58; */ "Balance" = "Balance"; @@ -827,3 +836,12 @@ /* ttkHyperlinkLabel.py: Copy the Inara SLEF Format of the active ship to the clipboard; */ "Copy Inara SLEF" = "Copy Inara SLEF"; + +/* edastro_core.py: Settings>EDAstro - Label on checkbox for 'send data'; */ +"Send data to EDAstro" = "Send data to EDAstro"; + +/* common_coreutils.py: Colonisation ship name, with title; In files: common_coreutils.py:81:81 */ +"System Colonisation Ship {NAME}" = "System Colonisation Ship {NAME}"; + +/* common_coreutils.py: Unidentified colonisation ship; In files: common_coreutils.py:84:84 */ +"System Colonisation Ship" = "System Colonisation Ship"; diff --git a/PLUGINS.md b/PLUGINS.md index 6fd20ef9..71aaec7a 100644 --- a/PLUGINS.md +++ b/PLUGINS.md @@ -163,6 +163,19 @@ circular imports. For an example of how this is done, look at the code in `plugins/common_coreutils.py` and the usage of these functions in other core plugins. +Plugins may import additional code modules from `load.py` in the usual way. Note +that using an import statement like `import my_module` or `from my_module import +foo` will only import one module with that name **across all plugins**. This can +be used to create a shared library used by several plugins if used carefully, +but may create a conflict if another plugin also has a module of the same name +containing different code. For example if two plugins both contain a +`widgets.py` file and both do `import widgets`, only one of the two modules will +actually be imported. + +To avoid this issue, use a relative import statement such as `import .widgets` +or `from .widgets import foo` to ensure that the module is loaded from the +current plugin, even if the name conflicts with another plugin's code. + --- ## Logging diff --git a/build.py b/build.py index 94585461..374562e2 100644 --- a/build.py +++ b/build.py @@ -38,8 +38,8 @@ def iss_build(template_path: str, output_file: str) -> None: def system_check(dist_dir: pathlib.Path) -> str: """Check if the system is able to build.""" - if sys.version_info < (3, 11): - sys.exit(f"Unexpected Python version {sys.version}") + if sys.version_info < (3, 13): + sys.exit(f"EDMC cannot build on Python {sys.version}. Minimum version: 3.13") if sys.platform != "win32": sys.exit(f"Unsupported platform {sys.platform}") @@ -121,6 +121,7 @@ def build() -> None: "plugins/edsy.py", "plugins/inara.py", "plugins/spansh_core.py", + "plugins/edastro_core.py", "plugins/common_coreutils.py" ] options: dict = { diff --git a/commodity.py b/commodity.py index 52c0f8b8..52c13928 100644 --- a/commodity.py +++ b/commodity.py @@ -1,9 +1,10 @@ """Export various CSV formats.""" + # -*- coding: utf-8 -*- import time +import csv from pathlib import Path - from config import config from edmc_data import commodity_bracketmap as bracketmap @@ -12,7 +13,7 @@ from edmc_data import commodity_bracketmap as bracketmap (COMMODITY_DEFAULT, COMMODITY_CSV) = range(2) -def export(data, kind=COMMODITY_DEFAULT, filename=None) -> None: +def export(data, kind=COMMODITY_DEFAULT, filename=None) -> None: # noqa: CCR001 """ Export commodity data from the given CAPI data. @@ -21,55 +22,48 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None) -> None: :param filename: Filename to write to, or None for a standard format name. :return: """ - querytime = config.get_int('querytime', default=int(time.time())) + querytime = config.get_int("querytime", default=int(time.time())) if not filename: - filename_system = data['lastSystem']['name'].strip() - filename_starport = data['lastStarport']['name'].strip() - filename_time = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)) - filename_kind = 'csv' - filename = f'{filename_system}.{filename_starport}.{filename_time}.{filename_kind}' - filename = Path(config.get_str('outdir')) / filename - - if kind == COMMODITY_CSV: - sep = ';' # BUG: for fixing later after cleanup - header = sep.join(('System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand', '', 'Supply', '', 'Date', '\n')) - rowheader = sep.join((data['lastSystem']['name'], data['lastStarport']['name'])) - - else: - sep = ',' - header = sep.join( - ('System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand', '', 'Supply', '', 'Average', 'FDevID', 'Date\n') + sysname = data["lastSystem"]["name"].strip() + station = data["lastStarport"]["name"].strip() + timestamp = time.strftime("%Y-%m-%dT%H.%M.%S", time.localtime(querytime)) + ext = "csv" if kind == COMMODITY_CSV else "scsv" + filename = ( + Path(config.get_str("outdir")) / f"{sysname}.{station}.{timestamp}.{ext}" ) - rowheader = sep.join((data['lastSystem']['name'], data['lastStarport']['name'])) + system = data["lastSystem"]["name"] + station = data["lastStarport"]["name"] - with open(filename, 'wt') as h: # codecs can't automatically handle line endings, so encode manually where required - h.write(header) + if kind == COMMODITY_CSV: + header = ["System", "Station", "Commodity", "Sell", "Buy", "Demand", + "", "Supply", "", "Date"] + else: + header = ["System", "Station", "Commodity", "Sell", "Buy", "Demand", + "", "Supply", "", "Average", "FDevID", "Date"] - for commodity in data['lastStarport']['commodities']: - line = sep.join(( - rowheader, - commodity['name'], - commodity['sellPrice'] and str(int(commodity['sellPrice'])) or '', - commodity['buyPrice'] and str(int(commodity['buyPrice'])) or '', - str(int(commodity['demand'])) if commodity['demandBracket'] else '', - bracketmap[commodity['demandBracket']], - str(int(commodity['stock'])) if commodity['stockBracket'] else '', - bracketmap[commodity['stockBracket']] - )) + with open(filename, "w", newline="", encoding="utf-8") as output_file: + writer = csv.writer(output_file, delimiter=";" if kind == COMMODITY_DEFAULT else ",") + + writer.writerow(header) + + for commodity in data["lastStarport"]["commodities"]: + row = [ + system, + station, + commodity["name"], + int(commodity["sellPrice"]) if commodity.get("sellPrice") is not None else "", + int(commodity["buyPrice"]) if commodity.get("buyPrice") is not None else "", + int(commodity["demand"]) if commodity.get("demandBracket") is not None else "", + bracketmap.get(commodity.get("demandBracket"), ""), + int(commodity["stock"]) if commodity.get("stockBracket") is not None else "", + bracketmap.get(commodity.get("stockBracket"), ""), + ] if kind == COMMODITY_DEFAULT: - line = sep.join( - ( - line, - str(int(commodity['meanPrice'])), - str(commodity['id']), - data['timestamp'] + '\n' - ) - ) + mean = int(commodity["meanPrice"]) if commodity.get("meanPrice") is not None else "" + row.extend([mean, commodity["id"]]) - else: - line = sep.join((line, data['timestamp'] + '\n')) - - h.write(line) + row.append(data["timestamp"]) + writer.writerow(row) diff --git a/common_utils.py b/common_utils.py index a6f78356..ce3c7986 100644 --- a/common_utils.py +++ b/common_utils.py @@ -29,23 +29,22 @@ def ensure_on_screen(self, parent: tk.Tk): :param self: The calling class instance of tk.TopLevel :param parent: The parent window """ - if sys.platform != 'win32': - return - try: - # Get monitor info for the monitor containing the parent window - monitor = win32api.MonitorFromWindow(parent.winfo_id(), win32con.MONITOR_DEFAULTTONEAREST) - monitor_info = win32api.GetMonitorInfo(monitor) - work_area = monitor_info['Work'] # Gets the working area (excludes taskbar) + if sys.platform == 'win32': + try: + # Get monitor info for the monitor containing the parent window + monitor = win32api.MonitorFromWindow(parent.winfo_id(), win32con.MONITOR_DEFAULTTONEAREST) + monitor_info = win32api.GetMonitorInfo(monitor) + work_area = monitor_info['Work'] # Gets the working area (excludes taskbar) - # Calculate optimal position - x = max(work_area[0], min(parent.winfo_rootx(), work_area[2] - self.winfo_width())) - y = max(work_area[1], min(parent.winfo_rooty(), work_area[3] - self.winfo_height())) + # Calculate optimal position + x = max(work_area[0], min(parent.winfo_rootx(), work_area[2] - self.winfo_width())) + y = max(work_area[1], min(parent.winfo_rooty(), work_area[3] - self.winfo_height())) - # Update window position - self.geometry(f"+{x}+{y}") + # Update window position + self.geometry(f"+{x}+{y}") - except Exception as e: - logger.debug(f"Failed to ensure window is on screen: {e}") + except Exception as e: + logger.debug(f"Failed to ensure window is on screen: {e}") def log_locale(prefix: str) -> None: diff --git a/companion.py b/companion.py index 384dcdd5..2c920144 100644 --- a/companion.py +++ b/companion.py @@ -29,7 +29,8 @@ import webbrowser from email.utils import parsedate from pathlib import Path from queue import Queue -from typing import TYPE_CHECKING, Any, Mapping, TypeVar +from typing import TYPE_CHECKING, Any, TypeVar +from collections.abc import Mapping import requests import config as conf_module import killswitch @@ -69,7 +70,7 @@ class CAPIData(UserDict): def __init__( self, - data: str | dict[str, Any] | 'CAPIData' | None = None, + data: str | dict[str, Any] | CAPIData | None = None, source_host: str | None = None, source_endpoint: str | None = None, request_cmdr: str | None = None @@ -821,7 +822,7 @@ class Session: capi_data = CAPIData(capi_json, capi_host, capi_endpoint, monitor.cmdr) self.capi_raw_data.record_endpoint( capi_endpoint, r.content.decode(encoding='utf-8'), - datetime.datetime.utcnow() + datetime.datetime.now(datetime.timezone.utc) ) except requests.ConnectionError as e: @@ -1204,7 +1205,7 @@ def fixup(data: CAPIData) -> CAPIData: # noqa: C901, CCR001 # Can't be usefully if not (config.app_dir_path / 'FDevIDs' / f).is_file(): logger.warning(f'FDevID file {f} not found! Generating output without these commodity name rewrites.') continue - with open(config.app_dir_path / 'FDevIDs' / f, 'r') as csvfile: + with open(config.app_dir_path / 'FDevIDs' / f) as csvfile: reader = csv.DictReader(csvfile) for row in reader: @@ -1325,7 +1326,7 @@ def index_possibly_sparse_list(data: Mapping[str, V] | list[V], key: int) -> V: if isinstance(data, list): return data[key] - if isinstance(data, (dict, dict)): + if isinstance(data, dict): return data[str(key)] raise ValueError(f'Unexpected data type {type(data)}') diff --git a/config/__init__.py b/config/__init__.py index e1660b35..94d84c14 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -12,27 +12,27 @@ from __future__ import annotations __all__ = [ # defined in the order they appear in the file - 'GITVERSION_FILE', - 'appname', - 'applongname', - 'appcmdname', - 'copyright', - 'update_interval', - 'debug_senders', - 'trace_on', - 'capi_pretend_down', - 'capi_debug_access_token', - 'logger', - 'git_shorthash_from_head', - 'appversion', - 'user_agent', - 'appversion_nobuild', - 'AbstractConfig', - 'config', - 'get_update_feed', + "GITVERSION_FILE", + "appname", + "applongname", + "appcmdname", + "copyright", + "update_interval", + "debug_senders", + "trace_on", + "capi_pretend_down", + "capi_debug_access_token", + "logger", + "git_shorthash_from_head", + "appversion", + "user_agent", + "appversion_nobuild", + "Config", + "config", + "get_update_feed", + "config_logger", ] -import abc import contextlib import logging import os @@ -40,24 +40,26 @@ import pathlib import re import subprocess import sys -import warnings -from abc import abstractmethod -from typing import Any, Callable, Type, TypeVar +import tomllib +import tomli_w +from time import gmtime +from typing import Any, TypeVar +from collections.abc import Callable import semantic_version from constants import GITVERSION_FILE, applongname, appname # Any of these may be imported by plugins -appcmdname = 'EDMC' +appcmdname = "EDMC" # appversion **MUST** follow Semantic Versioning rules: # # Major.Minor.Patch(-prerelease)(+buildmetadata) # NB: Do *not* import this, use the functions appversion() and appversion_nobuild() -_static_appversion = '5.13.3' +_static_appversion = "6.0.0-beta2" _cached_version: semantic_version.Version | None = None -copyright = '© 2015-2019 Jonathan Harris, 2020-2025 EDCD' +copyright = "© 2015-2019 Jonathan Harris, 2020-2025 EDCD" -update_interval = 8*60*60 # 8 Hours +update_interval = 8 * 60 * 60 # 8 Hours # Providers marked to be in debug mode. Generally this is expected to switch to sending data to a log file debug_senders: list[str] = [] # TRACE logging code that should actually be used. Means not spamming it @@ -68,51 +70,50 @@ capi_pretend_down: bool = False capi_debug_access_token: str | None = None # This must be done here in order to avoid an import cycle with EDMCLogging. # Other code should use EDMCLogging.get_main_logger -if os.getenv("EDMC_NO_UI"): - logger = logging.getLogger(appcmdname) - -else: - logger = logging.getLogger(appname) +logger = ( + logging.getLogger(appcmdname) + if os.getenv("EDMC_NO_UI") + else logging.getLogger(appname) +) -_T = TypeVar('_T') +_T = TypeVar("_T") def git_shorthash_from_head() -> str | None: """ Determine short hash for current git HEAD. - Includes `.DIRTY` if any changes have been made from HEAD + Includes `.DIRTY` if any changes have been made from HEAD. :return: str | None: None if we couldn't determine the short hash. """ - shorthash: str | None = None - try: - git_cmd = subprocess.Popen( - "git rev-parse --short HEAD".split(), - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, + result = subprocess.run( + ["git", "rev-parse", "--short", "HEAD"], + capture_output=True, + text=True, + check=True, ) - out, err = git_cmd.communicate() - + shorthash = result.stdout.strip() except (subprocess.CalledProcessError, FileNotFoundError) as e: logger.info(f"Couldn't run git command for short hash: {e!r}") + return None - else: - shorthash = out.decode().rstrip('\n') - if re.match(r'^[0-9a-f]{7,}$', shorthash) is None: - logger.error(f"'{shorthash}' doesn't look like a valid git short hash, forcing to None") - shorthash = None + if not re.fullmatch(r"[0-9a-f]{7,}", shorthash): + logger.error( + f"'{shorthash}' doesn't look like a valid git short hash, forcing to None" + ) + return None - if shorthash is not None: - with contextlib.suppress(Exception): - result = subprocess.run('git diff --stat HEAD'.split(), capture_output=True) - if len(result.stdout) > 0: - shorthash += '.DIRTY' - - if len(result.stderr) > 0: - logger.warning(f'Data from git on stderr:\n{str(result.stderr)}') + with contextlib.suppress(Exception): + diff_result = subprocess.run( + ["git", "diff", "--stat", "HEAD"], capture_output=True, check=True + ) + if diff_result.stdout: + shorthash += ".DIRTY" + if diff_result.stderr: + logger.warning(f"Data from git on stderr:\n{diff_result.stderr.decode()}") return shorthash @@ -127,10 +128,12 @@ def appversion() -> semantic_version.Version: if _cached_version is not None: return _cached_version - if getattr(sys, 'frozen', False): + if getattr(sys, "frozen", False): # Running frozen, so we should have a .gitversion file # Yes, .parent because if frozen we're inside library.zip - with open(pathlib.Path(sys.path[0]).parent / GITVERSION_FILE, encoding='utf-8') as gitv: + with open( + pathlib.Path(sys.path[0]).parent / GITVERSION_FILE, encoding="utf-8" + ) as gitv: shorthash: str | None = gitv.read() else: @@ -140,16 +143,18 @@ def appversion() -> semantic_version.Version: shorthash = git_shorthash_from_head() if shorthash is None: if pathlib.Path(sys.path[0] + "/" + GITVERSION_FILE).exists(): - with open(pathlib.Path(sys.path[0] + "/" + GITVERSION_FILE), encoding='utf-8') as gitv: + with open( + pathlib.Path(sys.path[0] + "/" + GITVERSION_FILE), encoding="utf-8" + ) as gitv: shorthash = gitv.read() else: - shorthash = 'UNKNOWN' + shorthash = "UNKNOWN" - _cached_version = semantic_version.Version(f'{_static_appversion}+{shorthash}') + _cached_version = semantic_version.Version(f"{_static_appversion}+{shorthash}") return _cached_version -user_agent = f'EDCD-{appname}-{appversion()}' +user_agent = f"EDCD-{appname}-{appversion()}" def appversion_nobuild() -> semantic_version.Version: @@ -161,12 +166,12 @@ def appversion_nobuild() -> semantic_version.Version: :return: App version without any build meta data. """ - return appversion().truncate('prerelease') + return appversion().truncate("prerelease") -class AbstractConfig(abc.ABC): +class Config: """ - Abstract root class of all platform specific Config implementations. + Platform-unified Config class for 6.0+. Commented lines are no longer supported or replaced. """ @@ -203,13 +208,123 @@ class AbstractConfig(abc.ABC): __eddn_tracking_ui = False # Show EDDN tracking UI ? __skip_timecheck = False # Skip checking event timestamps? - def __init__(self) -> None: + def __init__(self, app_path) -> None: self.home_path = pathlib.Path.home() + # Set Needed Platform Var for app_dir_path + self.app_dir_path = app_path + self.toml_path: pathlib.Path = self.app_dir_path / "config.toml" + self.generated: str | None = None + self.source: str | None = None + self.settings: dict[str, Any] = {} + self._load() + self.default_plugin_dir_path = self.app_dir_path / "plugins" + plugdir_str = self.get_str("plugin_dir") + if not self.get_str("plugin_dir"): + plugdir_str = str(self.default_plugin_dir) + self.plugin_dir_path = self.default_plugin_dir_path + self.set("plugin_dir", plugdir_str) + if plugdir_str is None or not pathlib.Path(plugdir_str).is_dir(): + self.set("plugin_dir", str(self.default_plugin_dir_path)) + plugdir_str = self.default_plugin_dir + self.plugin_dir_path = pathlib.Path(self.get('plugin_dir')) + self.plugin_dir_path.mkdir(exist_ok=True) + + # Call the rest of the platform var helpers + self._init_platform() + + def _init_platform(self): + if sys.platform == "win32": + from .windows import win_helper + + win_helper(self) + elif sys.platform == "linux": + from .linux import linux_helper + + linux_helper(self) + else: + raise RuntimeError(f"Unsupported platform: {sys.platform}") def set_shutdown(self): """Set flag denoting we're in the shutdown sequence.""" self.__in_shutdown = True + def _load(self): + """Load TOML from disk and store fields. Create file if missing.""" + if not self.toml_path.exists(): + # Ensure parent directories exist + self.toml_path.parent.mkdir(parents=True, exist_ok=True) + + # Replace None with TOML-serializable defaults + default_data = { + "generated": "", # empty string instead of None + "source": "", # empty string instead of None + "settings": {} # empty table + } + with self.toml_path.open("wb") as f: + tomli_w.dump(default_data, f) + + # Load the TOML file + with self.toml_path.open("rb") as f: + data = tomllib.load(f) + + # Capture metadata, fallback to empty string if missing + self.generated = data.get("generated", "") + self.source = data.get("source", "") + + # Settings dict created by write_registry_to_toml() + self.settings = dict(data.get("settings", {})) + + def get(self, key: str, default=None): + """Return raw stored value.""" + return self.settings.get(key.lower(), default) + + def get_str(self, key: str, default="") -> str: + """Return string value.""" + val = self.get(key.lower(), None) + return str(val) if val is not None else default + + def get_int(self, key: str, default=0) -> int: + """Adaptive int (handles booleans stored as ints).""" + val = self.get(key.lower()) + if isinstance(val, int): + return val + try: + return int(val) + except (ValueError, TypeError): + return default + + def get_bool(self, key: str, default=False) -> bool: + """ + Adaptive boolean reader. + + - Accepts ints 0/1 + - Accepts strings "true"/"false"/"1"/"0" + - Accepts real booleans + """ + val = self.get(key.lower()) + + if isinstance(val, bool): + return val + + if isinstance(val, int): + return val != 0 + + if isinstance(val, str): + v = val.strip().lower() + if v in ("1", "true", "yes", "on"): + return True + if v in ("0", "false", "no", "off"): + return False + + return default + + def get_list(self, key: str, default=None): + """Return the list referred to by the given key if it exists, or the default.""" + val = self.get(key.lower()) + return ( + val if isinstance(val, list) else (default if default is not None else []) + ) + @property def shutting_down(self) -> bool: """ @@ -319,10 +434,50 @@ class AbstractConfig(abc.ABC): """Return a string version of default_journal_dir.""" return str(self.default_journal_dir_path) + def reload_from_path(self, new_config_path: str | pathlib.Path) -> None: + """ + Replace the active config TOML file path and reload all settings. + + This allows the main application (after argparse) to override the + config file used without needing to recreate the Config instance. + """ + new_path = pathlib.Path(new_config_path) + + if not new_path.is_file(): + logger.error(f"Config file not found: {new_config_path}") + return # Use the default config. + + logger.info(f"Reloading config from alternate path: {new_path}") + + # Update path and reload + self.toml_path = new_path + self.generated = None + self.source = None + self.settings = {} + + # Load TOML content from the new file and setup system. + self._load() + self._init_platform() + + # Re-run plugin_dir logic + plugdir_str = self.get_str("plugin_dir") + if not plugdir_str: + plugdir_str = str(self.default_plugin_dir) + self.plugin_dir_path = self.default_plugin_dir_path + self.set("plugin_dir", plugdir_str) + elif not pathlib.Path(plugdir_str).is_dir(): + self.set("plugin_dir", str(self.default_plugin_dir_path)) + plugdir_str = self.default_plugin_dir + + self.plugin_dir_path = pathlib.Path(plugdir_str) + self.plugin_dir_path.mkdir(exist_ok=True) + @staticmethod def _suppress_call( - func: Callable[..., _T], exceptions: Type[BaseException] | list[Type[BaseException]] = Exception, - *args: Any, **kwargs: Any + func: Callable[..., _T], + exceptions: type[BaseException] | list[type[BaseException]] = Exception, + *args: Any, + **kwargs: Any, ) -> _T | None: if exceptions is None: exceptions = [Exception] @@ -335,151 +490,57 @@ class AbstractConfig(abc.ABC): return None - def get( - self, key: str, - default: list | str | bool | int | None = None - ) -> list | str | bool | int | None: - """ - Return the data for the requested key, or a default. - - :param key: The key data is being requested for. - :param default: The default to return if the key does not exist, defaults to None. - :raises OSError: On Windows, if a Registry error occurs. - :return: The data or the default. - """ - # DEPRECATED: Migrate to specific type getters. Will remove in 6.0 or later. - warnings.warn('get is Deprecated. use the specific getter for your type', DeprecationWarning, stacklevel=2) - - if (a_list := self._suppress_call(self.get_list, ValueError, key, default=None)) is not None: - return a_list - - if (a_str := self._suppress_call(self.get_str, ValueError, key, default=None)) is not None: - return a_str - - if (a_bool := self._suppress_call(self.get_bool, ValueError, key, default=None)) is not None: - return a_bool - - if (an_int := self._suppress_call(self.get_int, ValueError, key, default=None)) is not None: - return an_int - - return default - - @abstractmethod - def get_list(self, key: str, *, default: list | None = None) -> list: - """ - Return the list referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_list`. - """ - raise NotImplementedError - - @abstractmethod - def get_str(self, key: str, *, default: str | None = None) -> str: - """ - Return the string referred to by the given key if it exists, or the default. - - :param key: The key data is being requested for. - :param default: Default to return if the key does not exist, defaults to None. - :raises ValueError: If an internal error occurs getting or converting a value. - :raises OSError: On Windows, if a Registry error occurs. - :return: The requested data or the default. - """ - raise NotImplementedError - - @abstractmethod - def get_bool(self, key: str, *, default: bool | None = None) -> bool: - """ - Return the bool referred to by the given key if it exists, or the default. - - :param key: The key data is being requested for. - :param default: Default to return if the key does not exist, defaults to None - :raises ValueError: If an internal error occurs getting or converting a value - :raises OSError: On Windows, if a Registry error occurs. - :return: The requested data or the default - """ - raise NotImplementedError - - def getint(self, key: str, *, default: int = 0) -> int: - """ - Getint is a Deprecated getter method. - - See get_int for its replacement. - :raises OSError: On Windows, if a Registry error occurs. - """ - # DEPRECATED: Migrate to get_int. Will remove in 6.0 or later. - warnings.warn('getint is Deprecated. Use get_int instead', DeprecationWarning, stacklevel=2) - - return self.get_int(key, default=default) - - @abstractmethod - def get_int(self, key: str, *, default: int = 0) -> int: - """ - Return the int referred to by key if it exists in the config. - - For legacy reasons, the default is 0 and not None. - - :param key: The key data is being requested for. - :param default: Default to return if the key does not exist, defaults to 0. - :raises ValueError: If the internal representation of this key cannot be converted to an int. - :raises OSError: On Windows, if a Registry error occurs. - :return: The requested data or the default. - """ - raise NotImplementedError - - @abstractmethod - def set(self, key: str, val: int | str | list[str] | bool) -> None: - """ - Set the given key's data to the given value. - - :param key: The key to set the value on. - :param val: The value to set the key's data to. - :raises ValueError: On an invalid type. - :raises OSError: On Windows, if a Registry error occurs. - """ - raise NotImplementedError - - @abstractmethod def delete(self, key: str, *, suppress=False) -> None: - """ - Delete the given key from the config. + """Delete the given key from the config.""" + try: + self.settings.pop(key.lower(), None) + except (KeyError, IndexError): + if suppress: + return + raise + self.save() - :param key: The key to delete. - :param suppress: bool - Whether to suppress any errors. Useful in case - code to migrate settings is blindly removing an old key. - :raises OSError: On Windows, if a registry error occurs. - """ - raise NotImplementedError + def set(self, key: str, value: Any): + """Modify a setting and save to disk.""" + if isinstance(value, list): + self.settings[key.lower()] = value + else: + self.settings[key.lower()] = str(value) + self.save() - @abstractmethod - def save(self) -> None: - """ - Save the current configuration. + def save(self): + """Write updated config back to TOML.""" + data = { + "generated": self.generated, + "source": self.source, + "settings": self.settings, + } - :raises OSError: On Windows, if a Registry error occurs. - """ - raise NotImplementedError + with self.toml_path.open("wb") as f: + tomli_w.dump(data, f) - @abstractmethod def close(self) -> None: - """Close this config and release any associated resources.""" - raise NotImplementedError - -# DEPRECATED: Password system doesn't do anything. Will remove in 6.0 or later. - def get_password(self, account: str) -> None: - """Legacy password retrieval.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) - - def set_password(self, account: str, password: str) -> None: - """Legacy password setting.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) - - def delete_password(self, account: str) -> None: - """Legacy password deletion.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) -# End Dep Zone + """Save config changes before closing.""" + self.save() -def get_config(*args, **kwargs) -> AbstractConfig: +def get_appdirpath(): + """Grab the Application Directory early.""" + app_dir_path = None + if sys.platform == "win32": + base = pathlib.Path(os.getenv("LOCALAPPDATA")) # type: ignore + app_dir_path = base / appname + if sys.platform == "linux": + xdg_data_home = pathlib.Path( + os.getenv("XDG_DATA_HOME", default="~/.local/share") + ).expanduser() + app_dir_path = xdg_data_home / appname + if app_dir_path is None: + raise ValueError + return app_dir_path + + +def get_config(*args, **kwargs) -> Config: """ Get the appropriate config class for the current platform. @@ -487,15 +548,45 @@ def get_config(*args, **kwargs) -> AbstractConfig: :param kwargs: Args to be passed through to implementation. :return: Instance of the implementation. """ + app_dir_path = get_appdirpath() + config = None + if pathlib.Path.exists(app_dir_path / "config.toml"): + return Config(app_path=app_dir_path) + if sys.platform == "win32": # pragma: sys-platform-win32 - from .windows import WinConfig - return WinConfig(*args, **kwargs) + from .windows import WinConfigMinimal + try: + config = WinConfigMinimal() + except FileNotFoundError: + logger.error("No Config TOML found, no Windows Registry entry found.") + return Config(app_path=app_dir_path) # Nothing to Convert if sys.platform == "linux": # pragma: sys-platform-linux - from .linux import LinuxConfig - return LinuxConfig(*args, **kwargs) + from .linux import LinuxConfigMinimal + try: + config = LinuxConfigMinimal() + except (FileNotFoundError, TypeError): + logger.error("No Config TOML found, no Linux ini file found.") + return Config(app_path=app_dir_path) # Nothing to Convert - raise ValueError(f'Unknown platform: {sys.platform=}') + if config: + logger.info("Oldstyle config found. Converting...") + config.write_to_toml(f"{app_dir_path}/config.toml") # type: ignore + return Config(app_path=app_dir_path) + + raise ValueError(f"Unknown platform: {sys.platform=}") + + +# Set internal Config logger, because config is set up before main logger. +config_logger = logging.getLogger("pre_config") +config_logger.setLevel(logging.DEBUG) # Or INFO + +ch = logging.StreamHandler(sys.stdout) +ch.setLevel(logging.DEBUG) +formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s") +formatter.converter = gmtime # Optional: match your main logger's UTC timestamps +ch.setFormatter(formatter) +config_logger.addHandler(ch) config = get_config() @@ -504,15 +595,6 @@ config = get_config() # Wiki: https://github.com/EDCD/EDMarketConnector/wiki/Participating-in-Open-Betas-of-EDMC def get_update_feed() -> str: """Select the proper update feed for the current update track.""" - if config.get_bool('beta_optin'): - return 'https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector-beta.xml' - return 'https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector.xml' - - -# DEPRECATED: Migrate to get_update_feed(). Will remove in 6.0 or later. -def __getattr__(name: str): - if name == 'update_feed': - warnings.warn('update_feed is deprecated, and will be removed in 6.0 or later. ' - 'Please migrate to get_update_feed()', DeprecationWarning, stacklevel=2) - return get_update_feed() - raise AttributeError(name=name) + if config.get_bool("beta_optin"): + return "https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector-beta.xml" + return "https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector.xml" diff --git a/config/linux.py b/config/linux.py index 651fea09..39e98793 100644 --- a/config/linux.py +++ b/config/linux.py @@ -8,86 +8,49 @@ See LICENSE file. from __future__ import annotations import os -import pathlib import sys +from config import config_logger + +if sys.platform != "linux": + raise OSError("This file is for Linux only.") + + +import pathlib from configparser import ConfigParser -from config import AbstractConfig, appname, logger +import datetime +import tomli_w +from typing import Any, TYPE_CHECKING -if sys.platform != 'linux': - raise EnvironmentError("This file is for Linux only.") +if TYPE_CHECKING: + from . import Config -class LinuxConfig(AbstractConfig): - """Linux implementation of AbstractConfig.""" +class LinuxConfigMinimal: + """Minimal Linux config for exporting pre-6.0 config values to TOML.""" - SECTION = 'config' - # TODO: I dislike this, would rather use a sane config file format. But here we are. - __unescape_lut = {'\\': '\\', 'n': '\n', ';': ';', 'r': '\r', '#': '#'} - __escape_lut = {'\\': '\\', '\n': 'n', ';': ';', '\r': 'r'} + SECTION = "config" def __init__(self, filename: str | None = None) -> None: - super().__init__() - # http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html - xdg_data_home = pathlib.Path(os.getenv('XDG_DATA_HOME', default='~/.local/share')).expanduser() - self.app_dir_path = xdg_data_home / appname - self.app_dir_path.mkdir(exist_ok=True, parents=True) - - self.default_plugin_dir_path = self.app_dir_path / 'plugins' - self.respath_path = pathlib.Path(__file__).parent.parent - - self.internal_plugin_dir_path = self.respath_path / 'plugins' - self.default_journal_dir_path = None # type: ignore - self.identifier = f'uk.org.marginal.{appname.lower()}' # TODO: Unused? - - config_home = pathlib.Path(os.getenv('XDG_CONFIG_HOME', default='~/.config')).expanduser() - - self.filename = config_home / appname / f'{appname}.ini' - if filename is not None: - self.filename = pathlib.Path(filename) - - self.filename.parent.mkdir(exist_ok=True, parents=True) - - self.config: ConfigParser | None = ConfigParser(comment_prefixes=('#',), interpolation=None) - self.config.read(self.filename) # read() ignores files that dont exist - - # Ensure that our section exists. This is here because configparser will happily create files for us, but it - # does not magically create sections + # Determine config path + xdg_config_home = pathlib.Path( + os.getenv("XDG_CONFIG_HOME", "~/.config") + ).expanduser() + self.filename = ( + pathlib.Path(filename) + if filename + else xdg_config_home / "EDMarketConnector" / "EDMarketConnector.ini" + ) + if not pathlib.Path(self.filename).is_file(): + raise FileNotFoundError + self.config = ConfigParser(comment_prefixes=('#',), interpolation=None) try: - self.config[self.SECTION].get("this_does_not_exist", fallback=None) - except KeyError: - logger.info("Config section not found. Backing up existing file (if any) and readding a section header") - if self.filename.exists(): - (self.filename.parent / f'{appname}.ini.backup').write_bytes(self.filename.read_bytes()) + read_files = self.config.read(self.filename) + config_logger.debug("ConfigParser.read returned: %s", read_files) + except Exception as e: + config_logger.exception("Failed to read INI file %s: %s", self.filename, e) + raise - self.config.add_section(self.SECTION) - - if (plugdir_str := self.get_str('plugin_dir')) is None or not pathlib.Path(plugdir_str).is_dir(): - self.set("plugin_dir", str(self.default_plugin_dir_path)) - plugdir_str = self.default_plugin_dir - self.plugin_dir_path = pathlib.Path(plugdir_str) - self.plugin_dir_path.mkdir(exist_ok=True) - - if (outdir := self.get_str('outdir')) is None or not pathlib.Path(outdir).is_dir(): - self.set('outdir', self.home) - - def __escape(self, s: str) -> str: - """ - Escape a string using self.__escape_lut. - - This does NOT support multi-character escapes. - - :param s: str - String to be escaped. - :return: str - The escaped string. - """ - out = "" - for c in s: - if c not in self.__escape_lut: - out += c - continue - - out += '\\' + self.__escape_lut[c] - - return out + __unescape_lut = {'\\': '\\', 'n': '\n', ';': ';', 'r': '\r', '#': '#'} def __unescape(self, s: str) -> str: """ @@ -105,152 +68,86 @@ class LinuxConfig(AbstractConfig): i += 1 continue - # We have a backslash, check what its escaping - if i == len(s)-1: + # We have a backslash, check what it's escaping + if i == len(s) - 1: raise ValueError('Escaped string has unescaped trailer') - unescaped = self.__unescape_lut.get(s[i+1]) + unescaped = self.__unescape_lut.get(s[i + 1]) if unescaped is None: - raise ValueError(f'Unknown escape: \\ {s[i+1]}') + raise ValueError(f'Unknown escape: \\{s[i+1]}') out.append(unescaped) i += 2 return "".join(out) - def __raw_get(self, key: str) -> str | None: + def _get_settings_dict(self) -> dict[str, Any]: # noqa: CCR001 """ - Get a raw data value from the config file. + Return all keys/values from SECTION as a dict of strings or lists. - :param key: str - The key data is being requested for. - :return: str - The raw data, if found. + This decodes the historical encoding where lists are encoded as + newline-separated values separated by ';' """ - if self.config is None: - raise ValueError('Attempt to use a closed config') + result: dict[str, Any] = {} + for key in self.config[self.SECTION]: + raw = self.config[self.SECTION][key] - return self.config[self.SECTION].get(key) + if raw is None: + continue - def get_str(self, key: str, *, default: str | None = None) -> str: - """ - Return the string referred to by the given key if it exists, or the default. + # If the value contains newline(s), check for trailing ';' sentinel + if "\n" in raw: + parts = raw.split("\n") + if parts and parts[-1].strip() == ";": + items = [self.__unescape(p.strip()) for p in parts[:-1]] + result[key] = items + config_logger.debug("Decoded list for key '%s' with %d items", key, len(items)) + continue + else: + # Not a valid encoded list; fall back to unescaped string + try: + result[key] = self.__unescape(raw) + except Exception: + result[key] = raw + continue - Implements :meth:`AbstractConfig.get_str`. - """ - data = self.__raw_get(key) - if data is None: - return default # type: ignore # Yes it could be None, but we're _assuming_ that people gave us a default + # Single value: unescape and return + try: + result[key] = self.__unescape(raw) + except Exception: + result[key] = raw - if '\n' in data: - raise ValueError('asked for string, got list') + return result - return self.__unescape(data) + def write_to_toml(self, toml_path: str) -> None: + """Dump existing config to TOML file.""" + config_logger.debug("Generating New Config File") + config_data: dict[str, Any] = { + "generated": datetime.datetime.now(datetime.timezone.utc).isoformat(), + "source": "linux_ini", + "section": self.SECTION, + "settings": self._get_settings_dict(), + } - def get_list(self, key: str, *, default: list | None = None) -> list: - """ - Return the list referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_list`. - """ - data = self.__raw_get(key) - - if data is None: - return default # type: ignore # Yes it could be None, but we're _assuming_ that people gave us a default - - split = data.split('\n') - if split[-1] != ';': - raise ValueError('Encoded list does not have trailer sentinel') - - return list(map(self.__unescape, split[:-1])) - - def get_int(self, key: str, *, default: int = 0) -> int: - """ - Return the int referred to by key if it exists in the config. - - Implements :meth:`AbstractConfig.get_int`. - """ - data = self.__raw_get(key) - - if data is None: - return default - - try: - return int(data) - - except ValueError as e: - raise ValueError(f'requested {key=} as int cannot be converted to int') from e - - def get_bool(self, key: str, *, default: bool | None = None) -> bool: - """ - Return the bool referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_bool`. - """ - if self.config is None: - raise ValueError('attempt to use a closed config') - - data = self.__raw_get(key) - if data is None: - return default # type: ignore # Yes it could be None, but we're _assuming_ that people gave us a default - - return bool(int(data)) - - def set(self, key: str, val: int | str | list[str]) -> None: - """ - Set the given key's data to the given value. - - Implements :meth:`AbstractConfig.set`. - """ - if self.config is None: - raise ValueError('attempt to use a closed config') - - to_set: str | None = None - if isinstance(val, bool): - to_set = str(int(val)) - - elif isinstance(val, str): - to_set = self.__escape(val) - - elif isinstance(val, int): - to_set = str(val) - - elif isinstance(val, list): - to_set = '\n'.join([self.__escape(s) for s in val] + [';']) - - else: - raise ValueError(f'Unexpected type for value {type(val)=}') - - self.config.set(self.SECTION, key, to_set) - self.save() - - def delete(self, key: str, *, suppress=False) -> None: - """ - Delete the given key from the config. - - Implements :meth:`AbstractConfig.delete`. - """ - if self.config is None: - raise ValueError('attempt to use a closed config') - - self.config.remove_option(self.SECTION, key) - self.save() - - def save(self) -> None: - """ - Save the current configuration. - - Implements :meth:`AbstractConfig.save`. - """ - if self.config is None: - raise ValueError('attempt to use a closed config') - - with open(self.filename, 'w', encoding='utf-8') as f: - self.config.write(f) + path_obj = pathlib.Path(toml_path) + path_obj.parent.mkdir(parents=True, exist_ok=True) + config_logger.debug("Writing Config File to %s", path_obj) + with path_obj.open("wb") as f: + tomli_w.dump(config_data, f) def close(self) -> None: - """ - Close this config and release any associated resources. + """Release resources (stub).""" + del self.config - Implements :meth:`AbstractConfig.close`. - """ - self.save() - self.config = None + +def linux_helper(config: Config) -> Config: + """Set Environment Specific Variables for Linux Config.""" + config_logger.debug("Linux environment detected. Setting platform-specific variables.") + config.respath_path = pathlib.Path(__file__).parent.parent + config.internal_plugin_dir_path = config.respath_path / "plugins" + config.default_journal_dir_path = None # type: ignore + if (outdir := config.get_str("outdir")) is None or not pathlib.Path( + outdir + ).is_dir(): + config.set("outdir", config.home) + return config diff --git a/config/windows.py b/config/windows.py index f5676424..6868493e 100644 --- a/config/windows.py +++ b/config/windows.py @@ -7,17 +7,21 @@ See LICENSE file. """ from __future__ import annotations -import functools import pathlib import sys import uuid import winreg -from typing import Literal -from config import AbstractConfig, applongname, appname, logger +import datetime +import tomli_w +from config import config_logger from win32comext.shell import shell +from typing import Any, TYPE_CHECKING -if sys.platform != 'win32': - raise EnvironmentError("This file is for Windows only.") +if TYPE_CHECKING: + from . import Config + +if sys.platform != "win32": + raise OSError("This file is for Windows only.") REG_RESERVED_ALWAYS_ZERO = 0 @@ -27,187 +31,92 @@ def known_folder_path(guid: uuid.UUID) -> str | None: return shell.SHGetKnownFolderPath(guid, 0, 0) -class WinConfig(AbstractConfig): - """Implementation of AbstractConfig for Windows.""" +class WinConfigMinimal: + """Minimal Windows config for exporting pre-6.0 config values to TOML.""" + + REGISTRY_SUBKEY = r"Software\Marginal\EDMarketConnector" def __init__(self) -> None: - super().__init__() - - REGISTRY_SUBKEY = r'Software\Marginal\EDMarketConnector' # noqa: N806 - create_key_defaults = functools.partial( - winreg.CreateKeyEx, - key=winreg.HKEY_CURRENT_USER, + # Try to open the key with read access + with winreg.OpenKey( + winreg.HKEY_CURRENT_USER, + self.REGISTRY_SUBKEY, + 0, + winreg.KEY_READ | winreg.KEY_WOW64_64KEY + ): + pass # If this fails, key doesn't exist, nothing to convert + # Open or create the registry key + self.__reg_handle = winreg.CreateKeyEx( + winreg.HKEY_CURRENT_USER, + self.REGISTRY_SUBKEY, access=winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY, ) - try: - self.__reg_handle: winreg.HKEYType = create_key_defaults(sub_key=REGISTRY_SUBKEY) + def _convert_reg_value_for_toml(self, data, data_type) -> Any: + """Convert Windows registry values into TOML-native types.""" + if data_type == winreg.REG_SZ: + return str(data) + if data_type == winreg.REG_EXPAND_SZ: + try: + return str(winreg.ExpandEnvironmentStrings(data)) + except Exception: + return str(data) + if data_type in (winreg.REG_DWORD, winreg.REG_QWORD): + return int(data) + if data_type == winreg.REG_MULTI_SZ: + return list(data) + # Fallback: force to string + return str(data) - except OSError: - logger.exception('Could not create required registry keys') - raise + def write_to_toml(self, toml_path: str) -> None: + """Export all registry values under REGISTRY_SUBKEY to TOML.""" + config_logger.debug("Generating New Config File") + key = self.__reg_handle + _, num_values, _ = winreg.QueryInfoKey(key) - if local_appdata := known_folder_path(shell.FOLDERID_LocalAppData): - self.app_dir_path = pathlib.Path(local_appdata) / appname - self.app_dir_path.mkdir(exist_ok=True) + config_data: dict[str, Any] = { + "generated": datetime.datetime.now(datetime.timezone.utc).isoformat(), + "source": "windows_registry", + "settings": {}, + } - self.default_plugin_dir_path = self.app_dir_path / 'plugins' - if (plugdir_str := self.get_str('plugin_dir')) is None or not pathlib.Path(plugdir_str).is_dir(): - self.set("plugin_dir", str(self.default_plugin_dir_path)) - plugdir_str = self.default_plugin_dir - self.plugin_dir_path = pathlib.Path(plugdir_str) - self.plugin_dir_path.mkdir(exist_ok=True) + for i in range(num_values): + name, data, data_type = winreg.EnumValue(key, i) + config_data["settings"][str(name).lower()] = self._convert_reg_value_for_toml( + data, data_type + ) - if getattr(sys, 'frozen', False): - self.respath_path = pathlib.Path(sys.executable).parent - self.internal_plugin_dir_path = self.respath_path / 'plugins' - else: - self.respath_path = pathlib.Path(__file__).parent.parent - self.internal_plugin_dir_path = self.respath_path / 'plugins' - - self.home_path = pathlib.Path.home() - - journal_dir_path = pathlib.Path( - known_folder_path(shell.FOLDERID_SavedGames)) / 'Frontier Developments' / 'Elite Dangerous' # type: ignore - self.default_journal_dir_path = journal_dir_path if journal_dir_path.is_dir() else None # type: ignore - - self.identifier = applongname - if (outdir_str := self.get_str('outdir')) is None or not pathlib.Path(outdir_str).is_dir(): - docs = known_folder_path(shell.FOLDERID_Documents) - self.set("outdir", docs if docs is not None else self.home) - - def __get_regentry(self, key: str) -> None | list | str | int: - """Access the Registry for the raw entry.""" - try: - value, _type = winreg.QueryValueEx(self.__reg_handle, key) - except FileNotFoundError: - # Key doesn't exist - return None - - # For programmers who want to actually know what is going on - if _type == winreg.REG_SZ: - return str(value) - - if _type == winreg.REG_DWORD: - return int(value) - - if _type == winreg.REG_MULTI_SZ: - return list(value) - - logger.warning(f'Registry key {key=} returned unknown type {_type=} {value=}') - return None - - def get_str(self, key: str, *, default: str | None = None) -> str: - """ - Return the string referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_str`. - """ - res = self.__get_regentry(key) - if res is None: - return default # type: ignore # Yes it could be None, but we're _assuming_ that people gave us a default - - if not isinstance(res, str): - raise ValueError(f'Data from registry is not a string: {type(res)=} {res=}') - - return res - - def get_list(self, key: str, *, default: list | None = None) -> list: - """ - Return the list referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_list`. - """ - res = self.__get_regentry(key) - if res is None: - return default # type: ignore # Yes it could be None, but we're _assuming_ that people gave us a default - - if not isinstance(res, list): - raise ValueError(f'Data from registry is not a list: {type(res)=} {res}') - - return res - - def get_int(self, key: str, *, default: int = 0) -> int: - """ - Return the int referred to by key if it exists in the config. - - Implements :meth:`AbstractConfig.get_int`. - """ - res = self.__get_regentry(key) - if res is None: - return default - - if not isinstance(res, int): - raise ValueError(f'Data from registry is not an int: {type(res)=} {res}') - - return res - - def get_bool(self, key: str, *, default: bool | None = None) -> bool: - """ - Return the bool referred to by the given key if it exists, or the default. - - Implements :meth:`AbstractConfig.get_bool`. - """ - res = self.get_int(key, default=default) # type: ignore - if res is None: - return default # Yes it could be None, but we're _assuming_ that people gave us a default - - return bool(res) - - def set(self, key: str, val: int | str | list[str] | bool) -> None: - """ - Set the given key's data to the given value. - - Implements :meth:`AbstractConfig.set`. - """ - # These are the types that winreg.REG_* below resolve to. - reg_type: Literal[1] | Literal[4] | Literal[7] - if isinstance(val, str): - reg_type = winreg.REG_SZ - - elif isinstance(val, int): - reg_type = winreg.REG_DWORD - - elif isinstance(val, list): - reg_type = winreg.REG_MULTI_SZ - - elif isinstance(val, bool): - reg_type = winreg.REG_DWORD - val = int(val) - - else: - raise ValueError(f'Unexpected type for value {type(val)=}') - - winreg.SetValueEx(self.__reg_handle, key, REG_RESERVED_ALWAYS_ZERO, reg_type, val) # type: ignore - - def delete(self, key: str, *, suppress=False) -> None: - """ - Delete the given key from the config. - - 'key' is relative to the base Registry path we use. - - Implements :meth:`AbstractConfig.delete`. - """ - try: - winreg.DeleteValue(self.__reg_handle, key) - except OSError: - if suppress: - return - - raise - - def save(self) -> None: - """ - Save the configuration. - - Not required for WinConfig as Registry keys are flushed on write. - """ - pass + path_obj = pathlib.Path(toml_path) + path_obj.parent.mkdir(parents=True, exist_ok=True) + config_logger.debug("Writing Config File") + with path_obj.open("wb") as f: + tomli_w.dump(config_data, f) def close(self): - """ - Close this config and release any associated resources. - - Implements :meth:`AbstractConfig.close`. - """ + """Close registry handle.""" self.__reg_handle.Close() + + +def win_helper(config: Config) -> Config: + """Set Environment Specific Variables for Windows Config.""" + config_logger.debug("Windows environment detected. Setting platform-specific variables.") + if getattr(sys, "frozen", False): + config.respath_path = pathlib.Path(sys.executable).parent + config.internal_plugin_dir_path = config.respath_path / "plugins" + else: + config.respath_path = pathlib.Path(__file__).parent.parent + config.internal_plugin_dir_path = config.respath_path / "plugins" + + journal_dir_path = ( + pathlib.Path(known_folder_path(shell.FOLDERID_SavedGames)) # type: ignore + / "Frontier Developments" + / "Elite Dangerous" + ) + config.default_journal_dir_path = journal_dir_path if journal_dir_path.is_dir() else None # type: ignore + + if (outdir_str := config.get_str("outdir")) is None or not pathlib.Path( + outdir_str + ).is_dir(): + docs = known_folder_path(shell.FOLDERID_Documents) + config.set("outdir", docs if docs is not None else config.home) + return config diff --git a/coriolis-update-files.py b/coriolis-update-files.py index 40d25fb3..f15d4067 100755 --- a/coriolis-update-files.py +++ b/coriolis-update-files.py @@ -45,7 +45,7 @@ if __name__ == "__main__": # noqa: C901 # Symbolic name from in-game name reverse_ship_map = {v: k for k, v in list(ship_name_map.items())} - bulkheads = list(outfitting.armour_map.keys()) + bulkheads = list(outfitting.armour_map) ships = {} modules = {} diff --git a/debug_webserver.py b/debug_webserver.py index 1471ec7d..545e62bd 100644 --- a/debug_webserver.py +++ b/debug_webserver.py @@ -7,15 +7,16 @@ import pathlib import threading import zlib from http import server -from typing import Any, Callable, Literal +from typing import Any, Literal +from collections.abc import Callable from urllib.parse import parse_qs -import config +from config import config from EDMCLogging import get_main_logger logger = get_main_logger() output_lock = threading.Lock() -output_data_path = pathlib.Path(config.app_dir_path / 'logs' / 'http_debug') +output_data_path = pathlib.Path(config.app_dir_path / 'logs' / 'http_debug') # type: ignore SAFE_TRANSLATE = str.maketrans(dict.fromkeys("!@#$%^&*()./\\\r\n[]-+='\";:?<>,~`", '_')) diff --git a/docs/Releasing.md b/docs/Releasing.md index 74151752..033f0a2b 100644 --- a/docs/Releasing.md +++ b/docs/Releasing.md @@ -38,8 +38,8 @@ You will need several pieces of software installed, or the files from their auto-select some others). NB: If you have need to uninstall this it's "Windows Software Development Kit - Windows 10.0.19041.1" in "Apps & Features", *not* "Windows SDK AddOn". -1. [Python](https://python.org): 32-bit version of Python 3.10 for Windows. - [v3.10.3](https://www.python.org/downloads/release/python-3103/) is the most +1. [Python](https://python.org): 32-bit version of Python 3.13 for Windows. + [v3.13.9](https://www.python.org/downloads/release/python-3139/) is the most recently tested version. You need the `Windows x86 executable installer` file, for the 32-bit version. Double-check the version against the `.python.version` file, as it should always contain the intended version. @@ -389,7 +389,7 @@ When changing the Python version (Major.Minor.Patch) used: 1. Any version change: 1. `ChangeLog.md` - The `We now test against, and package with, Python - M.m.P.` line. + M.m.` line. 1. Major or Minor level changes: diff --git a/edmc_data.py b/edmc_data.py index f29795f4..1a94b3ab 100644 --- a/edmc_data.py +++ b/edmc_data.py @@ -605,7 +605,7 @@ edmc_suit_symbol_localised = { # This is sourced from what the game is passed by the launcher, caveat emptor. It was mentioned that / is also # an option # This is only run once when this file is imported by something, no runtime cost or repeated expansions will occur -__keys = list(edmc_suit_symbol_localised.keys()) +__keys = list(edmc_suit_symbol_localised) for lang in __keys: new_lang = lang.replace('\\', r'\\') new_lang_2 = lang.replace('\\', '/') diff --git a/edshipyard.py b/edshipyard.py index 28e6dc2f..e1c14e23 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -106,7 +106,7 @@ def export(data, filename=None) -> None: # noqa: C901, CCR001 else: name = module['name'] # type: ignore - if name == 'Frame Shift Drive' or name == 'Frame Shift Drive (SCO)': + if name in ['Frame Shift Drive', 'Frame Shift Drive (SCO)']: fsd = module # save for range calculation if mods.get('OutfittingFieldType_FSDOptimalMass'): @@ -189,7 +189,7 @@ def export(data, filename=None) -> None: # noqa: C901, CCR001 raise if filename: - with open(filename, 'wt') as h: + with open(filename, 'w') as h: h.write(string) return @@ -206,5 +206,5 @@ def export(data, filename=None) -> None: # noqa: C901, CCR001 timestamp = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)) filename = pathlib.Path(config.get_str('outdir')) / f'{ship}.{timestamp}.txt' - with open(filename, 'wt') as h: + with open(filename, 'w') as h: h.write(string) diff --git a/hotkey/linux.py b/hotkey/linux.py index 7e4b9862..33e9cd7e 100644 --- a/hotkey/linux.py +++ b/hotkey/linux.py @@ -6,7 +6,7 @@ from EDMCLogging import get_main_logger from hotkey import AbstractHotkeyMgr if sys.platform != 'linux': - raise EnvironmentError("This file is for Linux only.") + raise OSError("This file is for Linux only.") logger = get_main_logger() diff --git a/hotkey/windows.py b/hotkey/windows.py index b292e7a7..9d32edaa 100644 --- a/hotkey/windows.py +++ b/hotkey/windows.py @@ -16,7 +16,7 @@ from EDMCLogging import get_main_logger from hotkey import AbstractHotkeyMgr if sys.platform != 'win32': - raise EnvironmentError("This file is for Windows only.") + raise OSError("This file is for Windows only.") logger = get_main_logger() diff --git a/journal_lock.py b/journal_lock.py index 3e16b309..8d90314e 100644 --- a/journal_lock.py +++ b/journal_lock.py @@ -13,7 +13,7 @@ import tkinter as tk from enum import Enum from os import getpid as os_getpid from tkinter import ttk -from typing import Callable +from collections.abc import Callable from l10n import translations as tr from config import config from EDMCLogging import get_main_logger diff --git a/killswitch.py b/killswitch.py index 22a40049..bec23ba8 100644 --- a/killswitch.py +++ b/killswitch.py @@ -11,9 +11,10 @@ import json import threading from copy import deepcopy from typing import ( - TYPE_CHECKING, Any, Callable, Mapping, MutableMapping, MutableSequence, NamedTuple, Sequence, + TYPE_CHECKING, Any, NamedTuple, TypedDict, TypeVar, cast, Union ) +from collections.abc import Callable, Mapping, MutableMapping, MutableSequence, Sequence import requests import semantic_version from semantic_version.base import Version @@ -217,7 +218,7 @@ class KillSwitchSet: def __init__(self, kill_switches: list[KillSwitches]) -> None: self.kill_switches = kill_switches - def get_disabled(self, id: str, *, version: Union[Version, str] = _current_version) -> DisabledResult: + def get_disabled(self, id: str, *, version: Version | str = _current_version) -> DisabledResult: """ Return whether the given feature ID is disabled by a killswitch for the given version. diff --git a/l10n.py b/l10n.py index 81faed21..48197592 100755 --- a/l10n.py +++ b/l10n.py @@ -10,15 +10,14 @@ Localization with gettext is a pain on non-Unix systems. """ from __future__ import annotations -import builtins import locale import numbers import re import sys -import warnings from contextlib import suppress from os import listdir, sep -from typing import Iterable, TextIO, cast +from typing import TextIO, cast +from collections.abc import Iterable import pathlib from config import config from EDMCLogging import get_main_logger @@ -77,8 +76,6 @@ class Translations: Use when translation is not desired or not available """ self.translations = {None: {}} - # DEPRECATED: Migrate to translations.translate or tr.tl. Will remove in 6.0 or later. - builtins.__dict__['_'] = lambda x: str(x).replace(r'\"', '"').replace('{CR}', '\n') def install(self, lang: str | None = None) -> None: # noqa: CCR001 """ @@ -121,12 +118,9 @@ class Translations: except Exception: logger.exception(f'Exception occurred while parsing {lang}.strings in plugin {plugin}') - # DEPRECATED: Migrate to translations.translate or tr.tl. Will remove in 6.0 or later. - builtins.__dict__['_'] = self.translate - def contents(self, lang: str, plugin_path: pathlib.Path | None = None) -> dict[str, str]: """Load all the translations from a translation file.""" - if lang not in self.available(): + if lang not in self.available() and lang != self.FALLBACK: # Because "en" doesn't appear in self.available() raise KeyError(f'Language {lang} not available') translations = {} @@ -262,21 +256,6 @@ def _wszarray_to_list(array): class _Locale: """Locale holds a few utility methods to convert data to and from localized versions.""" - # DEPRECATED: Migrate to _Locale.string_from_number. Will remove in 6.0 or later. - def stringFromNumber(self, number: float | int, decimals: int | None = None) -> str: # noqa: N802 - warnings.warn('use _Locale.string_from_number instead.', DeprecationWarning, stacklevel=2) - return self.string_from_number(number, decimals) # type: ignore - - # DEPRECATED: Migrate to _Locale.number_from_string. Will remove in 6.0 or later. - def numberFromString(self, string: str) -> int | float | None: # noqa: N802 - warnings.warn('use _Locale.number_from_string instead.', DeprecationWarning, stacklevel=2) - return self.number_from_string(string) - - # DEPRECATED: Migrate to _Locale.preferred_languages. Will remove in 6.0 or later. - def preferredLanguages(self) -> Iterable[str]: # noqa: N802 - warnings.warn('use _Locale.preferred_languages instead.', DeprecationWarning, stacklevel=2) - return self.preferred_languages() - def string_from_number(self, number: float | int, decimals: int = 5) -> str: """ Convert a number to a string. @@ -353,21 +332,6 @@ class _Locale: Locale = _Locale() translations = Translations() - -# DEPRECATED: Migrate to `translations`. Will be removed in 6.0 or later. -# 'Translations' singleton is deprecated. -# Begin Deprecation Zone -class _Translations(Translations): - def __init__(self): - warnings.warn('Translations and _Translations() are deprecated. ' - 'Please use translations and Translations() instead.', DeprecationWarning, stacklevel=2) - super().__init__() - - -# Yes, I know this is awful renaming garbage. But we need it for compat. -Translations: Translations = translations # type: ignore -# End Deprecation Zone - # generate template strings file - like xgettext # parsing is limited - only single ' or " delimited strings, and only one string per line if __name__ == "__main__": diff --git a/loadout.py b/loadout.py index 144439c0..c465cdbd 100644 --- a/loadout.py +++ b/loadout.py @@ -33,7 +33,7 @@ def export(data: companion.CAPIData, requested_filename: str | None = None) -> N ) # pretty print if requested_filename is not None and requested_filename: - with open(requested_filename, 'wt') as h: + with open(requested_filename, 'w') as h: h.write(string) return if not requested_filename: @@ -45,7 +45,7 @@ def export(data: companion.CAPIData, requested_filename: str | None = None) -> N regexp = re.compile(re.escape(ship) + r'\.\d\d\d\d-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt') oldfiles = sorted([x for x in listdir(config.get_str('outdir')) if regexp.match(x)]) if oldfiles: - with open(Path(config.get_str('outdir')) / Path(oldfiles[-1]), 'rU') as h: + with open(Path(config.get_str('outdir')) / Path(oldfiles[-1])) as h: if h.read() == string: return # same as last time - don't write @@ -57,5 +57,5 @@ def export(data: companion.CAPIData, requested_filename: str | None = None) -> N ship_time = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(query_time)) file_path = output_directory / f"{ship}.{ship_time}.txt" - with open(file_path, 'wt') as h: + with open(file_path, 'w') as h: h.write(string) diff --git a/monitor.py b/monitor.py index 91cf3375..bc1d44f0 100644 --- a/monitor.py +++ b/monitor.py @@ -18,7 +18,8 @@ from collections import defaultdict from os import SEEK_END, SEEK_SET, listdir from os.path import basename, expanduser, getctime, isdir, join from time import gmtime, localtime, mktime, sleep, strftime, strptime, time -from typing import TYPE_CHECKING, Any, BinaryIO, MutableMapping +from typing import TYPE_CHECKING, Any, BinaryIO +from collections.abc import MutableMapping import psutil import semantic_version import util_ships @@ -67,7 +68,7 @@ class EDLogs(FileSystemEventHandler): def __init__(self) -> None: # TODO(A_D): A bunch of these should be switched to default values (eg '' for strings) and no longer be Optional FileSystemEventHandler.__init__(self) # futureproofing - not need for current version of watchdog - self.root: 'tkinter.Tk' = None # type: ignore # Don't use Optional[] - mypy thinks no methods + self.root: tkinter.Tk = None # type: ignore # Don't use Optional[] - mypy thinks no methods self.currentdir: str | None = None # The actual logdir that we're monitoring self.logfile: str | None = None self.observer: BaseObserver | None = None @@ -188,7 +189,7 @@ class EDLogs(FileSystemEventHandler): }, } - def start(self, root: 'tkinter.Tk') -> bool: # noqa: CCR001 + def start(self, root: tkinter.Tk) -> bool: # noqa: CCR001 """ Start journal monitoring. @@ -347,7 +348,7 @@ class EDLogs(FileSystemEventHandler): """ return bool(self.thread and self.thread.is_alive()) - def on_created(self, event: 'FileSystemEvent') -> None: + def on_created(self, event: FileSystemEvent) -> None: """Watchdog callback when, e.g. client (re)started.""" if not event.is_directory and self._RE_LOGFILE.search(str(basename(event.src_path))): @@ -811,6 +812,10 @@ class EDLogs(FileSystemEventHandler): self.state['OnFoot'] = False self.state['Taxi'] = entry['Taxi'] + if entry['Multicrew']: + # Player has boarded another player's ship, but has not selected a role yet + self.state['Role'] = 'Idle' + # We can't now have anything in the BackPack, it's all in the # ShipLocker. self.backpack_set_empty() @@ -846,6 +851,10 @@ class EDLogs(FileSystemEventHandler): self.state['Taxi'] = False self.state['Dropship'] = False + # Since the player is now on foot, it's no longer possible to be multicrewing, so no + # need to actually check the multicrew parameter. + self.state['Role'] = None + elif event_type == 'dropshipdeploy': # We're definitely on-foot now self.state['OnFoot'] = True @@ -2267,7 +2276,7 @@ class EDLogs(FileSystemEventHandler): string = json.dumps(self.ship(False), ensure_ascii=False, indent=2, separators=(',', ': ')) # pretty print if filename: try: - with open(filename, 'wt', encoding='utf-8') as h: + with open(filename, 'w', encoding='utf-8') as h: h.write(string) except UnicodeError: @@ -2276,7 +2285,7 @@ class EDLogs(FileSystemEventHandler): ) try: - with open(filename, 'wt') as h: + with open(filename, 'w') as h: h.write(string) except OSError: @@ -2291,7 +2300,7 @@ class EDLogs(FileSystemEventHandler): ship = util_ships.ship_file_name(self.state['ShipName'], self.state['ShipType']) regexp = re.compile(re.escape(ship) + r'\.\d{4}-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt') - oldfiles = sorted((x for x in listdir(config.get_str('outdir')) if regexp.match(x))) + oldfiles = sorted(x for x in listdir(config.get_str('outdir')) if regexp.match(x)) if oldfiles: try: with open(join(config.get_str('outdir'), oldfiles[-1]), encoding='utf-8') as h: @@ -2323,13 +2332,13 @@ class EDLogs(FileSystemEventHandler): filename = join(config.get_str('outdir'), f'{ship}.{ts}.txt') try: - with open(filename, 'wt', encoding='utf-8') as h: + with open(filename, 'w', encoding='utf-8') as h: h.write(string) except UnicodeError: logger.exception("UnicodeError writing ship loadout to new filename with utf-8 encoding, trying without...") try: - with open(filename, 'wt') as h: + with open(filename, 'w') as h: h.write(string) except OSError: diff --git a/myNotebook.py b/myNotebook.py index c57a6deb..c28d7873 100644 --- a/myNotebook.py +++ b/myNotebook.py @@ -11,10 +11,10 @@ from __future__ import annotations import sys import tkinter as tk -import warnings from tkinter import ttk, messagebox from PIL import ImageGrab from l10n import translations as tr +from collections.abc import Callable if sys.platform == 'win32': PAGEFG = 'SystemWindowText' @@ -41,23 +41,28 @@ class Frame(ttk.Frame): """Custom ttk.Frame class to fix some display issues.""" def __init__(self, master: ttk.Notebook | None = None, **kw): - if sys.platform == 'win32': - ttk.Frame.__init__(self, master, style='nb.TFrame', **kw) - ttk.Frame(self).grid(pady=5) # top spacer - else: - ttk.Frame.__init__(self, master, **kw) - ttk.Frame(self).grid(pady=5) # top spacer - self.configure(takefocus=1) # let the frame take focus so that no particular child is focused + style = 'nb.TFrame' if sys.platform == 'win32' else None + super().__init__(master, style=style, **kw) # type: ignore + ttk.Frame(self).grid(pady=5) # Top spacer + self.configure(takefocus=1) # let the frame take focus so that no particular child is focused -class Label(tk.Label): - """Custom tk.Label class to fix some display issues.""" +class Label(ttk.Label): + """Custom ttk.Label class to fix some display issues.""" def __init__(self, master: ttk.Frame | None = None, **kw): - kw['foreground'] = kw.pop('foreground', PAGEFG if sys.platform == 'win32' - else ttk.Style().lookup('TLabel', 'foreground')) - kw['background'] = kw.pop('background', PAGEBG if sys.platform == 'win32' - else ttk.Style().lookup('TLabel', 'background')) + if sys.platform == 'win32': + fg_color = PAGEFG + else: + fg_color = ttk.Style().lookup('TLabel', 'foreground') + + if sys.platform == 'win32': + bg_color = PAGEBG + else: + bg_color = ttk.Style().lookup('TLabel', 'background') + + kw['foreground'] = kw.pop('foreground', fg_color) + kw['background'] = kw.pop('background', bg_color) super().__init__(master, **kw) @@ -122,32 +127,12 @@ class EntryMenu(ttk.Entry): pass -class Entry(EntryMenu): - """Custom ttk.Entry class to fix some display issues.""" - - # DEPRECATED: Migrate to EntryMenu. Will remove in 6.0 or later. - def __init__(self, master: ttk.Frame | None = None, **kw): - warnings.warn('Migrate to EntryMenu. Will remove in 6.0 or later.', DeprecationWarning, stacklevel=2) - EntryMenu.__init__(self, master, **kw) - - class Button(ttk.Button): """Custom ttk.Button class to fix some display issues.""" def __init__(self, master: ttk.Frame | None = None, **kw): - if sys.platform == 'win32': - ttk.Button.__init__(self, master, style='nb.TButton', **kw) - else: - ttk.Button.__init__(self, master, **kw) - - -class ColoredButton(tk.Button): - """Custom tk.Button class to fix some display issues.""" - - # DEPRECATED: Migrate to tk.Button. Will remove in 6.0 or later. - def __init__(self, master: ttk.Frame | None = None, **kw): - warnings.warn('Migrate to tk.Button. Will remove in 6.0 or later.', DeprecationWarning, stacklevel=2) - tk.Button.__init__(self, master, **kw) + style = 'nb.TButton' if sys.platform == 'win32' else None + super().__init__(master, style=style, **kw) # type: ignore class Checkbutton(ttk.Checkbutton): @@ -181,3 +166,186 @@ class OptionMenu(ttk.OptionMenu): # Workaround for https://bugs.python.org/issue25684 for i in range(0, self['menu'].index('end') + 1): self['menu'].entryconfig(i, variable=variable) + + +class ScrollableNotebook(Notebook): + """ + ScrollableNotebook – A tab bar that scrolls horizontally when there are too many tabs. + + Based off ttkScrollableNotebook by @muhammeteminturgut. + https://github.com/muhammeteminturgut/ttkScrollableNotebook + """ + + def __init__( + self, + master: ttk.Frame | None = None, + tabmenu: bool = False, + on_tab_change: Callable | None = None, + *args, + **kwargs + ) -> None: + super().__init__(master, **kwargs) + + # Sliding state + self.xLocation: int = 0 + self.timer: str | None = None + self.menuSpace: int = 50 if tabmenu else 30 + self.contentsManaged: list[tk.Widget] = [] + + # Notebook that holds the actual content widgets + self.notebookContent: ttk.Notebook = ttk.Notebook(self, **kwargs) + self.notebookContent.pack(fill="both", expand=True) + self.notebookContent.bind("", self._reset_slide) + + # Notebook that displays scrollable tabs + self.notebookTab: ttk.Notebook = ttk.Notebook(self, **kwargs) + self.notebookTab.place(x=0, y=0) + self.notebookTab.bind("<>", self._tab_changer) + if on_tab_change: + self.notebookTab.bind("<>", on_tab_change, add='+') + + # Sliding frame and controls + slide_frame: ttk.Frame = ttk.Frame(self) + slide_frame.place(relx=1.0, x=0, y=1, anchor=tk.NE) + + if tabmenu: + menu_btn = ttk.Label(slide_frame, text="\u2630") + menu_btn.bind("", self._bottom_menu) + menu_btn.pack(side=tk.RIGHT) + + left_arrow = ttk.Label(slide_frame, text=" \u276e") + left_arrow.bind("", self._left_slide_start) + left_arrow.bind("", self._slide_stop) + left_arrow.pack(side=tk.LEFT) + right_arrow = ttk.Label(slide_frame, text=" \u276f") + right_arrow.bind("", self._right_slide_start) + right_arrow.bind("", self._slide_stop) + right_arrow.pack(side=tk.RIGHT) + + # Outer Notebook geometry + self.grid(padx=10, pady=10, sticky=tk.NSEW) + + def _bottom_menu(self, event: tk.Event) -> None: + menu = tk.Menu(self, tearoff=0) + for tab in self.notebookTab.tabs(): + label = self.notebookTab.tab(tab, option="text") + menu.add_command(label=label, command=lambda t=tab: self.select(t)) # type: ignore + try: + menu.tk_popup(event.x_root, event.y_root) + finally: + menu.grab_release() + + def _tab_changer(self, event: tk.Event) -> None: + try: + idx = self.notebookTab.index("current") + self.notebookContent.select(idx) + except Exception: + pass + + def _right_slide_start(self, event: tk.Event | None = None) -> None: + if self._right_slide(event): + self.timer = self.after(100, self._right_slide_start) + + def _right_slide(self, event: tk.Event | None) -> bool: + tabs_w = self.notebookTab.winfo_width() + content_w = self.notebookContent.winfo_width() + + if tabs_w > content_w - self.menuSpace: + remaining = content_w - (tabs_w + self.notebookTab.winfo_x()) + if remaining <= self.menuSpace + 5: + self.xLocation -= 20 + self.notebookTab.place(x=self.xLocation, y=0) + return True + return False + + def _left_slide_start(self, event: tk.Event | None = None) -> None: + if self._left_slide(event): + self.timer = self.after(100, self._left_slide_start) + + def _left_slide(self, event: tk.Event | None) -> bool: + if self.notebookTab.winfo_x() != 0: + self.xLocation += 20 + self.notebookTab.place(x=self.xLocation, y=0) + return True + return False + + def _slide_stop(self, event: tk.Event) -> None: + if self.timer is not None: + self.after_cancel(self.timer) + self.timer = None + + def _reset_slide(self, event: tk.Event | None = None) -> None: + self.notebookTab.place(x=0, y=0) + self.xLocation = 0 + + def add(self, child: tk.Widget, **kwargs) -> None: + """Add content + visual tab.""" + has_tabs = len(self.notebookTab.tabs()) > 0 + + # Content tab properties + content_kwargs = kwargs.copy() + content_kwargs["text"] = "" # content tabs never show text + + if has_tabs: + content_kwargs["state"] = "hidden" + else: + content_kwargs.pop("state", None) + + # Add content widget + self.notebookContent.add(child, **content_kwargs) + + # Add visible tab + self.notebookTab.add(ttk.Frame(self.notebookTab), **kwargs) + self.contentsManaged.append(child) + + def __content_tab_id(self, tab_id: str) -> str: + idx = self.notebookTab.tabs().index(tab_id) + return self.notebookContent.tabs()[idx] + + def forget(self, tab_id: str) -> None: # type: ignore + """Remove a tab from both the visible tab strip and the hidden content notebook.""" + idx = self.notebookTab.index(tab_id) + content_tab = self.__content_tab_id(tab_id) + + self.notebookContent.forget(content_tab) + self.notebookTab.forget(tab_id) + self.contentsManaged[idx].destroy() + self.contentsManaged.pop(idx) + + def hide(self, tab_id: str) -> None: + """Hide a tab without destroying it.""" + self.notebookContent.hide(self.__content_tab_id(tab_id)) + self.notebookTab.hide(tab_id) + + def identify(self, x: int, y: int) -> str | None: # type: ignore + """Identify the visible tab at a given x/y coord.""" + return self.notebookTab.identify(x, y) + + def index(self, tab_id: str) -> int: + """Return the index of a visible tab.""" + return self.notebookTab.index(tab_id) + + def insert(self, pos: int, child: tk.Widget, **kwargs) -> None: + """Insert a new tab at a specified position.""" + self.notebookContent.insert(pos, child, **kwargs) + self.notebookTab.insert(pos, ttk.Frame(self.notebookTab), **kwargs) + + def select(self, tab_id: str) -> None: # type: ignore + """Select a tab.""" + self.notebookTab.select(tab_id) + + def tab(self, tab_id: str, option: str | None = None, **kwargs): + """Get/Set Options for tabs.""" + c_kwargs = kwargs.copy() + c_kwargs["text"] = "" # content tabs never show text + self.notebookContent.tab(self.__content_tab_id(tab_id), **c_kwargs) + return self.notebookTab.tab(tab_id, option=option, **kwargs) + + def tabs(self) -> tuple[str, ...]: + """Tuple of all tabs.""" + return self.notebookTab.tabs() + + def enable_traversal(self) -> None: + """Enable keyboard shortcuts for tabs.""" + self.notebookContent.enable_traversal() + self.notebookTab.enable_traversal() diff --git a/outfitting.py b/outfitting.py index 207c4883..3256da31 100644 --- a/outfitting.py +++ b/outfitting.py @@ -296,14 +296,14 @@ def export(data, filename) -> None: header = 'System,Station,Category,Name,Mount,Guidance,Ship,Class,Rating,FDevID,Date\n' rowheader = f'{data["lastSystem"]["name"]},{data["lastStarport"]["name"]}' - with open(filename, 'wt') as h: + with open(filename, 'w') as h: h.write(header) for v in data["lastStarport"].get("modules", {}).values(): try: m = lookup(v, ship_name_map) if m: - h.write(f'{rowheader}, {m["category"]}, {m["name"]}, {m.get("mount","")},' - f'{m.get("guidance","")}, {m.get("ship","")}, {m["class"]}, {m["rating"]},' + h.write(f'{rowheader}, {m["category"]}, {m["name"]}, {m.get("mount", "")},' + f'{m.get("guidance", "")}, {m.get("ship", "")}, {m["class"]}, {m["rating"]},' f'{m["id"]}, {data["timestamp"]}\n') except ValueError as e: diff --git a/plug.py b/plug.py index eaf23d0d..77d73f9e 100644 --- a/plug.py +++ b/plug.py @@ -13,11 +13,13 @@ import logging import operator import os import sys +from dataclasses import dataclass, field import tkinter as tk from datetime import datetime, timedelta, timezone from pathlib import Path from tkinter import ttk -from typing import Any, Mapping, MutableMapping +from typing import Any +from collections.abc import Mapping, MutableMapping import companion import myNotebook as nb # noqa: N813 @@ -33,14 +35,12 @@ PLUGINS_broken = [] # For asynchronous error display +@dataclass class LastError: """Holds the last plugin error.""" - msg: str | None - root: tk.Tk - - def __init__(self) -> None: - self.msg = None + msg: str | None = None + root: tk.Tk | None = field(default=None, repr=False) # not initialized by default, hide in repr last_error = LastError() @@ -49,13 +49,20 @@ last_error = LastError() class Plugin: """An EDMC plugin.""" - def __init__(self, name: str, loadfile: Path | None, plugin_logger: logging.Logger | None): # noqa: CCR001 + def __init__( # noqa: CCR001 + self, + name: str, + loadfile: Path | None, + plugin_logger: logging.Logger | None, + internal: bool = False + ): """ Load a single plugin. :param name: Base name of the file being loaded from. :param loadfile: Full path/filename of the plugin. :param plugin_logger: The logging instance for this plugin to use. + :param internal: True to use internal plugin loader logic. Defaults to False. :raises Exception: Typically, ImportError or OSError """ self.name: str = name # Display name. @@ -66,14 +73,19 @@ class Plugin: if loadfile: logger.info(f'loading plugin "{name.replace(".", "_")}" from "{loadfile}"') try: - filename = 'plugin_' - filename += name.encode(encoding='ascii', errors='replace').decode('utf-8').replace('.', '_') - spec = importlib.util.spec_from_file_location(filename, loadfile) - # Replaces older load_module() code. Includes a safety check that the module name is set. - if spec is not None and spec.loader is not None: - module = importlib.util.module_from_spec(spec) - sys.modules[module.__name__] = module - spec.loader.exec_module(module) + if internal: + filename = 'plugin_' + filename += name.encode(encoding='ascii', errors='replace').decode('utf-8').replace('.', '_') + spec = importlib.util.spec_from_file_location(filename, loadfile) + # Replaces older load_module() code. Includes a safety check that the module name is set. + if spec is not None and spec.loader is not None: + module = importlib.util.module_from_spec(spec) + sys.modules[module.__name__] = module + spec.loader.exec_module(module) + else: + module = importlib.import_module('.load', name) + + if module: if getattr(module, 'plugin_start3', None): newname = module.plugin_start3(Path(loadfile).resolve().parent) self.name = str(newname) if newname else self.name @@ -171,7 +183,7 @@ def _load_internal_plugins(): try: plugin_name = name[:-3] plugin_path = config.internal_plugin_dir_path / name - plugin = Plugin(plugin_name, plugin_path, logger) + plugin = Plugin(plugin_name, plugin_path, logger, internal=True) plugin.folder = None internal.append(plugin) except Exception: diff --git a/plugins/common_coreutils.py b/plugins/common_coreutils.py index 6b90d4dc..2e3b58ae 100644 --- a/plugins/common_coreutils.py +++ b/plugins/common_coreutils.py @@ -22,13 +22,15 @@ referenced in this file (or only in any other core plugin), and if so... # pylint: disable=import-error from __future__ import annotations -from typing import Any, Mapping, cast +from typing import Any, cast +from collections.abc import Mapping import tkinter as tk import base64 import gzip import io import json import os +import re import myNotebook as nb # noqa: N813 from EDMCLogging import get_main_logger from companion import CAPIData @@ -47,6 +49,49 @@ SEPY = 10 # seperator line spacing STATION_UNDOCKED = '×' # "Station" name to display when not docked = U+00D7 +# Known colonisation ship names match one of the following: +# "$EXT_PANEL_ColonisationShip;" +# "$EXT_PANEL_ColonisationShip; " with "" being eg; "Okawara Horizons" +# "$EXT_PANEL_ColonisationShip:#index=;" +# "$EXT_PANEL_ColonisationShip:#index=; " +_RE_COLONISATION_SHIP = re.compile(r'\$EXT_PANEL_ColonisationShip(?::#index=(\d+))?;(?: (.+))?') + + +def localised_name(value: str | None) -> str | None: + """ + Handle localisation of strings in places like station names. + + This is currently an issue for system colonisation ships that have a station name along the + lines of "$EXT_PANEL_ColonisationShip; Okawara Horizons" when written to the journal, but would + be better displayed as "System Colonisation Ship Okawara Horizona" (or other appropriately + localised equivalent). + + If no (known) localisable strings are present, the input will be returned unmodified. + + :param value: The non-localised string. + :return: The localised string, or None if the input was None. + """ + if value is None: + return None + + colonisation_ship_match = _RE_COLONISATION_SHIP.match(value) + if colonisation_ship_match: + ship_index = colonisation_ship_match.group(1) + ship_name = colonisation_ship_match.group(2) + # Use the ship's index as its name if no name was provided. + ship_id = ship_name or ship_index + + if ship_id: + # LANG: Colonisation ship name, with title + value = tr.tl("System Colonisation Ship {NAME}").format(NAME=ship_id) + + else: + # LANG: Unidentified colonisation ship + value = tr.tl("System Colonisation Ship") + + return value + + def plugin_start3(plugin_dir: str) -> str: """ Start the plugin. @@ -121,7 +166,7 @@ def station_link_common(data: CAPIData, this): :param this: The module global from the calling module. """ if data['commander']['docked'] or this.on_foot and this.station_name: - this.station_link['text'] = this.station_name + this.station_link['text'] = localised_name(this.station_name) elif data['lastStarport']['name'] and data['lastStarport']['name'] != "": this.station_link['text'] = STATION_UNDOCKED else: @@ -163,12 +208,14 @@ def station_name_setter_common(this): :param this: The module global from the calling module. """ to_set: str = cast(str, this.station_name) + if not to_set: if this.system_population is not None and this.system_population > 0: to_set = STATION_UNDOCKED else: to_set = '' + to_set = str(localised_name(to_set)) this.station_link['text'] = to_set diff --git a/plugins/coriolis.py b/plugins/coriolis.py index 89f128f7..be0bd4be 100644 --- a/plugins/coriolis.py +++ b/plugins/coriolis.py @@ -22,7 +22,8 @@ referenced in this file (or only in any other core plugin), and if so... # pylint: disable=import-error from __future__ import annotations -from typing import Any, Mapping +from typing import Any +from collections.abc import Mapping import tkinter as tk from tkinter import ttk import myNotebook as nb # noqa: N813 # its not my fault. diff --git a/plugins/edastro_core.py b/plugins/edastro_core.py new file mode 100644 index 00000000..6e2be692 --- /dev/null +++ b/plugins/edastro_core.py @@ -0,0 +1,230 @@ +""" +edastro_core.py - Exporting Data to EDAstro. + +Copyright (c) EDCD, All Rights Reserved +Licensed under the GNU General Public License. +See LICENSE file. + +This is an EDMC 'core' plugin. +All EDMC plugins are *dynamically* loaded at run-time. + +We build for Windows using `py2exe`. +`py2exe` can't possibly know about anything in the dynamically loaded core plugins. + +Thus, you **MUST** check if any imports you add in this file are only +referenced in this file (or only in any other core plugin), and if so... + + YOU MUST ENSURE THAT PERTINENT ADJUSTMENTS ARE MADE IN + `build.py` TO ENSURE THE FILES ARE ACTUALLY PRESENT + IN AN END-USER INSTALLATION ON WINDOWS. +""" + +import json +from typing import Any +from tkinter import ttk +import requests +import tkinter as tk +from l10n import translations as tr +from config import config +from ttkHyperlinkLabel import HyperlinkLabel +import myNotebook as nb # noqa: N813 +from EDMCLogging import get_main_logger + +from plugins.common_coreutils import PADX, PADY, BUTTONX + +logger = get_main_logger() + + +class This: + """Holds module globals.""" + + app_name = "EDAstro" + edastro_push = "https://edastro.com/api/journal" + + event_filters = { + "CarrierStats": [ + "timestamp", + "event", + "Callsign", + "Name", + "CarrierID", + "AllowNotorious", + "PendingDecommission", + "DockingAccess", + "FuelLevel", + "Crew", + ], + "CarrierJumpRequest": [ + "timestamp", + "event", + "SystemName", + "SystemAddress", + "CarrierID", + "Body", + "BodyID" + "DepartureTime" + ], + "ScanOrganic": [ + "timestamp", + "ScanType", + "Genus_Localised", + "Species_Localised", + "Genus", + "event", + "Body", + "Species", + "Variant", + "SystemAddress", + "Variant_Localised", + ] + } + + def __init__(self): + self.log: tk.IntVar | None = None + self.log_button: ttk.Checkbutton | None = None + + +this = This() + + +def set_config_first_run() -> None: + """Enable EDAstro if the config key does not exist.""" + if config.get_int("edastro_send") is None: + logger.info("EDAstro First Run. Enabling") + config.set("edastro_send", 1) + + +# Plugin callbacks +def plugin_start3(plugin_dir: str) -> str: + """ + Start this plugin. + + :param plugin_dir: `str` - The full path to this plugin's directory. + :return: `str` - Name of this plugin to use in UI. + """ + set_config_first_run() + return "EDAstro" + + +def plugin_prefs(parent, cmdr: str, is_beta: bool) -> nb.Frame: + """ + Set up Preferences pane for this plugin. + + :param parent: tkinter parent to attach to. + :param cmdr: `str` - Name of current Cmdr. + :param is_beta: `bool` - True if this is a beta version of the Game. + :return: The tkinter frame we created. + """ + edastroframe = nb.Frame(parent) + edastroframe.columnconfigure(0, weight=1) + + cur_row = 0 + HyperlinkLabel( + edastroframe, + text="Elite Dangerous Astronomy", + background=nb.Label().cget("background"), + url="https://edastro.com", + underline=True, + ).grid( + row=cur_row, padx=PADX, pady=PADY, sticky=tk.W + ) # Don't translate + cur_row += 1 + this.log = tk.IntVar(value=config.get_int("edastro_send") and 1) + this.log_button = nb.Checkbutton( + edastroframe, + # LANG: Settings>EDAstro - Label on checkbox for 'send data' + text=tr.tl("Send data to EDAstro"), + variable=this.log, + ) + if this.log_button: + this.log_button.grid( + row=cur_row, columnspan=2, padx=BUTTONX, pady=PADY, sticky=tk.W + ) + cur_row += 1 + + return edastroframe + + +def prefs_changed(cmdr: str, is_beta: bool) -> None: + """ + Handle any changes to Settings once the dialog is closed. + + :param cmdr: Name of Commander. + :param is_beta: Whether game beta was detected. + """ + if this.log: + config.set("edastro_send", this.log.get()) + + +def filter_event_data(entry) -> dict[str, Any]: + """Format Journal Data for EDAstro.""" + if entry["event"] in this.event_filters: + return { + key: entry[key] + for key in this.event_filters[entry["event"]] + if key in entry + } + return entry + + +def edastro_update(system, entry, state): + """Send a processed event to EDAstro.""" + event_name = str(entry["event"]) + filtered_entry = filter_event_data(entry) + app_header = { + "appName": this.app_name, + "odyssey": state.get("Odyssey"), + "system": system, + } + event_object = [app_header, filtered_entry] + event_data = json.dumps(event_object) + try: + json_header = {"Content-Type": "application/json"} + response = requests.post( + url=this.edastro_push, headers=json_header, data=event_data, timeout=20 + ) + if response.status_code == 200: + edastro = json.loads(response.text) + if str(edastro["status"]) == "200" or str(edastro["status"]) == "401": + # 200 = at least one event accepted, 401 = none were accepted, but no errors either + logger.info(f"EDAstro: Data sent! ({event_name})") + else: + logger.debug( + f"Error Response:\nRequest: {this.edastro_push}\n " + f'Response ({edastro["status"]}): \n{edastro["message"]}' + ) + else: + logger.debug( + f"Unexpected Response:\nRequest: {this.edastro_push}\n " + f"Response ({response.status_code}):\n{response.text}" + ) + except Exception as ex: + logger.warning( + f"Failed to submit EDAstro data:\nRequest: {this.edastro_push}", + exc_info=ex, + ) + + +def journal_entry( + cmdr: str, + is_beta: bool, + system: str, + station: str, + entry: dict[str, Any], + state: dict[str, Any], +) -> str | None: + """ + Handle a new Journal event. + + :param cmdr: Name of Commander. + :param is_beta: Whether game beta was detected. + :param system: Name of current tracked system. + :param station: Name of current tracked station location. + :param entry: The journal event. + :param state: `monitor.state` + :return: None if no error, else an error string. + """ + if this.log and entry["event"] in ["CarrierStats", "CarrierJumpRequest", "ScanOrganic"]: + edastro_update(system, entry, state) + + return None diff --git a/plugins/eddn.py b/plugins/eddn.py index 39ecf147..8b783c8f 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -29,10 +29,12 @@ import pathlib import re import sqlite3 import tkinter as tk +from tkinter import ttk from platform import system from textwrap import dedent from threading import Lock -from typing import Any, Iterator, Mapping, MutableMapping +from typing import Any +from collections.abc import Iterator, Mapping, MutableMapping import requests import companion import edmc_data @@ -114,15 +116,15 @@ class This: # Tracking UI self.ui: tk.Frame - self.ui_system_name: tk.Label - self.ui_system_address: tk.Label - self.ui_j_body_name: tk.Label - self.ui_j_body_id: tk.Label - self.ui_j_body_type: tk.Label - self.ui_s_body_name: tk.Label - self.ui_station_name: tk.Label - self.ui_station_type: tk.Label - self.ui_station_marketid: tk.Label + self.ui_system_name: ttk.Label + self.ui_system_address: ttk.Label + self.ui_j_body_name: ttk.Label + self.ui_j_body_id: ttk.Label + self.ui_j_body_type: ttk.Label + self.ui_s_body_name: ttk.Label + self.ui_station_name: ttk.Label + self.ui_station_type: ttk.Label + self.ui_station_marketid: ttk.Label this = This() @@ -151,7 +153,7 @@ class EDDNSender: r"unable to validate.',\)]$" ) - def __init__(self, eddn: 'EDDN', eddn_endpoint: str) -> None: + def __init__(self, eddn: EDDN, eddn_endpoint: str) -> None: """ Prepare the system for processing messages. @@ -170,12 +172,6 @@ class EDDNSender: self.db_conn = self.sqlite_queue_v1() self.db = self.db_conn.cursor() - ####################################################################### - # Queue database migration - ####################################################################### - self.convert_legacy_file() - ####################################################################### - self.queue_processing = Lock() # Initiate retry/send-now timer logger.trace_if( @@ -183,7 +179,7 @@ class EDDNSender: f"First queue run scheduled for {self.eddn.REPLAY_STARTUP_DELAY}ms from now" ) if not os.getenv("EDMC_NO_UI"): - self.eddn.parent.after(self.eddn.REPLAY_STARTUP_DELAY, self.queue_check_and_send, True) + self.eddn.parent.winfo_toplevel().after(self.eddn.REPLAY_STARTUP_DELAY, self.queue_check_and_send, True) def sqlite_queue_v1(self) -> sqlite3.Connection: """ @@ -221,27 +217,6 @@ class EDDNSender: return db_conn - def convert_legacy_file(self): - """Convert a legacy file's contents into the sqlite3 db.""" - # DEPRECATED: Legacy files no longer used as of 5.6.0. Will remove in 6.0 or later. - filename = config.app_dir_path / 'replay.jsonl' - if not filename.exists(): - return - logger.info("Converting legacy `replay.jsonl` to `eddn_queue-v1.db`") - try: - with filename.open('r', encoding='utf-8') as replay_file: - for line in replay_file: - cmdr, msg = json.loads(line) - self.add_message(cmdr, msg) - except Exception as e: - logger.exception("Failed to convert legacy file: %s", e) - return - logger.info("Conversion complete, removing `replay.jsonl`") - try: - filename.unlink() - except Exception as e: - logger.warning("Could not delete legacy file: %s", e) - def close(self) -> None: """Clean up any resources.""" logger.debug('Closing db cursor.') @@ -2026,15 +2001,15 @@ def plugin_app(parent: tk.Tk) -> tk.Frame | None: # System ####################################################################### # SystemName - system_name_label = tk.Label(this.ui, text="J:SystemName:") + system_name_label = ttk.Label(this.ui, text="J:SystemName:") system_name_label.grid(row=row, column=0, sticky=tk.W) - this.ui_system_name = tk.Label(this.ui, name='eddn_track_system_name', anchor=tk.W) + this.ui_system_name = ttk.Label(this.ui, name='eddn_track_system_name', anchor=tk.W) this.ui_system_name.grid(row=row, column=1, sticky=tk.E) row += 1 # SystemAddress - system_address_label = tk.Label(this.ui, text="J:SystemAddress:") + system_address_label = ttk.Label(this.ui, text="J:SystemAddress:") system_address_label.grid(row=row, column=0, sticky=tk.W) - this.ui_system_address = tk.Label(this.ui, name='eddn_track_system_address', anchor=tk.W) + this.ui_system_address = ttk.Label(this.ui, name='eddn_track_system_address', anchor=tk.W) this.ui_system_address.grid(row=row, column=1, sticky=tk.E) row += 1 ####################################################################### @@ -2043,27 +2018,27 @@ def plugin_app(parent: tk.Tk) -> tk.Frame | None: # Body ####################################################################### # Body Name from Journal - journal_body_name_label = tk.Label(this.ui, text="J:BodyName:") + journal_body_name_label = ttk.Label(this.ui, text="J:BodyName:") journal_body_name_label.grid(row=row, column=0, sticky=tk.W) - this.ui_j_body_name = tk.Label(this.ui, name='eddn_track_j_body_name', anchor=tk.W) + this.ui_j_body_name = ttk.Label(this.ui, name='eddn_track_j_body_name', anchor=tk.W) this.ui_j_body_name.grid(row=row, column=1, sticky=tk.E) row += 1 # Body ID from Journal - journal_body_id_label = tk.Label(this.ui, text="J:BodyID:") + journal_body_id_label = ttk.Label(this.ui, text="J:BodyID:") journal_body_id_label.grid(row=row, column=0, sticky=tk.W) - this.ui_j_body_id = tk.Label(this.ui, name='eddn_track_j_body_id', anchor=tk.W) + this.ui_j_body_id = ttk.Label(this.ui, name='eddn_track_j_body_id', anchor=tk.W) this.ui_j_body_id.grid(row=row, column=1, sticky=tk.E) row += 1 # Body Type from Journal - journal_body_type_label = tk.Label(this.ui, text="J:BodyType:") + journal_body_type_label = ttk.Label(this.ui, text="J:BodyType:") journal_body_type_label.grid(row=row, column=0, sticky=tk.W) - this.ui_j_body_type = tk.Label(this.ui, name='eddn_track_j_body_type', anchor=tk.W) + this.ui_j_body_type = ttk.Label(this.ui, name='eddn_track_j_body_type', anchor=tk.W) this.ui_j_body_type.grid(row=row, column=1, sticky=tk.E) row += 1 # Body Name from Status.json - status_body_name_label = tk.Label(this.ui, text="S:BodyName:") + status_body_name_label = ttk.Label(this.ui, text="S:BodyName:") status_body_name_label.grid(row=row, column=0, sticky=tk.W) - this.ui_s_body_name = tk.Label(this.ui, name='eddn_track_s_body_name', anchor=tk.W) + this.ui_s_body_name = ttk.Label(this.ui, name='eddn_track_s_body_name', anchor=tk.W) this.ui_s_body_name.grid(row=row, column=1, sticky=tk.E) row += 1 ####################################################################### @@ -2072,21 +2047,21 @@ def plugin_app(parent: tk.Tk) -> tk.Frame | None: # Station ####################################################################### # Name - status_station_name_label = tk.Label(this.ui, text="J:StationName:") + status_station_name_label = ttk.Label(this.ui, text="J:StationName:") status_station_name_label.grid(row=row, column=0, sticky=tk.W) - this.ui_station_name = tk.Label(this.ui, name='eddn_track_station_name', anchor=tk.W) + this.ui_station_name = ttk.Label(this.ui, name='eddn_track_station_name', anchor=tk.W) this.ui_station_name.grid(row=row, column=1, sticky=tk.E) row += 1 # Type - status_station_type_label = tk.Label(this.ui, text="J:StationType:") + status_station_type_label = ttk.Label(this.ui, text="J:StationType:") status_station_type_label.grid(row=row, column=0, sticky=tk.W) - this.ui_station_type = tk.Label(this.ui, name='eddn_track_station_type', anchor=tk.W) + this.ui_station_type = ttk.Label(this.ui, name='eddn_track_station_type', anchor=tk.W) this.ui_station_type.grid(row=row, column=1, sticky=tk.E) row += 1 # MarketID - status_station_marketid_label = tk.Label(this.ui, text="J:StationID:") + status_station_marketid_label = ttk.Label(this.ui, text="J:StationID:") status_station_marketid_label.grid(row=row, column=0, sticky=tk.W) - this.ui_station_marketid = tk.Label(this.ui, name='eddn_track_station_id', anchor=tk.W) + this.ui_station_marketid = ttk.Label(this.ui, name='eddn_track_station_id', anchor=tk.W) this.ui_station_marketid.grid(row=row, column=1, sticky=tk.E) row += 1 ####################################################################### diff --git a/plugins/edsm.py b/plugins/edsm.py index a2e5004f..46c8ef4e 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -29,7 +29,8 @@ from queue import Queue from threading import Thread from time import sleep from tkinter import ttk -from typing import Any, Literal, Mapping, MutableMapping, cast, Sequence +from typing import Any, Literal, cast +from collections.abc import Mapping, MutableMapping, Sequence import requests import killswitch import monitor @@ -43,7 +44,7 @@ from ttkHyperlinkLabel import HyperlinkLabel from l10n import translations as tr from plugins.common_coreutils import (api_keys_label_common, PADX, PADY, BUTTONX, SEPY, BOXY, STATION_UNDOCKED, show_pwd_var_common, station_link_common, this_format_common, - cmdr_data_initial_common) + cmdr_data_initial_common, localised_name) # TODO: @@ -90,11 +91,11 @@ class This: self.newgame: bool = False # starting up - batch initial burst of events self.newgame_docked: bool = False # starting up while docked self.navbeaconscan: int = 0 # batch up burst of Scan events after NavBeaconScan - self.system_link: tk.Widget | None = None + self.system_link: ttk.Widget | None = None self.system_name: tk.Tk | None = None self.system_address: int | None = None # Frontier SystemAddress self.system_population: int | None = None - self.station_link: tk.Widget | None = None + self.station_link: ttk.Widget | None = None self.station_name: str | None = None self.station_marketid: int | None = None # Frontier MarketID self.on_foot = False @@ -109,7 +110,7 @@ class This: self.log: tk.IntVar | None = None self.log_button: ttk.Checkbutton | None = None - self.label: tk.Widget | None = None + self.label: ttk.Widget | None = None self.cmdr_label: nb.Label | None = None self.cmdr_text: nb.Label | None = None @@ -543,7 +544,7 @@ entry: {entry!r}''' to_set = '' if this.station_link: - this.station_link['text'] = to_set + this.station_link['text'] = localised_name(to_set) this.station_link['url'] = station_url(str(this.system_name), str(this.station_name)) this.station_link.update_idletasks() @@ -729,8 +730,8 @@ def send_to_edsm( # noqa: CCR001 # Respect rate limits if they exist if remaining == 0: # Calculate sleep time until the rate limit reset time - reset_time = datetime.utcfromtimestamp(reset) - current_time = datetime.utcnow() + reset_time = datetime.fromtimestamp(reset, tz=timezone.utc) + current_time = datetime.now(timezone.utc) sleep_time = (reset_time - current_time).total_seconds() diff --git a/plugins/edsy.py b/plugins/edsy.py index b1bdbaf1..109bb2f0 100644 --- a/plugins/edsy.py +++ b/plugins/edsy.py @@ -20,7 +20,8 @@ referenced in this file (or only in any other core plugin), and if so... """ from __future__ import annotations -from typing import Any, Mapping +from typing import Any +from collections.abc import Mapping from plugins.common_coreutils import shipyard_url_common # pylint: disable=E0401 diff --git a/plugins/inara.py b/plugins/inara.py index 7bc31a30..be3f517b 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -31,7 +31,8 @@ from datetime import datetime, timedelta, timezone from operator import itemgetter from threading import Lock, Thread from tkinter import ttk -from typing import Any, Callable, Deque, Mapping, NamedTuple, Sequence, cast, Union +from typing import Any, Deque, NamedTuple, cast, Union +from collections.abc import Callable, Mapping, Sequence import requests import edmc_data import killswitch @@ -113,20 +114,20 @@ class This: self.timer_run = True # Main window clicks - self.system_link: tk.Widget = None # type: ignore + self.system_link: ttk.Widget = None # type: ignore self.system_name: str | None = None # type: ignore self.system_address: str | None = None # type: ignore self.system_population: int | None = None - self.station_link: tk.Widget = None # type: ignore + self.station_link: ttk.Widget = None # type: ignore self.station_name = None self.station_marketid = None # Prefs UI - self.log: 'tk.IntVar' + self.log: tk.IntVar self.log_button: nb.Checkbutton self.label: HyperlinkLabel self.apikey: nb.EntryMenu - self.apikey_label: tk.Label + self.apikey_label: ttk.Label self.events: dict[Credentials, Deque[Event]] = defaultdict(deque) self.event_lock: Lock = threading.Lock() # protects events, for use when rewriting events @@ -533,7 +534,7 @@ def journal_entry( # noqa: C901, CCR001 new_add_event('setCommanderRankPower', entry['timestamp'], power_data) # Ship change - if event_name == 'Loadout' and this.shipswap: + elif event_name == 'Loadout' and this.shipswap: this.loadout = make_loadout(state) new_add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout) this.shipswap = False @@ -1610,7 +1611,7 @@ def handle_api_error(data: Mapping[str, Any], status: int, reply: dict[str, Any] """ error_message = reply['header'].get('eventStatusText', "") logger.warning(f'Inara\t{status} {error_message}') - logger.debug(f'JSON data:\n{json.dumps(data, indent=2, separators = (",", ": "))}') + logger.debug(f'JSON data:\n{json.dumps(data, indent=2, separators=(",", ": "))}') # LANG: INARA API returned some kind of error (error message will be contained in {MSG}) plug.show_error(tr.tl('Error: Inara {MSG}').format(MSG=error_message)) diff --git a/plugins/spansh_core.py b/plugins/spansh_core.py index 1b7536c5..5a3e541d 100644 --- a/plugins/spansh_core.py +++ b/plugins/spansh_core.py @@ -22,6 +22,7 @@ referenced in this file (or only in any other core plugin), and if so... from __future__ import annotations import tkinter as tk +from tkinter import ttk from typing import Any import requests from companion import CAPIData @@ -39,11 +40,11 @@ class This: def __init__(self): self.parent: tk.Tk self.shutting_down = False # Plugin is shutting down. - self.system_link: tk.Widget = None # type: ignore + self.system_link: ttk.Widget = None # type: ignore self.system_name: str | None = None self.system_address: str | None = None self.system_population: int | None = None - self.station_link: tk.Widget = None # type: ignore + self.station_link: ttk.Widget = None # type: ignore self.station_name = None self.station_marketid = None self.on_foot = False diff --git a/prefs.py b/prefs.py index bcdd04cf..9b82704a 100644 --- a/prefs.py +++ b/prefs.py @@ -1,20 +1,20 @@ -# -*- coding: utf-8 -*- """EDMC preferences library.""" from __future__ import annotations import contextlib import logging +import time from os.path import join, normpath from pathlib import Path import subprocess import sys import tkinter as tk -import warnings from os import system from tkinter import colorchooser as tkColorChooser # type: ignore # noqa: N812 from tkinter import ttk from types import TracebackType -from typing import Any, Callable, Optional, Type +from typing import Any +from collections.abc import Callable import myNotebook as nb # noqa: N813 import plug from config import appversion_nobuild, config @@ -37,14 +37,6 @@ logger = get_main_logger() # May be imported by plugins -# DEPRECATED: Migrate to open_log_folder. Will remove in 6.0 or later. -def help_open_log_folder() -> None: - """Open the folder logs are stored in.""" - warnings.warn('prefs.help_open_log_folder is deprecated, use open_log_folder instead. ' - 'This function will be removed in 6.0 or later', DeprecationWarning, stacklevel=2) - open_folder(Path(config.app_dir_path / 'logs')) - - def open_folder(file: Path) -> None: """Open the given file in the OS file explorer.""" if sys.platform.startswith('win'): @@ -178,8 +170,8 @@ class AutoInc(contextlib.AbstractContextManager): def __exit__( self, - exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[TracebackType] - ) -> Optional[bool]: + exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None + ) -> bool | None: """Do nothing.""" return None @@ -249,7 +241,7 @@ if sys.platform == 'win32': class PreferencesDialog(tk.Toplevel): """The EDMC preferences dialog.""" - def __init__(self, parent: tk.Tk, callback: Optional[Callable]): + def __init__(self, parent: tk.Tk, callback: Callable | None): super().__init__(parent) self.parent = parent @@ -273,7 +265,7 @@ class PreferencesDialog(tk.Toplevel): self.cmdr: str | bool | None = False # Note if Cmdr changes in the Journal self.is_beta: bool = False # Note if Beta status changes in the Journal - self.cmdrchanged_alarm: Optional[str] = None # This stores an ID that can be used to cancel a scheduled call + self.cmdrchanged_alarm: str | None = None # This stores an ID that can be used to cancel a scheduled call # Set up the main frame frame = ttk.Frame(self) @@ -284,8 +276,7 @@ class PreferencesDialog(tk.Toplevel): frame.rowconfigure(0, weight=1) frame.rowconfigure(1, weight=0) - notebook: nb.Notebook = nb.Notebook(frame) - notebook.bind('<>', self.tabchanged) # Recompute on tab change + notebook: nb.ScrollableNotebook = nb.ScrollableNotebook(frame, tabmenu=True, on_tab_change=self.tabchanged) self.PADX = 10 self.BUTTONX = 12 # indent Checkbuttons and Radiobuttons @@ -318,6 +309,11 @@ class PreferencesDialog(tk.Toplevel): self.cmdrchanged() self.themevarchanged() + # Init hotkey attributes to avoid linter errors + self.hotkey_text: ttk.Entry + self.hotkey_only_btn: nb.Checkbutton + self.hotkey_play_btn: nb.Checkbutton + # disable hotkey for the duration hotkeymgr.unregister() @@ -333,12 +329,6 @@ class PreferencesDialog(tk.Toplevel): # Set Log Directory self.logfile_loc = Path(config.app_dir_path / 'logs') - # Set minimum size to prevent content cut-off - self.update_idletasks() # Update "requested size" from geometry manager - min_width = self.winfo_reqwidth() - min_height = self.winfo_reqheight() - self.wm_minsize(min_width, min_height) - def __setup_output_tab(self, root_notebook: ttk.Notebook) -> None: output_frame = nb.Frame(root_notebook) output_frame.columnconfigure(0, weight=1) @@ -989,11 +979,19 @@ class PreferencesDialog(tk.Toplevel): ).grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get()) for plugin in enabled_plugins: + curr_row = row.get() label = nb.Label(plugins_frame, text=plugin.name if plugin.name == plugin.folder else f'{plugin.folder} ({plugin.name})') - label.grid(columnspan=2, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=row.get()) + label.grid(column=0, columnspan=2, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=curr_row) + + btn = ttk.Button( + plugins_frame, + text=tr.tl("Disable"), # LANG: Disable an Enabled Plugin + command=lambda plugin=plugin: self.disable_plugin(plugin) # type: ignore + ) + btn.grid(column=3, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=curr_row) ############################################################ # Show which plugins don't have Python 3.x support @@ -1035,9 +1033,18 @@ class PreferencesDialog(tk.Toplevel): ).grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get()) for plugin in disabled_plugins: + curr_row = row.get() nb.Label(plugins_frame, text=plugin.name).grid( - columnspan=2, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=row.get() + column=0, columnspan=2, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=curr_row ) + + btn = ttk.Button( + plugins_frame, + text=tr.tl("Enable"), # LANG: Enable a Disabled Plugin + command=lambda plugin=plugin: self.enable_plugin(plugin) # type: ignore + ) + btn.grid(column=3, padx=self.LISTX, pady=self.PADY, sticky=tk.W, row=curr_row) + ############################################################ # Show plugins that failed to load ############################################################ @@ -1059,6 +1066,55 @@ class PreferencesDialog(tk.Toplevel): # LANG: Label on Settings > Plugins tab notebook.add(plugins_frame, text=tr.tl('Plugins')) # Tab heading in settings + def disable_plugin(self, plugin): + """Disable an existing plugin and restart.""" + logger.debug(f"Calling plugin_stop() for {plugin.name}") + if plugin._get_func('plugin_stop'): # Try to stop cleanly. We're going regardless... + plugin.module.plugin_stop() + # LANG: Text of Notification Popup for EDMC Restart + restart_msg = tr.tl( # LANG: Disabling a Plugin + r"Disabling plugin {PLUGIN}. This will cause a restart. Click OK to continue..." + ).format(PLUGIN=plugin.name) + restart_box = tk.messagebox.Message( + title=tr.tl('Restart Required'), # LANG: Title of Notification Popup for EDMC Restart + message=restart_msg, + type=tk.messagebox.OK + ) + restart_box.show() + time.sleep(2) # Give plugin stop time to work. + plug.PLUGINS.remove(plugin) # Remove PLugin from active list + try: + folder = Path(config.plugin_dir_path / plugin.folder) + new_name = f"{folder}.disabled" + folder.rename(new_name) + logger.info(f"Disabled {plugin.name}.") + except FileExistsError: + logger.warning("Unable to disable the plugin!") + return + self.req_restart = True + self.apply() + + def enable_plugin(self, plugin): + """Enable an existing plugin and restart.""" + logger.debug(f"Attempting to enable {plugin.name}") + localfolder = plugin.folder + ".disabled" + folder = Path(config.plugin_dir_path) / localfolder + if not folder.exists(): + logger.warning(f"Folder does not exist: {folder}") + return + new_folder = folder.with_name(plugin.folder.replace(".disabled", "")) + if new_folder.exists(): + logger.warning(f"Target folder already exists: {new_folder}") + return + try: + folder.rename(new_folder) + logger.info(f"Enabled {plugin.name}.") + except Exception as e: + logger.error(f"Failed to enable plugin {plugin.name}: {e}") + return + self.req_restart = True + self.apply() + def cmdrchanged(self, event=None): """ Notify plugins of cmdr change. @@ -1080,7 +1136,7 @@ class PreferencesDialog(tk.Toplevel): """Handle preferences active tab changing.""" self.outvarchanged() - def outvarchanged(self, event: Optional[tk.Event] = None) -> None: + def outvarchanged(self, event: tk.Event | None = None) -> None: """Handle Output tab variable changes.""" self.displaypath(self.outdir, self.outdir_entry) self.displaypath(self.logdir, self.logdir_entry) @@ -1184,14 +1240,14 @@ class PreferencesDialog(tk.Toplevel): self.theme_button_0['state'] = state self.theme_button_1['state'] = state - def hotkeystart(self, event: 'tk.Event[Any]') -> None: + def hotkeystart(self, event: tk.Event[Any]) -> None: """Start listening for hotkeys.""" event.widget.bind('', self.hotkeylisten) event.widget.bind('', self.hotkeylisten) event.widget.delete(0, tk.END) hotkeymgr.acquire_start() - def hotkeyend(self, event: 'tk.Event[Any]') -> None: + def hotkeyend(self, event: tk.Event[Any]) -> None: """Stop listening for hotkeys.""" event.widget.unbind('') event.widget.unbind('') @@ -1202,7 +1258,7 @@ class PreferencesDialog(tk.Toplevel): # LANG: No hotkey/shortcut set hotkeymgr.display(self.hotkey_code, self.hotkey_mods) if self.hotkey_code else tr.tl('None')) - def hotkeylisten(self, event: 'tk.Event[Any]') -> str: + def hotkeylisten(self, event: tk.Event[Any]) -> str: """ Hotkey handler. diff --git a/protocol.py b/protocol.py index 58ee9fa3..774a012e 100644 --- a/protocol.py +++ b/protocol.py @@ -5,13 +5,14 @@ Copyright (c) EDCD, All Rights Reserved Licensed under the GNU General Public License. See LICENSE file. """ + from __future__ import annotations import os import sys import threading from urllib import parse -from typing import TYPE_CHECKING, Type +from typing import TYPE_CHECKING from config import config from constants import appname, protocolhandler_redirect from EDMCLogging import get_main_logger @@ -23,8 +24,8 @@ logger = get_main_logger() is_wine = False -if sys.platform == 'win32': - is_wine = bool(os.getenv('WINEPREFIX')) +if sys.platform == "win32": + is_wine = bool(os.getenv("WINEPREFIX")) class GenericProtocolHandler: @@ -32,10 +33,10 @@ class GenericProtocolHandler: def __init__(self) -> None: self.redirect = protocolhandler_redirect # Base redirection URL - self.master: 'tkinter.Tk' = None # type: ignore + self.master: tkinter.Tk = None # type: ignore self.lastpayload: str | None = None - def start(self, master: 'tkinter.Tk') -> None: + def start(self, master: tkinter.Tk) -> None: """Start Protocol Handler.""" self.master = master @@ -46,29 +47,50 @@ class GenericProtocolHandler: """Generate an auth event.""" self.lastpayload = url - logger.trace_if('frontier-auth', f'Payload: {self.lastpayload}') + logger.trace_if("frontier-auth", f"Payload: {self.lastpayload}") if not config.shutting_down: logger.debug('event_generate("<>")') - self.master.event_generate('<>', when="tail") + self.master.event_generate("<>", when="tail") -if (config.auth_force_edmc_protocol # noqa: C901 - or ( - sys.platform == 'win32' - and getattr(sys, 'frozen', False) - and not is_wine - and not config.auth_force_localserver - )): +if config.auth_force_edmc_protocol or ( # noqa: C901 + sys.platform == "win32" + and getattr(sys, "frozen", False) + and not is_wine + and not config.auth_force_localserver +): # This could be false if you use auth_force_edmc_protocol, but then you get to keep the pieces - if sys.platform != 'win32': - raise EnvironmentError("This code is for Windows only.") + if sys.platform != "win32": + raise OSError("This code is for Windows only.") # spell-checker: words HBRUSH HICON WPARAM wstring WNDCLASS HMENU HGLOBAL from ctypes import ( # type: ignore - windll, POINTER, WINFUNCTYPE, Structure, byref, c_long, c_void_p, create_unicode_buffer, wstring_at + windll, + POINTER, + WINFUNCTYPE, + Structure, + byref, + c_long, + c_void_p, + create_unicode_buffer, + wstring_at, ) from ctypes.wintypes import ( - ATOM, BOOL, HBRUSH, HGLOBAL, HICON, HINSTANCE, HWND, INT, LPARAM, LPCWSTR, LPMSG, LPVOID, LPWSTR, - MSG, UINT, WPARAM + ATOM, + BOOL, + HBRUSH, + HGLOBAL, + HICON, + HINSTANCE, + HWND, + INT, + LPARAM, + LPCWSTR, + LPMSG, + LPVOID, + LPWSTR, + MSG, + UINT, + WPARAM, ) import win32gui import win32api @@ -83,16 +105,16 @@ if (config.auth_force_edmc_protocol # noqa: C901 """ _fields_ = [ - ('style', UINT), - ('lpfnWndProc', WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM)), - ('cbClsExtra', INT), - ('cbWndExtra', INT), - ('hInstance', HINSTANCE), - ('hIcon', HICON), - ('hCursor', c_void_p), - ('hbrBackground', HBRUSH), - ('lpszMenuName', LPCWSTR), - ('lpszClassName', LPCWSTR) + ("style", UINT), + ("lpfnWndProc", WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM)), + ("cbClsExtra", INT), + ("cbWndExtra", INT), + ("hInstance", HINSTANCE), + ("hIcon", HICON), + ("hCursor", c_void_p), + ("hbrBackground", HBRUSH), + ("lpszMenuName", LPCWSTR), + ("lpszClassName", LPCWSTR), ] CW_USEDEFAULT = 0x80000000 @@ -140,52 +162,58 @@ if (config.auth_force_edmc_protocol # noqa: C901 # Windows Message handler stuff (IPC) # https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633573(v=vs.85) @WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM) - def WndProc(hwnd: HWND, message: UINT, wParam: WPARAM, lParam: LPARAM) -> c_long: # noqa: N803 N802 + def WndProc(hwnd: HWND, message: UINT, wParam: WPARAM, lParam: LPARAM) -> int: # noqa: N803 N802 """ - Deal with DDE requests. + Windows message procedure for handling DDE requests. - :param hwnd: Window handle - :param message: The message being sent - :param wParam: Additional Message Information (depends on message type) - :param lParam: Also additional message information - :return: ??? + This routine responds to WM_DDE_INITIATE messages for the application's + DDE service (the app name) and topic ("System"). It sends an ACK if the + incoming atoms match the expected values. + + Returns int due to Python 3.11+ CTYPES Callback - decorator changes to c_long """ - if message != WM_DDE_INITIATE: - # Not a DDE init message, bail and tell windows to do the default - # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-defwindowproca?redirectedfrom=MSDN - return win32gui.DefWindowProc(hwnd, message, wParam, lParam) + try: + if message != WM_DDE_INITIATE: + # Let Windows handle all other messages normally + return win32gui.DefWindowProc(hwnd, message, wParam, lParam) - service = create_unicode_buffer(256) - topic = create_unicode_buffer(256) - # Note that lParam is 32 bits, and broken into two 16 bit words. This will break on 64bit as the math is - # wrong - # if nonzero, the target application for which a conversation is requested - lparam_low = lParam & 0xFFFF # type: ignore - # if nonzero, the topic of said conversation - lparam_high = lParam >> 16 # type: ignore + # --- Decode the 64-bit lParam into LOWORD/HIWORD safely --- + # These helper functions handle pointer-sized values correctly on 64-bit builds + lparam_low = win32api.LOWORD(lParam) + lparam_high = win32api.HIWORD(lParam) - # if either of the words are nonzero, they contain - # atoms https://docs.microsoft.com/en-us/windows/win32/dataxchg/about-atom-tables - # which we can read out as shown below, and then compare. + service = create_unicode_buffer(256) + topic = create_unicode_buffer(256) - target_is_valid = lparam_low == 0 or ( - GlobalGetAtomNameW(lparam_low, service, 256) and service.value == appname - ) - - topic_is_valid = lparam_high == 0 or ( - GlobalGetAtomNameW(lparam_high, topic, 256) and topic.value.lower() == 'system' - ) - - if target_is_valid and topic_is_valid: - # if everything is happy, send an acknowledgement of the DDE request - SendMessageW( - wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, GlobalAddAtomW(appname), GlobalAddAtomW('System')) + # --- Retrieve atom names --- + target_is_valid = lparam_low == 0 or ( + GlobalGetAtomNameW(lparam_low, service, 256) + and service.value == appname ) - # It works as a constructor as per - return c_long(0) + topic_is_valid = lparam_high == 0 or ( + GlobalGetAtomNameW(lparam_high, topic, 256) + and topic.value.lower() == "system" + ) - return c_long(1) # This is an utter guess -Ath + if target_is_valid and topic_is_valid: + # Send acknowledgment of successful DDE handshake + SendMessageW( + wParam, + WM_DDE_ACK, + hwnd, + PackDDElParam( + WM_DDE_ACK, GlobalAddAtomW(appname), GlobalAddAtomW("System") + ), + ) + return 0 # Success + + return 1 # Not handled / invalid DDE initiation + + except Exception as e: + # Log unexpected exceptions but don’t crash the message loop + logger.error(f"WndProc error: {e}", exc_info=True) + return win32gui.DefWindowProc(hwnd, message, wParam, lParam) class WindowsProtocolHandler(GenericProtocolHandler): """ @@ -199,10 +227,10 @@ if (config.auth_force_edmc_protocol # noqa: C901 super().__init__() self.thread: threading.Thread | None = None - def start(self, master: 'tkinter.Tk') -> None: + def start(self, master: tkinter.Tk) -> None: """Start the DDE thread.""" super().start(master) - self.thread = threading.Thread(target=self.worker, name='DDE worker') + self.thread = threading.Thread(target=self.worker, name="DDE worker") self.thread.daemon = True self.thread.start() @@ -226,41 +254,33 @@ if (config.auth_force_edmc_protocol # noqa: C901 wndclass.hCursor = None wndclass.hbrBackground = None wndclass.lpszMenuName = None - wndclass.lpszClassName = 'DDEServer' + wndclass.lpszClassName = "DDEServer" if not RegisterClassW(byref(wndclass)): - print('Failed to register Dynamic Data Exchange for cAPI') + print("Failed to register Dynamic Data Exchange for cAPI") return # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw hwnd = win32gui.CreateWindowEx( - 0, # dwExStyle - 'DDEServer', # lpClassName (use string directly as win32gui expects it) - "DDE Server", # lpWindowName - 0, # dwStyle - win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, - win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, # X, Y, nWidth, nHeight + 0, # dwExStyle + "DDEServer", # lpClassName (use string directly as win32gui expects it) + "DDE Server", # lpWindowName + 0, # dwStyle + win32con.CW_USEDEFAULT, + win32con.CW_USEDEFAULT, + win32con.CW_USEDEFAULT, + win32con.CW_USEDEFAULT, # X, Y, nWidth, nHeight self.master.winfo_id(), # hWndParent - 0, # hMenu (use 0 instead of None for win32gui) - wndclass.hInstance, # hInstance - None # lpParam + 0, # hMenu (use 0 instead of None for win32gui) + wndclass.hInstance, # hInstance + None, # lpParam ) msg = MSG() - # Calls GetMessageW: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagew - # Something is off with the return from this, meaning you'll see: - # Exception ignored on converting result of ctypes callback function: - # Traceback (most recent call last): - # File "C:\Users\Athan\Documents\Devel\EDMarketConnector\protocol.py", line 323, in worker - # while int(GetMessageW(byref(msg), None, 0, 0)) != 0: - # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - # TypeError: 'c_long' object cannot be interpreted as an integer - # - # But it does actually work. Either getting a non-0 value and - # entering the loop, or getting 0 and exiting it. while GetMessageW(byref(msg), None, 0, 0) != 0: - logger.trace_if('frontier-auth.windows', f'DDE message of type: {msg.message}') + logger.trace_if( + "frontier-auth.windows", f"DDE message of type: {msg.message}" + ) if msg.message == WM_DDE_EXECUTE: # GlobalLock does some sort of "please dont move this?" # https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globallock @@ -268,28 +288,41 @@ if (config.auth_force_edmc_protocol # noqa: C901 GlobalUnlock(msg.lParam) # Unlocks the GlobalLock-ed object if args.lower().startswith('open("') and args.endswith('")'): - logger.trace_if('frontier-auth.windows', f'args are: {args}') + logger.trace_if("frontier-auth.windows", f"args are: {args}") url = parse.unquote(args[6:-2]).strip() if url.startswith(self.redirect): - logger.debug(f'Message starts with {self.redirect}') + logger.debug(f"Message starts with {self.redirect}") self.event(url) # Send back a WM_DDE_ACK. this is _required_ with WM_DDE_EXECUTE - win32gui.SetForegroundWindow(win32gui.GetParent(self.master.winfo_id())) - PostMessageW(msg.wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, 0x80, msg.lParam)) + win32gui.SetForegroundWindow( + win32gui.GetParent(self.master.winfo_id()) + ) + PostMessageW( + msg.wParam, + WM_DDE_ACK, + hwnd, + PackDDElParam(WM_DDE_ACK, 0x80, msg.lParam), + ) else: # Send back a WM_DDE_ACK. this is _required_ with WM_DDE_EXECUTE - PostMessageW(msg.wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, 0, msg.lParam)) + PostMessageW( + msg.wParam, + WM_DDE_ACK, + hwnd, + PackDDElParam(WM_DDE_ACK, 0, msg.lParam), + ) elif msg.message == WM_DDE_TERMINATE: win32gui.PostMessage(msg.wParam, WM_DDE_TERMINATE, hwnd, 0) else: - TranslateMessage(byref(msg)) # "Translates virtual key messages into character messages" ??? + TranslateMessage( + byref(msg) + ) # "Translates virtual key messages into character messages" ??? DispatchMessageW(byref(msg)) - else: # Linux / Run from source from http.server import BaseHTTPRequestHandler, HTTPServer @@ -303,17 +336,17 @@ else: # Linux / Run from source def __init__(self) -> None: super().__init__() - self.httpd = HTTPServer(('localhost', 0), HTTPRequestHandler) - self.redirect = f'http://localhost:{self.httpd.server_port}/auth' + self.httpd = HTTPServer(("localhost", 0), HTTPRequestHandler) + self.redirect = f"http://localhost:{self.httpd.server_port}/auth" if not os.getenv("EDMC_NO_UI"): - logger.info(f'Web server listening on {self.redirect}') + logger.info(f"Web server listening on {self.redirect}") self.thread: threading.Thread | None = None - def start(self, master: 'tkinter.Tk') -> None: + def start(self, master: tkinter.Tk) -> None: """Start the HTTP server thread.""" GenericProtocolHandler.start(self, master) - self.thread = threading.Thread(target=self.worker, name='OAuth worker') + self.thread = threading.Thread(target=self.worker, name="OAuth worker") self.thread.daemon = True self.thread.start() @@ -321,20 +354,20 @@ else: # Linux / Run from source """Shutdown the HTTP server thread.""" thread = self.thread if thread: - logger.debug('Thread') + logger.debug("Thread") self.thread = None if self.httpd: - logger.info('Shutting down httpd') + logger.info("Shutting down httpd") self.httpd.shutdown() - logger.info('Joining thread') + logger.info("Joining thread") thread.join() # Wait for it to quit else: - logger.debug('No thread') + logger.debug("No thread") - logger.debug('Done.') + logger.debug("Done.") def worker(self) -> None: """HTTP Worker.""" @@ -346,10 +379,12 @@ else: # Linux / Run from source def parse(self) -> bool: """Parse a request.""" - logger.trace_if('frontier-auth.http', f'Got message on path: {self.path}') + logger.trace_if("frontier-auth.http", f"Got message on path: {self.path}") url = parse.unquote(self.path) - if url.startswith('/auth'): - logger.debug('Request starts with /auth, sending to protocolhandler.event()') + if url.startswith("/auth"): + logger.debug( + "Request starts with /auth, sending to protocolhandler.event()" + ) protocolhandler.event(url) self.send_response(200) return True @@ -364,14 +399,14 @@ else: # Linux / Run from source def do_GET(self) -> None: # noqa: N802 # Required to override """Handle GET Request.""" if self.parse(): - self.send_header('Content-Type', 'text/html') + self.send_header("Content-Type", "text/html") self.end_headers() self.wfile.write(self._generate_auth_response().encode()) else: self.send_response(404) self.end_headers() - def log_request(self, code: int | str = '-', size: int | str = '-') -> None: + def log_request(self, code: int | str = "-", size: int | str = "-") -> None: """Override to prevent logging.""" def _generate_auth_response(self) -> str: @@ -381,33 +416,34 @@ else: # Linux / Run from source :return: The HTML content of the authentication response. """ return ( - '' - '' - 'Authentication successful - Elite: Dangerous' - '' - '' - '' - '

Authentication successful

' - '

Thank you for authenticating.

' - '

Please close this browser tab now.

' - '' - '' + "h1 { text-align: center; margin-top: 100px; }" + "p { text-align: center; }" + "" + "" + "" + "

Authentication successful

" + "

Thank you for authenticating.

" + "

Please close this browser tab now.

" + "" + "" ) -def get_handler_impl() -> Type[GenericProtocolHandler]: +def get_handler_impl() -> type[GenericProtocolHandler]: """ Get the appropriate GenericProtocolHandler for the current system and config. :return: An instantiatable GenericProtocolHandler """ - if ( - (sys.platform == 'win32' and config.auth_force_edmc_protocol) - or (getattr(sys, 'frozen', False) and not is_wine and not config.auth_force_localserver) + if (sys.platform == "win32" and config.auth_force_edmc_protocol) or ( + getattr(sys, "frozen", False) + and not is_wine + and not config.auth_force_localserver ): return WindowsProtocolHandler @@ -415,4 +451,4 @@ def get_handler_impl() -> Type[GenericProtocolHandler]: # *late init* singleton -protocolhandler: GenericProtocolHandler +protocolhandler: GenericProtocolHandler = None # type: ignore diff --git a/requirements-dev.txt b/requirements-dev.txt index ef9c84c0..fba501f3 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,39 +4,38 @@ setuptools==80.9.0 # Static analysis tools -flake8==7.2.0 +flake8==7.3.0 flake8-annotations-coverage==0.0.6 flake8-cognitive-complexity==0.1.0 -flake8-comprehensions==3.16.0 +flake8-comprehensions==3.17.0 flake8-docstrings==1.7.0 flake8-json==24.4.0 flake8-noqa==1.4.0 -flake8-polyfill==1.0.2 flake8-use-fstring==1.4 -mypy==1.18.2 +mypy==1.19.0 pep8-naming==0.15.1 -safety==3.6.2 -types-requests==2.32.0.20250328 -types-setuptools==80.9.0.20250529 +safety==3.7.0 +types-requests==2.32.4.20250913 +types-setuptools==80.9.0.20250822 # Code formatting tools autopep8==2.3.2 # Git pre-commit checking -pre-commit==4.2.0 +pre-commit==4.5.0 # HTML changelogs -mistune==3.1.3 +mistune==3.1.4 # Packaging # We only need py2exe on windows. -py2exe==0.13.0.2; sys_platform == 'win32' +py2exe==0.14.0.0; sys_platform == 'win32' # Testing -pytest==8.4.2 -pytest-cov==6.1.1 # Pytest code coverage support -coverage[toml]==7.11.0 # pytest-cov dep. This is here to ensure that it includes TOML support for pyproject.toml configs +pytest==9.0.1 +pytest-cov==7.0.0 # Pytest code coverage support +coverage[toml]==7.12.0 # pytest-cov dep. This is here to ensure that it includes TOML support for pyproject.toml configs coverage-conditional-plugin==0.9.0 diff --git a/requirements.txt b/requirements.txt index 05a05733..32001a17 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,5 +4,6 @@ watchdog==6.0.0 simplesystray==0.1.0; sys_platform == 'win32' semantic-version==2.10.0 # For manipulating folder permissions and the like. -pywin32==310; sys_platform == 'win32' -psutil==7.1.0 +pywin32==311; sys_platform == 'win32' +psutil==7.1.3 +tomli-w==1.2.0 diff --git a/scripts/build_changelog.py b/scripts/build_changelog.py index 42157947..3406ed50 100644 --- a/scripts/build_changelog.py +++ b/scripts/build_changelog.py @@ -43,7 +43,7 @@ def build_html(md_changelog: str, version: str) -> str: """Convert markdown changelog to HTML.""" html_out = f"

Release {version}

\n" html_out += mistune.html(md_changelog) # type: ignore - html_out = re.sub(r"h1", "h2", html_out) + "\n
" + html_out = re.sub(r"h1", "h2", html_out) + "
" with open("script_output/html_changelog.txt", "w", encoding="utf-8") as html_file: html_file.write(html_out) @@ -75,6 +75,8 @@ def build_fdev( f"[HEADING=2][URL='{gh_link}'][SIZE=7]Release {version}[/SIZE][/URL][/HEADING]\n" f"[URL='{vt_signed}']Pre-emptive upload to VirusTotal[/URL]. " f"([URL='{vt_unsigned}']Unsigned Installer[/URL])\n\n" + f"Please note: EDMC now distributes 64-bit builds by default. 32-bit (x86) builds " + f"are still available for download on GitHub.\n\n" ) if version.startswith(("Pre-Release", "Beta")): @@ -118,7 +120,7 @@ You can also view the Elite: Dangerous Forum thread [HERE](https://forums.fronti [Unsigned Installer](""" - reddit_mid_2 = """) if you don't want to use the code-signed option. + reddit_mid_2 = """) if you don't want to use the code-signed option. Please note: EDMC now distributes 64-bit builds by default. 32-bit (x86) builds are still available for download on GitHub. ~~----------------------------------------------------~~ """ diff --git a/scripts/find_localised_strings.py b/scripts/find_localised_strings.py index 19a5efb6..02f928e3 100644 --- a/scripts/find_localised_strings.py +++ b/scripts/find_localised_strings.py @@ -188,7 +188,7 @@ class FileLocation: line_end_col: int | None @staticmethod - def from_call(path: pathlib.Path, c: ast.Call) -> "FileLocation": + def from_call(path: pathlib.Path, c: ast.Call) -> FileLocation: """ Create a FileLocation from a Call and Path. diff --git a/stats.py b/stats.py index 4c4b0e3d..67fc9379 100644 --- a/stats.py +++ b/stats.py @@ -12,7 +12,8 @@ import json import sys import tkinter as tk from tkinter import ttk -from typing import Any, AnyStr, Callable, NamedTuple, Sequence, cast +from typing import Any, AnyStr, NamedTuple, cast +from collections.abc import Callable, Sequence import companion import EDMCLogging import myNotebook as nb # noqa: N813 @@ -457,7 +458,7 @@ class StatsResults(tk.Toplevel): # label = HyperlinkLabel(parent, text=col_content, popup_copy=True) label = nb.Label(parent, text=col_content) if with_copy: - label.bind('', self.copy_callback(label, col_content)) + label.bind('', self.copy_callback(label, col_content)) # type: ignore if i == 0: label.grid(padx=10, sticky=tk.W) diff --git a/td.py b/td.py index 484e8d29..a5f0d6cd 100644 --- a/td.py +++ b/td.py @@ -30,18 +30,18 @@ def export(data: CAPIData) -> None: # required with open(data_path / data_filename, 'wb') as h: # Format described here: https://github.com/eyeonus/Trade-Dangerous/wiki/Price-Data - h.write('#! trade.py import -\n'.encode('utf-8')) + h.write(b'#! trade.py import -\n') this_platform = system() cmdr_name = data['commander']['name'].strip() h.write( - f'# Created by {applongname} {appversion()} on {this_platform} for Cmdr {cmdr_name}.\n'.encode('utf-8') + f'# Created by {applongname} {appversion()} on {this_platform} for Cmdr {cmdr_name}.\n'.encode() ) h.write( - '#\n# \n\n'.encode('utf-8') + b'#\n# \n\n' ) system_name = data['lastSystem']['name'].strip() starport_name = data['lastStarport']['name'].strip() - h.write(f'@ {system_name}/{starport_name}\n'.encode('utf-8')) + h.write(f'@ {system_name}/{starport_name}\n'.encode()) # sort commodities by category by_category = defaultdict(list) @@ -50,7 +50,7 @@ def export(data: CAPIData) -> None: timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%SZ')) for category in sorted(by_category): - h.write(f' + {format(category)}\n'.encode('utf-8')) + h.write(f' + {format(category)}\n'.encode()) # corrections to commodity names can change the sort order for commodity in sorted(by_category[category], key=itemgetter('name')): h.write( @@ -61,5 +61,5 @@ def export(data: CAPIData) -> None: f"{demandbracketmap[commodity['demandBracket']]:1}" f" {int(commodity['stock']) if commodity['stockBracket'] else '':8}" f"{stockbracketmap[commodity['stockBracket']]:1}" - f" {timestamp}\n".encode('utf-8') + f" {timestamp}\n".encode() ) diff --git a/tests/config/_old_config.py b/tests/config/_old_config.py deleted file mode 100644 index 2b5244b6..00000000 --- a/tests/config/_old_config.py +++ /dev/null @@ -1,407 +0,0 @@ -"""Old Configuration Test File.""" -from __future__ import annotations - -import numbers -import sys -import warnings -from configparser import NoOptionError -from os import getenv, makedirs, mkdir -from os.path import dirname, expanduser, isdir, join -from typing import TYPE_CHECKING -from config import applongname, appname, update_interval -from EDMCLogging import get_main_logger - -logger = get_main_logger() - -if sys.platform == 'win32': - import ctypes - import uuid - from ctypes.wintypes import DWORD, HANDLE, HKEY, LONG, LPCVOID, LPCWSTR - if TYPE_CHECKING: - import ctypes.windll # type: ignore - - FOLDERID_Documents = uuid.UUID('{FDD39AD0-238F-46AF-ADB4-6C85480369C7}') - FOLDERID_LocalAppData = uuid.UUID('{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}') - FOLDERID_Profile = uuid.UUID('{5E6C858F-0E22-4760-9AFE-EA3317B67173}') - FOLDERID_SavedGames = uuid.UUID('{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}') - - SHGetKnownFolderPath = ctypes.windll.shell32.SHGetKnownFolderPath - SHGetKnownFolderPath.argtypes = [ctypes.c_char_p, DWORD, HANDLE, ctypes.POINTER(ctypes.c_wchar_p)] - - CoTaskMemFree = ctypes.windll.ole32.CoTaskMemFree - CoTaskMemFree.argtypes = [ctypes.c_void_p] - - # winreg in Python <= 3.7.4 handles REG_MULTI_SZ incorrectly, so do this instead. https://bugs.python.org/issue32587 - HKEY_CURRENT_USER = 0x80000001 - KEY_ALL_ACCESS = 0x000F003F - REG_CREATED_NEW_KEY = 0x00000001 - REG_OPENED_EXISTING_KEY = 0x00000002 - REG_SZ = 1 - REG_DWORD = 4 - REG_MULTI_SZ = 7 - - REG_RESERVED_ALWAYS_ZERO = 0 - - RegCreateKeyEx = ctypes.windll.advapi32.RegCreateKeyExW - RegCreateKeyEx.restype = LONG - RegCreateKeyEx.argtypes = [ - HKEY, LPCWSTR, DWORD, LPCVOID, DWORD, DWORD, LPCVOID, ctypes.POINTER(HKEY), ctypes.POINTER(DWORD) - ] - - RegOpenKeyEx = ctypes.windll.advapi32.RegOpenKeyExW - RegOpenKeyEx.restype = LONG - RegOpenKeyEx.argtypes = [HKEY, LPCWSTR, DWORD, DWORD, ctypes.POINTER(HKEY)] - - RegCloseKey = ctypes.windll.advapi32.RegCloseKey - RegCloseKey.restype = LONG - RegCloseKey.argtypes = [HKEY] - - RegQueryValueEx = ctypes.windll.advapi32.RegQueryValueExW - RegQueryValueEx.restype = LONG - RegQueryValueEx.argtypes = [HKEY, LPCWSTR, LPCVOID, ctypes.POINTER(DWORD), LPCVOID, ctypes.POINTER(DWORD)] - - RegSetValueEx = ctypes.windll.advapi32.RegSetValueExW - RegSetValueEx.restype = LONG - RegSetValueEx.argtypes = [HKEY, LPCWSTR, LPCVOID, DWORD, LPCVOID, DWORD] - - RegCopyTree = ctypes.windll.advapi32.RegCopyTreeW - RegCopyTree.restype = LONG - RegCopyTree.argtypes = [HKEY, LPCWSTR, HKEY] - - RegDeleteKey = ctypes.windll.advapi32.RegDeleteTreeW - RegDeleteKey.restype = LONG - RegDeleteKey.argtypes = [HKEY, LPCWSTR] - - RegDeleteValue = ctypes.windll.advapi32.RegDeleteValueW - RegDeleteValue.restype = LONG - RegDeleteValue.argtypes = [HKEY, LPCWSTR] - - def known_folder_path(guid: uuid.UUID) -> str | None: - """Look up a Windows GUID to actual folder path name.""" - buf = ctypes.c_wchar_p() - if SHGetKnownFolderPath(ctypes.create_string_buffer(guid.bytes_le), 0, 0, ctypes.byref(buf)): - return None - retval = buf.value # copy data - CoTaskMemFree(buf) # and free original - return retval - -elif sys.platform == 'linux': - import codecs - from configparser import RawConfigParser - - -class OldConfig: - """Object that holds all configuration data.""" - - OUT_EDDN_SEND_STATION_DATA = 1 - # OUT_MKT_BPC = 2 # No longer supported - OUT_MKT_TD = 4 - OUT_MKT_CSV = 8 - OUT_SHIP = 16 - # OUT_SHIP_EDS = 16 # Replaced by OUT_SHIP - # OUT_SYS_FILE = 32 # No longer supported - # OUT_STAT = 64 # No longer available - # OUT_SHIP_CORIOLIS = 128 # Replaced by OUT_SHIP - # OUT_SYS_EDSM = 256 # Now a plugin - # OUT_SYS_AUTO = 512 # Now always automatic - OUT_MKT_MANUAL = 1024 - OUT_EDDN_SEND_NON_STATION = 2048 - OUT_EDDN_DELAY = 4096 - OUT_STATION_ANY = OUT_EDDN_SEND_STATION_DATA | OUT_MKT_TD | OUT_MKT_CSV - - if sys.platform == 'win32': # noqa: C901 - - def __init__(self): - self.app_dir = join(known_folder_path(FOLDERID_LocalAppData), appname) # type: ignore - if not isdir(self.app_dir): - mkdir(self.app_dir) - - self.plugin_dir = join(self.app_dir, 'plugins') - if not isdir(self.plugin_dir): - mkdir(self.plugin_dir) - - if getattr(sys, 'frozen', False): - self.internal_plugin_dir = join(dirname(sys.executable), 'plugins') - self.respath = dirname(sys.executable) - - else: - self.internal_plugin_dir = join(dirname(__file__), 'plugins') - self.respath = dirname(__file__) - - # expanduser in Python 2 on Windows doesn't handle non-ASCII - http://bugs.python.org/issue13207 - self.home = known_folder_path(FOLDERID_Profile) or r'\\' - - journaldir = known_folder_path(FOLDERID_SavedGames) - if journaldir: - self.default_journal_dir: str | None = join(journaldir, 'Frontier Developments', 'Elite Dangerous') - - else: - self.default_journal_dir = None - - self.identifier = applongname - self.hkey: ctypes.c_void_p | None = HKEY() - disposition = DWORD() - if RegCreateKeyEx( - HKEY_CURRENT_USER, - r'Software\Marginal\EDMarketConnector', - 0, - None, - 0, - KEY_ALL_ACCESS, - None, - ctypes.byref(self.hkey), - ctypes.byref(disposition) - ): - raise Exception() - - # set WinSparkle defaults - https://github.com/vslavik/winsparkle/wiki/Registry-Settings - edcdhkey = HKEY() - if RegCreateKeyEx( - HKEY_CURRENT_USER, - r'Software\EDCD\EDMarketConnector', - 0, - None, - 0, - KEY_ALL_ACCESS, - None, - ctypes.byref(edcdhkey), - ctypes.byref(disposition) - ): - raise Exception() - - sparklekey = HKEY() - if not RegCreateKeyEx( - edcdhkey, - 'WinSparkle', - 0, - None, - 0, - KEY_ALL_ACCESS, - None, - ctypes.byref(sparklekey), - ctypes.byref(disposition) - ): - if disposition.value == REG_CREATED_NEW_KEY: - buf = ctypes.create_unicode_buffer('1') - RegSetValueEx(sparklekey, 'CheckForUpdates', 0, 1, buf, len(buf) * 2) - - buf = ctypes.create_unicode_buffer(str(update_interval)) - RegSetValueEx(sparklekey, 'UpdateInterval', 0, 1, buf, len(buf) * 2) - RegCloseKey(sparklekey) - - if not self.get('outdir') or not isdir(self.get('outdir')): # type: ignore - self.set('outdir', known_folder_path(FOLDERID_Documents) or self.home) - - def get(self, key: str, default: None | list | str = None) -> None | list | str: - """Look up a string configuration value.""" - key_type = DWORD() - key_size = DWORD() - # Only strings are handled here. - if ( - RegQueryValueEx( - self.hkey, - key, - 0, - ctypes.byref(key_type), - None, - ctypes.byref(key_size) - ) - or key_type.value not in (REG_SZ, REG_MULTI_SZ) - ): - return default - - buf = ctypes.create_unicode_buffer(int(key_size.value / 2)) - if RegQueryValueEx(self.hkey, key, 0, ctypes.byref(key_type), buf, ctypes.byref(key_size)): - return default - - if key_type.value == REG_MULTI_SZ: - return list(ctypes.wstring_at(buf, len(buf)-2).split('\x00')) - - return str(buf.value) - - def getint(self, key: str, default: int = 0) -> int: - """Look up an integer configuration value.""" - key_type = DWORD() - key_size = DWORD(4) - key_val = DWORD() - if ( - RegQueryValueEx( - self.hkey, - key, - 0, - ctypes.byref(key_type), - ctypes.byref(key_val), - ctypes.byref(key_size) - ) - or key_type.value != REG_DWORD - ): - return default - - return key_val.value - - def set(self, key: str, val: int | str | list) -> None: - """Set value on the specified configuration key.""" - if isinstance(val, str): - buf = ctypes.create_unicode_buffer(val) - RegSetValueEx(self.hkey, key, 0, REG_SZ, buf, len(buf)*2) - - elif isinstance(val, numbers.Integral): - RegSetValueEx(self.hkey, key, 0, REG_DWORD, ctypes.byref(DWORD(val)), 4) - - elif isinstance(val, list): - # null terminated non-empty strings - string_val = '\x00'.join([str(x) or ' ' for x in val] + ['']) - buf = ctypes.create_unicode_buffer(string_val) - RegSetValueEx(self.hkey, key, 0, REG_MULTI_SZ, buf, len(buf)*2) - - else: - raise NotImplementedError() - - def delete(self, key: str) -> None: - """Delete the specified configuration key.""" - RegDeleteValue(self.hkey, key) - - def save(self) -> None: - """Save current configuration to disk.""" - pass # Redundant since registry keys are written immediately - - def close(self) -> None: - """Close the configuration.""" - RegCloseKey(self.hkey) - self.hkey = None - - elif sys.platform == 'linux': - SECTION = 'config' - - def __init__(self): - - # http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html - self.app_dir = join(getenv('XDG_DATA_HOME', expanduser('~/.local/share')), appname) - if not isdir(self.app_dir): - makedirs(self.app_dir) - - self.plugin_dir = join(self.app_dir, 'plugins') - if not isdir(self.plugin_dir): - mkdir(self.plugin_dir) - - self.internal_plugin_dir = join(dirname(__file__), 'plugins') - self.default_journal_dir: str | None = None - self.home = expanduser('~') - self.respath = dirname(__file__) - self.identifier = f'uk.org.marginal.{appname.lower()}' - - self.filename = join(getenv('XDG_CONFIG_HOME', expanduser('~/.config')), appname, f'{appname}.ini') - if not isdir(dirname(self.filename)): - makedirs(dirname(self.filename)) - - self.config = RawConfigParser(comment_prefixes=('#',)) - try: - with codecs.open(self.filename) as h: - self.config.read_file(h) - - except Exception as e: - logger.debug('Reading config failed, assuming we\'re making a new one...', exc_info=e) - self.config.add_section(self.SECTION) - - if not self.get('outdir') or not isdir(self.get('outdir')): # type: ignore # Not going to change - self.set('outdir', expanduser('~')) - - def get(self, key: str, default: None | list | str = None) -> None | list | str: - """Look up a string configuration value.""" - try: - val = self.config.get(self.SECTION, key) - if '\n' in val: # list - # ConfigParser drops the last entry if blank, - # so we add a spurious ';' entry in set() and remove it here - assert val.split('\n')[-1] == ';', val.split('\n') - return [self._unescape(x) for x in val.split('\n')[:-1]] - return self._unescape(val) - - except NoOptionError: - logger.debug(f'attempted to get key {key} that does not exist') - return default - - except Exception as e: - logger.debug('And the exception type is...', exc_info=e) - return default - - def getint(self, key: str, default: int = 0) -> int: - """Look up an integer configuration value.""" - try: - return self.config.getint(self.SECTION, key) - - except ValueError as e: - logger.error(f"Failed to int({key=})", exc_info=e) - - except NoOptionError: - logger.debug(f'attempted to get key {key} that does not exist') - - except Exception: - logger.exception(f'unexpected exception while attempting to access {key}') - - return default - - def set(self, key: str, val: int | str | list) -> None: - """Set value on the specified configuration key.""" - if isinstance(val, bool): - self.config.set(self.SECTION, key, val and '1' or '0') - - elif isinstance(val, (numbers.Integral, str)): - self.config.set(self.SECTION, key, self._escape(val)) - - elif isinstance(val, list): - self.config.set(self.SECTION, key, '\n'.join([self._escape(x) for x in val] + [';'])) - - else: - raise NotImplementedError() - - def delete(self, key: str) -> None: - """Delete the specified configuration key.""" - self.config.remove_option(self.SECTION, key) - - def save(self) -> None: - """Save current configuration to disk.""" - with codecs.open(self.filename, 'w', 'utf-8') as h: - self.config.write(h) - - def close(self) -> None: - """Close the configuration.""" - self.save() - self.config = None # type: ignore - - def _escape(self, val: str) -> str: - """Escape a string for storage.""" - return str(val).replace('\\', '\\\\').replace('\n', '\\n').replace(';', '\\;') - - def _unescape(self, val: str) -> str: - """Un-escape a string from storage.""" - chars = list(val) - i = 0 - while i < len(chars): - if chars[i] == '\\': - chars.pop(i) - if chars[i] == 'n': - chars[i] = '\n' - i += 1 - return ''.join(chars) - - else: - def __init__(self): - raise NotImplementedError('Implement me') - - # Common - - def get_password(self, account: str) -> None: - """Legacy password retrieval.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) - - def set_password(self, account: str, password: str) -> None: - """Legacy password setting.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) - - def delete_password(self, account: str) -> None: - """Legacy password deletion.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) - - -old_config = OldConfig() diff --git a/tests/config/test_config.py b/tests/config/test_config.py deleted file mode 100644 index e839afc7..00000000 --- a/tests/config/test_config.py +++ /dev/null @@ -1,246 +0,0 @@ -""" -Test the config system. - -Note: These tests to arbitrary reads and writes to an existing config, including -key deletions. Said modifications are to keys that are generated internally. - -Most of these tests are parity tests with the "old" config, and likely one day can be -entirely removed. -""" -import contextlib -import itertools -import pathlib -import random -import string -import sys -from typing import Any, Iterable, cast - -import pytest -from pytest import mark - -# isort: split - -sys.path += [str(pathlib.Path(__file__).parent.parent.resolve()), "."] -print(sys.path) - -from _old_config import old_config # noqa: E402 - -from config import config # noqa: E402 - - -def _fuzz_list(length: int) -> list[str]: - out = [] - for _ in range(length): - out.append(_fuzz_generators[str](random.randint(0, 1337))) - - return cast(list[str], out) - - -_fuzz_generators = { # Type annotating this would be a nightmare. - int: lambda i: random.randint(min(0, i), max(0, i)), - # This doesn't cover unicode, or random bytes. Use them at your own peril - str: lambda s: "".join(random.choice(string.ascii_letters + string.digits + '\r\n') for _ in range(s)), - bool: lambda _: bool(random.choice((True, False))), - list: _fuzz_list, -} - - -def _get_fuzz(_type: Any, num_values=50, value_length=(0, 10)) -> list: - return [_fuzz_generators[_type](random.randint(*value_length)) for _ in range(num_values)] - - -int_tests = [0, 1, 2, 3, (1 << 32)-1, -1337] - -string_tests = [ - "test", "", "this\nis\na\ntest", "orange sidewinder", "needs \\ more backslashes\\", "\\; \\n", r"\\\\ \\\\; \\\\n", - r"entry with escapes \\ \\; \\n" -] - -list_tests = [ - ["test"], - ["multiple", "entries"], - ["multiple", "entries", "with", "", "blanks"], - ["entry", "that ends", "in a", ""], - ["entry with \n", "newlines\nin", "weird\nplaces"], - [r"entry with escapes \\ \\; \\n"], - [r"\\\\ \\\\; \\\\n"] -] -bool_tests = [True, False] - -big_int = int(0xFFFFFFFF) # 32 bit int - - -def _make_params(args: list[Any], id_name: str = 'random_test_{i}') -> list: - return [pytest.param(x, id=id_name.format(i=i)) for i, x in enumerate(args)] - - -def _build_test_list(static_data, random_data, random_id_name='random_test_{i}') -> Iterable: - return itertools.chain(static_data, _make_params(random_data, id_name=random_id_name)) - - -class TestNewConfig: - """Test the new config with an array of hand-picked and random data.""" - - def __update_linuxconfig(self) -> None: - """On linux config uses ConfigParser, which doesn't update from disk changes. Force the update here.""" - if sys.platform != 'linux': - return - - from config.linux import LinuxConfig # type: ignore - - if isinstance(config, LinuxConfig) and config.config is not None: - config.config.read(config.filename) - - @mark.parametrize("i", _build_test_list(int_tests, _get_fuzz(int, value_length=(-big_int, big_int)))) - def test_ints(self, i: int) -> None: - """Save int and then unpack it again.""" - if sys.platform == 'win32': - i = abs(i) - - name = f"int_test_{i}" - config.set(name, i) - config.save() - self.__update_linuxconfig() - assert i == config.get_int(name) - config.delete(name) - - @mark.parametrize("string", _build_test_list(string_tests, _get_fuzz(str, value_length=(0, 512)))) - def test_string(self, string: str) -> None: - """Save a string and then ask for it back.""" - name = f'str_test_{hash(string)}' - config.set(name, string) - config.save() - self.__update_linuxconfig() - assert string == config.get_str(name) - config.delete(name) - - @mark.parametrize("lst", _build_test_list(list_tests, _get_fuzz(list))) - def test_list(self, lst: list[str]) -> None: - """Save a list and then ask for it back.""" - name = f'list_test_{ hash("".join(lst)) }' - config.set(name, lst) - - config.save() - self.__update_linuxconfig() - - assert lst == config.get_list(name) - - config.delete(name) - - @mark.parametrize('b', bool_tests) - def test_bool(self, b: bool) -> None: - """Save a bool and ask for it back.""" - name = str(b) - config.set(name, b) - config.save() - self.__update_linuxconfig() - assert b == config.get_bool(name) - config.delete(name) - - def test_get_no_error(self) -> None: - """Regression test to ensure that get() doesn't throw a TypeError.""" - name = 'test-get' - config.set(name, '1337') - config.save() - self.__update_linuxconfig() - with pytest.deprecated_call(): - res = config.get(name) - - assert res == '1337' - config.delete(name) - config.save() - - -class TestOldNewConfig: - """Tests going through the old config and out the new config.""" - - KEY_PREFIX = 'oldnew_' - - def teardown_method(self) -> None: - """ - Teardown for all config tests to save out configs. - - It is expected that tests have cleaned up whatever keys they have aded. - This is here to ensure that a save out happens for configs that need it. - """ - old_config.save() - config.save() - - def cleanup_entry(self, entry: str) -> None: - """Remove the given key, on both sides if on linux.""" - config.delete(entry) - if sys.platform == 'linux': - old_config.delete(entry) - - def __update_linuxconfig(self) -> None: - """On linux config uses ConfigParser, which doesn't update from disk changes. Force the update here.""" - if sys.platform != 'linux': - return - - from config.linux import LinuxConfig # type: ignore - if isinstance(config, LinuxConfig) and config.config is not None: - config.config.read(config.filename) - - @mark.parametrize("i", _build_test_list(int_tests, _get_fuzz(int, 50, (-big_int, big_int)))) - def test_int(self, i: int) -> None: - """Save an int though the old config, recall it using the new config.""" - if sys.platform == 'win32': - i = abs(i) - - name = self.KEY_PREFIX + f'int_{i}' - old_config.set(name, i) - old_config.save() - - self.__update_linuxconfig() - - res = config.get_int(name) - with contextlib.ExitStack() as stack: - stack.callback(self.cleanup_entry, name) - assert res == i - - @mark.parametrize("string", _build_test_list(string_tests, _get_fuzz(str, value_length=(0, 512)))) - def test_string(self, string: str) -> None: - """Save a string though the old config, recall it using the new config.""" - string = string.replace("\r", "") # The old config does _not_ support \r in its entries. We do. - name = self.KEY_PREFIX + f'string_{hash(string)}' - old_config.set(name, string) - old_config.save() - - self.__update_linuxconfig() - - res = config.get_str(name) - with contextlib.ExitStack() as stack: - stack.callback(self.cleanup_entry, name) - assert res == string - - @mark.parametrize("lst", _build_test_list(list_tests, _get_fuzz(list))) - def test_list(self, lst: list[str]) -> None: - """Save a list though the old config, recall it using the new config.""" - lst = [x.replace("\r", "") for x in lst] # OldConfig on linux fails to store these correctly - if sys.platform == 'win32': - # old conf on windows replaces empty entries with spaces as a workaround for a bug. New conf does not - # So insert those spaces here, to ensure that it works otherwise. - lst = [e if len(e) > 0 else ' ' for e in lst] - - name = self.KEY_PREFIX + f'list_test_{ hash("".join(lst)) }' - old_config.set(name, lst) - old_config.save() - - self.__update_linuxconfig() - - res = config.get_list(name) - with contextlib.ExitStack() as stack: - stack.callback(self.cleanup_entry, name) - assert res == lst - - @mark.skipif(sys.platform == 'win32', reason="Old Windows config does not support bool types") - @mark.parametrize("b", bool_tests) - def test_bool(self, b: bool) -> None: - """Save a bool though the old config, recall it using the new config.""" - name = str(b) - old_config.set(name, b) - old_config.save() - self.__update_linuxconfig() - with contextlib.ExitStack() as stack: - stack.callback(self.cleanup_entry, name) - assert config.get_bool(name) == b diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index 649140c8..438d1dd5 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -5,7 +5,7 @@ import multiprocessing as mp import os import pathlib import sys -from typing import Generator +from collections.abc import Generator import pytest from pytest import MonkeyPatch, TempdirFactory, TempPathFactory from config import config diff --git a/theme.py b/theme.py index 99ccb158..fe431ebb 100644 --- a/theme.py +++ b/theme.py @@ -15,7 +15,7 @@ import sys import tkinter as tk from tkinter import font as tk_font from tkinter import ttk -from typing import Callable +from collections.abc import Callable from l10n import translations as tr from config import config from EDMCLogging import get_main_logger @@ -35,11 +35,13 @@ if sys.platform == 'win32': from ctypes.wintypes import DWORD, LPCVOID, LPCWSTR import win32gui AddFontResourceEx = ctypes.windll.gdi32.AddFontResourceExW - AddFontResourceEx.restypes = [LPCWSTR, DWORD, LPCVOID] # type: ignore + AddFontResourceEx.argtypes = [LPCWSTR, DWORD, LPCVOID] + AddFontResourceEx.restype = ctypes.c_int FR_PRIVATE = 0x10 FR_NOT_ENUM = 0x20 - font_path = config.respath_path / 'EUROCAPS.TTF' - AddFontResourceEx(str(font_path), FR_PRIVATE, 0) + font_path = str(config.respath_path / 'EUROCAPS.TTF') + if not AddFontResourceEx(font_path, FR_PRIVATE, None): + raise RuntimeError(f"Failed to load font {font_path}") elif sys.platform == 'linux': # pyright: reportUnboundVariable=false @@ -206,7 +208,7 @@ class _Theme: if isinstance(widget, (tk.Frame, ttk.Frame)): for child in widget.winfo_children(): - self.register(child) + self.register(child) # type: ignore def register_alternate(self, pair: tuple, gridopts: dict) -> None: self.widgets_pair.append((pair, gridopts)) @@ -311,7 +313,7 @@ class _Theme: self._update_widget(widget) if isinstance(widget, (tk.Frame, ttk.Frame)): for child in widget.winfo_children(): - self._update_widget(child) + self._update_widget(child) # type: ignore # Apply current theme to a single widget def _update_widget(self, widget: tk.Widget | tk.BitmapImage) -> None: # noqa: CCR001, C901 diff --git a/update.py b/update.py index f9574242..7ea619b8 100644 --- a/update.py +++ b/update.py @@ -12,9 +12,10 @@ import shutil import sys import threading import os +from dataclasses import dataclass from tkinter import messagebox from traceback import print_exc -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, cast, Any from xml.etree import ElementTree import requests import semantic_version @@ -25,58 +26,68 @@ from l10n import translations as tr if TYPE_CHECKING: import tkinter as tk - logger = get_main_logger() -def check_for_fdev_updates(silent: bool = False, local: bool = False) -> None: # noqa: CCR001 +def read_normalized_file(path: pathlib.Path) -> str: + """Read a UTF-8 (with BOM-safe) file and normalize line endings and whitespace.""" + try: + return path.read_bytes().decode('utf-8-sig').replace('\r\n', '\n').strip() + except FileNotFoundError: + return '' + + +def copy_bundle_file(filename: str, dest_dir: pathlib.Path) -> str: + """Attempt to copy a missing file from the bundled FDevIDs folder.""" + try: + shutil.copy(f"FDevIDs/{filename}", dest_dir) + return read_normalized_file(dest_dir / filename) + except (FileNotFoundError, shutil.SameFileError): + logger.info("Bundle copy failed or identical; continuing with empty content.") + return '' + + +def fetch_remote_file(url: str) -> str | None: + """Fetch a remote file and normalize its content.""" + try: + response = requests.get(url, timeout=20) + response.raise_for_status() + return response.text.replace('\r\n', '\n').strip() + except requests.RequestException: + return None + + +def check_for_fdev_updates(silent: bool = False, local: bool = False) -> None: """Check for and download FDEV ID file updates.""" - pathway = config.respath_path if local else config.app_dir_path + base_path = config.respath_path if local else config.app_dir_path + fdevid_dir = pathlib.Path(base_path, 'FDevIDs') + fdevid_dir.mkdir(parents=True, exist_ok=True) - files_urls = [ - ('commodity.csv', 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/commodity.csv'), - ('rare_commodity.csv', 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/rare_commodity.csv') - ] + files_urls = { + 'commodity.csv': 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/commodity.csv', + 'rare_commodity.csv': 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/rare_commodity.csv' + } - for file, url in files_urls: - fdevid_file = pathlib.Path(pathway / 'FDevIDs' / file) - fdevid_file.parent.mkdir(parents=True, exist_ok=True) - try: - with open(fdevid_file, newline='', encoding='utf-8') as f: - local_content = f.read() - except FileNotFoundError: - logger.info(f'File {file} not found. Writing from bundle...') - try: - for localfile in files_urls: - filepath = pathlib.Path(f"FDevIDs/{localfile[0]}") - try: - shutil.copy(filepath, pathway / 'FDevIDs') - except shutil.SameFileError: - logger.info("Not replacing same file...") - fdevid_file = pathlib.Path(pathway / 'FDevIDs' / file) - with open(fdevid_file, newline='', encoding='utf-8') as f: - local_content = f.read() - except FileNotFoundError: - local_content = None - - try: - response = requests.get(url, timeout=20) - response.raise_for_status() - except requests.RequestException: + for filename, url in files_urls.items(): + file_path = fdevid_dir / filename + local_content = read_normalized_file(file_path) or copy_bundle_file(filename, fdevid_dir) + remote_content = fetch_remote_file(url) + if not remote_content: if not silent: - logger.error(f'Failed to download {file}! Unable to continue.') + logger.error(f'Failed to download {filename}! Unable to continue.') continue - if local_content == response.text: + if local_content == remote_content: if not silent: - logger.info(f'FDEV ID file {file} already up to date.') - else: - if not silent: - logger.info(f'FDEV ID file {file} not up to date. Downloading...') - with open(fdevid_file, 'w', newline='', encoding='utf-8') as csvfile: - csvfile.write(response.text) + logger.info(f'FDEV ID file {filename} already up to date.') + continue + + if not silent: + logger.info(f'Updating FDEV ID file {filename}...') + file_path.write_text(remote_content, encoding='utf-8', newline='\n') +@dataclass(slots=True) class EDMCVersion: """ Hold all the information about an EDMC version. @@ -91,10 +102,9 @@ class EDMCVersion: semantic_version object for this version """ - def __init__(self, version: str, title: str, sv: semantic_version.base.Version): - self.version: str = version - self.title: str = title - self.sv: semantic_version.base.Version = sv + version: str + title: str + sv: semantic_version.Version class Updater: @@ -105,6 +115,21 @@ class Updater: WinSparkle on win32. """ + def __init__(self, tkroot: tk.Tk | None = None, provider: str = 'internal'): + """ + Initialise an Updater instance. + + :param tkroot: reference to the root window of the GUI + :param provider: 'internal' or other string if not + """ + self.root: tk.Tk | None = tkroot + self.provider: str = provider + self.thread: threading.Thread | None = None + self.updater: Any | None = None # ensure attribute exists + + if not self.use_internal() and sys.platform == 'win32': + self._init_winsparkle() + def shutdown_request(self) -> None: """Receive (Win)Sparkle shutdown request and send it to parent.""" if not config.shutting_down and self.root: @@ -116,61 +141,38 @@ class Updater: :return: bool """ - if self.provider == 'internal': - return True + return self.provider == 'internal' - return False + def _init_winsparkle(self) -> None: + """Initialize WinSparkle updater for Windows.""" + import ctypes + try: + self.updater = cast(ctypes.CDLL, ctypes.cdll.WinSparkle) + self.updater.win_sparkle_set_appcast_url(get_update_feed().encode()) # Set the appcast URL - def __init__(self, tkroot: tk.Tk | None = None, provider: str = 'internal'): - """ - Initialise an Updater instance. + # Set the appversion *without* build metadata, as WinSparkle + # doesn't do proper Semantic Version checks. + # NB: It 'accidentally' supports pre-release due to how it + # splits and compares strings: + # + self.updater.win_sparkle_set_app_build_version(str(appversion_nobuild())) - :param tkroot: reference to the root window of the GUI - :param provider: 'internal' or other string if not - """ - self.root: tk.Tk | None = tkroot - self.provider: str = provider - self.thread: threading.Thread | None = None + # set up shutdown callback + self.callback_t = ctypes.CFUNCTYPE(None) # keep reference + self.callback_fn = self.callback_t(self.shutdown_request) + self.updater.win_sparkle_set_shutdown_request_callback(self.callback_fn) - if self.use_internal(): - return + # Get WinSparkle running + self.updater.win_sparkle_init() - if sys.platform == 'win32': - import ctypes - - try: - self.updater: ctypes.CDLL | None = ctypes.cdll.WinSparkle - - # Set the appcast URL - self.updater.win_sparkle_set_appcast_url(get_update_feed().encode()) - - # Set the appversion *without* build metadata, as WinSparkle - # doesn't do proper Semantic Version checks. - # NB: It 'accidentally' supports pre-release due to how it - # splits and compares strings: - # - self.updater.win_sparkle_set_app_build_version(str(appversion_nobuild())) - - # set up shutdown callback - self.callback_t = ctypes.CFUNCTYPE(None) # keep reference - self.callback_fn = self.callback_t(self.shutdown_request) - self.updater.win_sparkle_set_shutdown_request_callback(self.callback_fn) - - # Get WinSparkle running - self.updater.win_sparkle_init() - - except Exception: - print_exc() - self.updater = None - if not os.getenv("EDMC_NO_UI"): - messagebox.showerror( - title=appname, - message="Updater Failed to Initialize. Please file a bug report!" - ) - else: - logger.error("Updater Failed to Initialize. Please file a bug report!") - - return + except Exception: + print_exc() + self.updater = None + msg = "Updater Failed to Initialize. Please file a bug report!" + if not os.getenv("EDMC_NO_UI"): + messagebox.showerror(title=appname, message=msg) + else: + logger.error(msg) def set_automatic_updates_check(self, onoroff: bool) -> None: """ @@ -187,10 +189,8 @@ class Updater: def check_for_updates(self) -> None: """Trigger the requisite method to check for an update.""" if self.use_internal(): - self.thread = threading.Thread(target=self.worker, name='update worker') - self.thread.daemon = True + self.thread = threading.Thread(target=self.worker, name='update worker', daemon=True) self.thread.start() - elif sys.platform == 'win32' and self.updater: self.updater.win_sparkle_check_update_with_ui() @@ -218,7 +218,6 @@ class Updater: except requests.RequestException as ex: logger.exception(f'Error retrieving update_feed file: {ex}') - return None try: @@ -226,7 +225,6 @@ class Updater: except SyntaxError as ex: logger.exception(f'Syntax error in update_feed file: {ex}') - return None # For *these* purposes all systems are the same as 'windows', as @@ -270,7 +268,6 @@ class Updater: # LANG: Update Available Text status['text'] = tr.tl("{NEWVER} is available").format(NEWVER=newversion.title) self.root.update_idletasks() - else: logger.info("No new version available at this time") diff --git a/util_ships.py b/util_ships.py index b2105106..4f99e731 100644 --- a/util_ships.py +++ b/util_ships.py @@ -5,6 +5,8 @@ Copyright (c) EDCD, All Rights Reserved Licensed under the GNU General Public License. See LICENSE file. """ +import os +import sys from pathlib import Path from edmc_data import ship_name_map @@ -17,7 +19,7 @@ def ship_file_name(ship_name: str, ship_type: str) -> str: name = Path(name).with_suffix("").name # Check if the name is a reserved filename - if Path(name).is_reserved(): + if sys.platform == 'win32' and os.path.isreserved(name): name += "_" return name.translate(