-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (71 loc) · 2.27 KB
/
Copy pathutils.py
File metadata and controls
86 lines (71 loc) · 2.27 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
"""
Shared utilities for the 2PAC toolkit.
"""
import logging
import tempfile
import os
from contextlib import contextmanager
try:
from colorama import Fore, Style, init as _colorama_init
_colorama_init()
_HAS_COLORAMA = True
except ImportError:
_HAS_COLORAMA = False
Fore = None
Style = None
def setup_logging(verbose, no_color=False):
level = logging.DEBUG if verbose else logging.INFO
if not no_color and _HAS_COLORAMA:
COLORS = {
'DEBUG': Fore.CYAN,
'INFO': Fore.GREEN,
'WARNING': Fore.YELLOW,
'ERROR': Fore.RED,
'CRITICAL': Fore.MAGENTA + Style.BRIGHT,
'RESET': Style.RESET_ALL,
}
class ColoredFormatter(logging.Formatter):
def format(self, record):
levelname = record.levelname
if levelname in COLORS:
record.levelname = f"{COLORS[levelname]}{levelname}{COLORS['RESET']}"
record.msg = f"{COLORS[levelname]}{record.msg}{COLORS['RESET']}"
return super().format(record)
formatter = ColoredFormatter('%(asctime)s - %(levelname)s - %(message)s')
else:
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.basicConfig(
level=level,
handlers=[handler],
)
def slider_to_sensitivity(slider_value, default='medium'):
"""
Map a 1-10 slider value to a sensitivity string.
Args:
slider_value: Integer 1-10
default: Default sensitivity if mapping fails
Returns:
'low', 'medium', or 'high'
"""
sens_map = {
1: 'low', 2: 'low', 3: 'low',
4: 'medium', 5: 'medium', 6: 'medium',
7: 'high', 8: 'high', 9: 'high', 10: 'high',
}
return sens_map.get(slider_value, default)
@contextmanager
def temp_image_path(suffix='.png'):
"""
Context manager that creates a temp file, yields its path,
and ensures cleanup on exit.
"""
path = None
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
path = tmp.name
yield path
finally:
if path and os.path.exists(path):
os.unlink(path)