Skip to content

Commit d7139b9

Browse files
author
d.kovalenko
committed
ConfigurationOsOps is added
1 parent 1a11fd0 commit d7139b9

File tree

7 files changed

+173
-33
lines changed

7 files changed

+173
-33
lines changed

src/core/controller_utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
from .model import FileData as PgCfgModel__FileData
1515
# fmt: on
1616

17+
from ..os.abstract.configuration_os_ops import ConfigurationOsOps
18+
1719
from .raise_error import RaiseError
1820
from .bugcheck_error import BugCheckError
1921
from .helpers import Helpers
2022

21-
import os
2223
import typing
2324

2425
# //////////////////////////////////////////////////////////////////////////////
@@ -350,12 +351,13 @@ def Cfg__CreateAndAddTopLevelFile__AUTO(
350351
assert type(cfgData) == PgCfgModel__ConfigurationData
351352
assert type(cfgData.m_Files) == list
352353
assert type(cfgData.m_AllFilesByName) == dict
354+
assert isinstance(cfgData.OsOps, ConfigurationOsOps)
353355
assert type(file_name) == str
354356
assert file_name != ""
355-
assert os.path.basename(file_name) == file_name
357+
assert cfgData.OsOps.Path_BaseName(file_name) == file_name
356358

357-
newFilePath = os.path.join(cfgData.m_DataDir, file_name)
358-
newFilePath = os.path.normpath(newFilePath)
359+
newFilePath = cfgData.OsOps.Path_Join(cfgData.m_DataDir, file_name)
360+
newFilePath = cfgData.OsOps.Path_NormPath(newFilePath)
359361

360362
assert type(newFilePath) == str
361363
assert newFilePath != ""
@@ -370,9 +372,10 @@ def Cfg__CreateAndAddTopLevelFile__USER(
370372
assert type(cfgData.m_Files) == list
371373
assert type(cfgData.m_AllFilesByName) == dict
372374
assert type(path) == str
375+
assert isinstance(cfgData.OsOps, ConfigurationOsOps)
373376
assert path != ""
374377

375-
newFilePath = Helpers.NormalizeFilePath(cfgData.m_DataDir, path)
378+
newFilePath = Helpers.NormalizeFilePath(cfgData.OsOps, cfgData.m_DataDir, path)
376379

377380
assert type(newFilePath) == str
378381
assert newFilePath != ""
@@ -395,11 +398,12 @@ def Cfg__GetOrCreateFile__USER(
395398
assert type(cfgData) == PgCfgModel__ConfigurationData
396399
assert type(cfgData.m_Files) == list
397400
assert type(cfgData.m_AllFilesByName) == dict
401+
assert isinstance(cfgData.OsOps, ConfigurationOsOps)
398402
assert type(baseFolder) == str
399403
assert type(path) == str
400404
assert path != ""
401405

402-
newFilePath = Helpers.NormalizeFilePath(baseFolder, path)
406+
newFilePath = Helpers.NormalizeFilePath(cfgData.OsOps, baseFolder, path)
403407

404408
assert type(newFilePath) == str
405409
assert newFilePath != ""
@@ -492,13 +496,14 @@ def Helper__RegFileInCfgData(
492496
assert cfgData is not None
493497
assert fileData is not None
494498
assert type(cfgData) == PgCfgModel__ConfigurationData
499+
assert isinstance(cfgData.OsOps, ConfigurationOsOps)
495500
assert type(fileData) == PgCfgModel__FileData
496501
assert type(fileData.m_Path) == str
497502
assert fileData.m_Path != ""
498503

499504
assert fileData.IsAlive()
500505

501-
fileName = os.path.basename(fileData.m_Path)
506+
fileName = cfgData.OsOps.Path_BaseName(fileData.m_Path)
502507
assert type(fileName) == str
503508
assert fileName != ""
504509

@@ -513,12 +518,13 @@ def Helper__UnRegFileFromCfgData(
513518
assert cfgData is not None
514519
assert fileData is not None
515520
assert type(cfgData) == PgCfgModel__ConfigurationData
521+
assert isinstance(cfgData.OsOps, ConfigurationOsOps)
516522
assert type(fileData) == PgCfgModel__FileData
517523
assert fileData.m_Path != ""
518524

519525
assert fileData.IsAlive()
520526

521-
fileName = os.path.basename(fileData.m_Path)
527+
fileName = cfgData.OsOps.Path_BaseName(fileData.m_Path)
522528
assert type(fileName) == str
523529
assert fileName != ""
524530

src/core/helpers.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from .model import OptionData
55
from .bugcheck_error import BugCheckError
66

7+
from ..os.abstract.configuration_os_ops import ConfigurationOsOps
8+
79
import typing
8-
import os
910

1011
# //////////////////////////////////////////////////////////////////////////////
1112

@@ -63,24 +64,30 @@ def DoesContainerContainsValue__NotNullAndExact(
6364
return False
6465

6566
# --------------------------------------------------------------------
66-
def NormalizeFilePath(baseFolder: str, filePath: str) -> str:
67+
def NormalizeFilePath(
68+
cfgOsOps: ConfigurationOsOps,
69+
baseFolder: str,
70+
filePath: str
71+
) -> str:
72+
assert cfgOsOps is not None
73+
assert isinstance(cfgOsOps, ConfigurationOsOps)
6774
assert type(baseFolder) == str
6875
assert type(filePath) == str
6976
assert filePath != ""
7077

7178
newFilePath = None
7279

73-
if os.path.isabs(filePath):
74-
newFilePath = os.path.normpath(filePath)
80+
if cfgOsOps.Path_IsAbs(filePath):
81+
newFilePath = cfgOsOps.Path_NormPath(filePath)
7582
else:
76-
newFilePath = os.path.join(baseFolder, filePath)
77-
newFilePath = os.path.normpath(newFilePath)
83+
newFilePath = cfgOsOps.Path_Join(baseFolder, filePath)
84+
newFilePath = cfgOsOps.Path_NormPath(newFilePath)
7885

7986
assert type(newFilePath) == str
8087
assert newFilePath != ""
8188

82-
newFilePath = os.path.abspath(newFilePath)
83-
newFilePath = os.path.normcase(newFilePath)
89+
newFilePath = cfgOsOps.Path_AbsPath(newFilePath)
90+
newFilePath = cfgOsOps.Path_NormCase(newFilePath)
8491

8592
return newFilePath
8693

src/core/model.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
from .raise_error import RaiseError
77

8+
from ..os.abstract.configuration_os_ops import ConfigurationOsOps
9+
810
import typing
9-
import os
1011
import enum
1112
import datetime
1213

@@ -213,9 +214,9 @@ class FileData(ObjectData):
213214
def __init__(self, parent: ConfigurationData, path: str):
214215
assert type(parent) == ConfigurationData
215216
assert type(path) == str
216-
assert os.path.isabs(path)
217-
assert os.path.normpath(path) == path
218-
assert os.path.normcase(path) == path
217+
assert parent.OsOps.Path_IsAbs(path)
218+
assert parent.OsOps.Path_NormPath(path) == path
219+
assert parent.OsOps.Path_NormCase(path) == path
219220

220221
super().__init__()
221222

@@ -251,24 +252,35 @@ def IsAlive(self) -> bool:
251252

252253
class ConfigurationData(ObjectData):
253254
m_DataDir: str
255+
m_OsOps: ConfigurationOsOps
256+
254257
m_Files: list[FileData]
255258

256259
m_AllOptionsByName: dict[str, typing.Union[OptionData, list[OptionData]]]
257260
m_AllFilesByName: dict[str, typing.Union[FileData, list[FileData]]]
258261

259262
# --------------------------------------------------------------------
260-
def __init__(self, data_dir: str):
263+
def __init__(self, data_dir: str, osOps: ConfigurationOsOps):
261264
assert type(data_dir) == str
265+
assert isinstance(osOps, ConfigurationOsOps)
262266

263267
super().__init__()
264268

265269
self.m_DataDir = data_dir
270+
self.m_OsOps = osOps
271+
266272
self.m_Files = list[FileData]()
267273
self.m_AllOptionsByName = dict[
268274
str, typing.Union[OptionData, list[OptionData]]
269275
]()
270276
self.m_AllFilesByName = dict[str, typing.Union[FileData, list[FileData]]]()
271277

278+
# Own interface ------------------------------------------------------
279+
@property
280+
def OsOps(self) -> ConfigurationOsOps:
281+
assert isinstance(self.m_OsOps, ConfigurationOsOps)
282+
return self.m_OsOps
283+
272284
# Object interface ---------------------------------------------------
273285
def get_Parent(self) -> FileData:
274286
return None

src/implementation/v00/configuration_base.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
from ...core.bugcheck_error import BugCheckError
5959
# fmt: on
6060

61+
from ...os.abstract.configuration_os_ops import ConfigurationOsOps
62+
6163
import typing
6264
import os
6365
import io
@@ -470,7 +472,7 @@ def AddInclude(
470472
# Add/Get file
471473
# Add include element
472474

473-
baseFolder = os.path.dirname(self.m_FileLineData.m_Parent.m_Path)
475+
baseFolder = cfg.m_Data.OsOps.Path_DirName(self.m_FileLineData.m_Parent.m_Path)
474476
assert type(baseFolder) == str
475477

476478
fileData = PgCfgModel__DataControllerUtils.Cfg__GetOrCreateFile__USER(
@@ -828,7 +830,7 @@ def AddInclude(self, path: str) -> PostgresConfigurationInclude_Base:
828830
# Add empty line
829831
# Add include element
830832

831-
baseFolder = os.path.dirname(self.m_FileData.m_Path)
833+
baseFolder = self.m_Cfg.m_Data.OsOps.Path_DirName(self.m_FileData.m_Path)
832834
assert type(baseFolder) == str
833835

834836
fileData = PgCfgModel__DataControllerUtils.Cfg__GetOrCreateFile__USER(
@@ -1264,7 +1266,7 @@ def GetFileByName(self, file_name: str) -> PostgresConfigurationFile_Base:
12641266
assert type(self.m_Cfg.m_Data) == PgCfgModel__ConfigurationData
12651267
assert type(self.m_Cfg.m_Data.m_AllFilesByName) == dict
12661268

1267-
file_name2 = os.path.normcase(file_name)
1269+
file_name2 = self.m_Cfg.m_Data.OsOps.Path_NormCase(file_name)
12681270

12691271
if not (file_name2 in self.m_Cfg.m_Data.m_AllFilesByName.keys()):
12701272
RaiseError.UnknownFileName(file_name)
@@ -1276,7 +1278,7 @@ def GetFileByName(self, file_name: str) -> PostgresConfigurationFile_Base:
12761278
typeOfIndexData = type(indexData)
12771279

12781280
if typeOfIndexData == PgCfgModel__FileData:
1279-
assert os.path.basename(indexData.m_Path) == file_name2
1281+
assert self.m_Cfg.m_Data.OsOps.Path_BaseName(indexData.m_Path) == file_name2
12801282
file = PostgresConfigurationFactory_Base.GetObject(self.m_Cfg, indexData)
12811283
assert file is not None
12821284
assert isinstance(file, PostgresConfigurationFile_Base)
@@ -1395,11 +1397,14 @@ class PostgresConfiguration_Base(PostgresConfiguration, PgCfgModel__DataHandler)
13951397
m_AllOptions: PostgresConfiguration_Base__AllOptions
13961398

13971399
# --------------------------------------------------------------------
1398-
def __init__(self, data_dir: str):
1400+
def __init__(self, data_dir: str, osOps: ConfigurationOsOps):
1401+
assert type(data_dir) == str # noqa: E721
1402+
assert isinstance(osOps, ConfigurationOsOps)
1403+
13991404
super(PostgresConfiguration, self).__init__()
14001405
super(PgCfgModel__DataHandler, self).__init__()
14011406

1402-
self.m_Data = PgCfgModel__ConfigurationData(data_dir)
1407+
self.m_Data = PgCfgModel__ConfigurationData(data_dir, osOps)
14031408
self.m_AllFiles = None
14041409
self.m_AllOptions = None
14051410

@@ -1415,7 +1420,9 @@ def get_Parent(self) -> PostgresConfigurationObject:
14151420
def AddTopLevelFile(self, path: str) -> PostgresConfigurationTopLevelFile_Base:
14161421
assert type(path) == str
14171422
assert path != ""
1418-
assert os.path.basename(path) != ""
1423+
assert type(self.m_Data) == PgCfgModel__ConfigurationData # noqa: E721
1424+
assert isinstance(self.m_Data.OsOps, ConfigurationOsOps)
1425+
assert self.m_Data.OsOps.Path_BaseName(path) != ""
14191426

14201427
fileData = PgCfgModel__DataControllerUtils.Cfg__CreateAndAddTopLevelFile__USER(
14211428
self.m_Data, path
@@ -2066,12 +2073,14 @@ def Helper__GetFileForSimpleOption(
20662073
def Helper__FindFile(self, file_name: str) -> PgCfgModel__FileData:
20672074
assert type(file_name) == str
20682075
assert file_name != ""
2069-
assert os.path.basename(file_name) == file_name
2076+
assert type(self.m_Data) == PgCfgModel__ConfigurationData # noqa: E721
2077+
assert isinstance(self.m_Data.OsOps, ConfigurationOsOps)
2078+
assert self.m_Data.OsOps.Path_BaseName(file_name) == file_name
20702079

20712080
assert type(self.m_Data) == PgCfgModel__ConfigurationData
20722081
assert type(self.m_Data.m_AllFilesByName) == dict
20732082

2074-
file_name_n = os.path.normcase(file_name)
2083+
file_name_n = self.m_Data.OsOps.Path_NormCase(file_name)
20752084

20762085
if not (file_name_n in self.m_Data.m_AllFilesByName.keys()):
20772086
return None
@@ -3150,6 +3159,8 @@ class PostgresConfigurationReader_Base:
31503159
def LoadConfigurationFile(
31513160
cfg: PostgresConfiguration_Base, filePath: str
31523161
) -> PostgresConfigurationTopLevelFile_Base:
3162+
assert cfg is not None
3163+
31533164
assert isinstance(cfg, PostgresConfiguration_Base)
31543165
assert type(filePath) == str
31553166
assert filePath != ""
@@ -3184,7 +3195,7 @@ def LoadConfigurationFile(
31843195
BugCheckError.UnkFileObjectDataType(fileName, typeOfIndexData)
31853196

31863197
# ----------------------------------------------------------------
3187-
filePath_n = Helpers.NormalizeFilePath(cfg.m_Data.m_DataDir, filePath)
3198+
filePath_n = Helpers.NormalizeFilePath(cfg.m_Data.OsOps, cfg.m_Data.m_DataDir, filePath)
31883199
assert type(filePath_n) == str
31893200

31903201
if filePath_n in existFileDatas:
@@ -3959,7 +3970,7 @@ def Helper__DoWork__Stage04__OpenNewFilesToWrite(
39593970

39603971
fileCtx.File.close() # raise
39613972

3962-
os.remove(filePath) # raise
3973+
ctx.Cfg.m_Data.OsOps.Remove(filePath) # raise
39633974
continue
39643975

39653976
raise

src/implementation/v00/configuration_std.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from .configuration_base import PgCfgModel__OptionHandlerToSetValueItem
1515
from .configuration_base import PgCfgModel__OptionHandlerToWrite
1616

17+
from ...os.local.configuration_os_ops import SingleInstance as LocalCfgOsOps
18+
1719
# fmt: off
1820
from ...core.option.handlers.prepare_set_value.option_handler_to_prepare_set_value__std__generic \
1921
import OptionHandlerToPrepareSetValue__Std__Generic
@@ -306,7 +308,7 @@ def __init__(
306308
def __init__(self, data_dir: str):
307309
assert type(data_dir) == str
308310

309-
super().__init__(data_dir)
311+
super().__init__(data_dir, LocalCfgOsOps)
310312

311313
# PostgresConfiguration_Base interface -------------------------------
312314
def Internal__GetAutoConfFileName(self):
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# //////////////////////////////////////////////////////////////////////////////
2+
# Postgres Pro. PostgreSQL Configuration Python Library.
3+
4+
from __future__ import annotations
5+
6+
from ...core.raise_error import RaiseError
7+
8+
# //////////////////////////////////////////////////////////////////////////////
9+
# class ConfigurationOsOps
10+
11+
12+
class ConfigurationOsOps:
13+
def Path_IsAbs(self, a: str) -> bool:
14+
assert type(a) == str # noqa: E721
15+
RaiseError.MethodIsNotImplemented(__class__, "Path_IsAbs")
16+
17+
def Path_Join(self, a: str, *p: tuple) -> str:
18+
assert type(a) == str # noqa: E721
19+
assert type(p) == tuple # noqa: E721
20+
RaiseError.MethodIsNotImplemented(__class__, "Path_Join")
21+
22+
def Path_NormPath(self, a: str) -> str:
23+
assert type(a) == str # noqa: E721
24+
RaiseError.MethodIsNotImplemented(__class__, "Path_NormPath")
25+
26+
def Path_AbsPath(self, a: str) -> str:
27+
assert type(a) == str # noqa: E721
28+
RaiseError.MethodIsNotImplemented(__class__, "Path_AbsPath")
29+
30+
def Path_NormCase(self, a: str) -> str:
31+
assert type(a) == str # noqa: E721
32+
RaiseError.MethodIsNotImplemented(__class__, "Path_NormCase")
33+
34+
def Path_DirName(self, a: str) -> str:
35+
assert type(a) == str # noqa: E721
36+
RaiseError.MethodIsNotImplemented(__class__, "Path_DirName")
37+
38+
def Path_BaseName(self, a: str) -> str:
39+
assert type(a) == str # noqa: E721
40+
RaiseError.MethodIsNotImplemented(__class__, "Path_BaseName")
41+
42+
def Remove(self, a: str) -> str:
43+
assert type(a) == str # noqa: E721
44+
RaiseError.MethodIsNotImplemented(__class__, "Remove")
45+
46+
47+
# //////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)