source: cows_wps/trunk/cows_wps/controllers/jobviewer.py @ 6898

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/jobviewer.py@6898
Revision 6898, 4.6 KB checked in by astephen, 10 years ago (diff)

many fixes

Line 
1import os
2import re
3import glob
4import logging
5import urllib
6import xml.etree.ElementTree as ET
7
8from pylons import request, response, session, tmpl_context as c
9from pylons.controllers.util import abort, redirect_to
10
11
12from cows.exceptions import *
13from cows.pylons.ows_controller import render_ows_exception
14
15from cows_wps.controllers import *
16from cows_wps import utils
17from cows_wps.renderer.ui_renderer import *
18from cows_wps.model.managers import requestManager
19from cows_wps.process_handler.context.process_context import ProcessContext
20from cows_wps.renderer import xml_renderer
21
22from cows_wps.utils.parse_wps_config import wps_config_dict
23from cows_wps.utils.parse_capabilities_config import caps_config_dict
24
25
26from cows_wps.model.managers import requestManager
27
28log = logging.getLogger(__name__)
29
30
31class JobviewerController(BaseController):
32
33
34    def index(self):
35        status_url = str(request.params.getone("status_url"))
36#        job_id = str(request.params.getone("job_id"))
37        getter = urllib.urlopen(status_url)
38        job_xml = getter.read()
39        getter.close()
40
41        job_id = re.search("<JobID>(.*)</JobID>", job_xml).groups()[0]
42
43        job_as_table = self._xmlToTable(job_xml)
44        job_xml_encoded = self._htmlifyXML(job_xml)
45        job_as_plots = "There are no plots available in the outputs of this job."
46
47        resp = """<div id="view_options" style="background: #EEF4FF; border: 2px outset blue; padding: 5px;">
48                <a href="javascript:switchViewTo('table_view');">View as table</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
49                <a href="javascript:switchViewTo('xml_view');">View as XML</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
50                <a href="javascript:switchViewTo('plots_view');">View Plots (if available)</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
51</div>
52<div id="view_container" style="background: white; border: 2px outset blue; padding: 5px;">
53        <div id="view_contents">
54                <div id="table_view" style="visibility: visible; height: auto;">%s</div>
55                <div id="xml_view" style="visibility: hidden; height: 0px;">%s</div>
56                <div id="plots_view" style="visibility: hidden; height: 0px;">%s</div>
57</div>""" % (job_as_table, job_xml_encoded, job_as_plots)
58
59        renderer = UIPageRenderer()
60        resp = renderer.render("WPS Job Information",
61                              [("Job: %s" % job_id, resp)])
62        return resp
63
64
65    def _xmlToTable(self, xml):
66        "Chops up xml to get table of outputs."
67        node = ET.fromstring(xml)
68
69        proc_id = node.find("{http://www.opengeospatial.net/ows}Identifier").text
70        job_details = node.find("{http://www.opengeospatial.net/wps}ProcessOutputs").find("{http://www.opengeospatial.net/wps}Output").find("{http://www.opengeospatial.net/wps}ComplexValue").find("{http://www.opengeospatial.net/wps}WPSResponseDetails").find("{http://www.opengeospatial.net/wps}JobDetails")
71
72        file_set_list = job_details.find("{http://www.opengeospatial.net/wps}FileSet").getchildren()
73
74        items = ["JobID", "JobCompletionTimeDate", "JobDuration", "JobVolume", "RequestDescription",
75                 "RequestType"]
76        mapped_names = ["Job ID", "Completion Time", "Job Duration", "Output Size (Bytes)", 
77                        "Request Description", "Request Type"]
78
79        resp = "<h2>DETAILS</h2>"
80
81        for (i, item) in enumerate(items):
82            x = job_details.find("{http://www.opengeospatial.net/wps}" + item).text
83            resp += "<b>%s</b> = %s<br>" % (mapped_names[i], x.strip())
84
85        file_items = ["FileSize"]
86        mapped_file_items = ["File Size (Bytes)"]
87
88        resp += "<h2>OUTPUTS</h2>The following file outputs are available from your job.<br>"
89        for fnode in file_set_list:
90
91            furl = fnode.find("{http://www.opengeospatial.net/wps}FileURL").text
92            fname = furl.split("/")[-1]
93            len_fname = len(fname)
94
95            p = 70
96            padding = p - len_fname
97            if padding < 0: padding = 0
98
99#            fname = fname[:p] + (padding * "&nbsp")
100            resp += ('<br><kbd>%s [<a href="%s">Download</a>]</kbd>' + (padding * "&nbsp;")) % (fname, furl)
101#            resp += '<br><span style="width: 500px;">%s [<a href="%s">Download</a>]</span>' % (fname, furl)
102
103            for (i, file_item) in enumerate(file_items):
104                x = fnode.find("{http://www.opengeospatial.net/wps}" + file_item).text
105                resp += '%s Bytes' % x
106
107        return resp
108
109
110    def _htmlifyXML(self, xml):
111        "Returns html string that will make XML look ok on HTML page."
112        xml = xml.replace("<", "&lt;").replace(">", "&gt;").replace("\n", "<br>")
113        return xml
114
115
Note: See TracBrowser for help on using the repository browser.