add option for delayed imports

will make setup checking easier, although it does not work for all cases
(from imports for example)

Change-Id: I2e5cf9b427a6f8e6f603d9662cdb9700d5f9ad9a
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/33893
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de>
Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
Alexander Zaft 2024-06-10 14:05:51 +02:00 committed by Markus Zolliker
parent e093bdae96
commit 6837b82791
4 changed files with 33 additions and 14 deletions

View File

@ -30,13 +30,12 @@ import traceback
import threading
import logging
from os.path import expanduser
from frappy.lib import delayed_import
from frappy.client import SecopClient, UnregisterCallback
from frappy.errors import SECoPError
from frappy.datatypes import get_datatype, StatusType
try:
import readline
except ImportError:
readline = None
readline = delayed_import('readline')
USAGE = """

View File

@ -25,13 +25,10 @@ import time
from frappy.gui.qt import QLabel, Qt, QVBoxLayout, QWidget, pyqtSignal
from frappy.gui.util import Colors
from frappy.lib import delayed_import
try:
import numpy as np
import pyqtgraph as pg
except ImportError:
pg = None
np = None
np = delayed_import('numpy')
pg = delayed_import('pyqtgraph')
def getPlotWidget(parent):

View File

@ -419,3 +419,26 @@ def merge_status(*args):
# use dict instead of set for preserving order
merged = {m: True for mm in merged for m in mm.split(', ')}
return maxcode, ', '.join(merged)
class _Raiser:
def __init__(self, modname):
self.modname = modname
def __getattr__(self, name):
# Just retry the import, it will give the most useful exception.
__import__(self.modname)
def __bool__(self):
return False
def delayed_import(modname):
"""Import a module, and return an object that raises a delayed exception
on access if it failed.
"""
try:
module = __import__(modname, None, None, ['*'])
except Exception:
return _Raiser(modname)
return module

View File

@ -24,17 +24,17 @@
from os import path
# eventually he3control
from he3d import he3cell # pylint: disable=import-error
from frappy.core import Attached
from frappy.datatypes import ArrayOf, FloatRange, IntRange, StatusType, \
StringType, TupleOf
from frappy.errors import CommandRunningError
from frappy.lib import delayed_import
from frappy.modules import Command, Drivable, Module, Parameter, Property, \
Readable
from frappy.rwhandler import CommonReadHandler
he3d = delayed_import('he3d')
integral = IntRange()
floating = FloatRange()
string = StringType()
@ -52,7 +52,7 @@ class Cell(Module):
def initModule(self):
super().initModule()
self.cell = he3cell.He3_cell(
self.cell = he3d.he3cell.He3_cell(
path.join(self.config_directory, 'cell.yml'))
# Commands