source: TI07-MOLES/trunk/PythonCode/wsgi/Utilities.py @ 2379

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/Utilities.py@2379
Revision 2379, 5.9 KB checked in by lawrence, 13 years ago (diff)

Improved handling of deployments in browse.

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