1 | from collections import deque # python 2.4 |
---|
2 | import ElementTree as ET |
---|
3 | import ConfigParser |
---|
4 | import os |
---|
5 | import re |
---|
6 | |
---|
7 | class myConfig: |
---|
8 | ''' Handle missing sections and variables in a config file a bit gracefully ''' |
---|
9 | def __init__(self,configfile): |
---|
10 | self.config=ConfigParser.ConfigParser() |
---|
11 | if not os.path.exists(configfile): |
---|
12 | raise 'No config file: %s'%configfile |
---|
13 | self.config.read(configfile) |
---|
14 | logfile=self.get('logging','debugLog',None) |
---|
15 | self.logfile=None |
---|
16 | if logfile is not None: |
---|
17 | self.logfile=open(logfile,'a') |
---|
18 | |
---|
19 | def get(self,section,key,default=None): |
---|
20 | try: |
---|
21 | return self.config.get(section,key) |
---|
22 | except: |
---|
23 | return default |
---|
24 | |
---|
25 | def log(self,string): |
---|
26 | ''' Log some debug information ''' |
---|
27 | if self.logfile is None: return |
---|
28 | if string is not None: |
---|
29 | self.logfile.write('%s\n'%string) |
---|
30 | else: |
---|
31 | self.logfile.write('empty log entry\n') |
---|
32 | |
---|
33 | class Response: |
---|
34 | ''' Holds all the parts of the response to an HTML get or post ''' |
---|
35 | def __init__(self,content='text/html'): |
---|
36 | self.headers=[] |
---|
37 | self.cookie=None |
---|
38 | self.contentType=content |
---|
39 | self.content='' |
---|
40 | def write(self,out): |
---|
41 | for name,value in self.headers: |
---|
42 | out.write("%s: %s\n"( name,value)) |
---|
43 | if self.cookie: |
---|
44 | out.write("%s\n"%self.cookie) |
---|
45 | out.write("Content-type: %s\n"%self.contentType) |
---|
46 | out.write("\n") |
---|
47 | out.write(self.content) |
---|
48 | |
---|
49 | class RingBuffer(deque): |
---|
50 | #deque is a python 2.4 class! |
---|
51 | #credit http://www.onlamp.com/pub/a/python/excerpt/pythonckbk_chap1/index1.html |
---|
52 | def __init__(self, size_max): |
---|
53 | deque.__init__(self) |
---|
54 | self.size_max = size_max |
---|
55 | def append(self, datum): |
---|
56 | deque.append(self, datum) |
---|
57 | if len(self) > self.size_max: |
---|
58 | self.popleft( ) |
---|
59 | def tolist(self): |
---|
60 | return list(self) |
---|
61 | |
---|
62 | def wrapGetText(element,xpathExpression,multiple=0): |
---|
63 | ''' Wraps a call to ET to get a text object in an error handler ''' |
---|
64 | if multiple: |
---|
65 | r=element.findall(xpathExpression) |
---|
66 | else: |
---|
67 | r=[element.find(xpathExpression),] |
---|
68 | try: |
---|
69 | rr=[i.text for i in r] |
---|
70 | except: |
---|
71 | rr=['',] |
---|
72 | if multiple: |
---|
73 | return rr |
---|
74 | else: return rr[0] |
---|
75 | |
---|
76 | def getURLdict(cgiFieldStorage): |
---|
77 | ''' takes a cgi field storage object and converts it to a dictionary ''' |
---|
78 | result={} |
---|
79 | for item in cgiFieldStorage: |
---|
80 | result[item]=cgiFieldStorage[item].value |
---|
81 | return result |
---|
82 | ## |
---|
83 | ### convert the followign two methods into one class that can handle |
---|
84 | ## xml directly too if necessary |
---|
85 | ## |
---|
86 | def DIFid2NDGid(string): |
---|
87 | ''' takes a dif thing parses it and produces an ET ndg element id ... |
---|
88 | and use this in dif ... ''' |
---|
89 | s=string.split(':') |
---|
90 | try: |
---|
91 | r='''<DIFid><schemeIdentifier>%s</schemeIdentifier> |
---|
92 | <repositoryIdentifier>%s</repositoryIdentifier> |
---|
93 | <localIdentifier>%s</localIdentifier></DIFid>'''%(s[1],s[0],s[2]) |
---|
94 | return ET.fromstring(r) |
---|
95 | except: |
---|
96 | r='''<DIFid><schemeIdentifier>DIF</schemeIdentifier> |
---|
97 | <repositoryIdentifier>Unknown</repositoryIdentifier> |
---|
98 | <localIdentifier>%s</localIdentifier></DIFid>'''%string |
---|
99 | return ET.fromstring(r) |
---|
100 | def idconvert(e): |
---|
101 | ''' Converts an XML ndg identifier into a uri version ''' |
---|
102 | s='%s:%s:%s'%(wrapGetText(e,'repositoryIdentifier'), |
---|
103 | wrapGetText(e,'schemeIdentifier'), |
---|
104 | wrapGetText(e,'localIdentifier')) |
---|
105 | return s |
---|
106 | |
---|
107 | if __name__=="__main__": |
---|
108 | x='''<test attr='something'>some content with <a href="target"> display</a> url</test>''' |
---|
109 | import ElementTree as ET |
---|
110 | y=ET.fromstring(x) |
---|
111 | print PrettyPrintHTML(y,['content',]) |
---|
112 | print PrettyPrint(y) |
---|
113 | |
---|