from __future__ import annotations
import os
import sys
import threading
import types
STUB_MARKER = "__ferrocad_pyside_stub__"
class QtStubModule(types.ModuleType):
def __getattr__(self, name):
exports = self.__dict__.get("__all__", ())
if name.startswith("__") and name.endswith("__"):
raise AttributeError(name)
raise AttributeError(
f"module {self.__name__!r} has no attribute {name!r}; "
f"the FerroCAD PySide shim exports only {{{', '.join(exports)}}}. "
"Set FERROCAD_PYSIDE_BACKEND=PySide6 to import a real Qt."
)
_RESOURCE_CACHE: dict[str, bytes | None] = {}
def clear_resource_cache() -> None:
_RESOURCE_CACHE.clear()
def _resource_roots():
for entry in sys.path:
if entry and os.path.isdir(entry):
yield entry
def resource_bytes(relative: str) -> bytes | None:
if relative in _RESOURCE_CACHE:
return _RESOURCE_CACHE[relative]
found = None
for root in _resource_roots():
candidate = os.path.join(root, "Resources", relative)
if os.path.isfile(candidate):
found = candidate
break
if found is None:
for root in _resource_roots():
try:
children = sorted(os.listdir(root))
except OSError:
continue
for child in children:
candidate = os.path.join(root, child, "Resources", relative)
if os.path.isfile(candidate):
found = candidate
break
if found is not None:
break
data = None
if found is not None:
with open(found, "rb") as handle:
data = handle.read()
_RESOURCE_CACHE[relative] = data
return data
class QByteArray:
__slots__ = ("_data",)
def __init__(self, data=b"", /):
if isinstance(data, QByteArray):
data = data._data
self._data = bytes(data)
def data(self) -> bytes:
return self._data
def constData(self) -> bytes:
return self._data
def size(self) -> int:
return len(self._data)
def isEmpty(self) -> bool:
return not self._data
def toStdString(self) -> str:
return self._data.decode("utf-8", "replace")
def __bytes__(self) -> bytes:
return self._data
def __len__(self) -> int:
return len(self._data)
def __getitem__(self, item):
return self._data[item]
def __eq__(self, other):
if isinstance(other, QByteArray):
return self._data == other._data
if isinstance(other, (bytes, bytearray)):
return self._data == bytes(other)
return NotImplemented
def __repr__(self) -> str:
return f"QByteArray({self._data!r})"
class QIODevice:
NotOpen = 0x0000
ReadOnly = 0x0001
WriteOnly = 0x0002
ReadWrite = ReadOnly | WriteOnly
Append = 0x0004
Truncate = 0x0008
Text = 0x0010
Unbuffered = 0x0020
def __init__(self, *args, **kwargs):
self._open = False
def isOpen(self) -> bool:
return self._open
def isReadable(self) -> bool:
return self._open
def isWritable(self) -> bool:
return False
def close(self) -> None:
self._open = False
def open(self, mode): raise NotImplementedError(
"PySide shim: QIODevice.open() is abstract; use QFile"
)
class QFile(QIODevice):
def __init__(self, path="", /, *args, **kwargs):
super().__init__()
self._path = os.fspath(path)
self._data: bytes | None = None
self._pos = 0
def fileName(self) -> str:
return self._path
def exists(self) -> bool:
if self._path.startswith(":/"):
return resource_bytes(self._path[2:]) is not None
return os.path.isfile(self._path)
def size(self) -> int:
if self._data is None:
return 0
return len(self._data)
def open(self, mode) -> bool:
if mode & (self.WriteOnly | self.Append | self.Truncate):
raise NotImplementedError(
"PySide shim: QFile is read-only; "
f"open mode {mode!r} for {self._path!r} needs a real Qt"
)
if self._path.startswith(":/"):
data = resource_bytes(self._path[2:])
else:
try:
with open(self._path, "rb") as handle:
data = handle.read()
except OSError:
data = None
if data is None:
self._data = None
self._pos = 0
self._open = False
return False
self._data = data
self._pos = 0
self._open = True
return True
def _remaining(self) -> bytes:
if not self._open or self._data is None:
raise ValueError(
f"PySide shim: read from a closed QFile ({self._path!r}); "
"call open() first"
)
return self._data[self._pos :]
def readAll(self) -> QByteArray:
chunk = self._remaining()
self._pos = len(self._data)
return QByteArray(chunk)
def read(self, maxSize=-1) -> QByteArray:
chunk = self._remaining()
if maxSize is None or maxSize < 0:
chunk, consumed = chunk, len(chunk)
else:
chunk, consumed = chunk[:maxSize], min(maxSize, len(chunk))
self._pos += consumed
return QByteArray(chunk)
def readLine(self, maxSize=0) -> QByteArray:
chunk = self._remaining()
newline = chunk.find(b"\n")
end = len(chunk) if newline < 0 else newline + 1
if maxSize and maxSize > 0:
end = min(end, maxSize)
self._pos += end
return QByteArray(chunk[:end])
def atEnd(self) -> bool:
return self._data is None or self._pos >= len(self._data)
class QFileInfo:
def __init__(self, path="", /):
self._path = os.fspath(path)
def filePath(self) -> str:
return self._path
def absoluteFilePath(self) -> str:
return os.path.abspath(self._path) if not self._path.startswith(":/") else self._path
def fileName(self) -> str:
return os.path.basename(self._path)
def exists(self) -> bool:
if self._path.startswith(":/"):
return resource_bytes(self._path[2:]) is not None
return os.path.exists(self._path)
def isFile(self) -> bool:
if self._path.startswith(":/"):
return resource_bytes(self._path[2:]) is not None
return os.path.isfile(self._path)
def QT_TRANSLATE_NOOP(context, text):
return text
class QCoreApplication:
@staticmethod
def translate(context, text, disambiguation=None, n=-1):
return text
@classmethod
def instance(cls):
return None
@staticmethod
def installTranslator(translator):
raise NotImplementedError(
"PySide shim: QCoreApplication.installTranslator() needs a real Qt"
)
class QLocale:
def __init__(self, *args, **kwargs):
self._name = "C"
def decimalPoint(self) -> str:
return "."
def name(self) -> str:
return self._name
def toString(self, value) -> str:
return str(value)
class SignalSpec:
__slots__ = ("signature",)
def __init__(self, signature):
self.signature = str(signature)
@property
def name(self) -> str:
return self.signature.split("(", 1)[0].strip()
def __repr__(self) -> str:
return f"SIGNAL({self.signature!r})"
def SIGNAL(signature) -> SignalSpec:
return SignalSpec(signature)
class Signal:
__slots__ = ("name", "owner", "_slots", "_blocked")
def __init__(self, name, owner=None):
self.name = name
self.owner = owner
self._slots = []
self._blocked = False
def connect(self, slot):
if slot not in self._slots:
self._slots.append(slot)
return slot
def disconnect(self, slot=None):
if slot is None:
self._slots.clear()
elif slot in self._slots:
self._slots.remove(slot)
def emit(self, *args):
if self._blocked:
return
if self.owner is not None and getattr(self.owner, "_signals_blocked", False):
return
for slot in list(self._slots):
slot(*args)
__call__ = emit
def __repr__(self) -> str:
return f"<Signal {self.name!r} of {type(self.owner).__name__}>"
class QObject:
def __init__(self, parent=None, /):
self._parent = None
self._children = []
self._object_name = ""
self._signals_blocked = False
self._signals: dict[str, Signal] = {}
if parent is not None:
self.setParent(parent)
def setParent(self, parent):
if self._parent is not None:
try:
self._parent._children.remove(self)
except ValueError:
pass
self._parent = parent
if parent is not None:
parent._children.append(self)
def parent(self):
return self._parent
def children(self):
return list(self._children)
def objectName(self) -> str:
return self._object_name
def setObjectName(self, name) -> None:
self._object_name = str(name)
def deleteLater(self) -> None:
self.setParent(None)
def blockSignals(self, block):
previous = self._signals_blocked
self._signals_blocked = bool(block)
return previous
def signalsBlocked(self) -> bool:
return self._signals_blocked
def signal(self, name) -> Signal:
signal = self._signals.get(name)
if signal is None:
signal = self._signals[name] = Signal(name, self)
return signal
@staticmethod
def connect(receiver, signal, slot, *args):
name = signal.name if isinstance(signal, SignalSpec) else str(signal)
target = getattr(receiver, name, None)
if not isinstance(target, Signal):
raise AttributeError(
"PySide shim: QObject.connect() found no signal "
f"{name!r} on {type(receiver).__name__}; "
"the shim only supports signals it defines"
)
return target.connect(slot)
class QTimer(QObject):
def __init__(self, parent=None, /):
super().__init__(parent)
self.timeout = self.signal("timeout")
self._interval = 0
self._single_shot = False
self._timer: threading.Timer | None = None
def setSingleShot(self, value) -> None:
self._single_shot = bool(value)
def isSingleShot(self) -> bool:
return self._single_shot
def setInterval(self, msec) -> None:
self._interval = int(msec)
def interval(self) -> int:
return self._interval
def isActive(self) -> bool:
return self._timer is not None and self._timer.is_alive()
def start(self, msec=None) -> None:
if msec is not None:
self._interval = int(msec)
self.stop()
def fire():
self._timer = None
self.timeout.emit()
if not self._single_shot and self._interval > 0:
self.start(self._interval)
if self._interval <= 0:
fire()
else:
self._timer = threading.Timer(self._interval / 1000.0, fire)
self._timer.daemon = True
self._timer.start()
def stop(self) -> None:
if self._timer is not None:
self._timer.cancel()
self._timer = None
@staticmethod
def singleShot(msec, receiver, member=None):
delay = int(msec)
def fire():
if member is None:
receiver()
elif callable(member):
member()
elif isinstance(member, str):
getattr(receiver, member)()
else: raise TypeError(
"PySide shim: QTimer.singleShot(ms, receiver, member) needs "
"a callable, a slot name or a callable member"
)
if delay <= 0:
fire()
return None
timer = threading.Timer(delay / 1000.0, fire)
timer.daemon = True
timer.start()
return timer
class QStandardPaths:
DesktopLocation = 0
DocumentsLocation = 1
FontsLocation = 2
ApplicationsLocation = 3
MusicLocation = 4
MoviesLocation = 5
PicturesLocation = 6
TempLocation = 7
HomeLocation = 8
DataLocation = 9
CacheLocation = 10
@staticmethod
def standardLocations(location):
import tempfile
home = os.path.expanduser("~")
if location == QStandardPaths.FontsLocation:
data_home = os.environ.get("XDG_DATA_HOME") or os.path.join(
home, ".local", "share"
)
data_dirs = os.environ.get("XDG_DATA_DIRS") or "/usr/local/share:/usr/share"
candidates = [
os.path.join(data_home, "fonts"),
os.path.join(home, ".fonts"),
]
candidates += [
os.path.join(entry, "fonts")
for entry in data_dirs.split(":") if entry
]
seen = set()
found = []
for path in candidates:
path = os.path.normpath(path)
if path not in seen and os.path.isdir(path):
seen.add(path)
found.append(path)
return found
if location == QStandardPaths.HomeLocation:
return [home] if os.path.isdir(home) else []
if location == QStandardPaths.TempLocation:
return [tempfile.gettempdir()]
return []
@staticmethod
def writableLocation(location):
locations = QStandardPaths.standardLocations(location)
if not locations:
raise NotImplementedError(
"PySide shim: QStandardPaths.writableLocation() for "
f"location {location!r} is not implemented"
)
return locations[0]
class QApplication(QCoreApplication):
@staticmethod
def translate(context, text, disambiguation=None, n=-1):
return text
@classmethod
def instance(cls):
return None
@staticmethod
def processEvents(*args, **kwargs):
raise NotImplementedError(
"PySide shim: QApplication.processEvents() needs a real Qt event loop"
)
class _UnsupportedWidget:
_qt_base = "QWidget"
def __init__(self, *args, **kwargs):
raise NotImplementedError(
f"PySide shim: {type(self).__name__} extends {self._qt_base}, and the "
"shim does not construct Qt widgets; render the form with ferrocad_ui "
"or run with FERROCAD_PYSIDE_BACKEND=PySide6"
)
class QWidget(_UnsupportedWidget):
_qt_base = "QWidget"
class QLineEdit(_UnsupportedWidget):
_qt_base = "QLineEdit"
class QStyledItemDelegate(_UnsupportedWidget):
_qt_base = "QStyledItemDelegate"
QTCORE_EXPORTS = {
"QT_TRANSLATE_NOOP": QT_TRANSLATE_NOOP,
"QCoreApplication": QCoreApplication,
"QLocale": QLocale,
"QObject": QObject,
"QTimer": QTimer,
"SIGNAL": SIGNAL,
"QIODevice": QIODevice,
"QFile": QFile,
"QFileInfo": QFileInfo,
"QStandardPaths": QStandardPaths,
}
QTGUI_EXPORTS: dict[str, object] = {}
QTWIDGETS_EXPORTS = {
"QApplication": QApplication,
"QWidget": QWidget,
"QLineEdit": QLineEdit,
"QStyledItemDelegate": QStyledItemDelegate,
}
__all__ = [
"QTCORE_EXPORTS",
"QTGUI_EXPORTS",
"QTWIDGETS_EXPORTS",
"QtStubModule",
"QByteArray",
"QIODevice",
"QFile",
"QFileInfo",
"QObject",
"QTimer",
"Signal",
"SignalSpec",
"SIGNAL",
"QCoreApplication",
"QApplication",
"QLocale",
"QStandardPaths",
"QT_TRANSLATE_NOOP",
"clear_resource_cache",
"resource_bytes",
]