coding style: adopt pep8

Change-Id: Ic037f925271c970406284e52a00a6c56d048452e
This commit is contained in:
Enrico Faulhaber
2016-08-31 11:24:41 +02:00
parent 0cf2633717
commit 5339f0c4fe
6 changed files with 42 additions and 24 deletions

View File

@ -1,5 +1,6 @@
import fileinput import fileinput
class LineHandler(): class LineHandler():
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -14,6 +15,7 @@ class LineHandler():
''' '''
self.send_line("> " + line) self.send_line("> " + line)
class LineServer(): class LineServer():
def __init__(self, isfile, lineHandlerClass): def __init__(self, isfile, lineHandlerClass):
@ -26,7 +28,7 @@ class LineServer():
try: try:
if self.isfile: if self.isfile:
line = raw_input("") line = raw_input("")
print "> "+line print "> " + line
else: else:
line = raw_input("> ") line = raw_input("> ")
except EOFError: except EOFError:

View File

@ -33,26 +33,37 @@ else:
class SecopError(Exception): class SecopError(Exception):
def __init__(self, message, text): def __init__(self, message, text):
Exception.__init__(self, message, text) Exception.__init__(self, message, text)
class UnknownDeviceError(SecopError): class UnknownDeviceError(SecopError):
def __init__(self, message, args): def __init__(self, message, args):
SecopError.__init__(self, "NoSuchDevice", args[0]) SecopError.__init__(self, "NoSuchDevice", args[0])
class UnknownParamError(SecopError): class UnknownParamError(SecopError):
def __init__(self, message, args): def __init__(self, message, args):
SecopError.__init__(self, "NoSuchParam", "%s:%s" % args) SecopError.__init__(self, "NoSuchParam", "%s:%s" % args)
class UnknownPropError(SecopError): class UnknownPropError(SecopError):
def __init__(self, message, args): def __init__(self, message, args):
SecopError.__init__(self, "NoSuchProperty", "%s:%s:%s" % args) SecopError.__init__(self, "NoSuchProperty", "%s:%s:%s" % args)
class SyntaxError(SecopError): class SyntaxError(SecopError):
def __init__(self, message): def __init__(self, message):
SecopError.__init__(self, "SyntaxError", message) SecopError.__init__(self, "SyntaxError", message)
class OtherError(SecopError): class OtherError(SecopError):
def __init__(self, message): def __init__(self, message):
SecopError.__init__(self, "OtherError", message) SecopError.__init__(self, "OtherError", message)
@ -64,12 +75,14 @@ def encode(input):
return enc return enc
return inp return inp
def gettype(obj): def gettype(obj):
typ = str(type(obj).__name__) typ = str(type(obj).__name__)
if typ == "unicode": if typ == "unicode":
typ = "string" typ = "string"
return typ return typ
def wildcard(dictionary, pattern): def wildcard(dictionary, pattern):
list = [] list = []
if pattern == "": if pattern == "":
@ -89,11 +102,14 @@ def wildcard(dictionary, pattern):
class SecopClientProps(object): class SecopClientProps(object):
def __init__(self): def __init__(self):
self.reply_items = {".":"t", "writable":1} self.reply_items = {".": "t", "writable": 1}
self.compact_output = {".":0, "writable":1} self.compact_output = {".": 0, "writable": 1}
class SecopLineHandler(lineserver.LineHandler): class SecopLineHandler(lineserver.LineHandler):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
lineserver.LineHandler.__init__(self, *args, **kwargs) lineserver.LineHandler.__init__(self, *args, **kwargs)
self.props = SecopClientProps() self.props = SecopClientProps()
@ -101,7 +117,7 @@ class SecopLineHandler(lineserver.LineHandler):
def handle_line(self, msg): def handle_line(self, msg):
try: try:
if msg[-1:] == "\r": # strip CR at end (for CRLF) if msg[-1:] == "\r": # strip CR at end (for CRLF)
msg=msg[:-1] msg = msg[:-1]
if msg[:1] == "+": if msg[:1] == "+":
self.subscribe(msg[1:].split(":")) self.subscribe(msg[1:].split(":"))
elif msg[:1] == "-": elif msg[:1] == "-":
@ -111,14 +127,13 @@ class SecopLineHandler(lineserver.LineHandler):
else: else:
j = msg.find("=") j = msg.find("=")
if j >= 0: if j >= 0:
self.write(msg[0:j], msg[j+1:]) self.write(msg[0:j], msg[j + 1:])
else: else:
self.read(msg) self.read(msg)
except SecopError as e: except SecopError as e:
self.send_line("~%s~ %s" % (e.args[0], e.args[1])) self.send_line("~%s~ %s" % (e.args[0], e.args[1]))
self.send_line("") self.send_line("")
def get_device(self, d): def get_device(self, d):
if d == "": if d == "":
d = "." d = "."
@ -127,7 +142,7 @@ class SecopLineHandler(lineserver.LineHandler):
try: try:
return secNodeDict[d] return secNodeDict[d]
except KeyError: except KeyError:
raise UnknownDeviceError("",(d)) raise UnknownDeviceError("", (d))
def get_param(self, d, p): def get_param(self, d, p):
if p == "": if p == "":
@ -135,7 +150,7 @@ class SecopLineHandler(lineserver.LineHandler):
try: try:
return self.get_device(d)[p] return self.get_device(d)[p]
except KeyError: except KeyError:
raise UnknownParamError("",(d,p)) raise UnknownParamError("", (d, p))
def get_prop(self, d, p, y): def get_prop(self, d, p, y):
if y == "": if y == "":
@ -144,7 +159,7 @@ class SecopLineHandler(lineserver.LineHandler):
paramDict = self.get_param(d, p) paramDict = self.get_param(d, p)
return (paramDict, paramDict[y]) return (paramDict, paramDict[y])
except KeyError: except KeyError:
raise UnknownPropertyError("",(d,p,y)) raise UnknownPropertyError("", (d, p, y))
def clear_output_path(self): def clear_output_path(self):
# used for compressing only # used for compressing only
@ -185,19 +200,20 @@ class SecopLineHandler(lineserver.LineHandler):
path = pathArg.split(":") path = pathArg.split(":")
while len(path) < 3: while len(path) < 3:
path.append("") path.append("")
d,p,y = path d, p, y = path
parDict = self.get_param(d, p) parDict = self.get_param(d, p)
if (y != "." and y != "") or not parDict.get("writable", 0): if (y != "." and y != "") or not parDict.get("writable", 0):
self.send_line("? %s is not writable" % self.output_path(d,p,y)) self.send_line("? %s is not writable" % self.output_path(d, p, y))
typ = type(parDict["."]) typ = type(parDict["."])
try: try:
val = (typ)(value) val = (typ)(value)
except ValueError: except ValueError:
raise SyntaxError("can not convert '%s' to %s" % (value, gettype(value))) raise SyntaxError("can not convert '%s' to %s" %
(value, gettype(value)))
parDict["."] = val parDict["."] = val
parDict["t"] = datetime.utcnow() parDict["t"] = datetime.utcnow()
self.send_line(self.output_path(d, p, ".") + "=" + encode(parDict["."])) self.send_line(self.output_path(d, p, ".") +
"=" + encode(parDict["."]))
def read(self, pathArg): def read(self, pathArg):
self.clear_output_path() self.clear_output_path()
@ -240,7 +256,8 @@ class SecopLineHandler(lineserver.LineHandler):
if path[2] == "": if path[2] == "":
msg = self.output_path(d, p, "") + "=" + encode(paramDict["."]) msg = self.output_path(d, p, "") + "=" + encode(paramDict["."])
for y in replyitems: for y in replyitems:
if y == ".": continue # do not show the value twice if y == ".":
continue # do not show the value twice
try: try:
msg += ";" + y + "=" + encode(paramDict[y]) msg += ";" + y + "=" + encode(paramDict[y])
except KeyError: except KeyError:
@ -296,7 +313,8 @@ else:
server = lineserver.LineServer("localhost", port, SecopLineHandler) server = lineserver.LineServer("localhost", port, SecopLineHandler)
secNodeDict=json.load(open("secnode.json", "r"), object_pairs_hook=OrderedDict) secNodeDict = json.load(open("secnode.json", "r"),
object_pairs_hook=OrderedDict)
#json.dump(secNodeDict, open("secnode_out.json", "w"), indent=2, separators=(",",":")) #json.dump(secNodeDict, open("secnode_out.json", "w"), indent=2, separators=(",",":"))
for d in secNodeDict: for d in secNodeDict:
devDict = secNodeDict[d] devDict = secNodeDict[d]

View File

@ -1,6 +1,7 @@
import asyncore import asyncore
import socket import socket
class LineHandler(asyncore.dispatcher_with_send): class LineHandler(asyncore.dispatcher_with_send):
def __init__(self, sock): def __init__(self, sock):
@ -34,6 +35,7 @@ class LineHandler(asyncore.dispatcher_with_send):
''' '''
self.send_line("> " + line) self.send_line("> " + line)
class LineServer(asyncore.dispatcher): class LineServer(asyncore.dispatcher):
def __init__(self, host, port, lineHandlerClass): def __init__(self, host, port, lineHandlerClass):

View File

@ -29,7 +29,7 @@ from devices.core import Readable, Driveable, PARAM
try: try:
from epics import PV from epics import PV
except ImportError: except ImportError:
PV=None PV = None
class EPICS_PV(Driveable): class EPICS_PV(Driveable):

View File

@ -72,5 +72,3 @@ class Temp(Driveable):
def write_target(self, target): def write_target(self, target):
pass pass

View File

@ -212,5 +212,3 @@ class HelpRequest(Request):
class HelpReply(Reply): class HelpReply(Reply):
pass pass