fix: type hint
chore: extract common UT GameOption(s)
This commit is contained in:
61
src/pygamecfg/common_ut.py
Normal file
61
src/pygamecfg/common_ut.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""common UT functions"""
|
||||
from __future__ import annotations
|
||||
from typing import Union
|
||||
|
||||
from os.path import join
|
||||
from pathlib import Path
|
||||
|
||||
from pysimpleini import PySimpleINI
|
||||
|
||||
from .core import GameOption, OptionType
|
||||
|
||||
|
||||
class GameOption_UT(GameOption):
|
||||
"""generic UT Option class"""
|
||||
|
||||
szGameType = ""
|
||||
TValueType = OptionType.OT_INVALID
|
||||
|
||||
szOptionName = ""
|
||||
szSectionName = ""
|
||||
szKeyName = ""
|
||||
bForceAdd: bool = False
|
||||
bRemovable: bool = False
|
||||
|
||||
cachedFile: Union[None, PySimpleINI] = None
|
||||
cachedFilePath: Union[None, Path] = None
|
||||
|
||||
Cls_PySimpleINI: type[PySimpleINI] = PySimpleINI
|
||||
|
||||
@classmethod
|
||||
def openFile(cls, filepath: Path) -> PySimpleINI:
|
||||
"""Open the file"""
|
||||
if (not cls.cachedFile) or (filepath != cls.cachedFilePath):
|
||||
cls.cachedFilePath = filepath
|
||||
cls.cachedFile = cls.Cls_PySimpleINI(filepath)
|
||||
return cls.cachedFile
|
||||
|
||||
def __init__(self, GameRootDir: str, ConfigFileRelPath: str) -> None:
|
||||
super().__init__(GameRootDir, ConfigFileRelPath)
|
||||
self.mainConfigFilePath: Path = Path(join(GameRootDir, ConfigFileRelPath))
|
||||
self.inifile = self.openFile(self.mainConfigFilePath)
|
||||
|
||||
def set(self, value: str) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
self.format(value)
|
||||
self.inifile.setaddkeyvalue(self.szSectionName, self.szKeyName, self.szFormatedValue, self.bForceAdd)
|
||||
self.inifile.writefile()
|
||||
|
||||
def rem(self, value: Union[None, str]) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
if not self.bRemovable:
|
||||
raise RuntimeError("this options is not removable")
|
||||
self.inifile.delkey_ex(self.szSectionName, self.szKeyName, None, value)
|
||||
self.inifile.writefile()
|
||||
|
||||
def get(self) -> Union[str, list[str]]:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName not set")
|
||||
return self.inifile.getkeyvalue(self.szSectionName, self.szKeyName)
|
||||
@@ -1,4 +1,4 @@
|
||||
# pylint: disable=duplicate-code,missing-class-docstring,missing-module-docstring,missing-function-docstring
|
||||
# pylint: disable=missing-class-docstring,missing-module-docstring,missing-function-docstring,duplicate-code
|
||||
from __future__ import annotations
|
||||
from typing import Union
|
||||
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
# pylint: disable=duplicate-code,missing-class-docstring,missing-module-docstring,missing-function-docstring
|
||||
# pylint: disable=missing-class-docstring,missing-module-docstring,missing-function-docstring,duplicate-code
|
||||
from __future__ import annotations
|
||||
from typing import Union
|
||||
from os.path import join
|
||||
from pathlib import Path
|
||||
|
||||
from pysimpleini import KeyNotFoundError
|
||||
|
||||
from .core import GameOptions_Factory_Register, GameOption, OptionType
|
||||
from .core import GameOptions_Factory_Register, OptionType
|
||||
from .tool_ini import PySimpleINI_GroupKeysInSection
|
||||
from .common_ut import GameOption_UT
|
||||
|
||||
|
||||
class ChaChaSimpleINI_UT2k4(PySimpleINI_GroupKeysInSection):
|
||||
class PySimpleINI_UT2k4(PySimpleINI_GroupKeysInSection):
|
||||
GroupRules = [
|
||||
("Engine.GameEngine", "ServerPackages"),
|
||||
("Engine.GameEngine", "ServerActors"),
|
||||
@@ -22,50 +21,9 @@ class ChaChaSimpleINI_UT2k4(PySimpleINI_GroupKeysInSection):
|
||||
]
|
||||
|
||||
|
||||
class GameOption_UT2k4(GameOption):
|
||||
class GameOption_UT2k4(GameOption_UT):
|
||||
szGameType = "ut2k4"
|
||||
TValueType = OptionType.OT_INVALID
|
||||
|
||||
szOptionName = ""
|
||||
szSectionName = ""
|
||||
szKeyName = ""
|
||||
bForceAdd: bool = False
|
||||
bRemovable: bool = False
|
||||
|
||||
cachedFile: Union[None, ChaChaSimpleINI_UT2k4] = None
|
||||
cachedFilePath: Union[None, Path] = None
|
||||
|
||||
@classmethod
|
||||
def openFile(cls, filepath: Path) -> ChaChaSimpleINI_UT2k4:
|
||||
if (not GameOption_UT2k4.cachedFile) or (filepath != GameOption_UT2k4.cachedFilePath):
|
||||
GameOption_UT2k4.cachedFilePath = filepath
|
||||
GameOption_UT2k4.cachedFile = ChaChaSimpleINI_UT2k4(filepath)
|
||||
return GameOption_UT2k4.cachedFile
|
||||
|
||||
def __init__(self, GameRootDir: str, ConfigFileRelPath: str) -> None:
|
||||
super().__init__(GameRootDir, ConfigFileRelPath)
|
||||
self.mainConfigFilePath: Path = Path(join(GameRootDir, ConfigFileRelPath))
|
||||
self.inifile = ChaChaSimpleINI_UT2k4(self.mainConfigFilePath)
|
||||
|
||||
def set(self, value: str) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
self.format(value)
|
||||
self.inifile.setaddkeyvalue(self.szSectionName, self.szKeyName, self.szFormatedValue, self.bForceAdd)
|
||||
self.inifile.writefile()
|
||||
|
||||
def rem(self, value: Union[None, str]) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
if not self.bRemovable:
|
||||
raise RuntimeError("this options is not removable")
|
||||
self.inifile.delkey_ex(self.szSectionName, self.szKeyName, None, value)
|
||||
self.inifile.writefile()
|
||||
|
||||
def get(self) -> Union[str, list[str]]:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName not set")
|
||||
return self.inifile.getkeyvalue(self.szSectionName, self.szKeyName)
|
||||
Cls_PySimpleINI = PySimpleINI_UT2k4
|
||||
|
||||
|
||||
class GameOption_UT2k4_GenAdd(GameOption_UT2k4):
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
# pylint: disable=duplicate-code,missing-class-docstring,missing-module-docstring,missing-function-docstring
|
||||
# pylint: disable=missing-class-docstring,missing-module-docstring,missing-function-docstring,duplicate-code
|
||||
from __future__ import annotations
|
||||
from typing import Union
|
||||
|
||||
from os.path import join
|
||||
from pathlib import Path
|
||||
|
||||
from pysimpleini import KeyNotFoundError, SectionNotFoundError
|
||||
|
||||
from .core import GameOptions_Factory_Register, GameOption, OptionType
|
||||
from .core import GameOptions_Factory_Register, OptionType
|
||||
from .tool_ini import PySimpleINI_GroupKeysInSection
|
||||
from .common_ut import GameOption_UT
|
||||
|
||||
|
||||
class PySimpleINI_UT99(PySimpleINI_GroupKeysInSection):
|
||||
@@ -22,51 +20,9 @@ class PySimpleINI_UT99(PySimpleINI_GroupKeysInSection):
|
||||
]
|
||||
|
||||
|
||||
class GameOption_UT99(GameOption):
|
||||
class GameOption_UT99(GameOption_UT):
|
||||
szGameType = "ut99"
|
||||
TValueType = OptionType.OT_INVALID
|
||||
|
||||
szOptionName = ""
|
||||
szSectionName = ""
|
||||
szKeyName = ""
|
||||
bForceAdd: bool = False
|
||||
bRemovable: bool = False
|
||||
|
||||
cachedFile: Union[None, PySimpleINI_UT99] = None
|
||||
cachedFilePath: Union[None, Path] = None
|
||||
|
||||
@classmethod
|
||||
def openFile(cls, filepath: Path) -> PySimpleINI_UT99:
|
||||
if (not GameOption_UT99.cachedFile) or (filepath != GameOption_UT99.cachedFilePath):
|
||||
GameOption_UT99.cachedFilePath = filepath
|
||||
GameOption_UT99.cachedFile = PySimpleINI_UT99(filepath)
|
||||
return GameOption_UT99.cachedFile
|
||||
|
||||
def __init__(self, GameRootDir: str, ConfigFileRelPath: str) -> None:
|
||||
super().__init__(GameRootDir, ConfigFileRelPath)
|
||||
self.mainConfigFilePath: Path = Path(join(GameRootDir, ConfigFileRelPath))
|
||||
# self.inifile = PySimpleINI_UT99(self.mainConfigFilePath)
|
||||
self.inifile = GameOption_UT99.openFile(self.mainConfigFilePath)
|
||||
|
||||
def set(self, value: str) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
self.format(value)
|
||||
self.inifile.setaddkeyvalue(self.szSectionName, self.szKeyName, self.szFormatedValue, self.bForceAdd)
|
||||
self.inifile.writefile()
|
||||
|
||||
def rem(self, value: Union[None, str]) -> None:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName is not set")
|
||||
if not self.bRemovable:
|
||||
raise RuntimeError("this options is not removable")
|
||||
self.inifile.delkey_ex(self.szSectionName, self.szKeyName, None, value)
|
||||
self.inifile.writefile()
|
||||
|
||||
def get(self) -> Union[str, list[str]]:
|
||||
if not self.szOptionName:
|
||||
raise RuntimeError("szOptionName not set")
|
||||
return self.inifile.getkeyvalue(self.szSectionName, self.szKeyName)
|
||||
Cls_PySimpleINI = PySimpleINI_UT99
|
||||
|
||||
|
||||
class GameOption_UT99_GenAdd(GameOption_UT99):
|
||||
|
||||
@@ -10,9 +10,6 @@ import unittest
|
||||
from io import StringIO
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
|
||||
print(__name__)
|
||||
print(__package__)
|
||||
|
||||
from src import pygamecfg
|
||||
from src.pygamecfg.__main__ import fct_main
|
||||
|
||||
|
||||
@@ -11,17 +11,11 @@ from os import linesep
|
||||
from io import StringIO
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
|
||||
print(__name__)
|
||||
print(__package__)
|
||||
|
||||
from src import pygamecfg
|
||||
from src.pygamecfg.__main__ import fct_main
|
||||
|
||||
|
||||
class Testtest_ut99(unittest.TestCase):
|
||||
def test_normal_TESSTTT(self):
|
||||
fct_main(["-v", "-g", "ut99", "-b", "test/data/UT99", "GetOption", "ServerPackages"])
|
||||
|
||||
def test_normal_ServerPackages(self):
|
||||
with redirect_stdout(StringIO()) as capted_stdout, redirect_stderr(StringIO()) as capted_stderr:
|
||||
fct_main(["-g", "ut99", "-b", "test/data/UT99", "GetOption", "ServerPackages"])
|
||||
@@ -98,8 +92,6 @@ class Testtest_ut99(unittest.TestCase):
|
||||
self.assertEqual("", capted_stderr.getvalue())
|
||||
|
||||
def test_normal_AdminEmail(self):
|
||||
# fct_main(["-g", "ut99", "-b", "test/data/UT99", "GetOption", "AdminEmail"])
|
||||
# return
|
||||
with redirect_stdout(StringIO()) as capted_stdout, redirect_stderr(StringIO()) as capted_stderr:
|
||||
fct_main(["-g", "ut99", "-b", "test/data/UT99", "GetOption", "AdminEmail"])
|
||||
# /!\ add '\n' at the end of the string cause Python terminal newline is always this, regardless Windows / Linux os.linesep
|
||||
|
||||
Reference in New Issue
Block a user