1 | """ |
---|
2 | proc_config_convertor.py |
---|
3 | ======================== |
---|
4 | |
---|
5 | Converts what is in the process configuration file into a set of |
---|
6 | form elements. |
---|
7 | |
---|
8 | """ |
---|
9 | |
---|
10 | import logging |
---|
11 | |
---|
12 | from cows_wps.utils.create_process_config import createProcessConfig |
---|
13 | |
---|
14 | |
---|
15 | log = logging.getLogger(__name__) |
---|
16 | |
---|
17 | class ProcConfigConvertor(object): |
---|
18 | """ |
---|
19 | Class to convert process configuration into form element dict. |
---|
20 | """ |
---|
21 | |
---|
22 | def convertConfig(self, proc_id): |
---|
23 | proc_config = createProcessConfig(proc_id)["Capabilities"] |
---|
24 | can_cache = proc_config["wps_interface"]["caching_enabled"] |
---|
25 | globals = self.convertGlobals(proc_config["globals"]) |
---|
26 | globals.append( ("Is process cachable?", str(can_cache)) ) |
---|
27 | inputs = self.convertInputs(proc_config["DataInputs"], proc_config["OrderedDataInputs"]) |
---|
28 | outputs = self.convertOutputs(proc_config["ProcessOutputs"]) |
---|
29 | |
---|
30 | resp = {"Process Information": globals, |
---|
31 | "Inputs": inputs, |
---|
32 | "Outputs": outputs} |
---|
33 | |
---|
34 | return resp |
---|
35 | |
---|
36 | def convertGlobals(self, d): |
---|
37 | "Returns an ordered list." |
---|
38 | ordered_ids = ["Title", "ProcessDescription", "Abstract", "ProcessVersion"] |
---|
39 | ordered_long_names = ["Process Name", "Short Description", "Full Description", "Version number"] |
---|
40 | return self._genericMapper(d, ordered_ids, ordered_long_names) |
---|
41 | |
---|
42 | def convertInputs(self, d, ordered_ids): |
---|
43 | "Return an ordered list of [(id, long_name, value_dict)]." |
---|
44 | ordered_long_names = ordered_ids |
---|
45 | return self._genericMapper(d, ordered_ids, ordered_long_names) |
---|
46 | |
---|
47 | def convertOutputs(self, d): |
---|
48 | "Return an ordered list." |
---|
49 | ordered_ids = ["NOT DONE YET"] |
---|
50 | ordered_long_names = ["NOTE DONE YET"] |
---|
51 | d["NOT DONE YET"] = "NOT DONE" |
---|
52 | return self._genericMapper(d, ordered_ids, ordered_long_names) |
---|
53 | |
---|
54 | def _genericMapper(self, dct, ordered_ids, ordered_long_names): |
---|
55 | """ |
---|
56 | Returns an ordered list of (long_name, dct[ordered_id]) pairs |
---|
57 | built from dct values and ids. All values are converted to strings. |
---|
58 | Length of ordered_ids and ordered_long_names must be the same. |
---|
59 | """ |
---|
60 | if len(ordered_ids) != len(ordered_long_names): |
---|
61 | log.exception("ordered_ids and ordered_long_names must be same length in call to _genericMapper().") |
---|
62 | |
---|
63 | rt_list = [] |
---|
64 | |
---|
65 | for (i, id) in enumerate(ordered_ids): |
---|
66 | |
---|
67 | value = dct[id] |
---|
68 | if type(value) != type({1:2}): value = str(value) |
---|
69 | key = ordered_long_names[i] |
---|
70 | rt_list.append( (key, value) ) |
---|
71 | |
---|
72 | return rt_list |
---|