Fixed / overloading when enabling true division.
[tx] / xpath_misc.py
1 # -*- coding:utf-8 -*-
2
3 # XPath misc
4 # Copyright (C) 2005  Frédéric Jolliton <frederic@jolliton.com>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 try :
21         import cStringIO as StringIO
22 except :
23         import StringIO as StringIO
24
25 def lispy( e ) :
26
27         assert isinstance( e , tuple ) and e , 'expected a non empty tuple'
28         s = StringIO.StringIO()
29         prt = s.write
30         parens = []
31         x = [ 1 ]
32         breakFlag = [ 0 ]
33         last = [ None ]
34         def forward( n ) :
35                 x[ 0 ] += n
36         def push() :
37                 parens.append( x[ 0 ] )
38         def couldBreak() :
39                 breakFlag[ 0 ] += 1
40                 last[ 0 ] = parens.pop()
41         def breakHere() :
42                 if breakFlag[ 0 ] :
43                         prt( '\n' )
44                         prt( ' ' * ( last[ 0 ] - 1 ) )
45                         breakFlag[ 0 ] = 0
46                         x[ 0 ] = last[ 0 ]
47                 else :
48                         prt( ' ' )
49                         forward( 1 )
50         def lispy_( e ) :
51                 prt( '(' )
52                 push()
53                 forward( 1 )
54                 if isinstance( e[ 0 ] , tuple ) :
55                         lispy_( e[ 0 ] )
56                 else :
57                         t = e[ 0 ].replace( '\\' , '\\\\' ).replace( '\n' , '\\n' )
58                         prt( t )
59                         forward( len( t ) )
60                 for item in e[ 1 : ] :
61                         breakHere()
62                         if isinstance( item , tuple ) :
63                                 lispy_( item )
64                         else :
65                                 s = `item`
66                                 if s[ 0 ].startswith( 'u' ) :
67                                         s = s[ 1 : ]
68                                 if s[ 0 ] == "'" : # force double-quote style
69                                         s = '"%s"' % ( s[ 1 : -1 ].replace( '"' , '\\"' ) )
70                                 push()
71                                 prt( s )
72                                 forward( len( s ) )
73                                 couldBreak()
74                 prt( ')' )
75                 forward( 1 )
76                 couldBreak()
77         lispy_( e )
78         s.seek( 0 )
79         return s.read()
80
81 # Local Variables:
82 # tab-width: 4
83 # python-indent: 4
84 # End:
85
86