-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
169 lines (134 loc) · 4.05 KB
/
common.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Core infrastructure module providing enhanced resource management and error handling.
This module integrates and coordinates the core components required for video processing
while avoiding circular dependencies through proper type management.
"""
import logging
#import os
#from pathlib import Path
#import signal
#import threading
#from contextlib import contextmanager
from typing import Optional #Dict, Optional, Set, TypeVar
# Import core types
from core.types import (
Frame, RGBFrame, GrayFrame,
FrameMetadata, VideoMetadata,
FrameProcessor, FrameAnalyzer, FrameFilter,
VideoReader, AnalysisResult, ProcessingState,
ProcessingResults
)
# Import error types
from core.errors import (
VideoError,
CodecError,
ResourceError,
TimeoutError,
FrameError
)
# Import configuration
from config import OutputFormat, VideoConfig
# Import core functionality
from retry import RetryManager
from resource import VideoResource, timeout
__all__ = [
# Core types
'Frame', 'RGBFrame', 'GrayFrame',
'FrameMetadata', 'VideoMetadata',
'FrameProcessor', 'FrameAnalyzer', 'FrameFilter',
'VideoReader', 'AnalysisResult', 'ProcessingState',
'ProcessingResults',
# Errors
'VideoError', 'CodecError', 'ResourceError',
'TimeoutError', 'FrameError',
# Configuration
'OutputFormat', 'VideoConfig',
# Core classes
'RetryManager', 'VideoResource',
# Utilities
'timeout',
# Module functions
'setup_logging', 'initialize'
]
# Module constants
DEFAULT_LOGGING_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
DEFAULT_LOG_LEVEL = logging.INFO
# Global state
_initialized = False
_log_configured = False
def setup_logging(
level: int = DEFAULT_LOG_LEVEL,
format_string: str = DEFAULT_LOGGING_FORMAT,
log_file: Optional[str] = None
) -> None:
"""
Configure logging for the video processing system.
Args:
level: Logging level (default: INFO)
format_string: Log format string
log_file: Optional log file path
This function is idempotent and can be called multiple times.
"""
global _log_configured
if _log_configured:
logger.debug("Logging already configured")
return
# Configure basic logging
logging.basicConfig(
level=level,
format=format_string,
filename=log_file
)
# Add console handler if logging to file
if log_file:
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(format_string))
logging.getLogger().addHandler(console_handler)
logger.info("Logging configured at level %s", level)
_log_configured = True
def initialize(
log_level: int = DEFAULT_LOG_LEVEL,
log_format: str = DEFAULT_LOGGING_FORMAT,
log_file: Optional[str] = None
) -> None:
"""
Initialize the video processing system.
Args:
log_level: Logging level to use
log_format: Logging format string
log_file: Optional log file path
This function is idempotent and can be called multiple times.
"""
global _initialized
if _initialized:
logger.debug("System already initialized")
return
# Configure logging first
setup_logging(log_level, log_format, log_file)
# Additional initialization can be added here
# For example:
# - Check system capabilities
# - Initialize global resources
# - Set up signal handlers
logger.info("Video processing system initialized")
_initialized = True
def cleanup() -> None:
"""
Cleanup system resources.
This function should be called before system shutdown.
"""
global _initialized, _log_configured
# Add cleanup logic here
# For example:
# - Release global resources
# - Close log handlers
# - Reset state
_initialized = False
_log_configured = False
logger.info("System cleanup completed")
logger = logging.getLogger(__name__)
# Initialize system on module import
initialize()
# Register cleanup on interpreter shutdown
import atexit
atexit.register(cleanup)