source: TI05-delivery/ows_framework/trunk/ows_wps/ows_wps/process.py @ 2659

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_wps/ows_wps/process.py@2659
Revision 2659, 3.2 KB checked in by spascoe, 15 years ago (diff)

Commiting midway through development to change platform. Various classes
have been added to support process execution.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6A class that encapsulates a WPS process.
7
8"""
9
10from ows_wps.execute import ExecuteStatus
11from ows_common.exceptions import OwsError
12from ows_wps.process_description import ProcessDescription
13
14class Process(object):
15    """
16    Process objects are instantiated with configuration options from the
17    environment and then the execute() method is called to perform the process.
18
19    Subclasses implement specific processes.  They should suply
20    Process.description to describe the process and implement the
21    run() method.
22
23    @cvar description: Describes the process implemented in this class.
24    @type description: ProcessDescription
25    @ivar status: describes the execute request and execution status
26    @type status: ExecuteStatus
27    @ivar context: describes the environment in which the process is executed.
28        This will provide filesystem hooks, etc.
29    """
30
31    description = NotImplemented
32
33    def __init__(self, status, context):
34        """
35        Initialise a process.  The instance communicates through the status
36        object.  Process parameters are extracted from status and the outputs
37        are referenced by the status object.
38
39        """
40        self.status = status
41        self.context = context
42
43        self.status.flag = self.status.ACCEPTED
44
45
46    def execute(self):
47        """
48        Execute the process.  This is called by the framework.  The default
49        implementation passes self.inputs to self.run() as keyword arguments,
50        trapping any ows exceptions.
51
52        """
53        try:
54            outputs = self.run(**self.status.inputs)
55        except OwsError, e:
56            self.exceptionReport = e.report
57            self.status.flag = self.status.FAILED
58        else:
59            # Set the status outputs
60            self.status.setOutputs(**outputs)
61            self.status.flag = self.status.SUCCEEDED
62
63    def run(self, **inputs):
64        """
65        Main process execution function.  Override in subclasses.
66
67        By default this method is passed self.status.inputs as it's keyword
68        arguments.  It should return a dictionary of outputs.
69
70        """
71        raise NotImplementedError
72
73
74#----------------------------------------------------------------------------
75
76from ows_wps.process_description import LiteralDomain
77
78class FloatDomain(LiteralDomain):
79    def load(self, text):
80        return float(text)
81
82class ExampleProcess(Process):
83    """
84    Suply sum, difference and product of 2 floats.
85
86    """
87
88    description = ProcessDescription(
89        identifier='ExampleProcess',
90        titles = ['Example of a very simple process'],
91        abstracts = ['Take two input and return the sum, difference '
92                     'and product'],
93        inputs = {'x': FloatDomain('1.2'),
94                  'y': FloatDomain('6.5')},
95        outputs = {'sum': FloatDomain(), 'diff': FloatDomain(),
96                   'product': FloatDomain()}
97        )
98
99    def run(self, x, y):
100        return dict(sum=x+y, diff=x-y, product=x*y)
101   
Note: See TracBrowser for help on using the repository browser.