-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
120 lines (90 loc) · 2.65 KB
/
Copy pathhelpers.py
File metadata and controls
120 lines (90 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Helper functions for GoSubtitle application.
Provides utility functions for file validation, time conversion,
and other common operations.
"""
import os
import ctypes
from pathlib import Path
from typing import Optional
def has_console():
"""Check whether the process has a console window attached."""
try:
return bool(ctypes.windll.kernel32.GetConsoleWindow())
except Exception:
return False
def validate_xml_file(file_path: str) -> bool:
"""
Validate that a file exists and has .xml extension.
Args:
file_path: Path to the file to validate
Returns:
True if file is valid, False otherwise
"""
if not file_path:
return False
path = Path(file_path)
return path.exists() and path.suffix.lower() == '.xml'
def validate_srt_path(file_path: str) -> bool:
"""
Validate that a save path is valid for SRT files.
Args:
file_path: Path where the SRT file will be saved
Returns:
True if path is valid, False otherwise
"""
if not file_path:
return False
path = Path(file_path)
# Check if directory exists (or can be created)
parent_dir = path.parent
if not parent_dir.exists():
return False
# Ensure .srt extension
return path.suffix.lower() == '.srt'
def ensure_srt_extension(file_path: str) -> str:
"""
Ensure a file path has the .srt extension.
Args:
file_path: Original file path
Returns:
File path with .srt extension
"""
path = Path(file_path)
if path.suffix.lower() != '.srt':
return str(path.with_suffix('.srt'))
return file_path
def format_duration(frames: float, fps: int = 24) -> str:
"""
Format a duration in frames as a human-readable string.
Args:
frames: Duration in frames
fps: Frames per second
Returns:
Formatted duration string (e.g., "1h 23m 45s")
"""
seconds = frames / fps
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
parts = []
if hours > 0:
parts.append(f"{hours}h")
if minutes > 0:
parts.append(f"{minutes}m")
parts.append(f"{secs}s")
return " ".join(parts)
def get_project_directory() -> Path:
"""
Get the root directory of the project.
Returns:
Path to the project root directory
"""
return Path(__file__).resolve().parent
def get_ui_directory() -> Path:
"""
Get the UI directory path.
Returns:
Path to the UI directory
"""
return get_project_directory() / "ui"