Skip to content

Commit 06f7670

Browse files
committed
Add openeo_driver.config.load.exec_py_file
related to Open-EO/openeo-geopyspark-driver#936
1 parent 0c7b31d commit 06f7670

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and start a new "In Progress" section above it.
2121

2222
## In progress
2323

24+
# 0.118.0
25+
26+
- Add `openeo_driver.config.load.exec_py_file` (related to [Open-EO/openeo-geopyspark-driver#936)](https://github.com/Open-EO/openeo-geopyspark-driver/issues/936))
27+
2428
# 0.116.0
2529
- Propagate alternate `href`s of job result assets ([Open-EO/openeo-geopyspark-driver#883](https://github.com/Open-EO/openeo-geopyspark-driver/issues/883))
2630
- Ensure that a top level UDF can return a DriverVectorCube. Previously it only returned a JSONResult ([#323](https://github.com/Open-EO/openeo-python-driver/issues/323))

openeo_driver/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.117.0a2"
1+
__version__ = "0.118.0a1"

openeo_driver/config/load.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
_log = logging.getLogger(__name__)
2121

2222

23+
def exec_py_file(path: Union[str, Path]) -> dict:
24+
"""Compile and execute given python file, and get the globals from doing that."""
25+
# Based on flask's Config.from_pyfile
26+
path = Path(path)
27+
with path.open(mode="r", encoding="utf-8") as f:
28+
src = f.read()
29+
30+
ast = compile(src, path, "exec")
31+
globals = {"__file__": str(path)}
32+
exec(ast, globals)
33+
34+
return globals
35+
36+
2337
def load_from_py_file(
2438
path: Union[str, Path],
2539
variable: str = "config",
@@ -29,11 +43,7 @@ def load_from_py_file(
2943
path = Path(path)
3044
_log.debug(f"Loading configuration from Python file {path!r} (variable {variable!r})")
3145

32-
# Based on flask's Config.from_pyfile
33-
with path.open(mode="rb") as f:
34-
code = compile(f.read(), path, "exec")
35-
globals = {"__file__": str(path)}
36-
exec(code, globals)
46+
globals = exec_py_file(path)
3747

3848
try:
3949
config = globals[variable]

tests/test_config.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import re
44
import textwrap
55
from pathlib import Path
6-
from typing import List, Optional
6+
from typing import List, Optional, Callable
77

88
import attrs.exceptions
99
import pytest
10+
import dirty_equals
1011

1112
import openeo_driver.config.load
1213
from openeo_driver.config import (
@@ -18,7 +19,7 @@
1819
)
1920
from openeo_driver.config.config import check_config_definition
2021
from openeo_driver.config.env import default_from_env_as_list
21-
from openeo_driver.config.load import load_from_py_file
22+
from openeo_driver.config.load import load_from_py_file, exec_py_file
2223
import openeo_driver.config.env
2324
from .conftest import enhanced_logging
2425

@@ -31,6 +32,28 @@ def test_config_immutable():
3132
assert conf.id == "dontchangeme!"
3233

3334

35+
@pytest.mark.parametrize("path_type", [str, Path])
36+
def test_exec_py_file(tmp_path, path_type):
37+
path = tmp_path / "custom.py"
38+
39+
content = f"""
40+
def pi():
41+
return 3.14159265359
42+
43+
x = pi()
44+
"""
45+
path.write_text(textwrap.dedent(content))
46+
47+
globals = exec_py_file(path_type(path))
48+
assert globals == dirty_equals.IsPartialDict(
49+
{
50+
"__file__": str(path),
51+
"pi": dirty_equals.IsInstance(Callable),
52+
"x": 3.14159265359,
53+
}
54+
)
55+
56+
3457
@pytest.mark.parametrize("path_type", [str, Path])
3558
def test_load_from_py_file_default(tmp_path, path_type):
3659
path = tmp_path / "myconfig.py"

0 commit comments

Comments
 (0)