#!/usr/bin/python

__doc__ = """
usage: 
  tango_monitor <device name> [ <attr regexp> ]*
"""

import sys
import time
import PyTango
import fandango as fn

def monitor(d, args=[], events = []):
  """
  monitor(device,[attributes])
  """
    
  dp = PyTango.DeviceProxy(d)
  cb = PyTango.utils.EventCallBack()

  args = map(str.strip, args) if args else ["state"]
  attrs = [a for a in dp.get_attribute_list()
           if any(fn.clmatch(r,a) for r in args)]
  events = events or [PyTango.EventType.CHANGE_EVENT]
    
  print('%s matched %d attributes' % (d,len(attrs)))

  eis,worked,failed = [],[],[]
  for a in attrs:
    try:
      for e in events:
        eis.append(dp.subscribe_event(a,e,cb))
      worked.append(a)
    except:
      failed.append(a)

  print('%d attributes NOT provide events: %s' % (len(failed),failed))      
  print('%d attributes provide events: %s' % (len(worked),worked))
  print('-'*80 + '\n' + '-'*80)
  try:
      while True:
          time.sleep(1)
  except: #KeyboardInterrupt
      print(fn.excep2str())
      print('-'*80)
      print "Finished monitoring"
      
  [dp.unsubscribe_event(ei) for ei in eis];
    
if __name__ == '__main__':
  import sys
  try:
    monitor(sys.argv[1],sys.argv[2:])
  except:
    print(fn.except2str())
    print(__doc__)
    

