2010-04-09 18:46:56 +00:00
#!/usr/bin/env python
import os
2010-08-26 20:42:50 +00:00
import re
2010-04-09 18:46:56 +00:00
import sys
2010-08-20 22:20:45 +00:00
import tempfile
2010-08-26 20:42:50 +00:00
2010-08-26 21:44:43 +00:00
HAS_LXML = False
2010-08-26 20:42:50 +00:00
try :
import lxml . etree as etree
2010-08-26 21:44:43 +00:00
HAS_LXML = True
2010-08-26 20:42:50 +00:00
except ImportError :
try :
import elementtree . ElementTree as etree
2010-08-26 21:44:43 +00:00
except ImportError :
import xml . etree . ElementTree as etree
2010-04-09 18:46:56 +00:00
2010-08-20 22:20:45 +00:00
def indent ( elem , level = 0 ) :
""" http://infix.se/2007/02/06/gentlemen-indent-your-xml
"""
i = " \n " + level * " "
if len ( elem ) :
if not elem . text or not elem . text . strip ( ) :
elem . text = i + " "
for e in elem :
indent ( e , level + 1 )
if not e . tail or not e . tail . strip ( ) :
e . tail = i + " "
if not e . tail or not e . tail . strip ( ) :
e . tail = i
else :
if level and ( not elem . tail or not elem . tail . strip ( ) ) :
elem . tail = i
2010-04-09 18:46:56 +00:00
def name2expr ( sym ) :
name = sym . attrib [ ' name ' ]
2010-10-22 00:23:02 +00:00
if re . match ( ' ^ \ [.* \ ] ' , name ) is None :
#import pdb;pdb.set_trace()
2010-04-09 18:47:04 +00:00
print >> sys . stderr , " Fixing %s " % name
expression = ' [ %s ] ' % name
sym . attrib [ ' name ' ] = expression
2010-08-26 20:42:50 +00:00
def handle_attr_changes ( sym ) :
# http://www.w3schools.com/css/pr_text_text-transform.asp
# http://code.google.com/p/mapnik-utils/issues/detail?id=32&colspec=ID%20Type%20Status%20Priority%20Component%20Owner%20Summary
text_transform = sym . attrib . get ( ' text_convert ' )
# note: css supports text-transform:capitalize but Mapnik does not (yet)
2010-09-26 11:52:06 +00:00
t_ = { ' tolower ' : ' lowercase ' , ' toupper ' : ' uppercase ' , ' none ' : ' none ' }
2010-08-26 20:42:50 +00:00
if text_transform :
new = t_ . get ( text_transform )
if new :
sym . attrib [ ' text_transform ' ] = new
2010-04-09 18:46:56 +00:00
def fixup_pointsym ( sym ) :
if sym . attrib . get ( ' width ' ) :
sym . attrib . pop ( ' width ' )
if sym . attrib . get ( ' height ' ) :
sym . attrib . pop ( ' height ' )
2010-05-07 19:46:05 +00:00
if sym . attrib . get ( ' type ' ) :
sym . attrib . pop ( ' type ' )
2010-04-09 18:46:56 +00:00
def fixup_sym_attributes ( sym ) :
2010-11-08 19:54:28 +00:00
# copy, so we don't loose after clear()
attrib = dict ( sym . attrib )
2010-08-26 20:42:50 +00:00
for css in sym . findall ( ' CssParameter ' ) :
2010-04-09 18:46:56 +00:00
key = css . attrib . get ( ' name ' )
value = css . text
2010-06-02 12:53:13 +00:00
attrib [ key ] = value
2010-04-09 18:47:04 +00:00
sym . clear ( ) # remove CssParameter elements
for k , v in attrib . items ( ) : # insert attributes instead
2010-04-09 18:46:56 +00:00
sym . attrib [ k ] = v
2010-06-02 12:53:13 +00:00
2010-04-09 18:46:56 +00:00
if __name__ == " __main__ " :
2010-04-09 18:47:04 +00:00
2010-06-29 08:59:50 +00:00
#required parameters:
2010-10-22 00:23:02 +00:00
# input_xml: outdated stylesheet file
2010-06-29 08:59:50 +00:00
# output_file: new stylesheet file
if len ( sys . argv ) != 3 :
2010-10-22 00:23:02 +00:00
sys . stderr . write ( ' Usage: %s <input_xml> <output_xml> \n ' % os . path . basename ( sys . argv [ 0 ] ) )
2010-04-09 18:46:56 +00:00
sys . exit ( 1 )
2010-10-22 00:23:02 +00:00
input_xml = sys . argv [ 1 ]
output_xml = sys . argv [ 2 ]
if input_xml == output_xml :
sys . stderr . write ( ' Sorry, this upgrade script does not allow you to overwrite the input xml, please provide a different output name than " %s " \n ' % output_xml )
sys . exit ( 1 )
pre_read = open ( input_xml , ' r ' )
2010-08-26 21:44:43 +00:00
if ' !ENTITY ' in pre_read . read ( ) and not HAS_LXML :
sys . stderr . write ( ' \n Sorry, it appears the xml you are trying to upgrade has entities, which requires lxml (python bindings to libxml2) \n ' )
sys . stderr . write ( ' Install lxml with: " easy_install lxml " or download from http://codespeak.net/lxml/ \n ' )
sys . exit ( 1 )
2010-10-22 00:23:02 +00:00
tree = etree . parse ( input_xml )
2010-08-26 21:44:43 +00:00
if hasattr ( tree , ' xinclude ' ) :
2010-08-26 20:45:08 +00:00
tree . xinclude ( )
2010-04-09 18:46:56 +00:00
root = tree . getroot ( )
2010-07-19 12:01:34 +00:00
# rename 'bgcolor' to 'background-color'
if root . attrib . get ( ' bgcolor ' ) :
root . attrib [ ' background-color ' ] = root . attrib . get ( ' bgcolor ' )
root . attrib . pop ( ' bgcolor ' )
2010-11-08 19:12:29 +00:00
# set new minimum_version
root . set ( ' minimum_version ' , ' 0.8.0 ' )
2010-08-26 20:42:50 +00:00
styles = root . findall ( ' Style ' )
if not len ( styles ) :
2010-08-20 22:20:45 +00:00
sys . stderr . write ( ' ### Warning, no styles encountered and nothing able to be upgraded! \n ' )
2010-08-26 20:42:50 +00:00
else :
for style in styles :
for rule in style . findall ( ' Rule ' ) :
for sym in rule . findall ( ' TextSymbolizer ' ) or [ ] :
name2expr ( sym )
2010-09-18 19:33:13 +00:00
handle_attr_changes ( sym )
2010-08-26 20:42:50 +00:00
for sym in rule . findall ( ' ShieldSymbolizer ' ) or [ ] :
name2expr ( sym )
for sym in rule . findall ( ' PointSymbolizer ' ) or [ ] :
fixup_pointsym ( sym )
for sym in rule . findall ( ' LineSymbolizer ' ) or [ ] :
fixup_sym_attributes ( sym )
for sym in rule . findall ( ' PolygonSymbolizer ' ) or [ ] :
fixup_sym_attributes ( sym )
for sym in rule . findall ( ' RasterSymbolizer ' ) or [ ] :
fixup_sym_attributes ( sym )
for sym in rule . findall ( ' BuildingSymbolizer ' ) or [ ] :
fixup_sym_attributes ( sym )
2010-06-29 08:59:50 +00:00
2010-08-26 21:44:43 +00:00
# TODO - make forcing indent an option
indent ( root )
2010-10-22 00:23:02 +00:00
tree . write ( output_xml )