source: cows_wps/trunk/cows_wps/tests/xml_response/xml_response_utils.py @ 7512

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/tests/xml_response/xml_response_utils.py@7512
Revision 7512, 7.1 KB checked in by astephen, 10 years ago (diff)

Fixed a typo

Line 
1import re
2from lxml import etree
3
4from cows_wps.utils.parse_capabilities_config import caps_config_dict
5
6from cows_wps.tests.xml_response.response_objects.process_description import ProcessDescription
7from cows_wps.tests.xml_response.response_objects.execute_response import ExecuteResponse
8from cows_wps.tests.xml_response.response_objects.job_details import JobDetails
9from cows_wps.tests.xml_response.response_objects.data_input_list import DataInputList, buildInputListFromItems
10from cows_wps.tests.xml_response.response_objects.file_set import FileSet
11from cows_wps.tests.xml_response.response_objects.process_specific_context import ProcessSpecificContent
12from cows_wps.tests.xml_response.response_objects.process_status import ProcessStatusResponse, PROCESS_STATUS
13from cows_wps.tests.xml_response.response_objects.file_details import FileDetails , FileTypes
14from cows_wps.tests.xml_response.response_objects.file_set_builder import getResultFileset
15
16
17def formatXML(xmlString):
18    noWhiteSpaceXML = re.sub(">\s+", ">", xmlString.strip())
19    return etree.tostring(etree.fromstring(noWhiteSpaceXML), pretty_print=True)
20
21def printXML(xmlString):
22    print "\n" + "*"*30 + "\n\n"
23    print formatXML(xmlString)
24    print "\n" + "*"*30 + "\n\n"
25   
26def getOutputFileName(requestId, format, special=None):
27    if special == None:
28        s = "ukcp_output_%s.%s" 
29    elif special == 'thumbnail':
30        s = "ukcp_output_tn_%s.%s"
31    else:
32        raise Exception("Unknown special format %s" % (special,))
33
34    return s % (requestId, format)
35   
36def getDownloadURL(requestId, format, special=None):
37   
38    outputFile = getOutputFileName(requestId, format, special)
39   
40    url = s = "http://localhost/dl/%s/%s"  % (requestId, outputFile)
41    return url
42
43def buildExpectedPlotResponse(identifier, inputs, response,
44                              additionalContentItems=None,
45                              inputItemsInPSC=['ImageHeight','ImageWidth','LegendPosition']):
46    """
47    Creates a response element based what the xml is expected to contain.
48   
49    @type  identifier: string
50    @param identifier: the identifier of the process
51
52    @type  inputs: dictionary
53    @param inputs: the inputs dictionary of the request
54
55    @type  response: ExecuteResponse
56    @param response: the actual response object recieved, this is needed
57        because some of the items are copied across instead of generated
58        such as the completion time.
59
60    @type    additionalContentItems: dictionary
61    @keyword additionalContentItems: any additional content items to be put
62        in the process specific content object.
63
64    @type    inputItemsInPSC: list
65    @keyword inputItemsInPSC: a list of the input items that appear in the process
66        specific content.
67   
68    """
69   
70    requestID = inputs['RequestID']
71
72    requestDescription = inputs.get('RequestDescription',
73                caps_config_dict[identifier]['globals']['ProcessDescription'])
74   
75    requestDescription = requestDescription.replace('%20',' ')
76
77
78    metadataFileName = "metadata.xml"
79    plotName = getOutputFileName(requestID, inputs['ImageOutputFormat'])
80    thumbnailName = getOutputFileName(requestID, 'png', special='thumbnail')
81    zipFileName = 'output.zip' 
82    copyrightFile = 'copyright.txt'
83   
84    infoLookup = { metadataFileName:'metadata file',
85                   plotName:'Full-size image made.',
86                   thumbnailName:'Reduced size image made.',
87                   zipFileName:'Zip file 1' ,
88                   copyrightFile:'Copyright information'}
89   
90    outputURLBASE = 'http://localhost/dl/%s/' % requestID
91   
92    resultFileset = getResultFileset(requestID, identifier, outputURLBASE, infoLookup,
93                                     onlyIncludeZipFiles=True)
94       
95    expectedInputs = buildInputListFromItems(inputs.items())
96   
97    expectedDefinition = ProcessDescription(identifier,
98                                     "Responds to a request for %s returning xml, the file URL or the file itself." % 'an image',
99                                     "Provides a file based on some arguments, if cached then gets from cache. Depending on the request can return image only, URL to image or XML about the image.")
100       
101    expectedJobDetails = JobDetails(requestID,
102                                    unixCompletionTime=response.jobDetails.unixCompletionTime,
103                                    completionTimeDate=response.jobDetails.completionTimeDate,
104                                    submissionTime=response.jobDetails.submissionTime,
105                                    duration=response.jobDetails.duration,
106                                    description=requestDescription, 
107                                    capabilities='download',
108                                    type='image sync', 
109                                    volume=resultFileset.getSize(),
110                                    fileSet=resultFileset)
111       
112    status = ProcessStatusResponse(PROCESS_STATUS.SUCCEEDED, "The End")
113   
114   
115    pscContent = {}
116   
117    for inputItem in inputItemsInPSC:
118        if inputs.has_key(inputItem):
119            pscContent[inputItem] = inputs[inputItem]
120        else:
121            pscContent[inputItem] = caps_config_dict[identifier]['DataInputs'][inputItem]['default']
122           
123    # These value keep changing so take the value form response
124    for copyItem in ['XLimits','YLimits','XLimitsInPixels','YLimitsInPixels']:
125        pscContent[copyItem] = response.processSpecificContent.dict[copyItem]
126     
127    if additionalContentItems != None:
128        for k, v in additionalContentItems.items():
129            pscContent[k] = v
130     
131    procSpecificContent = ProcessSpecificContent(pscContent)       
132           
133    statusLoc  = "http://localhost/status/%s" % requestID
134    expectedResponse = ExecuteResponse(identifier,
135                                       version="0.4.0", 
136                                       schemaLocation="http://www.opengeospatial.net/wps ../wpsExecute.xsd", 
137                                       statusLocation=statusLoc, 
138                                       status=status,
139                                       inputs=expectedInputs, 
140                                       definition=expectedDefinition, 
141                                       jobDetails=expectedJobDetails, 
142                                       processSpecificContent=procSpecificContent,
143                                        )
144   
145    return expectedResponse
146
147def getFileSizesFromGetDataResponse(response):
148    responseZipFileSet = response.jobDetails.fileSet.files[0]
149   
150    zipFileSize = responseZipFileSet.size
151   
152    responseFileSets = responseZipFileSet.content
153   
154    metadataFileSet = filter(lambda fs: fs.URL.find('metadata.xml') >1 , responseFileSets)[0]
155    outputFileSet   = filter(lambda fs: fs.URL.find('ukcp_output') >1 , responseFileSets)[0]
156    copyrightFileSet   = filter(lambda fs: fs.URL.find('copyright.txt') >1 , responseFileSets)[0]
157   
158    outputSize = outputFileSet.size
159    metadataFileSize = metadataFileSet.size
160    copyrightFileSize = copyrightFileSet.size
161   
162    return zipFileSize, outputSize, metadataFileSize, copyrightFileSize
163   
Note: See TracBrowser for help on using the repository browser.