92 lines
2.7 KiB
Python
Executable File
92 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#*-----------------------------------------------------------------------*
|
|
#| |
|
|
#| Copyright (c) 2016 by Paul Scherrer Institute (http://www.psi.ch) |
|
|
#| |
|
|
#| Author Thierry Zamofing (thierry.zamofing@psi.ch) |
|
|
#*-----------------------------------------------------------------------*
|
|
'''
|
|
utilities classes
|
|
'''
|
|
import json
|
|
import numpy as np
|
|
import time,os
|
|
|
|
|
|
class dotdict(dict):
|
|
"""dot.notation access to dictionary attributes"""
|
|
def __init__(self,arg=None,**kwargs):
|
|
if arg!=None:
|
|
self.__fill__(arg)
|
|
self.__fill__(kwargs)
|
|
|
|
def __fill__(self,kw):
|
|
for k,v in kw.iteritems():
|
|
if type(v)==dict:
|
|
self[k]=dotdict(v)
|
|
else:
|
|
self[k]=v
|
|
if type(v)==list:
|
|
for i,w in enumerate(v):
|
|
if type(w)==dict:
|
|
v[i]=dotdict(w)
|
|
pass
|
|
|
|
def __dir__(self):
|
|
l=dir(object)
|
|
#l.extend(self.keys())
|
|
l.extend(map(str,self.keys()))
|
|
return l
|
|
|
|
def __getattr__(self, attr):
|
|
#return self.get(attr)
|
|
try:
|
|
return self[attr]
|
|
except KeyError as e:
|
|
raise AttributeError("%r instance has no attribute %r" % (self.__class__, attr))
|
|
|
|
def __repr__(self):
|
|
return '<' + dict.__repr__(self)[1:-1] + '>'
|
|
|
|
def PrettyPrint(self,indent=0):
|
|
for k,v in self.iteritems():
|
|
if type(v)==dotdict:
|
|
print ' '*indent,str(k)+':'
|
|
v.PrettyPrint(indent+2)
|
|
else:
|
|
print ' '*indent+str(k)+'\t'+str(v)
|
|
|
|
__setattr__= dict.__setitem__
|
|
__delattr__= dict.__delitem__
|
|
#__getattr__= dict.__getattr__
|
|
|
|
|
|
def ConvUtf8(s):
|
|
'convert unicoded json object to ASCII encoded'
|
|
#http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
|
|
if isinstance(s, dict):
|
|
return {ConvUtf8(key): ConvUtf8(value) for key, value in s.iteritems()}
|
|
elif isinstance(s, list):
|
|
return [ConvUtf8(element) for element in s]
|
|
elif isinstance(s, unicode):
|
|
return s.encode('utf-8')
|
|
else:
|
|
return s
|
|
|
|
class GpasciiCommunicator():
|
|
'''Communicates with the Delta Tau gpascii programm
|
|
'''
|
|
gpascii_ack="\x06\r\n"
|
|
gpascii_inp='Input\r\n'
|
|
|
|
def connect(self, host, username='root', password='deltatau',prompt='ppmac# '):
|
|
p=telnetlib.Telnet(host)
|
|
print p.read_until('login: ')
|
|
p.write(username+'\n')
|
|
print p.read_until('Password: ')
|
|
p.write(password+'\n')
|
|
print p.read_until(prompt) # command prompt
|
|
p.write('gpascii -2\n') # execute gpascii command
|
|
print p.read_until(self.gpascii_inp)
|
|
return p
|