source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/Utilities.py @ 3456

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/Utilities.py@3456
Revision 3456, 6.9 KB checked in by cbyrom, 13 years ago (diff)

Add simple object to stubB to represent selected items and retain the important
associated data.
Add new utility method, recreateListFromUnicode(string), to recreate the KML and
WMC URL lists passed via AJAX calls as unicode strings.

Line 
1from collections import deque # python 2.4
2try: #python 2.5
3    from xml.etree import ElementTree as ET
4except ImportError:
5    try:
6        # if you've installed it yourself it comes this way
7        import ElementTree as ET
8    except ImportError:
9        # if you've egged it this is the way it comes
10        from elementtree import ElementTree as ET
11from ETxmlView import *
12import ConfigParser
13import os
14import re
15import urllib
16import logging
17
18
19class myConfig:
20   
21   ''' Handle missing sections and variables in a config file a bit gracefully. Also
22   instantiates a logger if necessary '''
23   
24   def __init__(self,configfile,logName='NDGLOG'):
25       self.config=ConfigParser.ConfigParser()
26       if not os.path.exists(configfile): 
27           raise ValueError("Config file [%s] doesn't exist in [%s]"%(configfile,os.getcwd()))
28       self.config.read(configfile)
29       logfile=self.get('logging','debugLog',None)
30       # move to a wsgi logger ... safer I think in a multithread environment
31       #
32       #self.logfile=None #deprecated
33       self.logger=None
34       self.logfile=logfile
35       #
36       if logfile is not None:
37           #logger=logging.getLogger(logName)
38           #handler=logging.FileHandler(logfile)
39           #formatter=logging.Formatter('%(asctime)s %(levelname)s %(message)s')
40           #handler.setFormatter(formatter)
41           #logger.addHandler(handler)
42           #logger.setLevel(logging.INFO)
43           #self.logger=logger
44           self.logger=None
45       
46   def get(self,section,key,default=None):
47       ''' Return a config file value for key frm section '''
48       try:
49           return self.config.get(section,key)
50       except:
51           return default
52       
53   def log(self,string):
54       ''' Log some debug information '''
55       if self.logger is None: return
56       if string is not None:
57          self.logger.info(string)
58       else:
59          self.logger.info('empty log entry')
60         
61   def getLog(self):
62       return self.logger
63
64class RingBuffer(deque):
65    #deque is a python 2.4 class!
66    #credit http://www.onlamp.com/pub/a/python/excerpt/pythonckbk_chap1/index1.html
67    def __init__(self, size_max):
68        deque.__init__(self)
69        self.size_max = size_max
70    def append(self, datum):
71        deque.append(self, datum)
72        if len(self) > self.size_max:
73            self.popleft( )
74    def tolist(self):
75        return list(self)
76    def remove(self,value):
77        try: # python 2.5
78            deque.remove(self,value)
79        except: # python 2.4
80            #yuck, two copies ...
81            x=self.tolist()
82            x.remove(value)
83            self.clear()
84            for i in x:self.append(i)
85           
86
87def wrapGetText(element,xpathExpression,multiple=0):
88        ''' Wraps a call to ET to get a text object in an error handler '''
89        def none2txt(i):
90            if i is None: return ''
91            return i
92        if element is None:
93            if multiple:
94                 return ['',]
95            else: return ''
96        if multiple:
97                r=element.findall(xpathExpression)
98        else:
99                r=[element.find(xpathExpression),]
100        try:
101                rr=[i.text for i in r]
102        except:
103                rr=['',]
104        rr=map(none2txt,rr) 
105        if multiple: 
106                return rr
107        else: return rr[0] 
108
109def getURLdict(cgiFieldStorage):
110    ''' takes a cgi field storage object and converts it to a dictionary '''
111    result={}
112    for item in cgiFieldStorage:
113            result[item]=cgiFieldStorage[item].value
114    return result
115##
116### convert the followign two methods into one class that can handle
117## xml directly too if necessary
118##
119def DIFid2NDGid(string):
120    ''' takes a dif thing parses it and produces an ET ndg element id ...
121    and use this in dif ... '''
122    s=string.split(':')
123    try:
124        r='''<DIFid><schemeIdentifier>%s</schemeIdentifier>
125         <repositoryIdentifier>%s</repositoryIdentifier>
126         <localIdentifier>%s</localIdentifier></DIFid>'''%(s[1],s[0],s[2])
127        return ET.fromstring(r)
128    except:
129        r='''<DIFid><schemeIdentifier>DIF</schemeIdentifier>
130        <repositoryIdentifier>Unknown</repositoryIdentifier>
131        <localIdentifier>%s</localIdentifier></DIFid>'''%string
132        return ET.fromstring(r)
133
134def EnumerateString(string):
135    ''' Takes a string, and if it's got a number on the end, increments it,
136    otherwise adds a number on the end, used to differentiate strings which
137    would otherwise be identical '''
138    def addNum(matchObj):
139        s=matchObj.group()
140        return str(int(s)+1)
141    r=re.sub('\d+$',addNum,string)
142    if r==string: r=r+'1'
143    return r
144
145def dateParse(string,instruction):
146    ''' Simple date manipulations on a string, if it is understood ...
147       if instruction = YYYY, return the year '''
148    s=string.split('-')
149    if instruction=='YYYY':
150        if len(s)==3: # expecting year,mon,day or day,mon,year ...
151            if int(s[0])>int(s[2]): 
152                return s[0]
153            else:
154                return s[2]
155        else:
156            return string # unknown format as yet ...
157    else:
158        return 'unknown instruction to dateParse %s'%instruction
159
160def idget(xml,dataType='DIF'):
161    ''' Given an xml document (string), parse it using ElementTree and
162    find the identifier within it. Supports dataTypes of 'DIF' ...
163    (actually only DIF for now).
164    '''
165    et=loadET(xml)
166    helper=nsdumb(et)
167    if dataType=='DIF':
168        return helper.getText(et,'Entry_ID')
169    else:
170        raise TypeError,'idget does not support datatype [%s]'%dataType
171   
172
173def recreateListFromUnicode(string):
174    ''' Parse a list that has been passed as unicode over http and
175    recreate it in its list form
176    '''
177    newList = []
178    element = []
179    for c in string:
180        if c == '[' or c == ' ' or c == ']' or c == '\'':
181            continue
182        elif c == ',':
183            if (len(element) > 0):
184                newList.append(''.join(element))
185                element = []
186            continue
187        element.append(c)
188
189    # don't forget the last element
190    if (len(element) > 0):
191        newList.append(''.join(element))
192       
193    return newList
194
195       
196import unittest
197
198class TestCase(unittest.TestCase):
199    """ Tests as required """
200
201    configFile='examples/example.config'
202    difFile='examples/neodc.eg1.dif'
203   
204    def setUp(self):
205        # If pkg_resources is available assume the module is eggified and
206        # get a stream to the input data from the egg.
207        #try:
208        #    import pkg_resources
209        #    f = pkg_resources.resource_stream(__name__, self.configFile)
210        #except ImportError:
211            # Else take the input file from __file__
212            #import os
213        self.config=myConfig(self.configFile)
214        f=file(self.difFile,'r')
215        self.difxml=f.read()
216            #f=file(os.path.join(os.path.basepath(__file__), self.configFile))
217
218        #self.config=myConfig(f)
219
220    def testConfig(self):
221        print 'Discovery Icon [%s]'%self.config.get('DISCOVERY','icon')
222       
223    def testidget(self):
224        self.assertEqual(idget(self.difxml),'NOCSDAT192')
225
226
227if __name__=="__main__":
228    unittest.main()
229
230
231
Note: See TracBrowser for help on using the repository browser.