mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-15 08:40:34 +03:00
[2051] Replace More Old-Style Union/Opts
And re-adds the missing but deprecated abstracts in config
This commit is contained in:
parent
070a3989a0
commit
473bd1cdf6
config
tests
config
journal_lock.py
killswitch.py
@ -162,12 +162,23 @@ def appversion_nobuild() -> semantic_version.Version:
|
||||
|
||||
|
||||
class AbstractConfig(abc.ABC):
|
||||
"""Abstract root class of all platform specific Config implementations."""
|
||||
"""
|
||||
Abstract root class of all platform specific Config implementations.
|
||||
|
||||
Commented lines are no longer supported or replaced.
|
||||
"""
|
||||
|
||||
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
|
||||
|
@ -1,11 +1,13 @@
|
||||
"""Old Configuration Test File."""
|
||||
from __future__ import annotations
|
||||
|
||||
import numbers
|
||||
import sys
|
||||
import warnings
|
||||
from configparser import NoOptionError
|
||||
from os import getenv, makedirs, mkdir, pardir
|
||||
from os.path import dirname, expanduser, isdir, join, normpath
|
||||
from typing import TYPE_CHECKING, Optional, Union
|
||||
from typing import TYPE_CHECKING
|
||||
from config import applongname, appname, update_interval
|
||||
from EDMCLogging import get_main_logger
|
||||
|
||||
@ -80,7 +82,7 @@ elif sys.platform == 'win32':
|
||||
RegDeleteValue.restype = LONG
|
||||
RegDeleteValue.argtypes = [HKEY, LPCWSTR]
|
||||
|
||||
def known_folder_path(guid: uuid.UUID) -> Optional[str]:
|
||||
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)):
|
||||
@ -138,7 +140,7 @@ class OldConfig:
|
||||
self.identifier = f'uk.org.marginal.{appname.lower()}'
|
||||
NSBundle.mainBundle().infoDictionary()['CFBundleIdentifier'] = self.identifier
|
||||
|
||||
self.default_journal_dir: Optional[str] = join(
|
||||
self.default_journal_dir: str | None = join(
|
||||
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True)[0],
|
||||
'Frontier Developments',
|
||||
'Elite Dangerous'
|
||||
@ -152,7 +154,7 @@ class OldConfig:
|
||||
if not self.get('outdir') or not isdir(str(self.get('outdir'))):
|
||||
self.set('outdir', NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, True)[0])
|
||||
|
||||
def get(self, key: str, default: Union[None, list, str] = None) -> Union[None, list, str]:
|
||||
def get(self, key: str, default: None | list | str = None) -> None | list | str:
|
||||
"""Look up a string configuration value."""
|
||||
val = self.settings.get(key)
|
||||
if val is None:
|
||||
@ -179,7 +181,7 @@ class OldConfig:
|
||||
logger.debug('The exception type is ...', exc_info=e)
|
||||
return default
|
||||
|
||||
def set(self, key: str, val: Union[int, str, list]) -> None:
|
||||
def set(self, key: str, val: int | str | list) -> None:
|
||||
"""Set value on the specified configuration key."""
|
||||
self.settings[key] = val
|
||||
|
||||
@ -221,13 +223,13 @@ class OldConfig:
|
||||
|
||||
journaldir = known_folder_path(FOLDERID_SavedGames)
|
||||
if journaldir:
|
||||
self.default_journal_dir: Optional[str] = join(journaldir, 'Frontier Developments', 'Elite Dangerous')
|
||||
self.default_journal_dir: str | None = join(journaldir, 'Frontier Developments', 'Elite Dangerous')
|
||||
|
||||
else:
|
||||
self.default_journal_dir = None
|
||||
|
||||
self.identifier = applongname
|
||||
self.hkey: Optional[ctypes.c_void_p] = HKEY()
|
||||
self.hkey: ctypes.c_void_p | None = HKEY()
|
||||
disposition = DWORD()
|
||||
if RegCreateKeyEx(
|
||||
HKEY_CURRENT_USER,
|
||||
@ -280,7 +282,7 @@ class OldConfig:
|
||||
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: Union[None, list, str] = None) -> Union[None, list, str]:
|
||||
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()
|
||||
@ -327,7 +329,7 @@ class OldConfig:
|
||||
|
||||
return key_val.value
|
||||
|
||||
def set(self, key: str, val: Union[int, str, list]) -> None:
|
||||
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)
|
||||
@ -373,7 +375,7 @@ class OldConfig:
|
||||
mkdir(self.plugin_dir)
|
||||
|
||||
self.internal_plugin_dir = join(dirname(__file__), 'plugins')
|
||||
self.default_journal_dir: Optional[str] = None
|
||||
self.default_journal_dir: str | None = None
|
||||
self.home = expanduser('~')
|
||||
self.respath = dirname(__file__)
|
||||
self.identifier = f'uk.org.marginal.{appname.lower()}'
|
||||
@ -394,7 +396,7 @@ class OldConfig:
|
||||
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: Union[None, list, str] = None) -> Union[None, list, str]:
|
||||
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)
|
||||
@ -429,7 +431,7 @@ class OldConfig:
|
||||
|
||||
return default
|
||||
|
||||
def set(self, key: str, val: Union[int, str, list]) -> None:
|
||||
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')
|
||||
|
@ -13,7 +13,7 @@ import pathlib
|
||||
import random
|
||||
import string
|
||||
import sys
|
||||
from typing import Any, Iterable, List, cast
|
||||
from typing import Any, Iterable, cast
|
||||
|
||||
import pytest
|
||||
from pytest import mark
|
||||
@ -28,12 +28,12 @@ from _old_config import old_config # noqa: E402
|
||||
from config import config # noqa: E402
|
||||
|
||||
|
||||
def _fuzz_list(length: int) -> List[str]:
|
||||
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)
|
||||
return cast(list[str], out)
|
||||
|
||||
|
||||
_fuzz_generators = { # Type annotating this would be a nightmare.
|
||||
@ -70,7 +70,7 @@ bool_tests = [True, False]
|
||||
big_int = int(0xFFFFFFFF) # 32 bit int
|
||||
|
||||
|
||||
def _make_params(args: List[Any], id_name: str = 'random_test_{i}') -> list:
|
||||
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)]
|
||||
|
||||
|
||||
@ -115,7 +115,7 @@ class TestNewConfig:
|
||||
config.delete(name)
|
||||
|
||||
@mark.parametrize("lst", _build_test_list(list_tests, _get_fuzz(list)))
|
||||
def test_list(self, lst: List[str]) -> None:
|
||||
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)
|
||||
@ -214,7 +214,7 @@ class TestOldNewConfig:
|
||||
assert res == string
|
||||
|
||||
@mark.parametrize("lst", _build_test_list(list_tests, _get_fuzz(list)))
|
||||
def test_list(self, lst: List[str]) -> None:
|
||||
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':
|
||||
|
@ -1,9 +1,11 @@
|
||||
"""Tests for journal_lock.py code."""
|
||||
from __future__ import annotations
|
||||
|
||||
import multiprocessing as mp
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
from typing import Generator, Optional
|
||||
from typing import Generator
|
||||
import pytest
|
||||
from pytest import MonkeyPatch, TempdirFactory, TempPathFactory
|
||||
from config import config
|
||||
@ -118,7 +120,7 @@ class TestJournalLock:
|
||||
tmp_path_factory: TempdirFactory
|
||||
) -> Generator:
|
||||
"""Fixture for mocking config.get_str('journaldir')."""
|
||||
def get_str(key: str, *, default: Optional[str] = None) -> str:
|
||||
def get_str(key: str, *, default: str | None = None) -> str:
|
||||
"""Mock config.*Config get_str to provide fake journaldir."""
|
||||
if key == 'journaldir':
|
||||
return str(tmp_path_factory.getbasetemp())
|
||||
@ -137,7 +139,7 @@ class TestJournalLock:
|
||||
tmp_path_factory: TempdirFactory
|
||||
) -> Generator:
|
||||
"""Fixture for mocking config.get_str('journaldir')."""
|
||||
def get_str(key: str, *, default: Optional[str] = None) -> str:
|
||||
def get_str(key: str, *, default: str | None = None) -> str:
|
||||
"""Mock config.*Config get_str to provide fake journaldir."""
|
||||
if key == 'journaldir':
|
||||
return tmp_path_factory.mktemp("changing") # type: ignore
|
||||
|
@ -1,6 +1,8 @@
|
||||
"""Test the apply functions used by killswitch to modify data."""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
@ -61,7 +63,7 @@ def test_apply_no_error() -> None:
|
||||
(False, 0), (str((1 << 63)-1), (1 << 63)-1), (True, 1), (str(1 << 1337), 1 << 1337)
|
||||
]
|
||||
)
|
||||
def test_get_int(input: str, expected: Optional[int]) -> None:
|
||||
def test_get_int(input: str, expected: int | None) -> None:
|
||||
"""Check that _get_int doesn't throw when handed bad data."""
|
||||
assert expected == killswitch._get_int(input)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
"""Tests of killswitch behaviour."""
|
||||
import copy
|
||||
from typing import Optional, List
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import pytest
|
||||
import semantic_version
|
||||
|
||||
@ -34,7 +34,7 @@ TEST_SET = killswitch.KillSwitchSet([
|
||||
],
|
||||
)
|
||||
def test_killswitch(
|
||||
input: killswitch.UPDATABLE_DATA, kill: str, should_pass: bool, result: Optional[killswitch.UPDATABLE_DATA],
|
||||
input: killswitch.UPDATABLE_DATA, kill: str, should_pass: bool, result: killswitch.UPDATABLE_DATA | None,
|
||||
version: str
|
||||
) -> None:
|
||||
"""Simple killswitch tests."""
|
||||
@ -85,7 +85,7 @@ def test_operator_precedence(
|
||||
]
|
||||
)
|
||||
def test_check_multiple(
|
||||
names: List[str], input: killswitch.UPDATABLE_DATA, result: killswitch.UPDATABLE_DATA, expected_return: bool
|
||||
names: list[str], input: killswitch.UPDATABLE_DATA, result: killswitch.UPDATABLE_DATA, expected_return: bool
|
||||
) -> None:
|
||||
"""Check that order is correct when checking multiple killswitches."""
|
||||
should_return, data = TEST_SET.check_multiple_killswitches(input, *names, version='1.0.0')
|
||||
|
Loading…
x
Reference in New Issue
Block a user