57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
# region Imports
|
|
from __future__ import unicode_literals, annotations
|
|
import os
|
|
import sys
|
|
from typing import TYPE_CHECKING, Tuple
|
|
from pathlib import Path
|
|
import uno
|
|
import unohelper
|
|
from com.sun.star.task import XJob
|
|
|
|
if TYPE_CHECKING:
|
|
# just for design time
|
|
from com.sun.star.beans import NamedValue
|
|
|
|
# endregion Imports
|
|
|
|
# region Constants
|
|
|
|
implementation_name = "net.elmau.zaz.pip.PipRunner"
|
|
implementation_services = ("com.sun.star.task.Job",)
|
|
# endregion Constants
|
|
|
|
|
|
|
|
# region XJob
|
|
class PipRunner(unohelper.Base, XJob):
|
|
|
|
def __init__(self, ctx):
|
|
self._is_flatpak = bool(os.getenv("FLATPAK_ID", ""))
|
|
if self._is_flatpak:
|
|
site_packages = self.get_flatpak_site_packages_dir()
|
|
ss = str(site_packages)
|
|
if site_packages.exists() and ss not in sys.path:
|
|
sys.path.append(ss)
|
|
|
|
def execute(self, *args: Tuple[NamedValue, ...]) -> None:
|
|
pass
|
|
|
|
def get_flatpak_site_packages_dir(self) -> Path:
|
|
# should never be all users
|
|
sand_box = os.getenv("FLATPAK_SANDBOX_DIR", "") or str(
|
|
Path.home() / ".var/app/org.libreoffice.LibreOffice/sandbox"
|
|
)
|
|
major_minor = f"{sys.version_info.major}.{sys.version_info.minor}"
|
|
site_packages = Path(sand_box) / f"lib/python{major_minor}/site-packages"
|
|
site_packages.mkdir(parents=True, exist_ok=True)
|
|
return site_packages
|
|
|
|
# endregion XJob
|
|
|
|
# region Implementation
|
|
|
|
g_TypeTable = {}
|
|
g_ImplementationHelper = unohelper.ImplementationHelper()
|
|
|
|
g_ImplementationHelper.addImplementation(PipRunner, implementation_name, implementation_services)
|
|
# endregion Implementation |