Esempio n. 1
0
def parse(filename=None, factory=None):

    if filename is None or factory is None:
        return

    _, p = new_parser()
    print " \n\n\n zrobioby nowy parser"
    print "1: ", _
    print "2: ", p
    p.factory = factory
    p.modelStack = []
    p.modelStack2 = []

    res = None
    try:
        filename = os.path.realpath(filename)
        f = open(filename)
        #print f.read()
        print "\n\n\n otwprzylem plik w try"
        res = yacc.parse(f.read()) #nie bylo tracking=True, dodalem
        #for i in res:
        #    print "res i: ", i
        print "\n\n SPARSOWALEMW  TRY! \n"
        print "\n RES: ", res
    except:
        print "\n\n NIE SPARSOWALEM W TRY"
        log = Logger('JDraw Parser')
        #print "\n\n LOG1: ", log, "\n\n\n stop"
        log.warning("Failed to parse %s" % filename)
        #print "\n\n exec info: ?"
        log.debug("Details:", exc_info=1)
    return res
Esempio n. 2
0
    def __init__(self,
                 parent=None,
                 name='',
                 queue=None,
                 method=None,
                 cursor=None,
                 sleep=5000):
        """
        Parent most not be None and must be a TaurusGraphicsScene!
        """
        Qt.QThread.__init__(self, parent)
        self.name = name
        self.log = Logger('TaurusEmitterThread(%s)' % self.name)
        self.log.setLogLevel(self.log.Info)
        self.queue = queue or Queue.Queue()
        self.todo = Queue.Queue()
        self.method = method
        self.cursor = Qt.QCursor(
            Qt.Qt.WaitCursor) if cursor is True else cursor
        self._cursor = False
        self.timewait = sleep

        self.emitter = Qt.QObject()
        self.emitter.moveToThread(Qt.QApplication.instance().thread())
        # Mandatory!!! if parent is set before changing thread it could lead to
        # segFaults!
        self.emitter.setParent(Qt.QApplication.instance())
        self._done = 0
        # Moved to the end to prevent segfaults ...
        Qt.QObject.connect(self.emitter, Qt.SIGNAL("doSomething"),
                           self._doSomething)
        Qt.QObject.connect(self.emitter, Qt.SIGNAL("somethingDone"), self.next)
Esempio n. 3
0
    def basicConfig(self, log_file_name=None, maxBytes=1E7, backupCount=5,
                    with_gui_exc_handler=True):

        # prepare exception handler to report exceptions as error log messages
        # and to taurus message box (if with_gui is set)
        hook_to = None
        if with_gui_exc_handler:
            import taurus.qt.qtgui.dialog
            hook_to = taurus.qt.qtgui.dialog.TaurusExceptHookMessageBox()
        sys.excepthook = LogExceptHook(hook_to=hook_to)

        # create a file log handler
        try:
            if log_file_name is None:
                log_file_name = self.__buildLogFileName()
            f_h = logging.handlers.RotatingFileHandler(log_file_name,
                                                       maxBytes=maxBytes, backupCount=backupCount)
            Logger.addRootLogHandler(f_h)
            if self._out is not None:
                self._out.std = sys.__stdout__
                self._out.addLogHandler(f_h)
            if self._out is not None:
                self._err.std = sys.__stderr__
                self._err.addLogHandler(f_h)
            self.info("Logs will be saved in %s", log_file_name)
        except:
            self.warning("'%s' could not be created. Logs will not be stored",
                         log_file_name)
            self.debug("Error description", exc_info=1)
Esempio n. 4
0
 def init(self, *args, **kwargs):
     """Singleton instance initialization."""
     if PyTangoArchiving is None:
         raise TaurusException('PyTangoArchiving is not available')
     name = self.__class__.__name__
     Logger.__init__(self, name)
     TaurusFactory.__init__(self)
Esempio n. 5
0
    def run_macro(self, par_str_list, asynch=False):
        if isinstance(par_str_list, (str, unicode)):
            par_str_list = par_str_list,

        if not hasattr(self, "Output"):
            import sys
            import logging
            Logger.addLevelName(15, "OUTPUT")

            def output(loggable, msg, *args, **kw):
                loggable.getLogObj().log(Logger.Output, msg, *args, **kw)
            Logger.output = output

            Logger.disableLogOutput()
            Logger.setLogLevel(Logger.Output)
            #filter = taurus.core.util.LogFilter(level=Logger.Output)
            formatter = logging.Formatter(fmt="%(message)s")
            Logger.setLogFormat("%(message)s")
            handler = logging.StreamHandler(stream=sys.stdout)
            #handler.addFilter(filter)
            Logger.addRootLogHandler(handler)
            #handler.setFormatter(formatter)
            #logger.addHandler(handler)
            #logger.addFilter(filter)
            self.__logging_info = handler, filter, formatter

            # result of a macro
            #Logger.addLevelName(18, "RESULT")

        return self.macro_executor.run(par_str_list, asynch=asynch)
Esempio n. 6
0
    def basicConfig(self,
                    log_file_name=None,
                    maxBytes=1E7,
                    backupCount=5,
                    with_gui_exc_handler=True):

        # prepare exception handler to report exceptions as error log messages
        # and to taurus message box (if with_gui is set)
        hook_to = None
        if with_gui_exc_handler:
            import taurus.qt.qtgui.dialog
            hook_to = taurus.qt.qtgui.dialog.TaurusExceptHookMessageBox()
        sys.excepthook = LogExceptHook(hook_to=hook_to)

        # create a file log handler
        try:
            if log_file_name is None:
                log_file_name = self.__buildLogFileName()
            f_h = logging.handlers.RotatingFileHandler(log_file_name,
                                                       maxBytes=maxBytes,
                                                       backupCount=backupCount)
            Logger.addRootLogHandler(f_h)
            if self._out is not None:
                self._out.std = sys.__stdout__
                self._out.addLogHandler(f_h)
            if self._out is not None:
                self._err.std = sys.__stderr__
                self._err.addLogHandler(f_h)
            self.info("Logs will be saved in %s", log_file_name)
        except:
            self.warning("'%s' could not be created. Logs will not be stored",
                         log_file_name)
            self.debug("Error description", exc_info=1)
Esempio n. 7
0
 def __init__(self, capacity=500000, freq=0.25):
     super(Qt.QAbstractTableModel, self).__init__()
     logging.Handler.__init__(self)
     self._capacity = capacity
     self._records = []
     self._accumulated_records = []
     Logger.addRootLogHandler(self)
     self.startTimer(freq * 1000)
Esempio n. 8
0
    def addLogHandler(self, handler):
        """When called, set to use a private handler and DON'T send messages
        to parent loggers (basically will act as an independent logging system
        by itself)

        :param handler: new handler"""
        Logger.addLogHandler(self, handler)
        self.log_obj.propagate = not len(self.log_handlers)
Esempio n. 9
0
 def __init__(self, parent=None, data=None):
     Qt.QAbstractItemModel.__init__(self, parent)
     Logger.__init__(self)
     self._data_src = None
     self._rootItem = None
     self._filters = []
     self._selectables = [self.ColumnRoles[0][-1]]
     self.setDataSource(data)
Esempio n. 10
0
 def __init__(self, capacity=500000, freq=0.25):
     super(Qt.QAbstractTableModel, self).__init__()
     logging.Handler.__init__(self)
     self._capacity = capacity
     self._records = []
     self._accumulated_records = []
     Logger.addRootLogHandler(self)
     self.startTimer(freq * 1000)
Esempio n. 11
0
    def addLogHandler(self, handler):
        """When called, set to use a private handler and DON'T send messages
        to parent loggers (basically will act as an independent logging system
        by itself)

        :param handler: new handler"""
        Logger.addLogHandler(self, handler)
        self.log_obj.propagate = not len(self.log_handlers)
Esempio n. 12
0
class BaseControllerTestCase(object):
    """ Base test case for unit testing arbitrary controllers.
    This class will create a controller instance and define an axis from the
    class member attributes:
        KLASS <type> controller class
        PROPS <dict> properties of the controller
        AXIS <int> number of the axis
    """
    KLASS = None
    PROPS = {}
    AXIS = 1
    DEBUG = False

    def setUp(self):
        self.logger = Logger('BaseControllerTestCase')
        if self.DEBUG:
            self.logger.setLogLevel(Logger.Debug)

        if self.KLASS is None:
            raise Exception('Ctrl klass has not been defined')
        name = 'test_ctrl'
        self.ctrl = self.KLASS(name, self.PROPS)
        self.pre_AddDevice_hook()
        self.ctrl.AddDevice(self.AXIS)

    def tearDown(self):
        if self.ctrl is not None:
            self.ctrl.DeleteDevice(self.AXIS)

    def axisPar(self, name, value, expected_value=None):
        """ Helper for test the SetAxisPar & GetaxisPar methods
        """
        axis = self.AXIS
        if expected_value is None:
            expected_value = value
        self.ctrl.SetAxisPar(axis, name, value)
        r_value = self.ctrl.GetAxisPar(axis, name)
        msg = ('The %s value is %s, and the expected value is %s'
               % (name, r_value, expected_value))
        self.assertEqual(r_value, expected_value, msg)

    def stateOne(self, expected_state=State.On):
        """ Helper for test the stateOne method
        """
        sta, status = self.ctrl.StateOne(self.AXIS)
        msg = ('The current state of axis(%d) is %d when expected, %d'
               % (self.AXIS, sta, expected_state))
        self.assertEqual(sta, expected_state, msg)

    def start_action(self, configuration):
        """ This method set the axis parameters and pre start the axis.
        """
        for key, value in configuration.items():
            self.axisPar(key, value)
        self.ctrl.SynchOne(configuration)

    def pre_AddDevice_hook(self):
        pass
Esempio n. 13
0
class BaseControllerTestCase(object):
    """ Base test case for unit testing arbitrary controllers.
    This class will create a controller instance and define an axis from the
    class member attributes:
        KLASS <type> controller class
        PROPS <dict> properties of the controller
        AXIS <int> number of the axis
    """
    KLASS = None
    PROPS = {}
    AXIS = 1
    DEBUG = False

    def setUp(self):
        self.logger = Logger('BaseControllerTestCase')
        if self.DEBUG:
            self.logger.setLogLevel(Logger.Debug)

        if self.KLASS is None:
            raise Exception('Ctrl klass has not been defined')
        name = 'test_ctrl'
        self.ctrl = self.KLASS(name, self.PROPS)
        self.pre_AddDevice_hook()
        self.ctrl.AddDevice(self.AXIS)

    def tearDown(self):
        if self.ctrl is not None:
            self.ctrl.DeleteDevice(self.AXIS)

    def axisPar(self, name, value, expected_value=None):
        """ Helper for test the SetAxisPar & GetaxisPar methods
        """
        axis = self.AXIS
        if expected_value is None:
            expected_value = value
        self.ctrl.SetAxisPar(axis, name, value)
        r_value = self.ctrl.GetAxisPar(axis, name)
        msg = ('The %s value is %s, and the expected value is %s' %
               (name, r_value, expected_value))
        self.assertEqual(r_value, expected_value, msg)

    def stateOne(self, expected_state=State.On):
        """ Helper for test the stateOne method
        """
        sta, status = self.ctrl.StateOne(self.AXIS)
        msg = ('The current state of axis(%d) is %d when expected, %d' %
               (self.AXIS, sta, expected_state))
        self.assertEqual(sta, expected_state, msg)

    def start_action(self, configuration):
        """ This method set the axis parameters and pre start the axis.
        """
        for key, value in configuration.items():
            self.axisPar(key, value)
        self.ctrl.SynchOne(configuration)

    def pre_AddDevice_hook(self):
        pass
Esempio n. 14
0
 def __redirect_std(self):
     """Internal method to redirect stdout and stderr to log messages"""
     Logger.addLevelName(Logger.Critical + 10, 'CONSOLE')
     # only redirect if display hook has not been set (IPython does it)
     if sys.displayhook == sys.__displayhook__:
         self._out = STD(name="OUT", std=sys.stdout)
         sys.stdout = self._out
         self._err = STD(name="ERR", std=sys.stderr)
         sys.stderr = self._err
Esempio n. 15
0
 def __redirect_std(self):
     """Internal method to redirect stdout and stderr to log messages"""
     Logger.addLevelName(Logger.Critical + 10, 'CONSOLE')
     # only redirect if display hook has not been set (IPython does it)
     if sys.displayhook == sys.__displayhook__:
         self._out = STD(name="OUT", std=sys.stdout)
         sys.stdout = self._out
         self._err = STD(name="ERR", std=sys.stderr)
         sys.stderr = self._err
Esempio n. 16
0
 def __init__(self, **kwargs):
     EventGenerator.__init__(self)
     EventReceiver.__init__(self)
     self._type = kwargs.pop('elem_type')
     self._name = intern(kwargs.pop('name'))
     self._full_name = intern(kwargs.pop('full_name'))
     self._frontend = None
     Logger.__init__(self, self._name)
     self._manager = weakref.ref(kwargs.pop('manager'))
     self._parent = weakref.ref(kwargs.pop('parent', self.manager))
Esempio n. 17
0
 def __init__(self, job, callback, th_id, stack, *args, **kwargs):
     name = self.__class__.__name__
     threading.Thread.__init__(self, name=name)
     Logger.__init__(self, name)
     self.job = job
     self.callback = callback
     self.th_id = th_id
     self.stack = stack
     self.args = args
     self.kwargs = kwargs
Esempio n. 18
0
 def __init__(self, **kwargs):
     EventGenerator.__init__(self)
     EventReceiver.__init__(self)
     self._type = kwargs.pop('elem_type')
     self._name = intern(kwargs.pop('name'))
     self._full_name = intern(kwargs.pop('full_name'))
     self._frontend = None
     Logger.__init__(self, self._name)
     self._manager = weakref.ref(kwargs.pop('manager'))
     self._parent = weakref.ref(kwargs.pop('parent', self.manager))
Esempio n. 19
0
    def setUp(self):
        self.logger = Logger('BaseControllerTestCase')
        if self.DEBUG:
            self.logger.setLogLevel(Logger.Debug)

        if self.KLASS is None:
            raise Exception('Ctrl klass has not been defined')
        name = 'test_ctrl'
        self.ctrl = self.KLASS(name, self.PROPS)
        self.pre_AddDevice_hook()
        self.ctrl.AddDevice(self.AXIS)
Esempio n. 20
0
 def __init__(self, main_element, name="GlobalAction"):
     Logger.__init__(self, name)
     self._action_run_lock = threading.Lock()
     self._main_element = weakref.ref(main_element)
     self._aborted = False
     self._stopped = False
     self._elements = []
     self._pool_ctrl_dict = {}
     self._pool_ctrl_list = []
     self._finish_hook = None
     self._running = False
     self._state_info = OperationInfo()
     self._value_info = OperationInfo()
Esempio n. 21
0
 def __init__(self, parent=None, data=None):
     Qt.QAbstractItemModel.__init__(self, parent)
     Logger.__init__(self)
     # if qt < 4.6, beginResetModel and endResetModel don't exist. In this
     # case we set beginResetModel to be an empty function and endResetModel
     # to be reset.
     if not hasattr(Qt.QAbstractItemModel, "beginResetModel"):
         self.beginResetModel = lambda: None
         self.endResetModel = self.reset
     self._data_src = None
     self._rootItem = None
     self._filters = []
     self._selectables = [self.ColumnRoles[0][-1]]
     self.setDataSource(data)
Esempio n. 22
0
    def __init__(self,
                 parent=None,
                 name='',
                 queue=None,
                 method=None,
                 cursor=None,
                 sleep=5000,
                 polling=0,
                 loopwait=5):
        """
        Parent must be not None and must be a TaurusGraphicsScene!

        :param queue: pass an external action queue (optional)
        :param method: action processor (e.g. modelSetter)
        :param cursor: QCursor during process (optional)
        :param sleep: delay in ms before thread start
        :param polling: process actions at fix period (milliseconds)
        :param loopwait: wait N milliseconds between actions
        """
        Qt.QThread.__init__(self, parent)
        self.name = name
        self.log = Logger('TaurusEmitterThread(%s)' % self.name)
        self.log.setLogLevel(self.log.Info)
        self.queue = queue or Queue()
        self.todo = Queue()
        self.method = method
        self.cursor = Qt.QCursor(
            Qt.Qt.WaitCursor) if cursor is True else cursor
        self._cursor = False
        self.timewait = int(sleep)
        self.polling = int(polling)
        self.loopwait = int(loopwait)
        if self.polling:
            self.refreshTimer = Qt.QTimer()
            self.refreshTimer.timeout.connect(self.onRefresh)
        else:
            self.refreshTimer = None

        self.emitter = QEmitter()
        self.emitter.moveToThread(Qt.QApplication.instance().thread())
        # Mandatory!!! if parent is set before changing thread it could lead to
        # segFaults!
        self.emitter.setParent(Qt.QApplication.instance())
        self._done = 0
        # Moved to the end to prevent segfaults ...
        self.emitter.doSomething.connect(self._doSomething)

        if not self.refreshTimer:
            self.emitter.somethingDone.connect(self.next)
Esempio n. 23
0
 def __init__(self, parent=None, name='', queue=None, method=None, cursor=None, sleep=5000, log=Logger.Warning, start=True):
     self.name = name
     self.log = Logger('SingletonWorker(%s)' % self.name)
     self.log.setLogLevel(log)
     self.log.info('At SingletonWorker.__init__(%s)' % self.name)
     self.parent = parent
     self.method = method
     self._running = False
     if SingletonWorker._thread is None:
         SingletonWorker._thread = TaurusEmitterThread(
             parent, name='SingletonWorker', cursor=cursor, sleep=sleep)
     self.thread = SingletonWorker._thread
     self.queue = queue or Queue.Queue()
     if start:
         self.start()
Esempio n. 24
0
 def fromPanel(panel):
     name = str(panel.objectName())
     classname = panel.getWidgetClassName()
     modulename = panel.getWidgetModuleName()
     # in the case of classes known to the TaurusWidgetFactory,
     # do not store the modulename
     if modulename.startswith('taurus.') and \
        classname in TaurusWidgetFactory().getWidgetClassNames():
         modulename = None
     widgetname = None
     floating = panel.isFloating()
     sharedDataWrite = None
     sharedDataRead = None
     model = getattr(panel.widget(), 'model', None)
     if model is None or isinstance(model, basestring):
         pass
     elif hasattr(model, '__iter__'):
         # if model is a sequence, convert to space-separated string
         try:
             model = " ".join(model)
         except Exception, e:
             msg = ('Cannot convert %s to a space-separated string: %s' %
                    (model, e))
             Logger().debug(msg)
             model = None
Esempio n. 25
0
    def __init__(self, parent=None, name='', queue=None, method=None, cursor=None, sleep=5000):
        """
        Parent most not be None and must be a TaurusGraphicsScene!
        """
        Qt.QThread.__init__(self, parent)
        self.name = name
        self.log = Logger('TaurusEmitterThread(%s)' % self.name)
        self.log.setLogLevel(self.log.Info)
        self.queue = queue or Queue.Queue()
        self.todo = Queue.Queue()
        self.method = method
        self.cursor = Qt.QCursor(
            Qt.Qt.WaitCursor) if cursor is True else cursor
        self._cursor = False
        self.timewait = sleep

        self.emitter = QEmitter()
        self.emitter.moveToThread(Qt.QApplication.instance().thread())
        # Mandatory!!! if parent is set before changing thread it could lead to
        # segFaults!
        self.emitter.setParent(Qt.QApplication.instance())
        self._done = 0
        # Moved to the end to prevent segfaults ...
        self.emitter.doSomething.connect(self._doSomething)
        self.emitter.somethingDone.connect(self.next)
Esempio n. 26
0
 def setData(self, index, qvalue):
     taurus_role = index.model().role(index.column())
     str_value = Qt.from_qvariant(qvalue, str)
     if taurus_role in (ChannelView.Channel, ChannelView.Conditioning,
                        ChannelView.NXPath, ChannelView.DataType):
         data = str_value
     elif taurus_role in (ChannelView.Enabled, ChannelView.Output):
         data = Qt.from_qvariant(qvalue, bool)
     elif taurus_role == ChannelView.PlotType:
         data = PlotType[str_value]
     elif taurus_role == ChannelView.Normalization:
         data = Normalization[str_value]
     elif taurus_role == ChannelView.PlotAxes:
         data = [a for a in str_value.split('|')]
     elif taurus_role == ChannelView.Shape:
         s = str_value
         try:
             data = eval(s, {}, {})
             if not isinstance(data, (tuple, list)):
                 raise ValueError
         except:
             from taurus.core.util.log import Logger
             Logger(self.__class__.__name__).error('Invalid shape %s', s)
             data = ()
     else:
         raise NotImplementedError('Unknown role')
     ch_name, ch_data = self.itemData()
     key = self.itemdata_keys_map[taurus_role]
     ch_data[key] = data
Esempio n. 27
0
 def onModuleSelected(self):
     modulename = str(self.moduleNameLE.text())
     try:
         __import__(modulename)
         # We use this because __import__('x.y') returns x instead of y !!
         self.module = sys.modules[modulename]
         self.moduleNameLE.setStyleSheet('QLineEdit {color: green}')
     except Exception as e:
         Logger().debug(repr(e))
         self.moduleNameLE.setStyleSheet('QLineEdit {color: red}')
         return
     # inspect the module to find the members we want (classes or widgets
     # inheriting from QWidget)
     members = inspect.getmembers(self.module)
     classnames = sorted([
         n for n, m in members
         if inspect.isclass(m) and issubclass(m, Qt.QWidget)
     ])
     widgetnames = sorted(
         [n for n, m in members if isinstance(m, Qt.QWidget)])
     self.membersCB.clear()
     self.membersCB.addItem(self.CHOOSE_TYPE_TXT)
     self.membersCB.addItems(classnames)
     if classnames and widgetnames:
         self.membersCB.InsertSeparator(self.membersCB.count())
     self.membersCB.addItems(classnames)
Esempio n. 28
0
 def __init__(self, pool, name='PoolMonitor', period=5.0, min_sleep=1.0,
              auto_start=True):
     Logger.__init__(self, name)
     threading.Thread.__init__(self, name=name)
     self.daemon = True
     self._period = period
     self._min_sleep = min_sleep
     self._pool = pool
     self._stop = False
     self._pause = threading.Event()
     self._thread_pool = None
     self._ctrl_ids = []
     self._elem_ids = []
     pool.add_listener(self.on_pool_changed)
     if not auto_start:
         self.pause()
     self.start()
Esempio n. 29
0
    def __init__(self, name='', parent=None, format=None, std=None,
                 pure_text=True):
        """The STD Logger constructor

        :param name: (str) the logger name (default is empty string)
        :param parent: (Logger) the parent logger or None if no parent exists
                       (default is None)
        :param format: (str) the log message format or None to use the default
                       log format (default is None)
        :param std: std to forward write
        :param pure_text: if True, writes the 'message' parameter of the log
                          message in a separate line preserving the
                          indentation
        """
        Logger.__init__(self, name=name, parent=parent, format=format)
        self.buffer = ''
        self.log_obj.propagate = False
        self.std = std
Esempio n. 30
0
    def __init__(self, name='', parent=None, format=None, std=None,
                 pure_text=True):
        """The STD Logger constructor

        :param name: (str) the logger name (default is empty string)
        :param parent: (Logger) the parent logger or None if no parent exists
                       (default is None)
        :param format: (str) the log message format or None to use the default
                       log format (default is None)
        :param std: std to forward write
        :param pure_text: if True, writes the 'message' parameter of the log
                          message in a separate line preserving the
                          indentation
        """
        Logger.__init__(self, name=name, parent=parent, format=format)
        self.buffer = ''
        self.log_obj.propagate = False
        self.std = std
Esempio n. 31
0
 def __init__(self, name="FunctionGenerator"):
     EventGenerator.__init__(self)
     Logger.__init__(self, name)
     self._name = name
     self._initial_domain = None
     self._active_domain = None
     self._position_event = threading.Event()
     self._initial_domain_in_use = None
     self._active_domain_in_use = None
     self._active_events = list()
     self._passive_events = list()
     self._started = False
     self._stopped = False
     self._running = False
     self._start_time = None
     self._direction = None
     self._condition = None
     self._id = None
Esempio n. 32
0
 def __init__(self, inst, props, *args, **kwargs):
     self._inst_name = inst
     self._log = Logger("Controller.%s" % inst)
     self._log.log_obj.setLevel(getLogLevel())
     self._args = args
     self._kwargs = kwargs
     self._api_version = self._findAPIVersion()
     for prop_name, prop_value in list(props.items()):
         setattr(self, prop_name, prop_value)
Esempio n. 33
0
def new_parser(optimize=None, debug=0, outputdir=None):
    log = Logger('JDraw Parser')

    if optimize is None:
        from taurus import tauruscustomsettings
        optimize = getattr(tauruscustomsettings, 'PLY_OPTIMIZE', 1)
    if outputdir is None:
        # use '.taurus' dir in the user "home" dir
        outputdir = os.path.join(os.path.expanduser('~'), '.taurus')
        # create the output dir if it didn't exist
        if not os.path.exists(outputdir):
            os.makedirs(outputdir)

    debuglog = None
    if debug:
        debuglog = log

    common_kwargs = dict(optimize=optimize,
                         outputdir=outputdir,
                         debug=debug,
                         debuglog=debuglog,
                         errorlog=log)

    # lex/yacc v<3.0 do not accept  debuglog or errorlog keyword args
    if int(lex.__version__.split('.')[0]) < 3:
        common_kwargs.pop('debuglog')
        common_kwargs.pop('errorlog')

    try:
        _path = os.path.join(outputdir, 'jdraw_lextab.py')
        jdraw_lextab = imp.load_source('jdraw_lextab', _path)
    except:
        jdraw_lextab = 'jdraw_lextab'

    try:
        _path = os.path.join(outputdir, 'jdraw_yacctab.py')
        jdraw_yacctab = imp.load_source('jdraw_lextab', _path)
    except:
        jdraw_yacctab = 'jdraw_yacctab'

    # Lexer
    l = lex.lex(lextab=jdraw_lextab, **common_kwargs)

    # Yacc
    try:
        p = yacc.yacc(tabmodule=jdraw_yacctab,
                      debugfile=None,
                      write_tables=1,
                      **common_kwargs)
    except Exception, e:
        msg = (
            'Error creating jdraw parser.\n' +
            'HINT: Try removing jdraw_lextab.* and jdraw_yacctab.* from %s' %
            outputdir)
        raise RuntimeError(msg)
Esempio n. 34
0
    def setUp(self):
        self.logger = Logger('BaseControllerTestCase')
        if self.DEBUG:
            self.logger.setLogLevel(Logger.Debug)

        if self.KLASS is None:
            raise Exception('Ctrl klass has not been defined')
        name = 'test_ctrl'
        self.ctrl = self.KLASS(name, self.PROPS)
        self.pre_AddDevice_hook()
        self.ctrl.AddDevice(self.AXIS)
Esempio n. 35
0
 def __init__(self, name="FunctionGenerator"):
     EventGenerator.__init__(self)
     Logger.__init__(self, name)
     self._name = name
     self._initial_domain = None
     self._active_domain = None
     self._position_event = threading.Event()
     self._position = None
     self._initial_domain_in_use = None
     self._active_domain_in_use = None
     self._active_events = list()
     self._passive_events = list()
     self._started = False
     self._stopped = False
     self._running = False
     self._start_time = None
     self._direction = None
     self._condition = None
     self._id = None
     self._start_fired = False
Esempio n. 36
0
 def __init__(self,
              pool,
              name='PoolMonitor',
              period=5.0,
              min_sleep=1.0,
              auto_start=True):
     Logger.__init__(self, name)
     threading.Thread.__init__(self, name=name)
     self.daemon = True
     self._period = period
     self._min_sleep = min_sleep
     self._pool = pool
     self._stop = False
     self._pause = threading.Event()
     self._thread_pool = None
     self._ctrl_ids = []
     self._elem_ids = []
     pool.add_listener(self.on_pool_changed)
     if not auto_start:
         self.pause()
     self.start()
Esempio n. 37
0
def parse(filename=None, factory=None):

    if filename is None or factory is None:
        return

    _, p = new_parser()

    p.factory = factory
    p.modelStack = []
    p.modelStack2 = []

    res = None
    try:
        filename = os.path.realpath(filename)
        f = open(filename)
        res = yacc.parse(f.read())
    except:
        log = Logger('JDraw Parser')
        log.warning("Failed to parse %s" % filename)
        log.debug("Details:", exc_info=1)
    return res
Esempio n. 38
0
    def __init__(self, dclass, name):
        """Constructor"""
        self.in_constructor = True
        try:
            Device_4Impl.__init__(self, dclass, name)
            self.init(name)
            Logger.__init__(self, name)

            self._state = DevState.INIT
            self._status = 'Waiting to be initialized...'

            # access to some tango API (like MultiAttribute and Attribute) is
            # still not thread safe so we have this lock to protect
            # Wa can't always use methods which use internally the
            # C++ AutoTangoMonitor because it blocks the entire tango device.
            self.tango_lock = threading.RLock()

            self._event_thread_pool = get_thread_pool()
            self.init_device()
        finally:
            self.in_constructor = False
Esempio n. 39
0
    def __init__(self, dclass, name):
        """Constructor"""
        self.in_constructor = True
        try:
            Device_4Impl.__init__(self, dclass, name)
            self.init(name)
            Logger.__init__(self, name)

            self._state = DevState.INIT
            self._status = 'Waiting to be initialized...'

            # access to some tango API (like MultiAttribute and Attribute) is
            # still not thread safe so we have this lock to protect
            # Wa can't always use methods which use internally the
            # C++ AutoTangoMonitor because it blocks the entire tango device.
            self.tango_lock = threading.RLock()

            self._event_thread_pool = get_thread_pool()
            self.init_device()
        finally:
            self.in_constructor = False
Esempio n. 40
0
 def initializePage(self):
     try:
         widget = self.wizard().getPanelDescription().getWidget()
     except Exception as e:
         Logger().debug(repr(e))
         widget = None
     # prevent the user from changing the model if it was already set
     if isinstance(widget,
                   TaurusBaseComponent) and widget.getModelName() != '':
         self.modelLE.setText("(already set by the chosen widget)")
         self.modelGB.setEnabled(False)
     # try to get the SDM as if we were in a TaurusGui app
     try:
         if isinstance(Qt.qApp.SDM, SharedDataManager):
             sdm = Qt.qApp.SDM
     except Exception as e:
         Logger().debug(repr(e))
         sdm = None
     #@todo set selection filter in modelChooser based on the widget's modelclass
     self.itemDelegate = CommItemDelegate(widget=widget, sdm=sdm)
     self.commLV.setItemDelegate(self.itemDelegate)
Esempio n. 41
0
def parse(filename=None, factory=None):

    if filename is None or factory is None:
        return

    _, p = new_parser()
    print " \n\n\n zrobioby nowy parser"
    print "1: ", _
    print "2: ", p
    p.factory = factory
    p.modelStack = []
    p.modelStack2 = []

    res = None
    try:
        filename = os.path.realpath(filename)
        f = open(filename)
        #print f.read()
        print "\n\n\n otwprzylem plik w try"
        res = yacc.parse(f.read())  #nie bylo tracking=True, dodalem
        #for i in res:
        #    print "res i: ", i
        print "\n\n SPARSOWALEMW  TRY! \n"
        print "\n RES: ", res
    except:
        print "\n\n NIE SPARSOWALEM W TRY"
        log = Logger('JDraw Parser')
        #print "\n\n LOG1: ", log, "\n\n\n stop"
        log.warning("Failed to parse %s" % filename)
        #print "\n\n exec info: ?"
        log.debug("Details:", exc_info=1)
    return res
Esempio n. 42
0
 def getMemberDescription(self):
     try:
         membername = str(self.membersCB.currentText())
         member = getattr(self.module, membername, None)
         result = {'modulename': self.module.__name__}
     except Exception as e:
         Logger().debug('Cannot get member description: %s', repr(e))
         return None
     if inspect.isclass(member):
         result['classname'] = membername
     else:
         result['widgetname'] = membername
     return result
Esempio n. 43
0
def new_parser(optimize=None, debug=0, outputdir=None):
    log = Logger('JDraw Parser')

    if optimize is None:
        from taurus import tauruscustomsettings
        optimize = getattr(tauruscustomsettings, 'PLY_OPTIMIZE', 1)
    if outputdir is None:
        outputdir = os.path.dirname(os.path.realpath(__file__))
        #print "\n\n\n OUTPUT dir: ", outputdir

    debuglog = None
    if debug:
        debuglog = log

    common_kwargs = dict(optimize=optimize,
                         outputdir=outputdir,
                         debug=debug,
                         debuglog=debuglog,
                         errorlog=log)

    # lex/yacc v<3.0 do not accept  debuglog or errorlog keyword args
    if int(lex.__version__.split('.')[0]) < 3:
        common_kwargs.pop('debuglog')
        common_kwargs.pop('errorlog')

    try:
        from . import jdraw_lextab
    except ImportError:
        jdraw_lextab = 'jdraw_lextab'

    try:
        from . import jdraw_yacctab
    except ImportError:
        jdraw_yacctab = 'jdraw_yacctab'

    # Lexer
    l = lex.lex(lextab=jdraw_lextab, **common_kwargs)

    # Yacc
    try:
        p = yacc.yacc(tabmodule=jdraw_yacctab,
                      debugfile=None,
                      write_tables=1,
                      **common_kwargs)
    except Exception, e:
        print "\n\n Nie udal sie yacc yaccc"
        msg = ('Error while parsing. You may solve it by:\n' + \
               '  a) removing jdraw_lextab.* and jdraw_yacctab.* from\n' +\
               '     %s , or...\n' % os.path.dirname(__file__) + \
               '  b) setting PLY_OPTIMIZE=0 in tauruscustomsettings.py')
        raise RuntimeError(msg)
Esempio n. 44
0
    def fromPanel(panel):
        name = str(panel.objectName())
        classname = panel.getWidgetClassName()
        modulename = panel.getWidgetModuleName()
        # in the case of classes known to the TaurusWidgetFactory,
        # do not store the modulename
        if modulename.startswith('taurus.') and \
           classname in TaurusWidgetFactory().getWidgetClassNames():
            modulename = None
        widgetname = None
        floating = panel.isFloating()
        sharedDataWrite = None
        sharedDataRead = None
        model = getattr(panel.widget(), 'model', None)
        if model is None or isinstance(model, string_types):
            pass
        elif hasattr(model, '__iter__'):
            # if model is a sequence, convert to space-separated string
            try:
                model = " ".join(model)
            except Exception as e:
                msg = ('Cannot convert %s to a space-separated string: %s' %
                       (model, e))
                Logger().debug(msg)
                model = None
        else:
            # ignore other "model" attributes (they are not from Taurus)
            model = None

        return PanelDescription(name,
                                classname=classname,
                                modulename=modulename,
                                widgetname=widgetname,
                                floating=floating,
                                sharedDataWrite=sharedDataWrite,
                                sharedDataRead=sharedDataRead,
                                model=model,
                                icon=panel.icon,
                                model_in_config=panel.model_in_config,
                                modifiable_by_user=panel.modifiable_by_user,
                                widget_formatter=panel.widget_formatter,
                                widget_properties=panel.widget_properties)
Esempio n. 45
0
def main():
    from taurus.core.util.log import Logger
    from taurus.qt.qtgui.util import TaurusWidgetFactory
    Logger.setLogLevel(Logger.Debug)
    _log = Logger(__name__)

    try:
        wf = TaurusWidgetFactory()
        klasses = wf.getWidgetClasses()
        ok_nb, skipped_nb, e1_nb, e2_nb, e3_nb, e4_nb = 0, 0, 0, 0, 0, 0
        for widget_klass in klasses:
            name = widget_klass.__name__
            #_log.debug("Processing %s" % name)
            if name in _SKIP:
                #_log.debug("Skipped %s" % name)
                skipped_nb += 1
                continue
            # if getQtDesignerPluginInfo does not exist, returns None or raises
            # an exception: forget the widget
            cont = False
            try:
                qt_info = widget_klass.getQtDesignerPluginInfo()
                if qt_info is None:
                    #_log.debug("E1: Canceled %s (getQtDesignerPluginInfo)" % name)
                    e1_nb += 1
                    cont = True
            except AttributeError:
                #_log.debug("E2: Canceled %s (widget doesn't have getQtDesignerPluginInfo())" % name)
                e2_nb += 1
                cont = True
            except Exception, e:
                #_log.debug("E3: Canceled %s (%s)" % (name, str(e)))
                e3_nb += 1
                cont = True

            if cont:
                continue
            for k in ('module', ):
                if not qt_info.has_key(k):
                    #_log.debug("E4: Canceled %s (getQtDesignerPluginInfo doesn't have key %s)" % (name, k))
                    e4_nb += 1
                    cont = True
            if cont:
                continue

            plugin_klass = build_qtdesigner_widget_plugin(widget_klass)
            plugin_klass_name = plugin_klass.__name__
            globals()[plugin_klass_name] = plugin_klass
            _plugins[plugin_klass_name] = plugin_klass

            ok_nb += 1
            #_log.debug("DONE processing %s" % name)
        _log.info("Inpected %d widgets. %d (OK), %d (Skipped), %d (E1), %d (E2), %d (E3), %d(E4)" % (
            len(klasses), ok_nb, skipped_nb, e1_nb, e2_nb, e3_nb, e4_nb))
        _log.info("E1: getQtDesignerPluginInfo() returns None")
        _log.info("E2: widget doesn't implement getQtDesignerPluginInfo()")
        _log.info("E3: getQtDesignerPluginInfo() throws exception")
        _log.info(
            "E4: getQtDesignerPluginInfo() returns dictionary with missing key (probably 'module' key)")
Esempio n. 46
0
def log_dependencies():
    from taurus.core.util.log import Logger
    l = Logger("taurus")
    for msg in _check_dependencies(forlog=True):
        if msg[0] != -1:
            l.info(msg[1])
Esempio n. 47
0
class TaurusEmitterThread(Qt.QThread):
    """
    The TaurusEmitterThread Class
    ==========================

    This object get items from a python Queue and performs a thread safe operation on them.
    It is useful to serialize Qt tasks in a background thread.

    :param parent: a Qt/Taurus object
    :param name: identifies object logs
    :param queue: if None parent.getQueue() is used, if not then the queue passed as argument is used
    :param method: the method to be executed using each queue item as argument
    :param cursor: if True or QCursor a custom cursor is set while the Queue is not empty

    How TaurusEmitterThread works
    --------------------------

    TaurusEmitterThread is a worker thread that processes a queue of iterables passed as arguments to the specified method every time that  ``doSomething()`` is called:

     * ``self.method(*item)`` will be called if TaurusEmitterThread.method has been initialized.
     * ``item[0](item[1:])`` will be called if ``method`` is not initialized and the first element of the queued iterable is *callable*.

    TaurusEmitterThread uses two queues:

     * ``self.queue`` manages the objects added externally:

      + the ``next()`` method passes objects from ``self.queue`` to ``self.todo queue``
      + every time that a *somethingDone* signal arrives ``next()`` is called.
      + ``next()`` can be called also externally to ensure that the main queue is being processed.
      + the queue can be accessed externally using ``getQueue()``
      + ``getQueue().qsize()`` returns the number of remaining objects in queue.
      + while there are objects in queue the ``.next()`` method will override applications cursor. a call to ``next()`` with an empty queue will restore the original cursor.

     * ``self.todo`` is managed by the ``run()/start()`` method:

      - a loop waits continuously for new objects in ``self.todo`` queue.
      - if an object is found, it is sent in a *doSomething* signal.
      - if *"exit"* is found the loop exits.

    Usage example
    -------------

    .. code-block:: python

        #Applying TaurusEmitterThread to an existing class:
        import Queue
        from functools import partial

        def modelSetter(args):
            obj,model = args[0],args[1]
            obj.setModel(model)

        klass TaurusGrid(Qt.QFrame, TaurusBaseWidget):
            ...
            def __init__(self, parent = None, designMode = False):
                ...
                self.modelsQueue = Queue.Queue()
                self.modelsThread = TaurusEmitterThread(parent=self,queue=self.modelsQueue,method=modelSetter )
                ...
            def build_widgets(...):
                ...
                            previous,synoptic_value = synoptic_value,TaurusValue(cell_frame)
                            #synoptic_value.setModel(model)
                            self.modelsQueue.put((synoptic_value,model))
                ...
            def setModel(self,model):
                ...
                if hasattr(self,'modelsThread') and not self.modelsThread.isRunning():
                    self.modelsThread.start()
                elif self.modelsQueue.qsize():
                    self.modelsThread.next()
                ...

    """

    def __init__(self, parent=None, name='', queue=None, method=None, cursor=None, sleep=5000):
        """
        Parent most not be None and must be a TaurusGraphicsScene!
        """
        Qt.QThread.__init__(self, parent)
        self.name = name
        self.log = Logger('TaurusEmitterThread(%s)' % self.name)
        self.log.setLogLevel(self.log.Info)
        self.queue = queue or Queue.Queue()
        self.todo = Queue.Queue()
        self.method = method
        self.cursor = Qt.QCursor(
            Qt.Qt.WaitCursor) if cursor is True else cursor
        self._cursor = False
        self.timewait = sleep

        self.emitter = QEmitter()
        self.emitter.moveToThread(Qt.QApplication.instance().thread())
        # Mandatory!!! if parent is set before changing thread it could lead to
        # segFaults!
        self.emitter.setParent(Qt.QApplication.instance())
        self._done = 0
        # Moved to the end to prevent segfaults ...
        self.emitter.doSomething.connect(self._doSomething)
        self.emitter.somethingDone.connect(self.next)

    def getQueue(self):
        if self.queue:
            return self.queue
        elif hasattr(self.parent(), 'getQueue'):
            self.parent().getQueue()
        else:
            return None

    def getDone(self):
        """ Returns % of done tasks in 0-1 range """
        return self._done / (self._done + self.getQueue().qsize()) if self._done else 0.

    def clear(self):
        while not self.todo.empty():
            self.todo.get()
        while not self.getQueue().empty():
            self.getQueue().get()
            self._done += 1

    def purge(obj):
        nqueue = Queue.Queue()
        while not self.todo.empty():
            i = self.todo.get()
            if obj not in i:
                nqueue.put(i)
        while not self.queue.empty():
            i = self.queue.get()
            if obj not in i:
                nqueue.put(i)
        while not nqueue.empty():
            self.queue.put(nqueue.get())
        self.next()

    def _doSomething(self, params):
        self.log.debug('At TaurusEmitterThread._doSomething(%s)' % str(params))
        if not self.method:
            method, args = params[0], params[1:]
        else:
            method, args = self.method, params
        if method:
            try:
                method(*args)
            except:
                self.log.error('At TaurusEmitterThread._doSomething(%s): \n%s' % (
                    map(str, args), traceback.format_exc()))
        self.emitter.somethingDone.emit()
        self._done += 1
        return

    def next(self):
        queue = self.getQueue()
        msg = 'At TaurusEmitterThread.next(), %d items remaining.' % queue.qsize()
        (queue.empty() and self.log.info or self.log.debug)(msg)
        try:
            if not queue.empty():
                if not self._cursor and self.cursor is not None:
                    Qt.QApplication.instance().setOverrideCursor(Qt.QCursor(self.cursor))
                    self._cursor = True
                # A blocking get here would hang the GUIs!!!
                item = queue.get(False)
                self.todo.put(item)
                self.log.debug('Item added to todo queue: %s' % str(item))
            elif self._cursor:
                Qt.QApplication.instance().restoreOverrideCursor()
                self._cursor = False

        except Queue.Empty:
            self.log.warning(traceback.format_exc())
            pass
        except:
            self.log.warning(traceback.format_exc())
        return

    def run(self):
        Qt.QApplication.instance().thread().sleep(int(self.timewait / 1000)
                                                  if self.timewait > 10 else int(self.timewait))  # wait(self.sleep)
        self.log.info('#' * 80)
        self.log.info('At TaurusEmitterThread.run()')
        self.next()
        while True:
            self.log.debug('At TaurusEmitterThread.run() loop.')
            item = self.todo.get(True)
            if isString(item):
                if item == "exit":
                    break
                else:
                    continue
            self.log.debug('Emitting doSomething signal ...')
            self.emitter.doSomething.emit(item)
            # End of while
        self.log.info(
            '#' * 80 + '\nOut of TaurusEmitterThread.run()' + '\n' + '#' * 80)
Esempio n. 48
0
File: util.py Progetto: cmft/sardana
def prepare_logging(options, args, tango_args, start_time=None, log_messages=None):
    taurus.setLogLevel(taurus.Debug)
    root = Logger.getRootLog()

    # output logger configuration
    log_output_level = options.log_level
    log_level_map = { "0" : taurus.Critical, "critical" : taurus.Critical,
                       "1" : taurus.Error, "error" : taurus.Error,
                       "2" : taurus.Warning, "warning" : taurus.Warning,
                       "3" : taurus.Info, "info" : taurus.Info,
                       "4" : taurus.Debug, "debug" : taurus.Debug,
                       "5" : taurus.Trace, "trace" : taurus.Trace,
                     }
    log_output_level = log_level_map[log_output_level]
    root.handlers[0].setLevel(log_output_level)

    if not options.without_log_file:
        log_file_level = options.log_file_level
        log_file_level = log_level_map[log_file_level]

        # Create a file handler
        if options.log_file_name is None:
            _, ds_name = os.path.split(args[0])
            ds_name, _ = os.path.splitext(ds_name)
            ds_instance = args[-1].lower()
            import getpass
            try:
                tangodir = 'tango-%s' % getpass.getuser()  #include the user name to avoid permission errors
            except:
                tangodir = 'tango' % getpass.getuser()
            path = os.path.join(os.sep, "tmp", tangodir, ds_name, ds_instance)
            log_file_name = os.path.join(path, 'log.txt')
        else:
            log_file_name = options.log_file_name
        path = os.path.dirname(log_file_name)

        # because some versions of python have a bug in logging.shutdown (this
        # function is not protected against deleted handlers) we store the
        # handlers we create to make sure a strong reference exists when the
        # logging.shutdown is called
        taurus._handlers = handlers = []
        try:
            if not os.path.exists(path):
                os.makedirs(path, 0777)
            
            from sardana import sardanacustomsettings    
            maxBytes = getattr(sardanacustomsettings, 'LOG_FILES_SIZE', 1E7)
            backupCount = getattr(sardanacustomsettings, 'LOG_BCK_COUNT', 5)

            fmt = Logger.getLogFormat()
            f_h = logging.handlers.RotatingFileHandler(log_file_name,
                                                       maxBytes=maxBytes,
                                                       backupCount=backupCount)
            f_h.setFormatter(fmt)
            f_h.setLevel(log_file_level)
            root.addHandler(f_h)
            handlers.append(f_h)

            if start_time is not None:
                taurus.info("Started at %s", start_time)
            else:
                taurus.info("Starting up...")
            taurus.info("Log is being stored in %s", log_file_name)
        except:
            if start_time is not None:
                taurus.info("Started at %s", start_time)
            else:
                taurus.info("Starting up...")
            taurus.warning("'%s' could not be created. Logs will not be stored",
                           log_file_name)
            taurus.debug("Error description", exc_info=1)

    if log_messages is None:
        log_messages = []
    for log_message in log_messages:
        taurus.info(*log_message)

    taurus.debug("Start args=%s", args)
    taurus.debug("Start tango args=%s", tango_args)
    taurus.debug("Start options=%s", options)
    taurus.debug("Using PyTango %s from %s", PyTango.Release.version, PyTango.__path__[0])
    taurus.debug("Using taurus %s from %s", taurus.Release.version, taurus.__path__[0])
    taurus.debug("Using sardana %s from %s", sardana.Release.version, sardana.__path__[0])
Esempio n. 49
0
    def __init__(self, *args, **kwargs):
        """The constructor. Parameters are the same as QApplication plus a
        keyword parameter: 'cmd_line_parser' which should be an instance of
        :class:`optparse.OptionParser`"""

        # lock to safely get singleton elements (like IPython taurus
        # console app)
        self._lock = threading.Lock()

        if len(args) == 0:
            args = getattr(sys, 'argv', []),

        parser = None
        app_name, app_version, org_name, org_domain = None, None, None, None
        if 'app_name' in kwargs:
            app_name = kwargs.pop('app_name')
        if 'app_version' in kwargs:
            app_version = kwargs.pop('app_version')
        if 'org_name' in kwargs:
            org_name = kwargs.pop('org_name')
        if 'org_domain' in kwargs:
            org_domain = kwargs.pop('org_domain')
        if 'cmd_line_parser' in kwargs:
            parser = kwargs.pop('cmd_line_parser')

        try:
            Qt.QApplication.__init__(self, *args, **kwargs)
        except TypeError:
            Qt.QApplication.__init__(self, *args)

        Logger.__init__(self)

        self._out = None
        self._err = None

        if app_name is not None:
            self.setApplicationName(app_name)
        if app_version is not None:
            self.setApplicationVersion(app_version)
        if org_name is not None:
            self.setOrganizationName(org_name)
        if org_domain is not None:
            self.setOrganizationDomain(org_domain)

        # if the constructor was called without a parser or with a parser that
        # doesn't contain version information and with an application
        # name and/or version, then add the --version capability
        if (parser is None or parser.version is None) and app_version:
            v = app_version
            if app_name:
                v = app_name + " " + app_version
            if parser is None:
                parser = optparse.OptionParser(version=v)
            elif parser.version is None:
                parser.version = v
                parser._add_version_option()

        p, opt, args = \
            taurus.core.util.argparse.init_taurus_args(
                parser=parser, args=args[0][1:])

        self._cmd_line_parser = p
        self._cmd_line_options = opt
        self._cmd_line_args = args
        self.__registerQtLogger()
        self.__registerExtensions()
        self.__redirect_std()
Esempio n. 50
0
class SingletonWorker():  # Qt.QObject):
    """
    The SingletonWorker works
    =========================

    The SingletonWorker class is constructed using the same arguments than the TaurusTreadEmitter class ; but instead of creating a QThread for each instance of the class it creates a single QThread for all instances.

    The Queue is still different for each of the instances; it is connected to the TaurusEmitterThread signals (*next()* and *somethingDone()*) and each Worker queue is used as a feed for the shared QThread.

    This implementation reduced the cpu of vacca application in a 50% factor.

    :param parent: a Qt/Taurus object
    :param name: identifies object logs
    :param queue: if None parent.getQueue() is used, if not then the queue passed as argument is used
    :param method: the method to be executed using each queue item as argument
    :param cursor: if True or QCursor a custom cursor is set while the Queue is not empty
    This class is used to manage TaurusEmitterThread as Singleton objects:
    """
    _thread = None

    def __init__(self, parent=None, name='', queue=None, method=None, cursor=None, sleep=5000, log=Logger.Warning, start=True):
        self.name = name
        self.log = Logger('SingletonWorker(%s)' % self.name)
        self.log.setLogLevel(log)
        self.log.info('At SingletonWorker.__init__(%s)' % self.name)
        self.parent = parent
        self.method = method
        self._running = False
        if SingletonWorker._thread is None:
            SingletonWorker._thread = TaurusEmitterThread(
                parent, name='SingletonWorker', cursor=cursor, sleep=sleep)
        self.thread = SingletonWorker._thread
        self.queue = queue or Queue.Queue()
        if start:
            self.start()

    def put(self, item, block=True, timeout=None):
        self.getQueue().put(item, block, timeout)

    def size(self):
        return self.getQueue().qsize()

    def next(self, item=None):
        if item is not None:
            self.put(item)
        elif self.queue.empty():
            return
        msg = 'At SingletonWorker.next(), %d items not passed yet to Emitter.' % self.queue.qsize()
        self.log.info(msg)
        #(queue.empty() and self.log.info or self.log.debug)(msg)
        try:
            i = 0
            while not self.queue.empty():
                # A blocking get here would hang the GUIs!!!
                item = self.queue.get(False)
                if self.method:
                    self.thread.getQueue().put([self.method] + list(item))
                else:
                    self.thread.getQueue().put(item)
                i += 1
            self.log.info('%d Items added to emitter queue' % i)
            self.thread.emitter.newQueue.emit()
        except Queue.Empty:
            self.log.warning(traceback.format_exc())
        except:
            self.log.warning(traceback.format_exc())
        return

    def getQueue(self):
        return self.queue

    def getDone(self):
        return self.thread.getDone()

    def start(self):
        self.thread.emitter.somethingDone.connect(self.next)
        self.thread.emitter.newQueue.connect(self.thread.next)
        try:
            self.thread.start()
        except:
            pass
        self.next()
        self._running = True
        return

    def stop(self):
        self.thread.emitter.somethingDone.disconnect(self.next)
        self.thread.emitter.newQueue.disconnect(self.thread.next)
        self._running = False
        return

    def clear(self):
        """
        This method will clear queue only if next() has not been called.
        If you call self.thread.clear() it will clear objects for all workers!, be careful
        """
        while not self.queue.empty():
            self.queue.get()
        # self.thread.clear()

    def purge(obj):
        nqueue = Queue.Queue()
        while not self.queue.empty():
            i = self.queue.get()
            if obj not in i:
                nqueue.put(i)
        while not nqueue.empty():
            self.queue.put(nqueue.get())

    def isRunning(self):
        return self._running

    def isFinished(self):
        return self.thread.isFinished()

    def finished(self):
        return self.thread.finished()

    def started(self):
        return self._running

    def terminated(self):
        return self.thread.terminated()

    def sleep(self, s):
        return self.thread.sleep(s)
Esempio n. 51
0
 def __init__(self, widget, fileName):
     Qt.QObject.__init__(self)
     Logger.__init__(self)
     self._widget = widget
     self._fileName = fileName
     self.grab.connect(self.onGrab)
Esempio n. 52
0
def prepare_logstash(args):
    """Prepare logstash handler based on the configuration stored in the Tango
    database.

    :param args: process execution arguments
    :type args: list<str>

    .. note::
        The prepare_logstash function has been included in Sardana
        on a provisional basis. Backwards incompatible changes
        (up to and including its removal) may occur if
        deemed necessary by the core developers.
    """
    log_messages = []

    try:
        from logstash_async.handler import AsynchronousLogstashHandler
    except ImportError:
        msg = ("Unable to import logstash_async. Skipping logstash "
               + "configuration...", )
        log_messages.append(msg,)
        return log_messages

    def get_logstash_conf(dev_name):
        try:
            props = db.get_device_property(dev_name, "LogstashHost")
            host = props["LogstashHost"][0]
        except IndexError:
            host = None
        try:
            props = db.get_device_property(dev_name, "LogstashPort")
            port = int(props["LogstashPort"][0])
        except IndexError:
            port = None
        try:
            props = db.get_device_property(dev_name, "LogstashCacheDbPath")
            cache_db_path = props["LogstashCacheDbPath"][0]
        except IndexError:
            cache_db_path = None
        return host, port, cache_db_path

    db = Database()

    bin_name = args[0]
    try:
        instance_name = args[1]
    except IndexError:
        msg = ("Unknown %s instance name. " % bin_name
               + "Skipping logstash configuration...")
        log_messages.append(msg, )
        return log_messages
    server_name = bin_name + "/" + instance_name
    if bin_name in ["Pool", "MacroServer"]:
        class_name = bin_name
        dev_name = get_dev_from_class_server(db, class_name, server_name)[0]
        host, port, cache = get_logstash_conf(dev_name)
    else:
        dev_name = get_dev_from_class_server(db, "Pool", server_name)[0]
        host, port, cache = get_logstash_conf(dev_name)
        if host is None:
            dev_name = get_dev_from_class_server(db, "MacroServer",
                                                 server_name)[0]
            host, port, cache = get_logstash_conf(dev_name)

    if host is not None:
        root = Logger.getRootLog()
        handler = AsynchronousLogstashHandler(host, port, database_path=cache)
        # don't use full path for program_name
        handler._create_formatter_if_necessary()
        _, handler.formatter._program_name = os.path.split(handler.formatter._program_name)
        root.addHandler(handler)
        msg = ("Log is being sent to logstash listening on %s:%d",
               host, port)
        log_messages.append(msg)

    return log_messages