source: TI03-DataExtractor/trunk/pydxc/common.py @ 1109

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/trunk/pydxc/common.py@1109
Revision 1109, 5.4 KB checked in by astephen, 13 years ago (diff)

Stable-ish version with fully-ish working dxc client.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6common.py
7=========
8
9Holds common functions and classes used in the Data Extractor software.
10
11"""
12
13# Import python modules
14import os, re
15
16# Import package modules
17from clientConfig import *
18
19# Set up commmon variables. These are CAPITALISED to
20# improve visibility in other modules.
21HTTP_HEADER="Content-Type: text/html\n\n"
22
23STAGES=["DatasetGroupPage", "DatasetPage", "VariablesPage", "DomainPage", "ResultsPage"]
24
25TIME_KEYS=["Year", "Month", "Day", "Hour", "Minute", "Second"]
26
27HORIZ_KEYS=["northernExtent", "easternExtent", "southernExtent", "westernExtent"]
28
29dateTimePattern=re.compile(r"^(\d{4}).(\d{1,2}).(\d{1,2})(\s+|T)(\d+):(\d+):(\d+\.?.*)$")
30
31# Set up common functions
32
33def DEPRECATEDcreateTimeKeyList():
34    """
35    Returns all the separate date and time component arguments required as a list.
36    """
37    allTimeKeys=[]
38    for ts in ["start", "end"]:
39        for i in range(1, MAX_NUM_DATASETS+1):
40            for key in TIME_KEYS:
41                allTimeKeys.append("%s%s_%s" % (ts, key, i))
42    return allTimeKeys
43 
44
45def getDateTimeComponents(dateTimeString):
46    """
47    Takes in a time string in standard DateTime format and returns the items in it.
48    """
49    match=dateTimePattern.match(dateTimeString)
50    if not match:
51        raise "Cannot match date time string: %s" % dateTimeString
52
53    items=match.groups()
54    (year, month, day, hour, minute)=[int(i) for i in items[:3]+items[4:6]]
55    second=float(items[6])
56    return (year, month, day, hour, minute, second)
57
58 
59def createSummaryDict(summaryString):
60    """
61    Takes in a summary string containing details of a user request and
62    returns a more user-friendly dictionary version held as:
63   
64    dict["datasetGroups"], dict["datasets"] etc
65   
66    with all global selections as dict[selectionName] etc
67    """
68    d={"globals":{}, "axes":{}}
69    for i in "datasetGroup dataset variable datasetURI outputFormat".split():
70        d[i+"s"]={}
71       
72    s=summaryString
73   
74    items=s.split("\n")
75
76    matcher=re.compile(r"^(datasetGroup|dataset|variable|datasetURI|axis|outputFormat)_(\d+.*)$")
77   
78    for item in items:
79        if item.find(":")<0: continue
80        colon=item.find(":")
81       
82        (name, value)=(item[:colon], item[colon+1:])
83        (name, value)=(name.strip(), value.strip())
84        # deCamelCase later...???
85        match=matcher.match(name)
86       
87        if match:
88            (stringMatched, index)=match.groups()
89            if stringMatched=="axis":
90                d["axes"][index]=value
91            else:
92                d[stringMatched+"s"][index]=value
93        else:
94            d["globals"][name]=value
95           
96    return d   
97   
98   
99def getDictSubsetMatching(dct, pattern, mode="string match"):
100    """
101    Returns a dictionary of all items in input dictionary 'dct'
102    where keys match 'pattern'.
103    """
104    rtdict={}
105    for key in dct.keys():
106        if mode=="string match":
107            if key.find(pattern)>-1:
108                rtdict[key]=dct[key]
109        elif mode=="regex":
110            match=re.search(pattern, key)
111            if match:
112                rtdict[key]=dct[key]       
113    return rtdict
114   
115   
116
117def translateURI(uri):
118    """
119    Takes a URL and returns the location of the file on the local network
120    or takes a local path and returns the URL of the file/directory.
121    """
122    if uri.find("http")>-1:
123        newpath=uri.replace(OUTPUT_DIR_URL_PATH, OUTPUT_DIR_LOCAL_PATH)
124    else:
125        newpath=uri.replace(OUTPUT_DIR_LOCAL_PATH, OUTPUT_DIR_URL_PATH)
126    return newpath
127
128
129def deUnicodeObject(obj):
130    """
131    Returns the object identical except unicode strings are all
132    converted to normal strings.
133    """
134    tupleFound=None
135    if type(obj)==type((1,2)):
136        tupleFound=1
137        obj=list(obj)
138
139    if type(obj)==type([]):
140        rtobj=[]
141        for i in obj:
142            if type(i) in (type([]), type((1,2))):
143                rtobj.append(deUnicodeObject(i))
144            elif type(i)==type(u""):
145                rtobj.append(str(i))
146            else:
147                rtobj.append(i)
148        if tupleFound==1: rtobj=tuple(rtobj)
149
150    elif type(obj)==type(u""):
151        rtobj=str(obj)
152    else:
153        rtobj=obj
154    return rtobj
155
156
157# Set up common classes
158class RedirectStdout:
159    """
160    RedirectStdout class - used to direct standard output away from
161    the screen in CGI scripts.
162    """
163
164    def write(self, item):
165        """
166        write method - allows dummy standard out to work.
167        """
168        pass
169
170    def flush(self):
171        """
172        Method to do nothing, again!
173        """
174        pass
175
176
177if __name__=="__main__":
178    print createSummaryDict("""status: constructing
179    username: None
180    accessTime: 1146079648.25
181    variable_3.1.1: var3
182    proceed: Proceed
183    dataset_2.1: Test Dataset 2
184    userRoles: []
185    dataset_3.1: Test Dataset 3
186    datasetGroup_2: Test Data Group 2
187    datasetGroup_3: Test Data Group 3
188    sessionID: session_20060426190723114
189    datasetGroup_1: Test Data Group 1
190    variable_1.1.1: pqn
191    dataset_1.1: Test Dataset 1
192    variable_2.1.1: var2 """)
193   
194    print getDateTimeComponents("2004-1-1T12:0:0.0")
195    print getDateTimeComponents("2004-07-04T12:45:33.489")
Note: See TracBrowser for help on using the repository browser.