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

loading xml file?

Postby dagg » Thu Feb 26, 2009 3:48 pm

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?
dagg
Bounty Hunter
Bounty Hunter
 
Posts: 138
Topics: 26
Joined: Thu May 22, 2008 1:53 am

Share On:

Share on Facebook Facebook Share on Twitter Twitter Share on Digg Digg

Re: loading xml file?

Postby denster83 » Thu Feb 26, 2009 5:31 pm

Did you take a look at this already?
http://vegastrike.sourceforge.net/mediawiki/index.php?title=Development#Data_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.
denster83
Star Pilot
Star Pilot
 
Posts: 4
Topics: 1
Joined: Tue Nov 27, 2007 8:25 pm

Re: loading xml file?

Postby dagg » Fri Feb 27, 2009 12:05 am

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
Topics: 26
Joined: Thu May 22, 2008 1:53 am

Re: loading xml file?

Postby dagg » Fri Feb 27, 2009 9:27 am

denster83 wrote:Did you take a look at this already?
http://vegastrike.sourceforge.net/mediawiki/index.php?title=Development#Data_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
Topics: 26
Joined: Thu May 22, 2008 1:53 am

Re: loading xml file?

Postby dagg » Fri Feb 27, 2009 10:10 am

solved it using os.path.abspath()
dagg
Bounty Hunter
Bounty Hunter
 
Posts: 138
Topics: 26
Joined: Thu May 22, 2008 1:53 am

Re: loading xml file?

Postby ace123 » Sat Feb 28, 2009 7:29 am

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

Re: loading xml file?

Postby dagg » Sat Feb 28, 2009 8:46 am

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

Re: loading xml file?

Postby ace123 » Sun Mar 01, 2009 4:11 pm

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

Re: loading xml file?

Postby dagg » Mon Mar 02, 2009 12:38 am

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



Return to General Modding

Who is online

Users browsing this forum: No registered users and 3 guests

cron