I'm trying to modify an XML attribute every time my program starts.

Basically, I'm creating a GUI with PyGObject, and I want the version number in the about box to automatically update.

The XML is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.6 -->
  <object class="GtkAboutDialog" id="aboutDialog">
    <property name="can_focus">False</property>
    <property name="border_width">5</property>
    <property name="title" translatable="yes">About NetGUI</property>
    <property name="modal">True</property>
    <property name="type_hint">dialog</property>
    <property name="program_name">NetGUI</property>
    <property name="version">0.5</property>
    <property name="copyright" translatable="yes">Copyright (c) 2013. Cody Dostal   </property>
    <property name="comments" translatable="yes">NetGUI is a GUI frontend to NetCTL, a network manager developed for Arch Linux.</property>
    <property name="website">https://github.com/codywd/NetGUI</property>
    <property name="website_label" translatable="yes">GitHub</property>
...

How could I automatically edit the Version line to read a variable and change to it?

I have a progVer (program version) variable that it would use. Therefore, I would only increment progVer and it would automatically increment the XML file. If possible, I would like to avoid adding dependencies.

My problem is as follows: My program has a variable that states the program version (progVer is the variable, 0.3 at the moment). I have an external XML file (UI.glade) that holds my about box code. The XML has a property name for version, which I have to manually change to keep about box version with the program version. I want the XML file to automatically update to the newest version, as stated by progVer. I'm not sure how exactly to go about this.

有帮助吗?

解决方案

Here's an approach that uses Python properties to auto-save the XML.

import xml.etree.ElementTree as ET

class ProgramProperties(object):

    def __init__(self, xmlfile):

        # Parse the XML file
        self.__xmlfile = xmlfile
        self.__xml_tree = ET.parse(xmlfile)
        self.__version_element = self.__xml_tree.getroot().find(".//property[@name='version']")

        # Get an in-memory copy of 'version'
        self.__version = float(self.__version_element.text)

    @property
    def version(self):
        return self.__version

    @version.setter
    def version(self, vers):

        # Avoid unecessary file I/O
        if self.__version != vers:

            # Store in-memory
            self.__version = vers

            # Save the version to the file
            self.__version_element.text = str(vers)
            self.__xml_tree.write(self.__xmlfile)


prog = ProgramProperties('testdata.txt')

# Update the version, which automatically saves it in the setter
prog.version = 3.14

其他提示

def __init__(self):
    self.builder = gtk.Builder()
    with open('ui.glade', 'r') as f:
        self.glade = f.read()
    self.glade = self.glade.replace('[VERSION_TOKEN]', self.version, 1)
    self.builder.add_from_string(self.glade)
  1. Read the Glade file.
  2. Replace a token you defined within.
  3. Build GUI using function add_from_string.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top