def __init__(self,pattern='',klass='',devs_list='',servers_list='',hosts='',loadAll=False,log='WARNING',logger=None,tango_host=''): ''' def __init__(self,pattern='', klass='',devs_list='',servers_list=''): The ServersDict can be initialized using any of the three argument lists or a wildcard for Database.get_server_list(pattern) ServersDict('*') can be used to load all servers in database ''' self.call__init__(CaselessDict,self) if logger is None: self.log = Logger('ServersDict') self.log.setLogLevel(log) else: self.log=logger self.log.debug('ServersDict(%s)'%','.join(map(str,(pattern,klass,devs_list,servers_list,hosts,loadAll,log,logger,tango_host)))) ## proxies will keep a list of persistent device proxies self.proxies = ProxiesDict() ## db will keep a persistent link to PyTango database self.db = get_database() if not tango_host else get_database(*(tango_host.split(':'))) self.server_names = self.keys self.servers = self.values if loadAll: self.load_all_servers() elif klass: self.load_by_class(klass) elif devs_list: self.load_from_devs_list(devs_list) elif servers_list: self.load_from_servers_list(servers_list) #elif pattern: self.load_from_servers_list(self.db.get_server_list(pattern)) elif hosts: hosts = type(hosts) is str and (',' in hosts and hosts.split(',') or [hosts]) or hosts for h in hosts: self.load_by_host(h) elif pattern: self.load_by_name(pattern)
def __init__(self, pattern='', klass='', devs_list='', servers_list='', hosts='', loadAll=False, log='WARNING', logger=None, tango_host='', host='', devices=''): ''' def __init__(self,pattern='', klass='',devs_list='',servers_list=''): The ServersDict can be initialized using any of the three argument lists or a wildcard for Database.get_server_list(pattern) ServersDict('*') can be used to load all servers in database ''' self.call__init__(CaselessDict, self) if logger is None: self.log = Logger('ServersDict') self.log.setLogLevel(log) else: self.log = logger self.log.debug('ServersDict(%s)' % ','.join( map(str, (pattern, klass, devs_list, servers_list, hosts, loadAll, log, logger, tango_host)))) ## proxies will keep a list of persistent device proxies self.proxies = ProxiesDict(tango_host=tango_host) ## db will keep a persistent link to PyTango database self.db = get_database() if not tango_host else get_database( *(tango_host.split(':'))) self.server_names = self.keys self.servers = self.values if loadAll: self.load_all_servers() elif klass: self.load_by_class(klass) elif devs_list: self.load_from_devs_list(devs_list) elif devices: self.load_from_devs_list(devices) elif servers_list: self.load_from_servers_list(servers_list) #elif pattern: self.load_from_servers_list(self.db.get_server_list(pattern)) elif hosts: hosts = type(hosts) is str and (',' in hosts and hosts.split(',') or [hosts]) or hosts for h in hosts: self.load_by_host(h) elif host: self.load_by_host(host) elif pattern: self.load_by_name(pattern)
def __init__(self, name, receiver, sender, options=[], defaults={}): self.name = name self.receiver = receiver self.sender = sender self.defaults = defaults or self.defaults self.tangodb = ft.get_database() self.alarms = panic.api()
def get_hdbpp_databases(): cms = ft.get_class_devices('HdbConfigurationManager') dbs = {} for c in cms: props = ['LibConfiguration', 'ArchiverList'] props = ft.get_database().get_device_property(c, props) db = dict(t.split('=') for t in props['LibConfiguration'])['dbname'] dbs[db] = {c: None} for a in props['ArchiverList']: dbs[db][a] = ft.get_device_property(a, 'AttributeList') return dbs
def get_hdbpp_databases(archivers=[],dbs={}): """ Method to obtain list of dbs/archivers; it allows to match any archiver list against existing dbs. This method can be used in cached mode executed like: dbs = get_hdbpp_databases() for a in archivers: db = get_hdbpp_databases(a,dbs).keys()[0] """ if not dbs: dbs = {} print('Loading databases from Tango') cms = ft.get_class_devices('HdbConfigurationManager') for c in cms: try: props = ['LibConfiguration','ArchiverList'] props = ft.get_database().get_device_property(c,props) db = dict(t.split('=') for t in props['LibConfiguration'])['dbname'] dbs[db] = {c:None} for c in props['ArchiverList']: dbs[db][c] = ft.get_device_property(c,'AttributeList') except: print('Unable to load %s config' % str(c)) traceback.print_exc() else: dbs = dbs.copy() if archivers: archivers = list(archivers) #Don't use toList here! targets = [] for a in archivers: m = fn.parse_tango_model(a,fqdn=True) targets.extend((m.fullname, m.devicename, m.devicemodel)) print(targets) for db,archs in dbs.items(): narchs = {} for a in archs.keys(): if fn.inCl(a,targets): m = fn.parse_tango_model(a,fqdn=True).fullname narchs[m] = archs[a] if not narchs: dbs.pop(db) else: dbs[db] = narchs return dbs
def main(): global tangodb, alarms, receiver, sender, smtp_host args = sys.argv[1:] if not args or 'help' in str(args): print('Usage: test_mail.py receiver sender smtp_host:port') sys.exit(0) receiver, sender, smtp_host = args[:3] if ':' not in smtp_host: smtp_host += ':25' tangodb = ft.get_database() alarms = panic.api() cleanup() configure() try: run() except: traceback.print_exc() finally: cleanup(delete = False) fn.log.info('MAIL_TEST: DONE')
def load(tango_host, instance, devices, replace={}, overwrite=False, def_class='SimulatorDS'): """ the tango_host variable must match with the current tango_host; it is used to avoid accidental loading load .json files into simulated devices; one .json file per device devices may be a list of devices, a list of files or a dictionary {device:filename} """ assert tango_host == fn.get_tango_host() done = [] if isMapping(devices): filenames = devices else: devices = fn.toList(devices) filenames = {} for dd in devices: if os.path.isfile(dd): df, dd = dd, file2dev(dd) else: df, dd = dev2file(dd), dd filenames[dd] = df for dd, df in filenames.items(): exists = ft.get_matching_devices(dd) if exists and not overwrite: raise Exception('Device %s Already Exists!!!' % dd) data = json.load(open(df)) props = data['properties'] props = dict((str(k), map(str, v)) for k, v in props.items()) for r, rr in replace.items(): dd = clsub(r, rr, dd) for p, pp in props.items(): for i, l in enumerate(pp): props[p][i] = clsub(r, rr, l) if overwrite: props['polled_attr'] = [] props['polled_cmd'] = [] if data['dev_class'] == 'PyAlarm': if not exists: ft.add_new_device('PyAlarm/' + instance, 'PyAlarm', dd) props['AlarmReceivers'] = [] ft.put_device_property(dd, props) else: if not exists: ft.add_new_device(def_class + '/' + instance, def_class, dd) ft.put_device_property(dd, props) #if data['dev_class'] not in ('PySignalSimulator','PyAttributeProcessor','PyStateComposer','CopyCatDS'): vals = dict((str(k), v['value'] if v else 0) for k, v in data['attributes'].items()) dynattrs = [] attrprops = fn.dicts.defaultdict(dict) for k, v in sorted(vals.items()): if k.lower() in ('state', 'status'): continue if v is None: continue t = type(v).__name__ if t == 'unicode': t = 'str' v = str(v) if t != 'str' else "'%s'" % v dynattrs.append( #'%s = %s(%s) #%s'%( '%s = %s(VAR(%s,default=%s,WRITE=True)) #%s' % (k, k, t, v, data['attributes'][k]['data_type'])) attrprops[dd][k] = dict((p, data['attributes'][k].get(p, '')) for p in ('format', 'label', 'unit', 'min_alarm', 'max_alarm')) ft.put_device_property(dd, 'DynamicAttributes', dynattrs) try: ft.get_database().put_device_attribute_property( dd, dict((k, v) for k, v in attrprops[dd].items() if v)) except: fn.time.sleep(3.) done.append(dd) return done
def load(tango_host,instance,devices,replace={},overwrite=False): """ the tango_host variable must match with the current tango_host; it is used to avoid accidental loading load .json files into simulated devices devices may be a list of devices, a list of files or a dictionary {device:filename} """ assert tango_host == fn.get_tango_host() done = [] if isMapping(devices): filenames = devices else: devices = fn.toList(devices) filenames = {} for dd in devices: if os.path.isfile(dd): df,dd = dd,file2dev(dd) else: df,dd = dev2file(dd),dd filenames[dd] = df for dd,df in filenames.items(): exists = ft.get_matching_devices(dd) if exists and not overwrite: raise Exception('Device %s Already Exists!!!'%dd) data = json.load(open(df)) props = data['properties'] props = dict((str(k),map(str,v)) for k,v in props.items()) for r,rr in replace.items(): dd = clsub(r,rr,dd) for p,pp in props.items(): for i,l in enumerate(pp): props[p][i] = clsub(r,rr,l) if overwrite: props['polled_attr'] = [] props['polled_cmd'] = [] if data['dev_class'] == 'PyAlarm': if not exists: ft.add_new_device('PyAlarm/'+instance,'PyAlarm',dd) props['AlarmReceivers'] = [] ft.put_device_property(dd,props) else: if not exists: ft.add_new_device('PyAttributeProcessor/'+instance,'PyAttributeProcessor',dd) ft.put_device_property(dd,props) #if data['dev_class'] not in ('PySignalSimulator','PyAttributeProcessor','PyStateComposer','CopyCatDS'): vals = dict((str(k),v['value'] if v else 0) for k,v in data['attributes'].items()) dynattrs = [] attrprops = fn.dicts.defaultdict(dict) for k,v in sorted(vals.items()): if k.lower() in ('state','status'): continue if v is None: continue t = type(v).__name__ if t == 'unicode': t = 'str' v = str(v) if t!='str' else "'%s'"%v dynattrs.append( #'%s = %s(%s) #%s'%( '%s = %s(VAR(%s,default=%s,WRITE=True)) #%s'%(k, k,t,v,data['attributes'][k]['data_type'])) attrprops[dd][k] = dict((p,data['attributes'][k].get(p,'')) for p in ('format','label','unit','min_alarm','max_alarm')) ft.put_device_property(dd,'DynamicAttributes',dynattrs) try: ft.get_database().put_device_attribute_property(dd, dict((k,v) for k,v in attrprops[dd].items() if v)) except: fn.time.sleep(3.) done.append(dd) return done