Page 1 of 1

loading xml file?

Posted: Thu Feb 26, 2009 10:48 pm
by dagg
some py scripts in the modules are loading xml files from the main data directory, I'm trying to do the same, the file I need to load resides in the units directory, how can I open it from my script?

Re: loading xml file?

Posted: Fri Feb 27, 2009 12:31 am
by denster83
Did you take a look at this already?
http://vegastrike.sourceforge.net/media ... ta_Modding

The VsWiki usually answers a lot of my scripting questions.
I personally don't know much about scripting, but from what I can gather from your question...
I assume you're talking about,

fp = open ('factions.xml','w')

I assume something similar would be used to load an xml in the units directory, substituting 'w' with 'units'.
But I'm not sure. Like I said, I don't know much about scripting...and this is out of my league.
Check the VsWiki...it can help you more then I can.

Re: loading xml file?

Posted: Fri Feb 27, 2009 7:05 am
by dagg
yes and no, I use elementtree
I was offered to use packages but I'm not sure it will work

Re: loading xml file?

Posted: Fri Feb 27, 2009 4:27 pm
by dagg
denster83 wrote:Did you take a look at this already?
http://vegastrike.sourceforge.net/media ... ta_Modding

The VsWiki usually answers a lot of my scripting questions.
I personally don't know much about scripting, but from what I can gather from your question...
I assume you're talking about,

fp = open ('factions.xml','w')
soory
I assume something similar would be used to load an xml in the units directory, substituting 'w' with 'units'.
But I'm not sure. Like I said, I don't know much about scripting...and this is out of my league.
Check the VsWiki...it can help you more then I can.
the w is for writing permissions.... not for folder

Re: loading xml file?

Posted: Fri Feb 27, 2009 5:10 pm
by dagg
solved it using os.path.abspath()

Re: loading xml file?

Posted: Sat Feb 28, 2009 2:29 pm
by ace123
yikes--not sure how to handle opening files from python. The problem with open('somefile.xml', 'r') is that it will usually be the root of the tree, but is not guaranteed to be if the current directory has changed.

How exactly are you using os.path.abspath()? Just curious if that way is portable.

Re: loading xml file?

Posted: Sat Feb 28, 2009 3:46 pm
by dagg
not had the chance to test it within the game yet but this is what I've made:

Code: Select all

currPath=os.path.abspath(os.path.curdir).split(os.sep)
if currPath[len(currPath)-1]=='modules':
currPath[len(currPath)-1]='units'
this is based on the assumption that when calling the module, we are at the modules folder, I may be wrong in that assumption...

Re: loading xml file?

Posted: Sun Mar 01, 2009 11:11 pm
by ace123
No, that assumption is correct: The modules folder for both the mod and the main data directory is always added to the sys.path.
So I guess you've just come up with the official solution on how to open files from inside python. :-p

That sort of work should really be done in the engine, but it's not exactly fun to add a new function to the python API, and I'm never exactly sure what vsfilesystem is doing. A simple rule like "one directory above the sys.path" is much easier to depend upon.

Re: loading xml file?

Posted: Mon Mar 02, 2009 7:38 am
by dagg
I've went on and posted a path print in one of the modules that is known to be working in the game, the module resides in the modules sub folder, the output was the main tree.
I've modified it to this:

Code: Select all

relPath=os.sep + 'units' +os.sep