Skip to content

Commit

Permalink
Merge pull request #2260 from HullSeals/enhancement/2114/pathlib-hand…
Browse files Browse the repository at this point in the history
…over

[2114] Pathlib Handover
  • Loading branch information
Rixxan authored Jul 22, 2024
2 parents ac9b7b4 + 40463d4 commit 2adb440
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 131 deletions.
11 changes: 7 additions & 4 deletions EDMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import queue
import sys
from pathlib import Path
from time import sleep, time
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -212,22 +213,24 @@ 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 = os.path.abspath(args.j)
json_file = Path(args.j).resolve()
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)
config.set('querytime', int(os.path.getmtime(args.j)))
file_path = Path(args.j)
modification_time = file_path.stat().st_mtime
config.set('querytime', int(modification_time))

else:
# Get state from latest Journal file
logger.debug('Getting state from latest journal file')
try:
monitor.currentdir = config.get_str('journaldir', default=config.default_journal_dir)
monitor.currentdir = Path(config.get_str('journaldir', default=config.default_journal_dir))
if not monitor.currentdir:
monitor.currentdir = config.default_journal_dir
monitor.currentdir = config.default_journal_dir_path

logger.debug(f'logdir = "{monitor.currentdir}"')
logfile = monitor.journal_newest_filename(monitor.currentdir)
Expand Down
11 changes: 6 additions & 5 deletions EDMCLogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
To utilise logging in a 'found' (third-party) plugin, include this:
import os
from pathlib import Path
import logging
plugin_name = os.path.basename(os.path.dirname(__file__))
# Retrieve the name of the plugin folder
plugin_name = Path(__file__).resolve().parent.name
# Set up logger with hierarchical name including appname and plugin_name
# plugin_name here *must* be the name of the folder the plugin resides in
# See, plug.py:load_plugins()
logger = logging.getLogger(f'{appname}.{plugin_name}')
"""
from __future__ import annotations
Expand Down Expand Up @@ -485,8 +486,8 @@ def munge_module_name(cls, frame_info: inspect.Traceback, module_name: str) -> s
:return: The munged module_name.
"""
file_name = pathlib.Path(frame_info.filename).expanduser()
plugin_dir = pathlib.Path(config.plugin_dir_path).expanduser()
internal_plugin_dir = pathlib.Path(config.internal_plugin_dir_path).expanduser()
plugin_dir = config.plugin_dir_path.expanduser()
internal_plugin_dir = config.internal_plugin_dir_path.expanduser()
# Find the first parent called 'plugins'
plugin_top = file_name
while plugin_top and plugin_top.name != '':
Expand Down
17 changes: 9 additions & 8 deletions EDMCSystemProfiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
import webbrowser
import platform
import sys
from os import chdir, environ, path
from os import chdir, environ
import pathlib
import logging
from journal_lock import JournalLock

if getattr(sys, "frozen", False):
# Under py2exe sys.path[0] is the executable name
if sys.platform == "win32":
chdir(path.dirname(sys.path[0]))
chdir(pathlib.Path(sys.path[0]).parent)
# Allow executable to be invoked from any cwd
environ["TCL_LIBRARY"] = path.join(path.dirname(sys.path[0]), "lib", "tcl")
environ["TK_LIBRARY"] = path.join(path.dirname(sys.path[0]), "lib", "tk")
environ['TCL_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tcl')
environ['TK_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tk')

else:
# We still want to *try* to have CWD be where the main script is, even if
Expand All @@ -44,11 +44,12 @@ def get_sys_report(config: config.AbstractConfig) -> str:
plt = platform.uname()
locale.setlocale(locale.LC_ALL, "")
lcl = locale.getlocale()
monitor.currentdir = config.get_str(
monitor.currentdir = pathlib.Path(config.get_str(
"journaldir", default=config.default_journal_dir
)
)
if not monitor.currentdir:
monitor.currentdir = config.default_journal_dir
monitor.currentdir = config.default_journal_dir_path
try:
logfile = monitor.journal_newest_filename(monitor.currentdir)
if logfile is None:
Expand Down Expand Up @@ -115,12 +116,12 @@ def main() -> None:
root.withdraw() # Hide the window initially to calculate the dimensions
try:
icon_image = tk.PhotoImage(
file=path.join(cur_config.respath_path, "io.edcd.EDMarketConnector.png")
file=cur_config.respath_path / "io.edcd.EDMarketConnector.png"
)

root.iconphoto(True, icon_image)
except tk.TclError:
root.iconbitmap(path.join(cur_config.respath_path, "EDMarketConnector.ico"))
root.iconbitmap(cur_config.respath_path / "EDMarketConnector.ico")

sys_report = get_sys_report(cur_config)

Expand Down
14 changes: 7 additions & 7 deletions EDMarketConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import sys
import threading
import webbrowser
from os import chdir, environ, path
from os import chdir, environ
from time import localtime, strftime, time
from typing import TYPE_CHECKING, Any, Literal
from constants import applongname, appname, protocolhandler_redirect
Expand All @@ -31,10 +31,10 @@
if getattr(sys, 'frozen', False):
# Under py2exe sys.path[0] is the executable name
if sys.platform == 'win32':
chdir(path.dirname(sys.path[0]))
os.chdir(pathlib.Path(sys.path[0]).parent)
# Allow executable to be invoked from any cwd
environ['TCL_LIBRARY'] = path.join(path.dirname(sys.path[0]), 'lib', 'tcl')
environ['TK_LIBRARY'] = path.join(path.dirname(sys.path[0]), 'lib', 'tk')
environ['TCL_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tcl')
environ['TK_LIBRARY'] = str(pathlib.Path(sys.path[0]).parent / 'lib' / 'tk')

else:
# We still want to *try* to have CWD be where the main script is, even if
Expand Down Expand Up @@ -470,8 +470,8 @@ def open_window(systray: 'SysTrayIcon', *args) -> None:
self.w.wm_iconbitmap(default='EDMarketConnector.ico')

else:
self.w.tk.call('wm', 'iconphoto', self.w, '-default',
tk.PhotoImage(file=path.join(config.respath_path, 'io.edcd.EDMarketConnector.png')))
image_path = config.respath_path / 'io.edcd.EDMarketConnector.png'
self.w.tk.call('wm', 'iconphoto', self.w, '-default', image=tk.PhotoImage(file=image_path))

# TODO: Export to files and merge from them in future ?
self.theme_icon = tk.PhotoImage(
Expand Down Expand Up @@ -1652,7 +1652,7 @@ def shipyard_url(self, shipname: str) -> str | None:
# Avoid file length limits if possible
provider = config.get_str('shipyard_provider', default='EDSY')
target = plug.invoke(provider, 'EDSY', 'shipyard_url', loadout, monitor.is_beta)
file_name = path.join(config.app_dir_path, "last_shipyard.html")
file_name = config.app_dir_path / "last_shipyard.html"

with open(file_name, 'w') as f:
f.write(SHIPYARD_HTML_TEMPLATE.format(
Expand Down
19 changes: 9 additions & 10 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import sys
import pathlib
from string import Template
from os.path import join, isdir
import py2exe
from config import (
appcmdname,
Expand All @@ -37,7 +36,7 @@ def iss_build(template_path: str, output_file: str) -> None:
new_file.write(newfile)


def system_check(dist_dir: str) -> str:
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}")
Expand All @@ -55,17 +54,17 @@ def system_check(dist_dir: str) -> str:

print(f"Git short hash: {git_shorthash}")

if dist_dir and len(dist_dir) > 1 and isdir(dist_dir):
if dist_dir and pathlib.Path.is_dir(dist_dir):
shutil.rmtree(dist_dir)
return gitversion_file


def generate_data_files(
app_name: str, gitversion_file: str, plugins: list[str]
) -> list[tuple[str, list[str]]]:
) -> list[tuple[object, object]]:
"""Create the required datafiles to build."""
l10n_dir = "L10n"
fdevids_dir = "FDevIDs"
fdevids_dir = pathlib.Path("FDevIDs")
data_files = [
(
"",
Expand All @@ -88,13 +87,13 @@ def generate_data_files(
),
(
l10n_dir,
[join(l10n_dir, x) for x in os.listdir(l10n_dir) if x.endswith(".strings")],
[pathlib.Path(l10n_dir) / x for x in os.listdir(l10n_dir) if x.endswith(".strings")]
),
(
fdevids_dir,
[
join(fdevids_dir, "commodity.csv"),
join(fdevids_dir, "rare_commodity.csv"),
pathlib.Path(fdevids_dir / "commodity.csv"),
pathlib.Path(fdevids_dir / "rare_commodity.csv"),
],
),
("plugins", plugins),
Expand All @@ -104,7 +103,7 @@ def generate_data_files(

def build() -> None:
"""Build EDMarketConnector using Py2Exe."""
dist_dir: str = "dist.win32"
dist_dir: pathlib.Path = pathlib.Path("dist.win32")
gitversion_filename: str = system_check(dist_dir)

# Constants
Expand Down Expand Up @@ -142,7 +141,7 @@ def build() -> None:
}

# Function to generate DATA_FILES list
data_files: list[tuple[str, list[str]]] = generate_data_files(
data_files: list[tuple[object, object]] = generate_data_files(
appname, gitversion_filename, plugins
)

Expand Down
17 changes: 8 additions & 9 deletions collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import os
import pathlib
import sys
from os.path import isfile
from traceback import print_exc

import companion
Expand All @@ -35,7 +34,7 @@ def __make_backup(file_name: pathlib.Path, suffix: str = '.bak') -> None:
"""
backup_name = file_name.parent / (file_name.name + suffix)

if isfile(backup_name):
if pathlib.Path.is_file(backup_name):
os.unlink(backup_name)

os.rename(file_name, backup_name)
Expand All @@ -52,13 +51,13 @@ def addcommodities(data) -> None: # noqa: CCR001
return

try:
commodityfile = pathlib.Path(config.app_dir_path / 'FDevIDs' / 'commodity.csv')
commodityfile = config.app_dir_path / 'FDevIDs' / 'commodity.csv'
except FileNotFoundError:
commodityfile = pathlib.Path('FDevIDs/commodity.csv')
commodities = {}

# slurp existing
if isfile(commodityfile):
if pathlib.Path.is_file(commodityfile):
with open(commodityfile) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
Expand Down Expand Up @@ -86,7 +85,7 @@ def addcommodities(data) -> None: # noqa: CCR001
if len(commodities) <= size_pre:
return

if isfile(commodityfile):
if pathlib.Path.is_file(commodityfile):
__make_backup(commodityfile)

with open(commodityfile, 'w', newline='\n') as csvfile:
Expand All @@ -109,7 +108,7 @@ def addmodules(data): # noqa: C901, CCR001
fields = ('id', 'symbol', 'category', 'name', 'mount', 'guidance', 'ship', 'class', 'rating', 'entitlement')

# slurp existing
if isfile(outfile):
if pathlib.Path.is_file(outfile):
with open(outfile) as csvfile:
reader = csv.DictReader(csvfile, restval='')
for row in reader:
Expand Down Expand Up @@ -147,7 +146,7 @@ def addmodules(data): # noqa: C901, CCR001
if not len(modules) > size_pre:
return

if isfile(outfile):
if pathlib.Path.is_file(outfile):
__make_backup(outfile)

with open(outfile, 'w', newline='\n') as csvfile:
Expand All @@ -170,7 +169,7 @@ def addships(data) -> None: # noqa: CCR001
fields = ('id', 'symbol', 'name')

# slurp existing
if isfile(shipfile):
if pathlib.Path.is_file(shipfile):
with open(shipfile) as csvfile:
reader = csv.DictReader(csvfile, restval='')
for row in reader:
Expand Down Expand Up @@ -200,7 +199,7 @@ def addships(data) -> None: # noqa: CCR001
if not len(ships) > size_pre:
return

if isfile(shipfile):
if pathlib.Path.is_file(shipfile):
__make_backup(shipfile)

with open(shipfile, 'w', newline='\n') as csvfile:
Expand Down
4 changes: 2 additions & 2 deletions commodity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

import time
from os.path import join
from pathlib import Path

from config import config
from edmc_data import commodity_bracketmap as bracketmap
Expand All @@ -29,7 +29,7 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None) -> None:
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 = join(config.get_str('outdir'), filename)
filename = Path(config.get_str('outdir')) / filename

if kind == COMMODITY_CSV:
sep = ';' # BUG: for fixing later after cleanup
Expand Down
5 changes: 3 additions & 2 deletions companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import urllib.parse
import webbrowser
from email.utils import parsedate
from pathlib import Path
from queue import Queue
from typing import TYPE_CHECKING, Any, Mapping, TypeVar
import requests
Expand Down Expand Up @@ -1135,7 +1136,7 @@ def dump(self, r: requests.Response) -> None:

def dump_capi_data(self, data: CAPIData) -> None:
"""Dump CAPI data to file for examination."""
if os.path.isdir('dump'):
if Path('dump').is_dir():
file_name: str = ""
if data.source_endpoint == self.FRONTIER_CAPI_PATH_FLEETCARRIER:
file_name += f"FleetCarrier.{data['name']['callsign']}"
Expand Down Expand Up @@ -1203,7 +1204,7 @@ def fixup(data: CAPIData) -> CAPIData: # noqa: C901, CCR001 # Can't be usefully
if not commodity_map:
# Lazily populate
for f in ('commodity.csv', 'rare_commodity.csv'):
if not os.path.isfile(config.app_dir_path / 'FDevIDs/' / f):
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:
Expand Down
9 changes: 5 additions & 4 deletions dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import time
import tkinter as tk
from calendar import timegm
from os.path import getsize, isdir, isfile, join
from pathlib import Path
from typing import Any, cast
from watchdog.observers.api import BaseObserver
from config import config
Expand Down Expand Up @@ -57,7 +57,7 @@ def start(self, root: tk.Tk, started: int) -> bool:

logdir = config.get_str('journaldir', default=config.default_journal_dir)
logdir = logdir or config.default_journal_dir
if not isdir(logdir):
if not Path.is_dir(Path(logdir)):
logger.info(f"No logdir, or it isn't a directory: {logdir=}")
self.stop()
return False
Expand Down Expand Up @@ -164,7 +164,8 @@ def on_modified(self, event) -> None:
:param event: Watchdog event.
"""
if event.is_directory or (isfile(event.src_path) and getsize(event.src_path)):
modpath = Path(event.src_path)
if event.is_directory or (modpath.is_file() and modpath.stat().st_size):
# Can get on_modified events when the file is emptied
self.process(event.src_path if not event.is_directory else None)

Expand All @@ -177,7 +178,7 @@ def process(self, logfile: str | None = None) -> None:
if config.shutting_down:
return
try:
status_json_path = join(self.currentdir, 'Status.json')
status_json_path = Path(self.currentdir) / 'Status.json'
with open(status_json_path, 'rb') as h:
data = h.read().strip()
if data: # Can be empty if polling while the file is being re-written
Expand Down
Loading

0 comments on commit 2adb440

Please sign in to comment.