diff --git a/EDMC.py b/EDMC.py index 743bda5d1..6b72c7d1c 100755 --- a/EDMC.py +++ b/EDMC.py @@ -14,7 +14,6 @@ import os import queue import sys -from pathlib import Path from time import sleep, time from typing import TYPE_CHECKING, Any @@ -213,16 +212,14 @@ def main(): # noqa: C901, CCR001 # system, chances are its the current locale, and not utf-8. Otherwise if it was copied, its probably # utf8. Either way, try the system FIRST because reading something like cp1251 in UTF-8 results in garbage # but the reverse results in an exception. - json_file = Path(args.j).resolve() + json_file = os.path.abspath(args.j) try: with open(json_file) as file_handle: data = json.load(file_handle) except UnicodeDecodeError: with open(json_file, encoding='utf-8') as file_handle: data = json.load(file_handle) - file_path = Path(args.j) - modification_time = file_path.stat().st_mtime - config.set('querytime', int(modification_time)) + config.set('querytime', int(os.path.getmtime(args.j))) else: # Get state from latest Journal file diff --git a/EDMCSystemProfiler.py b/EDMCSystemProfiler.py index 77212db7a..92502cc8b 100644 --- a/EDMCSystemProfiler.py +++ b/EDMCSystemProfiler.py @@ -11,7 +11,7 @@ import webbrowser import platform import sys -from os import chdir, environ +from os import chdir, environ, path import pathlib import logging from journal_lock import JournalLock @@ -19,10 +19,10 @@ if getattr(sys, "frozen", False): # Under py2exe sys.path[0] is the executable name if sys.platform == "win32": - chdir(pathlib.Path(sys.path[0]).parent) + chdir(path.dirname(sys.path[0])) # Allow executable to be invoked from any cwd - environ['TCL_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tcl') - environ['TK_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tk') + environ["TCL_LIBRARY"] = path.join(path.dirname(sys.path[0]), "lib", "tcl") + environ["TK_LIBRARY"] = path.join(path.dirname(sys.path[0]), "lib", "tk") else: # We still want to *try* to have CWD be where the main script is, even if @@ -44,12 +44,11 @@ def get_sys_report(config: config.AbstractConfig) -> str: plt = platform.uname() locale.setlocale(locale.LC_ALL, "") lcl = locale.getlocale() - monitor.currentdir = pathlib.Path(config.get_str( + monitor.currentdir = config.get_str( "journaldir", default=config.default_journal_dir - ) ) if not monitor.currentdir: - monitor.currentdir = config.default_journal_dir_path + monitor.currentdir = config.default_journal_dir try: logfile = monitor.journal_newest_filename(monitor.currentdir) if logfile is None: @@ -116,12 +115,12 @@ def main() -> None: root.withdraw() # Hide the window initially to calculate the dimensions try: icon_image = tk.PhotoImage( - file=cur_config.respath_path / "io.edcd.EDMarketConnector.png" + file=path.join(cur_config.respath_path, "io.edcd.EDMarketConnector.png") ) root.iconphoto(True, icon_image) except tk.TclError: - root.iconbitmap(cur_config.respath_path / "EDMarketConnector.ico") + root.iconbitmap(path.join(cur_config.respath_path, "EDMarketConnector.ico")) sys_report = get_sys_report(cur_config) diff --git a/monitor.py b/monitor.py index ce6b60759..e19d6ad56 100644 --- a/monitor.py +++ b/monitor.py @@ -8,7 +8,7 @@ from __future__ import annotations import json -from pathlib import Path +import pathlib import queue import re import sys @@ -16,6 +16,7 @@ from calendar import timegm 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 import psutil @@ -67,7 +68,7 @@ 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.currentdir: Path | None = None # The actual logdir that we're monitoring + self.currentdir: str | None = None # The actual logdir that we're monitoring self.logfile: str | None = None self.observer: BaseObserver | None = None self.observed = None # a watchdog ObservedWatch, or None if polling @@ -190,9 +191,9 @@ def start(self, root: 'tkinter.Tk') -> bool: # noqa: CCR001 if journal_dir == '' or journal_dir is None: journal_dir = config.default_journal_dir - logdir = Path(journal_dir).expanduser() + logdir = expanduser(journal_dir) - if not logdir or not Path.is_dir(logdir): + if not logdir or not isdir(logdir): logger.error(f'Journal Directory is invalid: "{logdir}"') self.stop() return False @@ -265,10 +266,9 @@ def journal_newest_filename(self, journals_dir) -> str | None: # Odyssey Update 11 has, e.g. Journal.2022-03-15T152503.01.log # Horizons Update 11 equivalent: Journal.220315152335.01.log # So we can no longer use a naive sort. - journals_dir_path = Path(journals_dir) - journal_files = (journals_dir_path / Path(x) for x in journal_files) - latest_file = max(journal_files, key=lambda f: Path(f).stat().st_ctime) - return str(latest_file) + journals_dir_path = pathlib.Path(journals_dir) + journal_files = (journals_dir_path / pathlib.Path(x) for x in journal_files) + return str(max(journal_files, key=getctime)) return None @@ -337,7 +337,7 @@ def running(self) -> bool: 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(Path(event.src_path).name): + if not event.is_directory and self._RE_LOGFILE.search(basename(event.src_path)): self.logfile = event.src_path @@ -1076,7 +1076,7 @@ def parse_entry(self, line: bytes) -> MutableMapping[str, Any]: # noqa: C901, C self.state['Cargo'] = defaultdict(int) # From 3.3 full Cargo event (after the first one) is written to a separate file if 'Inventory' not in entry: - with open(self.currentdir / 'Cargo.json', 'rb') as h: # type: ignore + with open(join(self.currentdir, 'Cargo.json'), 'rb') as h: # type: ignore entry = json.load(h) self.state['CargoJSON'] = entry @@ -1103,7 +1103,7 @@ def parse_entry(self, line: bytes) -> MutableMapping[str, Any]: # noqa: C901, C # Always attempt loading of this, but if it fails we'll hope this was # a startup/boarding version and thus `entry` contains # the data anyway. - currentdir_path = Path(str(self.currentdir)) + currentdir_path = pathlib.Path(str(self.currentdir)) shiplocker_filename = currentdir_path / 'ShipLocker.json' shiplocker_max_attempts = 5 shiplocker_fail_sleep = 0.01 @@ -1172,7 +1172,7 @@ def parse_entry(self, line: bytes) -> MutableMapping[str, Any]: # noqa: C901, C # TODO: v31 doc says this is`backpack.json` ... but Howard Chalkley # said it's `Backpack.json` - backpack_file = Path(str(self.currentdir)) / 'Backpack.json' + backpack_file = pathlib.Path(str(self.currentdir)) / 'Backpack.json' backpack_data = None if not backpack_file.exists(): @@ -1548,7 +1548,7 @@ def parse_entry(self, line: bytes) -> MutableMapping[str, Any]: # noqa: C901, C entry = fcmaterials elif event_type == 'moduleinfo': - with open(self.currentdir / 'ModulesInfo.json', 'rb') as mf: # type: ignore + with open(join(self.currentdir, 'ModulesInfo.json'), 'rb') as mf: # type: ignore try: entry = json.load(mf) @@ -2259,14 +2259,14 @@ def export_ship(self, filename=None) -> None: # noqa: C901, CCR001 oldfiles = sorted((x for x in listdir(config.get_str('outdir')) if regexp.match(x))) if oldfiles: try: - with open(config.get_str('outdir') / Path(oldfiles[-1]), encoding='utf-8') as h: + with open(join(config.get_str('outdir'), oldfiles[-1]), encoding='utf-8') as h: if h.read() == string: return # same as last time - don't write except UnicodeError: logger.exception("UnicodeError reading old ship loadout with utf-8 encoding, trying without...") try: - with open(config.get_str('outdir') / Path(oldfiles[-1])) as h: + with open(join(config.get_str('outdir'), oldfiles[-1])) as h: if h.read() == string: return # same as last time - don't write @@ -2285,7 +2285,7 @@ def export_ship(self, filename=None) -> None: # noqa: C901, CCR001 # Write ts = strftime('%Y-%m-%dT%H.%M.%S', localtime(time())) - filename = config.get_str('outdir') / Path(f'{ship}.{ts}.txt') + filename = join(config.get_str('outdir'), f'{ship}.{ts}.txt') try: with open(filename, 'wt', encoding='utf-8') as h: @@ -2372,7 +2372,7 @@ def _parse_navroute_file(self) -> dict[str, Any] | None: try: - with open(self.currentdir / 'NavRoute.json') as f: + with open(join(self.currentdir, 'NavRoute.json')) as f: raw = f.read() except Exception as e: @@ -2398,7 +2398,7 @@ def _parse_fcmaterials_file(self) -> dict[str, Any] | None: try: - with open(self.currentdir / 'FCMaterials.json') as f: + with open(join(self.currentdir, 'FCMaterials.json')) as f: raw = f.read() except Exception as e: diff --git a/prefs.py b/prefs.py index 29e1e7b13..95f29e7fc 100644 --- a/prefs.py +++ b/prefs.py @@ -4,7 +4,7 @@ import contextlib import logging -from os.path import expandvars +from os.path import expandvars, join, normpath from pathlib import Path import subprocess import sys @@ -1100,7 +1100,7 @@ def displaypath(self, pathvar: tk.StringVar, entryfield: tk.Entry) -> None: if sys.platform == 'win32': start = len(config.home.split('\\')) if pathvar.get().lower().startswith(config.home.lower()) else 0 display = [] - components = Path(pathvar.get()).resolve().parts + components = normpath(pathvar.get()).split('\\') buf = ctypes.create_unicode_buffer(MAX_PATH) pidsRes = ctypes.c_int() # noqa: N806 # Windows convention for i in range(start, len(components)): @@ -1253,7 +1253,7 @@ def apply(self) -> None: # noqa: CCR001 config.set( 'outdir', - str(config.home_path / self.outdir.get()[2:]) if self.outdir.get().startswith('~') else self.outdir.get() + join(config.home_path, self.outdir.get()[2:]) if self.outdir.get().startswith('~') else self.outdir.get() ) logdir = self.logdir.get() @@ -1296,8 +1296,8 @@ def apply(self) -> None: # noqa: CCR001 if self.plugdir.get() != config.get('plugin_dir'): config.set( 'plugin_dir', - str(Path(config.home_path, self.plugdir.get()[2:])) if self.plugdir.get().startswith('~') else - str(Path(self.plugdir.get())) + join(config.home_path, self.plugdir.get()[2:]) if self.plugdir.get().startswith( + '~') else self.plugdir.get() ) self.req_restart = True