3
votes

I've searched extensively for the past few days and can't seem to find what I'm looking for. I've written a script using Python 2.7.3 and ElementTree to parse an XML file and edit an attribute buried deep within the XML file. The script works fine. I had a meeting late last week with the customer who informed me the target platform will be CentOS. I thought, no problem. To test on the anticipated platform I created a CentOS VMWare client and much to my surprise my script crapped the bed, giving me the error message, "SyntaxError: expected path separator ([)" In the course of my researching the nature of this error message I learned that CentOS 6.4 supports Python 2.6.6, which contains an older version of ElementTree that does not have support for searching for attributes [@attribute] syntax.
This customer won't upgrade Python on the platform, nor will they install additional libraries, so lxml is not an option for me. My question is, can I somehow still access the buried attribute and edit it without the ElementTree support for the [@attribute] facilities?

Here's an example of the kind of XML I'm dealing with:

`

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<my-gui>
    <vehicles>
        <car vendor="Ford"/>
    </vehicles>
    <options>
        <line transmission='manual'/>
    </options>
    <title>Dealership</title>
    <choice id='manual' title="Dealership">
        <pkg-deal id='manual' auth='manager'>.</pkg-deal>
    </choice>
    <choice id='manual' title='Dealership'/>
    <choice id='manual' DealerLocation='Dealer_Loc'/>
    <choices-outline color='color_choice'>
        <line choice='blue'/>
    </choices-outline>
    <choice id='cars' GroupID='convertables'>
        <pkg-deal id='model.Taurus' version="SEL" arguments='LeatherInterior' enabled='XMRadio'>Taurus</pkg-deal>
        <pkg-deal id='model.Mustang' version="GT" enabled='SIRIUSRadio'>Mustang</pkg-deal>
        <pkg-deal id='model.Focus' version="SE" enabled='XMRadio'>Focus</pkg-deal>
        <pkg-deal id='model.Fairlane'>Fairlane</pkg-deal>
        <pkg-deal id='model.Fusion' version="SE" arguments='ClothInerior'>Fusion</pkg-deal>
        <pkg-deal id='model.Fiesta' version="S Hatch" enabled="SIRIUSRadio">Fiesta</pkg-deal>
    </choice>
</my-gui>

`

Here's a snippet of the successful Python 2.7.3 code that breaks under Python 2.6.6:

if self.root.iterfind('pkg-deal'): 
          self.deal = self.root.find('.//pkg-deal[@id="model.fusion"]')
          self.arg = str(self.deal.get('arguments'))
          if self.arg.find('with Scotchguard=') > 0:  
            QtGui.QMessageBox.information(self, 'DealerAssist', 'The selected car is already updated. Nothing to do.')            
            self.leave()        
          self.deal.set('arguments', self.arg + ' with Scotchguard') 
          ...
      ...

Is there a way I can modify the first line of this 'if' statement block that will allow me to edit the 'arguments' attribute of the Fusion element? Or am I relegated to implementing libxml2, which promises to be a real pain?...

Thanks.

2
I don't know if this counts as installing additional libraries (and this is sort of side-stepping your question), but perhaps you could try copy-and-pasting the version of ElementTree from Python 2.7 into your script, and renaming the file to something like ElementTree2.py and just using that? - Michael0x2a
@Michael0x2a - wow! I hadn't thought of that. That's very creative and might just work. I think I may be able to include it as a separate file and import it. I'm going to play with that and let you know. - Don Bailey
@Michael0x2a - I tried copying ElementTree.py, renaming it ElementTree2.py and importing it, but kept getting invalid import errors. Good thought though. - Don Bailey
Huh. I guess I should have tried my suggestion out first :). It looks like ElementTree isn't meant to be a standalone file. What I ended up doing was going to the Lib\xml folder in C:\Python27, and copied the entire etree folder which ElementTree comes from (and renaming etree to etree2). Doing import etree2.ElementTree then appeared to work, although I didn't thoroughly test everything to make sure there were no more issues. (In particular, I'd rename and tweak all the files inside the etree folder to make sure there are no conflicts with the standard library) - Michael0x2a
@Michael0x2a - Thanks for the follow-up. I've give that a try later this morning and report back. Thanks so much. - Don Bailey

2 Answers

0
votes

This may be side-stepping the question, but you could just try copying-and-pasting the version of ElementTree from Python 2.7, renaming it to avoid conflicting with the standard library, and importing and using that.

However, since ElementTree isn't meant to be used as a standalone file, what you need to do is navigate to C:\Python27\Lib\xml and copy the entire etree folder and import ElementTree by doing import etree.ElementTree inside your script.

To avoid accidentally importing or using the version of ElementTree from Python 2.6, you should probably rename the etree folder, its contents, delete the .pyc files, and fix the imports inside the file to reference the Python 2.7 version.

0
votes

This same problem was solved by another user here.

This user filtered the attribute manually in Python 2.6. I'm posting their code example here even though the example pertains specifically to the asker's code:

def final_xml(self,username):
    users = self.root.findall("user")
    for user in users:
        if user.attrib.get('username') == 'user1':
            break
    else:
        raise ValueError('No such user')

    # `user` is now set to the correct element
    self.root.remove(user)
    print user
    tree = ET.ElementTree(self.root)
    tree.write("msl.xml")