Skip to content

migrate video_nodeQuery.sh to python #2851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Video/video.sh
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ else
if [[ "$session_id" != "null" && "$session_id" != "" && "$session_id" != "reserved" && "$recording_started" = "false" ]]; then
echo "$(date -u +"${ts_format}") [${process_name}] - Session: $session_id is created"
session_capabilities="$(jq -r "${JQ_SESSION_CAPABILITIES_QUERY}" "/tmp/status.json")"
return_list=($(bash "${VIDEO_CONFIG_DIRECTORY}/video_nodeQuery.sh" "${session_id}" "${session_capabilities}"))
return_list=($(python3 "${VIDEO_CONFIG_DIRECTORY}/video_nodeQuery.py" "${session_id}" "${session_capabilities}"))
caps_se_video_record="${return_list[0]}"
video_file_name="${return_list[1]}.mp4"
if [[ "$caps_se_video_record" = "true" ]]; then
Expand Down
117 changes: 117 additions & 0 deletions Video/video_nodeQuery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python3

import json
import os
import re
import sys
from typing import List, Tuple


def main() -> None:
"""
Process video recording configuration based on session capabilities.

Args:
sys.argv[1]: SESSION_ID
sys.argv[2]: SESSION_CAPABILITIES (JSON string)

Outputs:
Space-separated values: RECORD_VIDEO TEST_NAME
"""
# Define parameters
session_id = sys.argv[1] if len(sys.argv) > 1 else ""
session_capabilities = sys.argv[2] if len(sys.argv) > 2 else ""

# Environment variables with defaults
video_cap_name = os.environ.get("VIDEO_CAP_NAME", "se:recordVideo")
test_name_cap = os.environ.get("TEST_NAME_CAP", "se:name")
video_name_cap = os.environ.get("VIDEO_NAME_CAP", "se:videoName")
video_file_name_trim = os.environ.get("SE_VIDEO_FILE_NAME_TRIM_REGEX", "[:alnum:]-_")
video_file_name_suffix = os.environ.get("SE_VIDEO_FILE_NAME_SUFFIX", "true")

# Initialize variables
record_video = None
test_name = None
video_name = None

# Extract values from session capabilities if provided
if session_capabilities:
try:
capabilities = json.loads(session_capabilities)
record_video = capabilities.get(video_cap_name)
test_name = capabilities.get(test_name_cap)
video_name = capabilities.get(video_name_cap)
except (json.JSONDecodeError, AttributeError):
# If JSON parsing fails, continue with None values
pass

# Check if enabling to record video
if (isinstance(record_video, str) and record_video.lower() == "false") or record_video is False:
record_video = "false"
else:
record_video = "true"

# Check if video file name is set via capabilities
if video_name and video_name != "null":
test_name = video_name
elif test_name and test_name != "null":
test_name = test_name
Copy link
Preview

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment is redundant since test_name is being set to itself. You can remove the elif branch or adjust the logic to avoid no-op assignments.

Suggested change
test_name = test_name
pass

Copilot uses AI. Check for mistakes.

else:
test_name = ""

# Check if append session ID to the video file name suffix
if not test_name:
test_name = session_id
elif video_file_name_suffix.lower() == "true":
test_name = f"{test_name}_{session_id}"

# Normalize the video file name
test_name = normalize_filename(test_name, video_file_name_trim)

# Output the values for other scripts consuming
print(f"{record_video} {test_name}")


def normalize_filename(filename: str, trim_pattern: str) -> str:
"""
Normalize the filename by replacing spaces with underscores,
keeping only allowed characters, and truncating to 251 characters.

Args:
filename: The original filename
trim_pattern: Pattern defining allowed characters (e.g., "[:alnum:]-_")

Returns:
Normalized filename
"""
if not filename:
return ""

# Replace spaces with underscores
normalized = filename.replace(" ", "_")

# Convert trim pattern to regex
# Handle character classes like [:alnum:]
posix_classes = {
"[:alnum:]": "a-zA-Z0-9",
"[:alpha:]": "a-zA-Z",
"[:digit:]": "0-9",
"[:space:]": " \t\n\r\f\v"
}

allowed_chars = trim_pattern
for posix_class, replacement in posix_classes.items():
if posix_class in allowed_chars:
allowed_chars = allowed_chars.replace(posix_class, replacement)

pattern = f"[^{re.escape(allowed_chars)}]"

# Remove disallowed characters
normalized = re.sub(pattern, "", normalized)

# Truncate to 251 characters
return normalized[:251]


if __name__ == "__main__":
main()
49 changes: 0 additions & 49 deletions Video/video_nodeQuery.sh

This file was deleted.

Loading