source: cows_wps/trunk/cows_wps/utils/parse_outputs_rules.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/utils/parse_outputs_rules.py@7575
Revision 5615, 1.9 KB checked in by spascoe, 10 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

Line 
1"""
2parse_outputs_rules.py
3======================
4
5Holds the parseOutputsRules function used for reading the contents of the
6ini file into a dictionary.
7
8"""
9
10# Standard library imports
11import os
12import ConfigParser
13
14from cows_wps.utils.parse_wps_config import wps_config_dict
15
16import logging
17log = logging.getLogger(__name__)
18
19
20def scaleVolumeWithUnits(vol, units):
21    """
22    Returns the volume when converted to bytes.
23    """
24    scalers = ["b", "kb", "mb", "gb", "tb", "pb"]
25    real_vol = vol * (2 ** (scalers.index(units) * 10))
26    return real_vol
27 
28 
29def parseOutputsRules(output_ini=None):
30    """
31    Parses the outputs config files and returns a dictionary of the rules.
32    """
33    global outputs_rules_config_dict
34   
35    if output_ini == None:
36        try:
37            output_ini = wps_config_dict['outputs_rules']
38        except KeyError:
39            log.warn('No output_rules configuration in wps.ini')
40            base_dir = os.environ.get("WPS_BASE", "..")
41            output_ini  = os.path.join(base_dir, "outputs_rules.ini")
42   
43    c = ConfigParser.ConfigParser()
44    c.read(output_ini)
45
46    d = {}
47    for s in c.sections():
48        d[s] = {}
49        for key in c.options(s):
50            value = c.get(s, key)
51            if value.strip().find(" ") > -1:  value = value.split()
52            try:
53                value = float(value)
54            except:
55                pass
56            d[s][key] = value
57
58    # Fix volumes
59    units = d["volume_rules"]["volume_units"]
60    d["volume_rules"]["max_request_volume"] = \
61            scaleVolumeWithUnits(d["volume_rules"]["max_request_volume"], units)
62    d["volume_rules"]["max_file_volume"] = \
63            scaleVolumeWithUnits(d["volume_rules"]["max_file_volume"], units)
64           
65    outputs_rules_config_dict = d
66
67outputs_rules_config_dict = None
68parseOutputsRules()
69
70if __name__ == "__main__":
71
72    print outputs_rules_config_dict
Note: See TracBrowser for help on using the repository browser.