summaryrefslogtreecommitdiff
path: root/validation/validlib/anellib.py
diff options
context:
space:
mode:
authorJean-Philippe NOEL2011-12-09 14:11:03 +0100
committerJean-Philippe NOEL2011-12-09 14:11:03 +0100
commit1edb1c4c2a040be3ec4ad485dbe4dac91460d40c (patch)
tree6ca5b3eed81cd0941c4d6815e667ee0ea690f340 /validation/validlib/anellib.py
parent6099e70ac524047fd3fba1d5da4abd5e12a7275f (diff)
validation/validlib: library for split and merge tests, refs #2561
The library allows to command the following devices: * power strip * group of io's * attenuator * SPC 300
Diffstat (limited to 'validation/validlib/anellib.py')
-rw-r--r--validation/validlib/anellib.py103
1 files changed, 103 insertions, 0 deletions
diff --git a/validation/validlib/anellib.py b/validation/validlib/anellib.py
new file mode 100644
index 0000000000..731d559c53
--- /dev/null
+++ b/validation/validlib/anellib.py
@@ -0,0 +1,103 @@
+# -*- coding: utf-8 -*-
+
+"""Module allowing to command and control the Anel io's/power plugs.
+
+The parameters are the following:
+- "target": io or power plug - used for the messages printed to the user
+- "switch_path": HTML page allowing to switch on/off the target
+- "status_path": HTML page allowing to retrieve the status on/off of the
+ target
+- "body": body of the switching request
+- "letter": letter identifying the target in the statuses or switching requests
+- "config":configuration of the web server (IP address, login and password)
+- "size": number of the io's/power plugs
+
+What is called "key" is a couple identifying the power plug (resp. io):
+- the first element is the index of the power strip (resp. group of io's),
+- the second element is the index of the power plug (resp. io) on the power
+ strip (resp. group of io's)
+
+"""
+
+import base64
+import re
+import httplib
+
+def switch(command, keys, parameters):
+ """Switch on/off the io's/power plugs identified by the keys"""
+ (index, subindexes) = keys
+ if type(subindexes) != list:
+ subindexes = [subindexes]
+ assert (command == "off" or command == "on")
+ print "Switch", command, "the", parameters["target"], "(s)", keys
+ page = statuses_page(index, parameters)
+ new_subindexes = []
+ for subindex in subindexes:
+ if command != status_aux(page, subindex, parameters):
+ new_subindexes.append(subindex)
+ send_switching_request(index, new_subindexes, parameters)
+
+def send_request(index, path, body, parameters):
+ """Send an HTTP request to the web server managing the io's/power plugs"""
+ if body == "": method = "GET"
+ else: method = "POST"
+ (address, login, password) = parameters["config"][index]
+ token = base64.standard_b64encode(login + ":" + password)
+ headers = {"Authorization": "Basic " + token}
+ connection = httplib.HTTPConnection(address)
+ connection.request(method, path, body, headers)
+ response = connection.getresponse()
+ connection.close()
+ return response.read()
+
+def send_switching_request(index, subindexes, parameters):
+ """Send a switching request (on/off) to the web server managing the
+ io's/power plugs"""
+ body = parameters["body"]
+ assert body != ""
+ path = parameters["switch_path"]
+ letter = parameters["letter"]
+ for subindex in subindexes:
+ body = body + "&" + letter + str(subindex - 1) + "=S"
+ send_request(index, path, body, parameters)
+
+def status_aux(page, subindex, parameters):
+ """Return the status of the io's/power plugs contained in the page"""
+ letter = parameters["letter"]
+ green = "#00DD00"
+ criteria = "<td bgcolor=" + green + ">" + \
+ "<input class=bt type=submit value=[IO] name=" + \
+ letter + str(subindex - 1) + ">" + \
+ "</td>"
+ if re.search(criteria, page) is None: status = "off"
+ else: status = "on"
+ return status
+
+def statuses_page(index, parameters):
+ """Return the page containing the status of the io/power plug identified
+ by the index"""
+ return send_request(index, parameters["status_path"], "", parameters)
+
+def status(key, parameters):
+ """Return the status of the io/power plug identified by key"""
+ (index, subindex) = key
+ page = statuses_page(index, parameters)
+ return status_aux(page, subindex, parameters)
+
+def indexes(parameters):
+ """Return a list containing the index of each power strip or group of
+ io's"""
+ return parameters["config"].keys()
+
+def subindexes(parameters):
+ """Return a list containing the index of each power strip or group of
+ io's"""
+ return range(1, parameters["size"] + 1)
+
+def switch_off_all(parameters):
+ """Switch off all io's/power plugs"""
+ target = parameters["target"]
+ print "Switch off each", target, "...{"
+ for index in indexes(parameters):
+ switch("off", (index, subindexes(parameters)), parameters)
+ print "}"