loading xml file?

This is the location for all mods to collaborate. Anyone making or planning their own mod should post help requests, screen shots and news here.

Moderators: Omega, tillias, Mod Contributor

Post Reply
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

loading xml file?

Post 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?
denster83
Star Pilot
Star Pilot
Posts: 4
Joined: Wed Nov 28, 2007 3:25 am

Re: loading xml file?

Post 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.
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

Re: loading xml file?

Post by dagg »

yes and no, I use elementtree
I was offered to use packages but I'm not sure it will work
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

Re: loading xml file?

Post 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
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

Re: loading xml file?

Post by dagg »

solved it using os.path.abspath()
ace123
Lead Network Developer
Lead Network Developer
Posts: 2560
Joined: Sun Jan 12, 2003 9:13 am
Location: Palo Alto CA
Contact:

Re: loading xml file?

Post 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.
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

Re: loading xml file?

Post 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...
ace123
Lead Network Developer
Lead Network Developer
Posts: 2560
Joined: Sun Jan 12, 2003 9:13 am
Location: Palo Alto CA
Contact:

Re: loading xml file?

Post 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.
dagg
Bounty Hunter
Bounty Hunter
Posts: 138
Joined: Thu May 22, 2008 8:53 am

Re: loading xml file?

Post 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
Post Reply