source: cows_wps/trunk/cows_wps/utils/common.py @ 7000

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/utils/common.py@7000
Revision 7000, 7.5 KB checked in by astephen, 10 years ago (diff)

various fixes on bbox etc

Line 
1"""
2common.py
3=========
4
5Provides a number of utilities for the WPS.
6
7"""
8# Import standard library modules
9import sys
10import os
11import time
12import socket
13import random
14import md5
15import string
16import datetime
17import urllib2
18import Cookie
19
20from cows_wps.utils import case_sensitive_ordered_config_parser as cp
21
22import logging
23log = logging.getLogger(__name__)
24
25def exitNicely(doc_string="", msg=None):
26    "Write a message and then exit cleanly."
27    print doc_string + "\n" + msg
28    sys.exit()
29
30
31def generateLocalFilePath(proc_base_dir, job_id, file_name):
32    "Returns a system file path to read data from."
33   
34    file_path = os.path.join(proc_base_dir, job_id, "outputs", file_name)
35       
36    return file_path
37
38def parseQueryString(qs):
39    """Parses query string to dict."""
40    arg_dict = {}
41    args = qs.split("?")[-1].strip()
42
43    for item in args.split("&"):
44        if item=="": continue
45        name,value = item.split("=")
46        arg_dict[name] = value
47
48    return arg_dict
49
50
51def parseSimpleInputs(inputs_file):
52    """
53    Parses all inputs from simple file and returns a dictionary of valid types.
54    """
55    inputs = open(inputs_file)
56    lines = inputs.readlines()
57    inputs.close()
58
59    arg_dict = {}
60    for line in lines:
61        items, value = line.strip().split("=")
62        (name, item_or_array, item_type) = items.split(",")
63        if item_or_array == "array":
64            value = [eval("%s('%s')" % (item_type, v)) for v in value.split(",")]
65        elif item_or_array == "item":
66            value = eval("%s('%s')" % (item_type, value))
67        arg_dict[name] = value
68
69    return arg_dict
70
71
72def makeHTML(title, arg_dict={}, message=""):
73    """Returns a simple HTML doc that lists args sent."""
74    html = """<HTML>
75<HEAD><TITLE>%s</TITLE></HEAD>
76<BODY>
77<H2>%s</H2>
78
79<P>Welcome to this test service.</P>
80""" % (title, title)
81
82    if len(message)>0:
83        html = html+"<P>Message: "+message+"</P>\n"
84
85    if len(arg_dict)>0:
86        html = html+"""<P>You sent the arguments:</P>
87<UL>
88"""
89        keys = arg_dict.keys()
90        keys.sort()
91        for key in keys:
92            html=html+"""  <LI>%s = %s</LI>\n""" % (key, arg_dict[key])
93        html = html+"</UL>\n"
94    html=html+"""
95</BODY>
96</HTML>
97"""
98    return html
99
100
101def listIfNot(item, rttype = "string"):
102    "Returns non-list/tuple as only item in list."
103    if type(item) not in (type((1,2)), type([1,2])):
104        item = [item]
105
106    if rttype == "string":
107        item = [str(i) for i in item]
108    elif rttype == "float":
109        item = [float(i) for i in item] 
110
111    return item
112
113
114# Make dict for image formats
115imageFormats = {"image/png": "png",
116                "image/jpeg": "jpg",
117                "application/pdf": "pdf",
118                "application/postscript": "ps"}
119
120reverseImageFormats = {}
121for (k, v) in imageFormats.items():
122    reverseImageFormats[ v ] = k
123
124
125def fixTrueFalse(value):
126    "Make true or false value a lower title cased version."
127    if value.lower() in ("true", "false"):
128        value = eval(value.lower().title())
129    return value
130
131
132def getUniqueId(*args):
133    """
134    Generates a universally unique ID.
135    Any arguments only create more randomness.
136    """
137    t = long( time.time() * 1000 )
138    r = long( random.random()*100000000000000000L )
139    try:
140        a = socket.gethostbyname( socket.gethostname() )
141    except:
142        # if we can't get a network address, just imagine one
143        a = random.random()*100000000000000000L
144    data = str(t) + ' ' + str(r) + ' ' + str(a) + ' ' + str(args)
145    data = md5.md5(data).hexdigest()
146    return data
147
148
149def underscoreLowerToTitleCamel(s):
150    """
151    Returns a title camel case string version of an
152    underscore-delimited lower case string.
153    """
154    l = []
155    next_upper = True
156    for i in s:
157        if i == "_":
158            next_upper = True
159        else:
160            if next_upper == True:
161                i = i.upper()
162                next_upper = False
163            l.append(i)
164               
165    return "".join(l)
166
167
168def titleCamelToUnderscoreLower(s, spacer="_"):
169    """
170    Returns a lower case underscore-delimited version of
171    title camel case string.
172    """
173    l = []
174    exceptional_mappings = {"1":"1"}
175    if s in exceptional_mappings.keys():
176        return exceptional_mappings[s]
177
178    is_first = True
179    last = ""
180    for i in s:
181        if i in string.uppercase or i in "0123456789":
182            i = i.lower()
183            if is_first == False:
184                i = spacer + i
185
186        is_first = False
187        l.append(last)
188        last = i
189
190    l.append(last)
191
192    return "".join(l)
193
194def readLinesFromFile(path):
195    fout = open(path, 'r')
196    lines = fout.readlines()
197    fout.close()
198    return lines
199   
200
201def stringToDateTime(timeString, format="%Y-%m-%d %H:%M:%S", includeMicroseconds=False):
202   
203    if includeMicroseconds:
204        p1, p2 = str(timeString).split(".",1)
205        d = datetime.datetime.strptime(p1, format)
206        d = d.replace(microsecond = int(p2))
207    else:
208        d = datetime.datetime.strptime(timeString, format)
209       
210    return d
211
212
213def convertToBooleanIfString(value):
214    """
215    Accepts an argument, if that argument is not already a boolean it will
216    attempt to convert it from a string to a boolean and return it, otherwise
217    the original argument is returned.
218    """
219   
220    if type(value) != bool:
221        return stringToBool(value)
222    else:
223        return value
224
225def stringToBool(boolAsString):
226    """
227    Attempts to convert a string to a boolean, comparis the string with 'true' and 'false' (ignores case) and
228    returns the appropriate boolean value.
229   
230    If the string matches neither 'true' nor 'false' an exception is raised.
231    """
232   
233    if boolAsString.lower() == 'true':
234        return True
235    elif boolAsString.lower() == 'false':
236        return False
237    else:
238        raise Exception("Can't convert the string %s to a boolean" % (boolAsString,))
239   
240
241def buildProcessCallable(processModuleString):
242    package, klassName = processModuleString.split('#')
243   
244    module = __import__(package, fromlist=[''])
245    klass = getattr(module, klassName)   
246    processCallable = klass()
247   
248    return processCallable
249
250
251def downloadFromURL(url, cookies = None):
252    """
253    Downloads response from URL. Use cookies if ``cookies`` is defined.
254    """
255    req = urllib2.Request(url = url)
256
257    # Now go through each cookie and add it to the request object before sending
258    if cookies != None:
259        for cookie in cookies.items():
260            new_cookie = Cookie.SimpleCookie()
261            (k, v) = cookie
262            new_cookie[k] = v
263            new_cookie[k]["path"] = "/"
264            req.add_header('Cookie', new_cookie.output())
265
266    getter = urllib2.urlopen(req)
267    resp = getter.read()
268    getter.close()
269    return resp
270
271def mapDownloadURLToFilePath(url):
272    items = url.split("/")
273    (job_id, file_name) = items[-2:]
274    req = requestManager.getRequest(jobId, sgeUpdate=False)
275    processDir = req.job.process_dir
276#    identifier = req.job.process
277
278
279
280if __name__ == "__main__":
281
282    print generateDownloadURL("/disks/westerly1/test_dirs/proc_outputs/help/outputs/ukcp09_wxgen_help_007.nc")
283    for i in "abcde": print getUniqueId(i) 
284
285    print "Testing back and forth mappings..."
286    tof = titleCamelToUnderscoreLower
287    fromf = underscoreLowerToTitleCamel
288    for s in ("SamplingDict1", "WGenRuns", "MeaningPeriod", "HotPotato23Highway"):
289        n = tof(s)
290        new_s = fromf(n)
291        print "s:", s, "n:", n, "new_s:", new_s, "same:", (s == new_s)
292   
293    print "Testing use of space from Camel Case version."
294    print tof("SamplingDictNumber2", spacer=" ")
Note: See TracBrowser for help on using the repository browser.