Skip to content

Commit 744321b

Browse files
committed
py_lua_helper: added more helper methods: is_table, get_type, return fallback value when querying value directly from table
1 parent a4060aa commit 744321b

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

python_lua_helper/py_lua_helper.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ def _parse_results(self):
299299
self._variables[filename] = f.read().decode(
300300
"utf-8", errors="ignore"
301301
)
302-
303302
# Read metadata
304303
with open(os.path.join(self.meta_dir, filename), "rb") as f:
305304
self._metadata[filename] = f.read().decode("utf-8", errors="ignore")
@@ -343,8 +342,27 @@ def items(self) -> List[tuple]:
343342
"""Get list of (name, value) tuples."""
344343
return list(self._variables.items())
345344

345+
def is_table(self, key: str) -> bool:
346+
"""Check variable is a table, return true or false"""
347+
if key in self._metadata:
348+
match = re.match(r"^table.*", self._metadata[key])
349+
if match:
350+
return True
351+
return False
352+
353+
def get_type(self, key: str) -> str:
354+
"""Get variable type"""
355+
if self.is_table(key):
356+
return "table"
357+
if key in self._metadata and re.match(r"^string.*", self._metadata[key]):
358+
return "string"
359+
return self._metadata.get(key, "none")
360+
346361
def get(self, key: str, default: str = None) -> str:
347362
"""Get variable value with default."""
363+
# cannot get value of table directly, so, return default
364+
if self.is_table(key):
365+
return default
348366
return self._variables.get(key, default)
349367

350368
def get_int(self, key: str, default: int = None) -> int:

python_lua_helper/test_example.py

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
print("")
2929

3030
# Check variable availability
31+
print("example.py: === value availability tests ===")
3132
print("example.py: check for config.empty variable availability is ", end="")
3233
try:
3334
if "config.empty" in cfg:
3435
print("passed, but should fail !!!")
3536
else:
3637
print("failed, as expected")
3738
except Exception:
38-
print("failed, as expected")
39+
print("failed (exception)")
3940

4041
print("example.py: check for wrong.table variable availability is ", end="")
4142
try:
@@ -44,7 +45,7 @@
4445
else:
4546
print("failed, as expected")
4647
except Exception:
47-
print("failed, as expected")
48+
print("failed (exception)")
4849

4950
print("example.py: check for empty variable availability is ", end="")
5051
try:
@@ -53,7 +54,7 @@
5354
else:
5455
print("failed, as expected")
5556
except Exception:
56-
print("failed, as expected")
57+
print("failed (exception)")
5758

5859
print("example.py: check for config.value variable availability is ", end="")
5960
try:
@@ -62,7 +63,7 @@
6263
else:
6364
print("failed, as expected")
6465
except Exception:
65-
print("failed, as expected")
66+
print("failed (exception)")
6667

6768
print("example.py: check for config.sub.string variable availability is ", end="")
6869
try:
@@ -71,7 +72,7 @@
7172
else:
7273
print("failed, but should pass !!!")
7374
except Exception:
74-
print("failed, but should pass !!!")
75+
print("failed (exception)")
7576

7677
print("example.py: check for config.sub variable availability is ", end="")
7778
try:
@@ -80,35 +81,51 @@
8081
else:
8182
print("failed, but should pass !!!")
8283
except Exception:
83-
print("failed, but should pass !!!")
84-
85-
print(f"example.py: config.value is not selected for export, so cfg['config.value'] = {cfg.get('config.value', 'NOT_FOUND')}")
86-
print(f"example.py: config.empty is not found in lua config file, so cfg['config.empty'] = {cfg.get('config.empty', 'NOT_FOUND')}")
87-
print(f"example.py: cfg['config.paths.tempdir'] = {cfg.get('config.paths.tempdir', 'NOT_FOUND')}")
88-
print(f"example.py: cfg['config.paths.workdir'] = {cfg.get('config.paths.workdir', 'NOT_FOUND')}")
89-
print(f"example.py: cfg['config.paths.dynpath'] = {cfg.get('config.paths.dynpath', 'NOT_FOUND')}")
90-
print(f"example.py: cfg['config.paths.tempdir_raw'] = {cfg.get('config.paths.tempdir_raw', 'NOT_FOUND')}")
91-
print(f"example.py: cfg['config.paths.workdir_raw'] = {cfg.get('config.paths.workdir_raw', 'NOT_FOUND')}")
92-
print(f"example.py: cfg['config.paths.dynpath_raw'] = {cfg.get('config.paths.dynpath_raw', 'NOT_FOUND')}")
93-
print(f"example.py: cfg['config.sub.lua_v1'] = {cfg.get('config.sub.lua_v1', 'NOT_FOUND')}")
94-
print(f"example.py: cfg['config.sub.lua_v2'] = {cfg.get('config.sub.lua_v2', 'NOT_FOUND')}")
95-
print(f"example.py: cfg['config.sub.lua_v3'] = {cfg.get('config.sub.lua_v3', 'NOT_FOUND')}")
96-
print(f"example.py: cfg['config.sub.lua_num'] = {cfg.get('config.sub.lua_num', 'NOT_FOUND')}")
97-
print(f"example.py: cfg['config.sub.extra_1'] = {cfg.get('config.sub.extra_1', 'NOT_FOUND')}")
98-
print(f"example.py: cfg['config.sub.extra_2'] = {cfg.get('config.sub.extra_2', 'NOT_FOUND')}")
99-
print(f"example.py: cfg['config.sub.number1'] = {cfg.get('config.sub.number1', 'NOT_FOUND')}")
100-
print(f"example.py: cfg['config.sub.string'] = {cfg.get('config.sub.string', 'NOT_FOUND')}")
101-
print(f"example.py: cfg['config.sub.problematic_string'] = {cfg.get('config.sub.problematic_string', 'NOT_FOUND')}")
102-
print(f"example.py: cfg['config.sub.non_latin_string'] = {cfg.get('config.sub.non_latin_string', 'NOT_FOUND')}")
103-
print(f"example.py: (should be empty regardless of fallback value, because it is a container: cfg['config.sub.sub']) = {cfg.get('config.sub.sub', 'NOT_FOUND')}")
104-
print(f"example.py: cfg['config.sub.sub'] as int with fallback value -1) = {cfg.get_int('config.sub.sub',-1)}")
105-
print(f"example.py: cfg['config.sub.sub'] as int with fallback value -1.6) = {cfg.get_int('config.sub.sub',-1.6)}")
106-
print(f"example.py: cfg['config.sub.sub'] as float with fallback value -1.5) = {cfg.get_float('config.sub.sub',-1.5)}")
107-
print(f"example.py: cfg['config.sub.sub.message'] = {cfg.get('config.sub.sub.message', 'NOT_FOUND')}")
108-
print(f"example.py: cfg['config.sub.sub.message2'] = {cfg.get('config.sub.sub.message2', 'NOT_FOUND')}")
84+
print("failed (exception)")
85+
86+
print("example.py: === value query tests ===")
87+
print(f"example.py: not selected for export, should return fallback (NOT_FOUND): cfg['config.value'] = {cfg.get('config.value', 'NOT_FOUND')}, type = {cfg.get_type('config.value')}")
88+
print(f"example.py: not found in config, should return fallback (NOT_FOUND): cfg['config.empty'] = {cfg.get('config.empty', 'NOT_FOUND')}, type = {cfg.get_type('config.empty')}")
89+
print(f"example.py: cfg['config.paths.tempdir'] = {cfg.get('config.paths.tempdir', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.tempdir')}")
90+
print(f"example.py: cfg['config.paths.workdir'] = {cfg.get('config.paths.workdir', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.workdir')}")
91+
print(f"example.py: cfg['config.paths.dynpath'] = {cfg.get('config.paths.dynpath', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.dynpath')}")
92+
print(f"example.py: cfg['config.paths.tempdir_raw'] = {cfg.get('config.paths.tempdir_raw', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.tempdir_raw')}")
93+
print(f"example.py: cfg['config.paths.workdir_raw'] = {cfg.get('config.paths.workdir_raw', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.workdir_raw')}")
94+
print(f"example.py: cfg['config.paths.dynpath_raw'] = {cfg.get('config.paths.dynpath_raw', 'NOT_FOUND')}, type = {cfg.get_type('config.paths.dynpath_raw')}")
95+
print(f"example.py: cfg['config.sub.lua_v1'] = {cfg.get('config.sub.lua_v1', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.lua_v1')}")
96+
print(f"example.py: cfg['config.sub.lua_v2'] = {cfg.get('config.sub.lua_v2', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.lua_v2')}")
97+
print(f"example.py: cfg['config.sub.lua_v3'] = {cfg.get('config.sub.lua_v3', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.lua_v3')}")
98+
print(f"example.py: cfg['config.sub.lua_num'] = {cfg.get('config.sub.lua_num', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.lua_num')}")
99+
print(f"example.py: cfg['config.sub.extra_1'] = {cfg.get('config.sub.extra_1', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.extra_1')}")
100+
print(f"example.py: cfg['config.sub.extra_2'] = {cfg.get('config.sub.extra_2', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.extra_2')}")
101+
print(f"example.py: cfg['config.sub.number1'] = {cfg.get('config.sub.number1', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.number1')}")
102+
print(f"example.py: cfg['config.sub.number2'] = {cfg.get('config.sub.number2', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.number2')}")
103+
print(f"example.py: cfg['config.sub.string'] = {cfg.get('config.sub.string', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.string')}")
104+
print(f"example.py: cfg['config.sub.problematic_string'] = {cfg.get('config.sub.problematic_string', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.problematic_string')}")
105+
print(f"example.py: cfg['config.sub.non_latin_string'] = {cfg.get('config.sub.non_latin_string', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.non_latin_string')}")
106+
107+
# Test table access
108+
print("example.py: === table tests for cfg['config.sub.sub'] ===")
109+
print(f"example.py: cfg.is_table('config.sub.sub') = {cfg.is_table('config.sub.sub')}, type = {cfg.get_type('config.sub.sub')}")
110+
print(f"example.py: table value should return fallback (NOT_FOUND): cfg['config.sub.sub'] = {cfg.get('config.sub.sub', 'NOT_FOUND')}")
111+
print(f"example.py: table value as int should return fallback (-1): cfg['config.sub.sub'] = {cfg.get_int('config.sub.sub',-1)}")
112+
print(f"example.py: table value as int should return fallback (-1.6 as int == -1): cfg['config.sub.sub'] = {cfg.get_int('config.sub.sub',-1.6)}")
113+
print(f"example.py: table value as float should return fallback (-1.5): cfg['config.sub.sub'] = {cfg.get_float('config.sub.sub',-1.5)}")
114+
print("example.py: === table's value tests for cfg['config.sub.sub'] ===")
115+
print(f"example.py: cfg['config.sub.sub.message'] = {cfg.get('config.sub.sub.message', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.sub.message')}")
116+
print(f"example.py: cfg['config.sub.sub.message2'] = {cfg.get('config.sub.sub.message2', 'NOT_FOUND')}, type = {cfg.get_type('config.sub.sub.message2')}")
109117
print(f"example.py: cfg['config.sub.multiline_string'] = {cfg.get('config.sub.multiline_string', 'NOT_FOUND')}")
110118

119+
# Test empty table access
120+
print("example.py: === empty table tests for cfg['config.sub.empty_table'] ===")
121+
print(f"example.py: cfg.is_table('config.sub.empty_table') = {cfg.is_table('config.sub.empty_table')}, type = {cfg.get_type('config.sub.empty_table')}")
122+
print(f"example.py: table value should return fallback (NOT_FOUND): cfg['config.sub.empty_table'] = {cfg.get('config.sub.empty_table', 'NOT_FOUND')}")
123+
print(f"example.py: table start for cfg['config.sub.empty_table'] = {cfg.get_table_start('config.sub.empty_table')}")
124+
print(f"example.py: table end for cfg['config.sub.empty_table'] = {cfg.get_table_end('config.sub.empty_table')}")
125+
111126
# Test mixed table access (indexed and named elements)
127+
print("example.py: === mixed table tests for cfg['config.sub.mixed'] ===")
128+
print(f"example.py: cfg.is_table('config.sub.mixed') = {cfg.is_table('config.sub.mixed')}, type = {cfg.get_type('config.sub.mixed')}")
112129
print(f"example.py: table start for config.sub.mixed: {cfg.get_table_start('config.sub.mixed')}")
113130
print(f"example.py: table end for config.sub.mixed: {cfg.get_table_end('config.sub.mixed')}")
114131
print(f"example.py: table indices sequence for config.sub.mixed: {cfg.get_table_seq('config.sub.mixed')}")

python_lua_helper/test_example/example.cfg.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ config =
2424
extra_1=loader.extra[1],
2525
extra_2=loader.extra[2],
2626
loader_args=loader.args,
27-
mixed={ 1, "text", true, key="test_value" }
27+
mixed={ 1, "text", true, key="test_value" },
28+
empty_table={ }
2829
},
2930
paths=
3031
{

0 commit comments

Comments
 (0)