Example #1
0
        if ASF and not *; db('*'+filter+'*')
        """

        db = Database()
        all_devs = get_all_devices(db)
        devices = []
        for f in filters:
            if '*' in f and not isregexp(f): 
                f=f.replace('*','.*')
            if isregexp(f):
                devices+=[d.lower() for d in all_devs if re.match(f.lower(),d.lower()) and ('dserver' in f or not d.startswith('dserver'))]
            else:
                devices+=[d.lower() for d in all_devs if f.lower() in d.lower() and ('dserver' in f or not d.startswith('dserver'))]
                
        print 'Creating %s file for TangoProperties configuration for %d devices' % (out_file,len(devices))
        out_csv = CSVArray(out_file)
        out_csv.resize(1,1) #It cleans the content of the file
        out_csv.save() #It is really needed!
        out_csv.resize(len(devices),5)
        [out_csv.set(0,i,['server','class','device','property','value'][i]) for i in range(5)]
        counter = 1
        
        print ('Creating a dict with servers/classes/devices from the database ...')
        servers = ServersDict()
        klasses = {}
        
        print ('Generating output file ...')
        for device in sorted(devices):
            servers.load_by_name(device)
            server = servers.get_device_server(device)
            klass = servers.get_device_class(device,server)
Example #2
0
"""

import sys,os, traceback
from PyTango import *

sys.path.append('..')

import fandango
#import fandango.csv
from fandango.arrays import CSVArray

if len(sys.argv)<1:
    exit()

print 'Loading file ...'
array = CSVArray()
array.load(sys.argv[1],prune_empty_lines=True)
array.setOffset(x=1)
for s in ['server','class','device','property']: array.fill(head=s)

#print array.get(x=0)
print 'Getting device column ...'
answer = raw_input('Do you want to force all device names to be uppercase? (Yes/No)')
if answer.lower().startswith('y'):
    listofdevs = {}
    [listofdevs.__setitem__(s.upper(),i) for s,i in array.get(head='device',distinct=True).iteritems()]
else:
    listofdevs = array.get(head='device',distinct=True)
print '%s devices read from file.' % len(listofdevs)
lkeys = listofdevs.keys()
lkeys.sort()
Example #3
0
def loadCSVfile(filename,dedicated={},deletelist=[],context={}):
  ''' returns attrslist, dedicated, deletelist and a context_info object
  filename is the file from where the Array is loaded
  dedicated is a dictionary with key=attribute and value=archiver 
  deletelist is a list, contains the attributes to be removed from archiving
  context is a dictionary, contains the information about the authority of the configuration
  '''
  print 'Loading CSV/XML file ...',filename
  config = CSVArray()
  config.load(filename,comment='#')
  headers=['Device','Host','Attribute','Type','ArchivingMode','Periode','MinRange','MaxRange']
  print 'Searching headers ...'
  head=config.get(0)
  
  def checkHeaders():
    if not all(h in ''.join(head) for h in headers):
      print 'WRONG FILE HEADERS!'
      exit()
      
  [config.fill(head=h) for h in head]
  config.setOffset(1)
  
  print 'Getting attributes from the file ...'
  #it returns the list of device names and the lines that matches for each
  hosts=config.getAsTree(lastbranch='ArchivingMode')#config.get(head='Device',distinct=True)
  
  ##Parsing the params to create a Context
  #-------------------------------------------------
  defaultparams = {'@LABEL':'User_Code-0X','@AUTHOR':'Who?','@DATE':'When?','@DESCRIPTION':'What?','@REASON':'Why?'}
  transparams = {'@LABEL':'name','@AUTHOR':'author','@DATE':'time','@DESCRIPTION':'description','@REASON':'reason'}
  for p,v in defaultparams.items():
    if p not in hosts.keys() or not hosts[p] or hosts[p].keys()[0]==v:
      raise Exception('PARAMS_ERROR','All these defaultparams are MANDATORY!: %s'%str(defaultparams.keys()))
    defaultparams[p]=hosts.pop(p).keys()[0]
    context[p]=defaultparams[p]
    if p=='@DATE':
      t,time_fmts = None,['%Y-%m-%d','%Y-%m-%d %H:%M','%y-%m-%d','%y-%m-%d %H:%M','%d-%m-%Y','%d-%m-%Y %H:%M','%m-%d-%Y','%m-%d-%Y %H:%M',
        '%Y/%m/%d','%Y/%m/%d %H:%M','%y/%m/%d','%y/%m/%d %H:%M','%d/%m/%Y','%d/%m/%Y %H:%M','%m/%d/%Y','%m/%d/%Y %H:%M  ',]
      for tf in time_fmts:
        try:
          #print 'trying format %s'%str(tf)
          t = time.strftime('%Y-%m-%d',time.strptime(context[p],tf))
          break
        except: pass
      if t is not None: context[transparams[p]]=t
      else: raise Exception('PARAMS_ERROR','@DATE format should be YYYY-MM-DD!: %s'%str(context[p]))

  ##Reading the archiving modes
  #-------------------------------------------------
  attrslist = {}
  archmodes={'PERIODIC':'MODE_P','ABSOLUTE':'MODE_A','RELATIVE':'MODE_R','THRESHOLD':'MODE_T','CALC':'MODE_C','EXTERNAL':'MODE_E'}
  for host,devs in sorted(hosts.items()):
    for dev,attributes in sorted(devs.items()):
      print 'reading device %s:%s'%(dev,str(attributes))
      template=[a for a in attributes if '@COPY' in a]
      if template:
        if ':' in template[0]:
            dev2 = template[0].split(':')[1]
        elif template[0] in attributes:
            dev2 = attributes[template[0]].keys()[0]
        #else: dev2 = ''
        [attributes.__setitem__(k,v) for k,v in devs[dev2].items() if k and k not in attributes]
        [attributes.pop(t) for t in template]
      
      #If a @DELETE or @STOP is found as single attribute all dev. attributes are stopped
      elif '@DELETE' in attributes.keys() or '@STOP' in attributes.keys():
        deletelist.append(dev)
      
      defaults = [(a,v) for a,v in attributes.items() if '@DEFAULT' in a and v]
      DEFAULT_MODE = defaults and {} or {'MODE_P':[300000]}
      for a,tipus in defaults:
        mode,params = tipus.values()[0].items()[0]
        mode =  archmodes.get(mode.upper(),mode)
        DEFAULT_MODE[mode] = params
        print dev,'.DEFAULT_MODE=',DEFAULT_MODE
        attributes.pop(a)
      
      for attribute,modes in sorted(attributes.items()):
        config = dict(DEFAULT_MODE.items())
        if not modes:
          print '\treading attribute %s: using default modes %s'%(attribute,str(DEFAULT_MODE))
        else:
          print '\treading attribute %s:%s'%(attribute,str(modes))
          tipus=modes.keys()[0]
          #And modes is overriden by its own member
          modes=modes.values()[0]
          firstmode,firstparam=modes.keys()[0],modes.values()[0][0]
          if any(a.startswith('@') for a in [attribute,firstmode,tipus]):
            if attribute=='@DEDICATED':
              dedicated[dev]=tipus
              #print dev,'.DEDICATED=',tipus
            elif any(c in ['@STOP','@DELETE'] for c in [firstmode,tipus]):
              deletelist.append(dev+'/'+attribute)
              #print dev,'.STOP ARCHIVING'
          else:
            #Adding always a default value to the list of modes.
            if 'MODE_P' not in config: config.update(DEFAULT_MODE.items())
            for mode,params in modes.items():
              if not mode: continue
              print '\treading mode %s:%s'%(mode,str(params))
              mode=mode.upper()
              if mode in archmodes.keys():
                mode=archmodes[mode]
              elif mode not in archmodes.values():
                print 'Unknown mode!: ',mode
                continue
              params = [float(p) for p in params if p]
              config[mode]=params
            attrslist[dev+'/'+attribute]={'host':host,'type':tipus,'modes':config}
            #print 'attrslist[%s/%s]=%s'%(dev,attribute,attrslist[dev+'/'+attribute])

  print 'Specified %d attributes from %d hosts'%(len(attrslist),len(hosts))
  if dedicated: print '%d devices are dedicated'%(len(dedicated))
  if deletelist: print '%d attributes to delete'%(len(deletelist))
  
  return attrslist