Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ generate-test-outputs:
@rm -rf tests/outputs/with_hooks_dir
@uv run efemel process "**/*.py" --cwd tests/inputs/basic --out tests/outputs/with_hooks_dir --hooks tests/hooks/multiple

@rm -rf tests/outputs/with_hooks_dir
@uv run efemel process "**/*.py" --cwd tests/inputs/process_data --out tests/outputs/process_data --pick user_data
@rm -rf tests/outputs/process_data_pick
@uv run efemel process "**/*.py" --cwd tests/inputs/process_data --out tests/outputs/process_data_pick --pick user_data

@rm -rf tests/outputs/process_data_unwrap
@uv run efemel process "**/*.py" --cwd tests/inputs/process_data --out tests/outputs/process_data_unwrap --unwrap user_data


# Clean build artifacts and cache files
Expand Down
11 changes: 10 additions & 1 deletion efemel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def info():
multiple=True,
help="Pick specific dictionary keys to extract (can be used multiple times)",
)
def process(file_pattern, out, flatten, cwd, env, workers, hooks, pick):
@click.option(
"--unwrap",
"-u",
multiple=True,
help="Extract specific values from the processed data dictionary, merging them (can be used multiple times)",
)
def process(file_pattern, out, flatten, cwd, env, workers, hooks, pick, unwrap):
"""Process Python files and extract public dictionary variables to JSON.

FILE_PATTERN: Glob pattern to match Python files (e.g., "**/*.py")
Expand All @@ -75,6 +81,9 @@ def process(file_pattern, out, flatten, cwd, env, workers, hooks, pick):
if pick:
hooks_manager.add("process_data", process_data_hooks.pick_data(pick))

if unwrap:
hooks_manager.add("process_data", process_data_hooks.unwrap_data(unwrap))

# Load user-defined hooks if a path is specified
if hooks:
if os.path.isfile(hooks):
Expand Down
23 changes: 23 additions & 0 deletions efemel/hooks/process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,26 @@ def _pick(context):
context["data"] = {key: value for key, value in context["data"].items() if key in keys}

return _pick


def unwrap_data(keys):
"""Extracts specific values from the processed data dictionary, merges them."""

def _unwrap(context):
if not keys:
return

merged = {}

for key in keys:
if key not in context["data"]:
continue

value = context["data"][key]

if isinstance(value, dict):
merged.update(value)

context["data"] = merged

return _unwrap
10 changes: 10 additions & 0 deletions tests/outputs/process_data_unwrap/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"admin": {
"name": "Admin User",
"role": "admin"
},
"guest": {
"name": "Guest User",
"role": "guest"
}
}
10 changes: 10 additions & 0 deletions tests/outputs/with_hooks_dir/simple_test_1_2_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"test_dict": {
"key": "value",
"number": 42
},
"config": {
"app": "test",
"version": "1.0"
}
}
33 changes: 33 additions & 0 deletions tests/outputs/with_hooks_dir/test_data_1_2_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"config": {
"name": "test_app",
"version": "1.0.0",
"debug": true,
"features": [
"auth",
"api",
"ui"
]
},
"settings": {
"database": {
"host": "localhost",
"port": 5432,
"name": "app_db"
},
"cache": {
"type": "redis",
"ttl": 3600
}
},
"user_data": {
"admin": {
"name": "Admin User",
"role": "admin"
},
"guest": {
"name": "Guest User",
"role": "guest"
}
}
}
10 changes: 10 additions & 0 deletions tests/outputs/with_hooks_dir/test_dir/config_1_2_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app_config": {
"app_name": "my_app",
"port": 8080
},
"database_config": {
"url": "postgresql://localhost/mydb",
"pool_size": 10
}
}
14 changes: 14 additions & 0 deletions tests/outputs/with_hooks_dir/test_dir/subdir/nested_1_2_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"nested_data": {
"level": "deep",
"items": [
1,
2,
3
]
},
"metadata": {
"created_by": "test",
"timestamp": "2025-01-01"
}
}
1 change: 1 addition & 0 deletions tests/outputs/with_hooks_dir/test_dir/utils_1_2_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 7 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ def get_test_scenarios():
{
"name": "process data - pick",
"inputs_dir": test_dir / "inputs/process_data",
"outputs_dir": test_dir / "outputs/process_data",
"outputs_dir": test_dir / "outputs/process_data_pick",
"process_args": ["--pick", "user_data"],
},
{
"name": "process data - unwrap",
"inputs_dir": test_dir / "inputs/process_data",
"outputs_dir": test_dir / "outputs/process_data_unwrap",
"process_args": ["--unwrap", "user_data"],
},
]


Expand Down
Loading