Files
create/mods/sdk/kindred_sdk/origin.py
forbes 79f69e2856
All checks were successful
Build and Test / build (pull_request) Successful in 28m54s
feat(sdk): remove fallbacks, add deprecation warnings, bump v1.0.0 (#357)
- Bump SDK_VERSION to 1.0.0 in version.py and package.xml
- Remove <pure_python> tag from package.xml (kcsdk is C++)
- Remove all FreeCADGui.* fallback paths in context.py, dock.py,
  toolbar.py, menu.py; require kcsdk module
- Remove query fallbacks in origin.py (keep register/unregister
  which still need FreeCADGui.addOrigin/removeOrigin)
- Add deprecation warnings to 11 superseded FreeCADGui methods
  in ApplicationPy.cpp (not addOrigin/removeOrigin)
- All 39 Tier 1 tests pass
2026-03-01 14:42:00 -06:00

137 lines
3.5 KiB
Python

"""FileOrigin registration and query wrappers.
Registration (``register_origin`` / ``unregister_origin``) wraps
``FreeCADGui.addOrigin()`` / ``removeOrigin()`` with validation.
Query functions (``list_origins``, ``active_origin``, etc.) route
through the ``kcsdk`` C++ module. The kcsdk module is required for
query operations.
"""
import FreeCAD
try:
import kcsdk as _kcsdk
except ImportError:
_kcsdk = None
_REQUIRED_METHODS = ("id", "name", "type", "ownsDocument")
def _gui():
"""Lazy import of FreeCADGui (not available in console mode)."""
import FreeCADGui
return FreeCADGui
def _require_kcsdk():
if _kcsdk is None:
raise RuntimeError(
"kcsdk module not available. "
"The kindred_sdk requires the kcsdk C++ module (libKCSDK)."
)
def register_origin(origin):
"""Register a FileOrigin with FreeCADGui.
*origin* must be a Python object implementing at least ``id()``,
``name()``, ``type()``, and ``ownsDocument(doc)`` methods.
"""
missing = [m for m in _REQUIRED_METHODS if not hasattr(origin, m)]
if missing:
raise TypeError(f"origin is missing required methods: {', '.join(missing)}")
try:
_gui().addOrigin(origin)
FreeCAD.Console.PrintLog(f"kindred_sdk: Registered origin '{origin.id()}'\n")
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: Failed to register origin: {e}\n")
def unregister_origin(origin):
"""Remove a previously registered FileOrigin."""
try:
_gui().removeOrigin(origin)
FreeCAD.Console.PrintLog(f"kindred_sdk: Unregistered origin '{origin.id()}'\n")
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: Failed to unregister origin: {e}\n")
def list_origins():
"""Return IDs of all registered origins.
Returns
-------
list[str]
Origin IDs (always includes ``"local"``).
"""
_require_kcsdk()
try:
return _kcsdk.list_origins()
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: list_origins failed: {e}\n")
return []
def active_origin():
"""Return info about the currently active origin.
Returns
-------
dict or None
Origin info dict with keys: ``id``, ``name``, ``nickname``,
``type``, ``tracksExternally``, ``requiresAuthentication``,
``supportsRevisions``, ``supportsBOM``, ``supportsPartNumbers``,
``connectionState``.
"""
_require_kcsdk()
try:
return _kcsdk.active_origin()
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: active_origin failed: {e}\n")
return None
def set_active_origin(origin_id):
"""Set the active origin by ID.
Parameters
----------
origin_id : str
The origin ID to activate.
Returns
-------
bool
True if the origin was found and activated.
"""
_require_kcsdk()
try:
return _kcsdk.set_active_origin(origin_id)
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: set_active_origin failed: {e}\n")
return False
def get_origin(origin_id):
"""Get info about a specific origin by ID.
Parameters
----------
origin_id : str
The origin ID to look up.
Returns
-------
dict or None
Origin info dict, or None if not found.
"""
_require_kcsdk()
try:
return _kcsdk.get_origin(origin_id)
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: get_origin failed: {e}\n")
return None