forked from HappyFaceMonitoring/HappyFaceModules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot.py
87 lines (77 loc) · 3.11 KB
/
Plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
#
# Copyright 2012 Institut für Experimentelle Kernphysik - Karlsruher Institut für Technologie
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hf, time
from sqlalchemy import TEXT, Column
try:
import imghdr
except ImportError:
hf.module.ModuleBase.logger.warning("imghdr module not found, Plot module will not be able \
to check if downloaded file is actually an image.")
imghdr = None
class Plot(hf.module.ModuleBase):
config_keys = {
'plot_url': ('URL of the image to display', ''),
'use_start_end_time': ('Enable the mechanism to include two timestamps in the GET part of the URL', 'False'),
'starttime_parameter_name': ('Name of the GET argument for the starting timestamp', 'starttime'),
'endtime_parameter_name': ('Name of the GET argument for the end timestamp, which is now', 'endtime'),
'timerange_seconds': ('How far in the past is the start timestamp (in seconds)', '259200'),
}
config_hint = ''
table_columns = [
Column("plot_file", TEXT)
], ['plot_file']
def prepareAcquisition(self):
url = self.config["plot_url"]
use_start_end_time = False
try:
use_start_end_time = self.config["use_start_end_time"] == "True"
except hf.ConfigError:
pass
if use_start_end_time:
try:
url += "&"+self.config["starttime_parameter_name"]+"="+str(int(time.time())-int(self.config["timerange_seconds"]))
except KeyError:
pass
try:
url += "&"+self.config["endtime_parameter_name"]+"="+str(int(time.time()))
except KeyError:
pass
self.plot = hf.downloadService.addDownload(url)
self.source_url = self.plot.getSourceUrl()
def extractData(self):
data = {
}
if self.plot.isDownloaded():
if imghdr:
extension = imghdr.what(self.plot.getTmpPath())
else:
extension = 'png'
if extension is not None:
self.plot.copyToArchive(self, "." + extension)
data["plot_file"] = self.plot
else:
data.update({
"plot_file": None,
"status": -1.0,
"error_string": "Downloaded file was not an image, probably source server failed to deliver file.",
})
else:
data.update({
"plot_file": None,
"status": -1.0,
"error_string": "Plot was not downloaded :"+self.plot.error,
})
return data