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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
# Copyright (C) 2005-2011 Francois Meyer (dulle at free.fr)
# Copyright (C) 2012-2026 team free-astro (see more in AUTHORS file)
# Reference site is https://siril.org
# SPDX-License-Identifier: GPL-3.0-or-later
"""
SirilPy - Python interface for Siril astronomical image processing software.
This module provides bindings and utilities for interacting with Siril
from Python, enabling advanced astronomical image processing workflows.
"""
# Core imports required to configure stdout and stderr to accept utf-8
import io
import os
import sys
import locale
def _fix_locale():
"""
Validate and fix the process locale:
- Keep the current locale if it's supported by Python.
- Otherwise, fall back to a known good UTF-8 locale (en_US.UTF-8).
- Never fall back to 'C' or 'C.UTF-8'.
"""
def is_locale_supported(loc_name: str) -> bool:
"""Return True if loc_name works in both Python and Babel."""
try:
# Try to activate in Python's locale system
locale.setlocale(locale.LC_ALL, loc_name)
lang, encoding = locale.getlocale()
if not lang or not encoding:
return False
return True
except Exception:
return False
# 1: Detect current environment locale
current_locale = os.environ.get('LC_ALL') or os.environ.get('LANG') or ''
current_locale = current_locale.strip() or None
# 2: Try the current locale first
if current_locale and is_locale_supported(current_locale):
# Ensure environment variables are consistent
os.environ['LC_ALL'] = current_locale
os.environ['LANG'] = current_locale
locale.setlocale(locale.LC_ALL, current_locale)
return
# 3: Fallbacks to known good locales (all UTF-8, all English)
fallback_locales = [
'en_US.UTF-8',
'en_GB.UTF-8',
'en_AU.UTF-8',
'en_CA.UTF-8',
'en.UTF-8',
]
for loc in fallback_locales:
if is_locale_supported(loc):
os.environ['LC_ALL'] = loc
os.environ['LANG'] = loc
locale.setlocale(locale.LC_ALL, loc)
return
# 4: Final safety fallback if all else fails
fallback = 'en_US.UTF-8'
os.environ['LC_ALL'] = fallback
os.environ['LANG'] = fallback
try:
locale.setlocale(locale.LC_ALL, fallback)
except Exception as e:
print(f"Warning: Could not set locale to {fallback}: {e}", file=sys.stderr)
_fix_locale()
# Import translation functions first
from .translations import _
# Regular imports - all modules needed at runtime
from .enums import (
LogColor,
CommandStatus,
BitpixType,
StarProfile,
SequenceType,
DistoType,
PlotType,
SirilVport,
ImageType,
STFType,
SlidersMode
)
from .models import (
ImageStats,
FKeywords,
FFit,
Homography,
PSFStar,
BGSample,
RegData,
ImgData,
DistoData,
Sequence,
FPoint,
Polygon,
ImageAnalysis
)
from .plot import SeriesData, PlotData
from .shm import SharedMemoryWrapper
from .utility import (
truncate_utf8,
human_readable_size,
download_with_progress,
ensure_installed,
uninstall_package,
check_module_version,
needs_module_version,
SuppressedStdout,
SuppressedStderr,
)
from .gpuhelper import (
ONNXHelper,
TorchHelper,
JaxHelper,
)
from .exceptions import (
SirilError,
SirilConnectionError,
SharedMemoryError,
CommandError,
DataError,
NoImageError,
MouseModeError,
NoSequenceError,
ProcessingThreadBusyError,
ImageDialogOpenError
)
from .connection import SirilInterface
from .version import __version__, __author__, __license__, __copyright__
# Define public API
__all__ = [
'ensure_installed',
'uninstall_package',
'check_module_version',
'needs_module_version',
'SirilInterface',
'ImageStats',
'FKeywords',
'FFit',
'Homography',
'PSFStar',
'BGSample',
'RegData',
'ImgData',
'DistoData',
'Sequence',
'FPoint',
'Polygon',
'PlotType',
'SeriesData',
'PlotData',
'SirilError',
'SirilConnectionError',
'SharedMemoryError',
'CommandError',
'DataError',
'NoImageError',
'NoSequenceError',
'MouseModeError',
'ProcessingThreadBusyError',
'ImageDialogOpenError',
'SharedMemoryWrapper',
'truncate_utf8',
'SuppressedStdout',
'SuppressedStderr',
'ONNXHelper',
'TorchHelper',
'JaxHelper',
'human_readable_size',
'download_with_progress',
'LogColor',
'CommandStatus',
'BitpixType',
'StarProfile',
'SequenceType',
'DistoType',
'PlotType',
'SirilVport',
'ImageAnalysis',
'ImageType',
'STFType',
'SlidersMode',
'_'
]
def _safe_reconfigure_stream(stream_name):
stream = getattr(sys, stream_name)
if hasattr(stream, "reconfigure"):
try:
stream.reconfigure(encoding="utf-8", errors="strict")
return
except Exception:
pass # Fallback below
# Replace stream with a UTF-8 TextIOWrapper that uses errors='replace'
buffer = getattr(sys, f"{stream_name}.buffer", None)
if buffer is not None:
wrapper = io.TextIOWrapper(buffer, encoding="utf-8", errors="replace")
setattr(sys, stream_name, wrapper)
_safe_reconfigure_stream("stdout")
_safe_reconfigure_stream("stderr")
|