source: TI07-MOLES/trunk/PythonCode/wsgi/ETxmlView.py @ 2229

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

Modified version of nsdumb that can support default namespaces
which are declared without concommittant schemalocation defs.

Line 
1# Copyright Bryan Lawrence, Rutherford Appleton Laboratory, CCLRC, 2006
2#
3# This code is made available under the GPL, if you don't know what
4# that means, you have no rights to copy it or use it!
5#
6# Code to support xml and elementTree viewing as text and html
7try: #python 2.5
8    from xml.etree import ElementTree as ET
9except ImportError:
10    try:
11        # if you've installed it yourself it comes this way
12        import ElementTree as ET
13    except ImportError:
14        # if you've egged it this is the way it comes
15        from elementtree import ElementTree as ET
16import re
17
18class subAI:
19    ''' This is Alan Iwi's substitute and replace orphan <> code '''
20    def __init__(self):
21        self.r1=re.compile('<([^>]*(<|$))')
22        self.r2=re.compile('((^|>)[^<]*)>')
23    def sub(self,s):
24        old=''
25        while s != old:
26            old=s
27            s=self.r1.sub(r'&lt;\1',s)
28            s=self.r2.sub(r'\1&gt;',s)
29        return s
30       
31class nsdumb:
32    ''' provides an xpath interface to element tree nodes which
33    is namespace agnostic '''
34    def __init__(self,root=None):
35        ''' Provide a root element with namespace definitions when
36        instantiatin '''
37        if root is None: 
38            self.xmlns=''
39            return
40        ns=['xmlns','{http://www.w3.org/2001/XMLSchema-instance}schemaLocation']
41        for i in ns: 
42            if i in root.keys():
43                self.xmlns='{%s}'%root.attrib[i].split(' ')[0]
44                break
45            else:
46                if root.tag[0]<>'{': 
47                    self.xmlns=''
48                else:
49                    ns,local=root.tag.split('}')
50                    self.xmlns='{%s}'%ns[1:]
51    def __str__(self):
52        return 'Element Tree namespace helper with namespace: [%s]'%self.xmlns
53   
54    def __distributens(self,xpathExpression):
55        ''' Actually we only support tag finding in this '''
56        tags=xpathExpression.split('/')
57        new=''
58        for t in tags: new+=self.xmlns+t+'/'
59        new=new[0:-1]
60        return new
61    def getText(self,elem,xpathExpression,multiple=0):
62        ''' Get a text object sensibly '''
63        if elem is None: 
64            if multiple:
65                return ['',]
66            else: return '' 
67        if multiple:
68                r=elem.findall(self.__distributens(xpathExpression))
69        else:
70                r=[elem.find(self.__distributens(xpathExpression)),]
71        try:  # if element is None, this should fail ...
72                rr=[]
73                for i in r:
74                    t=i.text
75                    if t is not None: 
76                        rr.append(t)
77                    else: rr.append('')
78        except:
79                rr=['',]
80        if multiple: 
81                return rr
82        else: return rr[0] 
83       
84    def find(self,elem,xpathExpression):
85       ''' Return relevant subelement '''
86       if elem is None: return ''
87       xe=self.__distributens(xpathExpression)
88       return elem.find(xe)
89   
90    def findall(self,elem,xpathExpression):
91       ''' Return all relevant subelements '''
92       if elem is None: return []
93       xe=self.__distributens(xpathExpression)
94       return elem.findall(xe)
95
96    def strip(self,tag):
97        ''' Given a tag, strip the default namespace '''
98        return tag.lstrip(self.xmlns)
99
100def et2text(elem,indent='',html=0,space='   ',helper=None):
101        '''Lightweight pretty printing of elementTree elements'''
102        def estrip(elem):
103                ''' Just want to get rid of unwanted whitespace '''
104                if elem is None:
105                        return ''
106                else:
107                        return elem.strip()
108        ns=helper
109        if ns is None: ns=nsdumb(elem)
110        strAttrib=''
111        for att in elem.attrib:
112                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
113        result='%s<%s%s>%s'%(indent,ns.strip(elem.tag),strAttrib,estrip(elem.text))
114        children=len(elem)
115        if children:
116                for item in elem:
117                        result+='\n'+et2text(item,indent=indent+space,helper=ns)
118                result+='\n%s%s</%s>'%(indent,estrip(item.tail),ns.strip(elem.tag))
119        else:
120                result+='</%s>'%(ns.strip(elem.tag))
121        return result
122
123
124def et2html(elem,matchList=[],number=0,helper=None):
125    #this method is NDG code ... copyright CCLRC ...
126    '''Lightweight HTML pretty printing of elementTree elements + highlight
127    any words which occur in the element text (and tails) which occur in matchList,
128    and formatted using a css something like this:
129    ===
130    DIV.xmlElem {PADDING-LEFT: 20px;}
131    .xmlAttrVal {COLOR:Red; }
132    .xmlAttrTyp {COLOR:Green; }
133    .xmlElemTag {COLOR:Blue; }
134.   highlight {BACKGROUND-COLOR:Yellow; }
135    ===
136    Line number is not yet implemented.
137    '''
138    def span(x,c): return '<span class="%s">%s</span>'%(c,x)
139    def div(x,c): return '<div class="%s">%s</div>'%(c,x)
140    def match(x): 
141        if x is None: return ''
142        for w in matchList: x=re.sub(w,span(w,'highlight'),x)
143        return x
144    lt,gt='<b>&lt;</b>','<b>&gt;</b>'
145    ns=helper
146    if ns is None: ns=nsdumb(elem) 
147    strAttrib=''
148    for att in elem.attrib:
149        strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal'))
150    result='%s%s%s%s%s'%(lt,span(ns.strip(elem.tag),"xmlElemTag"),strAttrib,gt,match(elem.text))
151    children=len(elem)
152    if children:
153        for item in elem:
154            result+=et2html(item,matchList,helper=ns)
155        result+='%s%s/%s%s'%(match(item.tail),lt,span(ns.strip(elem.tag),'xmlElemTag'),gt)
156    else:
157        result+='%s/%s%s'%(lt,span(ns.strip(elem.tag),'xmlElemTag'),gt)
158    return div(result,'xmlElem')
159   
160def loadET(inputString):
161    ''' This method returns an elementtree object after some cleaning
162    of the string, essentially a hack to make sure that xml doesn't contain any
163    naughty & characters alone (typically from URL copies), or unescaped orphan
164    < or > signs ... and that the unicode has been processed to something that
165    might work'''
166   
167    if inputString is None: return None
168    inputString=re.sub(r'&(?!\w+;)', '&amp;', inputString)
169 
170    # first just try and do it so we don't waste time if we don't need to ...
171    try:
172        elem=ET.fromstring(inputString)
173        return elem
174    except:
175        pass
176        # and carry on
177
178    #ok, let's deal with orphan > and < signs then ...
179    subtool=subAI()
180    s=subtool.sub(inputString)
181
182    #now let's sort out an encoding
183    encodings=['utf-8','latin-1','iso-8859-1','ascii',]
184    elem=None
185    for option in encodings:
186        try:
187            s=s.encode(option,'replace')
188            try:
189                elem=ET.fromstring(s)
190            except:
191                s=re.sub('\n','<br/>LINE: ',s)
192                print '<p>%s</p>'%s
193                raise
194        except UnicodeError:
195            pass
196        else:
197            break
198    return elem
199
200def xml2text(xmlString):
201    tree=loadET(xmlString)
202    return et2text(tree)
203
204def xml2HTML(xmlString,**kw):
205    tree=loadET(xmlString)
206    return et2HTML(tree,**kw)
207
208import unittest
209from os.path import splitext
210
211class TestCase(unittest.TestCase):
212    """
213    """
214   
215    def setUp(self):
216        ''' Load example files for testing, and get pointers to their dataset identifiers '''
217        inputFiles = ['examples/neodc.eg1.dif','examples/ukho.eg1.mdip']
218        indexes={'.dif':'Entry_ID','.mdip':'DatasetIdentifier'}
219        results=['NOCSDAT192','RSDRA2006000377384']
220        ids=[]
221        for f in inputFiles:
222            ps=splitext(f)
223            ids.append(indexes[ps[1]])
224        files=[file(i,'r') for i in inputFiles]
225        self.strings=[i.read() for i in files]
226        self.ids=ids
227        self.results=results
228
229    def testEntries(self):
230        ''' Testing the file objects can be loaded using loadET'''
231        for s in self.strings:
232            tree=loadET(s)
233            #for i in tree: print i.tag
234           
235    def testnsDump(self):
236        ''' Test that nsdumb can mediate access to identifiers '''
237        for i in range(len(self.strings)):
238            tree=loadET(self.strings[i])
239            helper=nsdumb(tree)
240            self.assertEqual(helper.getText(tree,self.ids[i]),self.results[i])
241           
242
243if __name__=="__main__":
244    unittest.main()
Note: See TracBrowser for help on using the repository browser.