def __init__(self,tangoDevice,period=.1,threadname=None,wait=2, retries=3,log='DEBUG',blackbox=0):
        print "In SerialVacuumDevice::init_device(",tangoDevice,")"

        self.init = False
        self.trace = False
                
        self.readList = fandango.SortedDict() #Dictionary
        self.writeList = fandango.SortedDict() #Dictionary
        self.pollingList = fandango.SortedDict()
        self.PostCommand = []        
        self.args = (0,0) #Tuple
        self.kwargs = {'nada':0} #Dictionary

        self.blankChars = set([ '\n', '\r', ' ', '>' ])
        ## period (seconds): It will be divided between the number 
        # of pollings to determine the pause between readings
        self.period = max(period,.020)
        ## waitTime(seconds): It will be the maximum time that the 
        # device server will wait for an answer from the serial line.        
        self.waitTime = max(wait,.020)
        self.retries = retries
        
        self.lasttime = 0 #Used to store the time of the last communication
        self.lastrecv = ''
        self.lastsend = ''
        self.lasterror = ''
        self.lasterror_epoch = 0
        self.maxreadtime = 0
        self.errors = -1
        self.error_rate = 0
        self.error_rate_epoch = time.time()
        self.comms = 0
        self.stop_threads=False
        self._last_read = ''

        #Inherited: .tangoDevice = name, .dp = deviceproxy
        self.threadname = threadname    
        self.lock=threading.RLock();
        self.event=threading.Event();
        self.threadname=threadname
        self.updateThread = None
        
        TangoDev.__init__(self,tangoDevice)
        self.call__init__(Logger,'SVD('+tangoDevice+')',format='%(levelname)-8s %(asctime)s %(name)s: %(message)s')
        try: self.setLogLevel(log)
        except: print('Unable to set SerialVacuumDevice.LogLevel')
        self.errors = 0
        
        if blackbox: 
            self.blackbox = BlackBox(blackbox)
            self.dp.command_inout = self.blackbox.decorator(self.dp.command_inout)
            self.serialComm = self.blackbox.decorator(self.serialComm)
            print 'SerialVacuumDevice.BlackBox(%d) created'%self.blackbox.size
        else: self.blackbox = None
Beispiel #2
0
def get_archivers_filters(archivers=''):
    """
    Returns the value of AttributeFilters property for each archiver in the list

    :param archivers: sequence or regular expression  for matching archivers
    :return: dictionary {archiver:regexp}
    """
    archs = archs if fn.isSequence(archivers) else fn.find_devices(archivers)
    filters = fn.SortedDict(
        sorted((k, v['AttributeFilters'])
               for k, v in fn.tango.get_matching_device_properties(
                   archiver, 'AttributeFilters').items()))
    return filters
Beispiel #3
0
def get_archivers_filters(archiver='archiving/es/*'):
    filters = fn.SortedDict(sorted((k,v['AttributeFilters']) for k,v in 
                    fn.tango.get_matching_device_properties(
                    archiver,'AttributeFilters').items()))
    return filters
class Schemas(object):
    """ Schemas kept in a singleton object """

    SCHEMAS = fandango.SortedDict()
    MODULES = {
        'fandango': fun,
        'fun': fun
    }  #Limited access to fandango library
    LOCALS = fandango.functional.__dict__.copy()

    @classmethod
    def __contains__(k, o):
        return o in k.SCHEMAS.keys()

    @classmethod
    def keys(k):
        if not k.SCHEMAS: k.load()
        return k.SCHEMAS.keys()

    @classmethod
    def load(k, tango='', prop=''):
        tangodb = fandango.tango.get_database(tango)
        schemas = prop or tangodb.get_property('PyTangoArchiving',
                                               'Schemas')['Schemas']
        if not schemas:
            schemas = ['tdb', 'hdb']
            tangodb.put_property('PyTangoArchiving', {'Schemas': schemas})
        [k.getSchema(schema, tango, write=True) for schema in schemas]
        return k.SCHEMAS

    @classmethod
    def pop(k, key):
        k.SCHEMAS.pop(key)

    @classmethod
    def _load_object(k, obj, dct):
        rd = obj
        m = rd.split('(')[0].rsplit('.', 1)[0]
        c = rd[len(m) + 1:]
        if m not in k.MODULES:
            fandango.evalX('import %s' % m, modules=k.MODULES)
        #print('getSchema(%s): load %s reader'%(schema,dct.get('reader')))
        return fandango.evalX(obj, modules=k.MODULES, _locals=dct)

    @classmethod
    def getSchema(k, schema, tango='', prop='', logger=None, write=False):

        if schema.startswith('#') and EXPERT_MODE:
            schema = schema.strip('#')
            print('%s is enabled' % schema)

        if schema in k.SCHEMAS:
            # Failed schemas should be also returned (to avoid unneeded retries)
            return k.SCHEMAS[schema]

        dct = {
            'schema': schema,
            'dbname': schema,
            'match': clmatch,
            'clmatch': clmatch
        }

        try:
            tango = fandango.tango.get_database(tango)
            props = prop or tango.get_property('PyTangoArchiving',
                                               schema)[schema]
            assert len(props)
            if fandango.isSequence(props):
                props = [map(str.strip, t.split('=', 1)) for t in props]
            dct.update(props)

            rd = dct.get('reader', dct.get('api'))
            if rd:
                dct['logger'] = logger
                dct['reader'] = rd = k._load_object(rd, dct)

                if not hasattr(rd, 'is_attribute_archived'):
                    rd.is_attribute_archived = lambda *a, **k: True
                if not hasattr(rd, 'get_attributes'):
                    rd.get_attributes = lambda *a, **k: []
                if not hasattr(rd, 'get_attribute_values'):
                    if dct['method']:
                        rd.get_attribute_values = getattr(rd, dct['method'])

            if not hasattr(rd, 'schema'): rd.schema = dct['schema']

        except Exception, e:
            print('getSchema(%s): failed!' % schema)
            if logger:
                exc = traceback.format_exc()
                try:
                    logger.warning(exc)
                except:
                    print(exc)
            dct = None

        if write:
            k.SCHEMAS[schema] = dct
        return dct
Beispiel #5
0
class Schemas(object):
    """ Schemas kept in a singleton object """

    SCHEMAS = fn.SortedDict()
    #Limited access to fandango library
    MODULES = {
        'fandango': fn.functional,
        'fn': fn.functional,
        'fun': fn.functional
    }
    LOCALS = fn.functional.__dict__.copy()

    def __init__(self):
        self.load()

    @classmethod
    def __contains__(k, o):
        return o in k.SCHEMAS.keys()

    @classmethod
    def keys(k):
        if not k.SCHEMAS:
            k.load()
        return k.SCHEMAS.keys()

    @classmethod
    def values(k):
        if not k.SCHEMAS:
            k.load()
        return k.SCHEMAS.values()

    @classmethod
    def items(k):
        if not k.SCHEMAS:
            k.load()
        return k.SCHEMAS.items()

    @classmethod
    def __iter__():
        """ TODO: iter() does not work in classes!"""
        return k.SCHEMAS.__iter__()

    @classmethod
    def __contains__(k, key):
        return k.SCHEMAS.__contains__(key)

    @classmethod
    def __getitem__(k, key):
        return k.SCHEMAS.__getitem__(key)

    @classmethod
    def get(k, key, default=None):
        if not k.SCHEMAS:
            k.load()
        return k.SCHEMAS.get(key, default)

    @classmethod
    @fn.Cached(expire=60.)
    def load(k, tango='', prop='', logger=None):
        try:
            tangodb = fn.tango.get_database(tango)
            props = prop or tangodb.get_property('PyTangoArchiving',
                                                 ['DbSchemas', 'Schemas'])

            try:
                schemas = props.get('DbSchemas', [])
            except:
                print('PyTangoArchiving.DbSchemas not initialized')
                schemas = []

            if not schemas:
                schemas = props.get('Schemas', []) or []

            if not schemas:
                schemas = ['hdbpp']
                tangodb.put_property('PyTangoArchiving',
                                     {'DbSchemas': schemas})

            #print('Loading %s from tango@%s ... ' % (pname, tangodb.get_db_host()))

            [
                k.getSchema(schema, tango, write=True, logger=logger)
                for schema in schemas
            ]

            return k.SCHEMAS

        except:
            traceback.print_exc()
            return []

    @classmethod
    def pop(k, key):
        k.SCHEMAS.pop(key)

    @classmethod
    def _load_object(k, obj, dct):
        rd = obj
        m = rd.split('(')[0].rsplit('.', 1)[0]
        c = rd[len(m) + 1:]
        if m not in k.MODULES:
            fn.evalX('import %s' % m, modules=k.MODULES)
        #print('getSchema(%s): load %s reader'%(schema,dct.get('reader')))
        return fn.evalX(obj, modules=k.MODULES, _locals=dct)

    @classmethod
    def getReader(k, schema, dct=None):
        # This method initializes a reader object from Schema config
        # It does not update the Schema object, just returns a reader

        dct = dct if dct is not None else k.getSchema(
            schema if fn.isString(schema) else schema.get('schema'))
        rd = dct.get('reader', dct.get('api'))

        if rd and isinstance(rd, str):
            try:
                #print('Schemas.getReader(%s): instantiating reader' % schema)

                rd = k._load_object(rd, dct)
                #print('getReader(%s): %s' % (schema,type(rd)))
                if not hasattr(rd, 'is_attribute_archived'):
                    rd.is_attribute_archived = lambda *a, **k: True
                if not hasattr(rd, 'get_attributes'):
                    rd.get_attributes = lambda *a, **k: []
                if not hasattr(rd, 'get_attribute_values'):
                    if dct['method']:
                        rd.get_attribute_values = getattr(rd, dct['method'])
                if not hasattr(rd, 'schema'):
                    rd.schema = schema
            except:
                print('getReader(%s) failed!' % schema)
                #traceback.print_exc()
                rd = None

        return rd

    @classmethod
    def getSchema(k, schema, tango='', prop='', logger=None, write=False):
        """
        Get schema specs as defined in Tango.FreePropertis.DbSchemas
        """

        if schema.startswith('#') and EXPERT_MODE:
            schema = schema.strip('#')
            #print('%s is enabled'%schema)

        if schema in k.SCHEMAS:
            # Failed schemas should be also returned (to avoid unneeded retries)
            return k.SCHEMAS[schema]

        dct = {'match': clmatch, 'clmatch': clmatch}
        if ';' in schema:
            schema, dct = schema.split(';', 1)
            dct = dict(d.split('=', 1) for d in dct.split(';'))
        dct['schema'] = schema
        dct = SchemaDict(dct)
        props = []

        try:
            tango = fn.tango.get_database(tango)
            props = prop or tango.get_property('PyTangoArchiving',
                                               schema)[schema]
            assert len(props)
            if fn.isSequence(props):
                props = dict(map(str.strip, t.split('=', 1)) for t in props)
            if 'check' in dct:
                props.pop('check')
            dct.update(props)
            dct['logger'] = logger

        except Exception as e:
            print('getSchema(%s): failed!' % schema)
            print(dct, props)
            exc = traceback.format_exc()
            try:
                logger.warning(exc)
            except:
                print(exc)
            dct = None

        if write:
            k.SCHEMAS[schema] = dct

        return dct

    @classmethod
    def getSchemasForAttribute(attr, start=0, stop=fn.END_OF_TIME):
        """
        returns a fallback schema chain for the given dates
        """
        #print('getSchemasForAttribute(%s)' % attr)
        return [s for s in k.SCHEMAS if k.checkSchema(s, attr, start, stop)]

    @classmethod
    def checkSchema(k, schema, attribute='', start=None, stop=None):
        if not isinstance(schema, SchemaDict):
            schema = k.getSchema(schema)
        if not schema:
            return False

        f = schema.get('check')
        if not f:
            print('%s has no check function' % str(schema))
            return True

        try:
            now = time.time()
            start = (str2time(start) if fn.isString(start) else fn.notNone(
                start, now - 1))
            stop = (str2time(stop) if fn.isString(stop) else fn.notNone(
                stop, now))
            xmatch = lambda e, a: clmatch(e, a, extend=True)
            k.LOCALS.update({
                'attr':
                attribute.lower(),
                'attribute':
                attribute.lower(),
                'device':
                attribute.lower().rsplit('/', 1)[0],
                'match':
                lambda r: xmatch(r, attribute),
                'clmatch':
                xmatch,
                'overlap':
                overlap,
                'time2str':
                time2str,
                'str2time':
                str2time,
                't2s':
                time2str,
                's2t':
                str2time,
                'start':
                start,
                'stop':
                stop,
                'now':
                now,
                'begin':
                start,
                'end':
                stop,
                'NOW':
                now,
                'reader':
                schema.get('reader', schema.get('api')),
                'schema':
                schema.get('schema'),
                'dbname':
                schema.get('dbname',
                           schema.get('db_name', schema.get('schema', ''))),
            })
            if 'reader' in f:
                k.getReader(schema.get('schema'))
            if 'api' in f:
                k.getApi(schema.get('schema'))

            #print('In reader.Schemas.checkSchema(%s,%s,%s,%s): %s'
            #% (schema,attribute,start,stop,f))
            #print('(%s)%%(%s)'%(f,[t for t in k.LOCALS.items() if t[0] in f]))
            v = fn.evalX(f, k.LOCALS, k.MODULES)
        except:
            print('checkSchema(%s,%s) failed!' % (schema, attribute))
            traceback.print_exc()
            v = False

        #print('checkSchema(%s): %s'%(schema,v))
        return v

    @classmethod
    def getApi(k, schema):
        if schema == '*':
            import PyTangoArchiving.reader
            return PyTangoArchiving.reader.Reader()
        elif fn.isString(schema):
            schema = k.getSchema(schema)
            if schema is not None:
                api = schema.get('api', 'PyTangoArchiving.ArchivingAPI')
                if fn.isString(api):
                    api = k._load_object(api, schema)
                return api(schema['schema']) if isinstance(api, type) else api
        else:
            return schema