Configuration files¶
Question¶
I got inspired by an article in Inspirational Technology and added some configuration-option to the software as an XML-File.
In times before I just did a
copy \develeopment\somefile.ini \distribuiton\somefile.ini after runing
py2exe.
But after combining py2exe with Innosetup, it was really crucial to get these files copied to the py2exe dist-Directory during the py2exe Buildrun.
Solution¶
It was present all times in py2exe. But I did not really understand how to use it :)))
setup(
options = options,
# The lib directory contains everything except the executables and the python dll.
zipfile = zipfile,
windows = [wxprog],
# use out build_installer class as extended py2exe build command
cmdclass = {"py2exe": build_installer},
packages = ["encodings"],
data_files=[("prog",["kategorien.xml",])]
)
The crucial line is
here you are supposed to supply a list of tuples. [ (,), (,), ...]
Each tuple ("directoy",["list.txt", "of.txt", "files.txt) consists of 2
Elements:
- the subdirctory of the dist-base directory, where the files are copied to
- a list of files, which are supposed to be copied there
So in my example I have some configuration data in kategorien.xml and want to
get this file copied to the program-directory. (Maybe that is bad style and whe
should put it within \documents and settings\currentuser\application
settings\my bompany\myproduct\mysettings.xml, but for distribution WITHIN one
company I want to be able to support filepaths by phone :-)) )
For more information, see: Data files
and how do you deal with this .xml-settings¶
Just to be buzzword-compliant every application this days should store at least something in XML. There are many, many libraries to do XML-Stuff in Python, I found 2 of them rather pythonic:
- effbots ElementTree
- and gnosis.xml of David Mertz
Elementtree Documentation is easier to find (it's on the same website) and not totally up to date ("Element" is now a sub to "Elementtree" and no longer on module-level and such); gnosis-Tools are rather hidden GnosisDownload within a textual link which is only available from the end of an article in IBMs developer World. Also there is no website "getting started" for Gnosis, you have to dig for the /doc directory.
The configuration file is:
<?xml version="1.0" encoding="iso-8859-1"?>
<Kategorien>
<Kategorie>Kundentermin</Kategorie>
<Kategorie>Kundentelefonate </Kategorie>
<Kategorie>Auslandsbesuche </Kategorie>
</Kategorien>
and I read it with
p=jpath.path(sys.argv[0])
p=p.splitpath()[0]
p=p.joinpath("kategorien.xml")
from elementtree import ElementTree
t=ElementTree.fromstring(p.bytes())
kategorien=[k.text for k in t.getiterator(tag="Kategorie")]
after that kategorien contains a standard Python list of the categories
defined in kategorien.xml
jpath is a renamed Path module written by Jason Jarondorff
20040113HAM