[PYTHONCOMP] Python utility library (includes INI file reading)

TheLopez said:
Following your example I tested your code and it seems to work.

Thanks. I've uploaded a new version of the Zip file. This contains the fix to the exception handling for missing INI file options and the updates for limited discovery of the active mod name.

I have a few ideas for how to improve the mod name discovery, but they may turn out to be too complicated to be worthwhile. Really, although I don't like this approach because it basically boils down to "I don't know, you tell me the name," the truth is it's probably adequate for any real situation.
 
Dr Elmer Jiggle said:
Really, although I don't like this approach because it basically boils down to "I don't know, you tell me the name," the truth is it's probably adequate for any real situation.

I agree that this isn't the optimal solution but at least it lets mod makers get the active mod name programatically now.
 
Today I used that INI Parser the first time and had the problem, that the INI file I created has not been found in my user directory. After some investigation I found out, that the reason is my non-default installation directory.
Usually the directory name of the installation is "Sid Meier's Civilization 4". During the installation I changed that to "Civilization 4". There is no problem with the install directory, because the name is saved in the regisitry and is read out by your function _getInstallDir(). But the problem is, that the user directory in "My Documents" is also renamed into "Civilization 4" and this is not handeled by the _getUserDir() function.
I did made a small change in the _getUserDir() function and attached it. The change is marked with "#12monkeys begin/end". Maybe you want to take it over into your version.
However, the rest is working great and I will start to use it for my other mods as well. Good Job :goodjob:.
 
:clap: :clap: And we have another convert... :clap: :clap:
 
12monkeys said:
However, the rest is working great and I will start to use it for my other mods as well. Good Job :goodjob:.

Thanks. I'm going to fold your fix into the official version.

There was another bug in CvConfigParser.py that had to do with the directory search path too. I didn't think I had updated the attachment in this thread to fix that yet, but you seem to have the fix in your pathfinder mod. Did you find that fix on your own, or am I confused? Maybe you got the files from my Alerts mod? That has the latest code.
 
No, I took the code from the Mercenaries Mod from TheLopez, because he had the idea to implement the INI stuff (was the quickest path to it ;))
 
Merc Mod is/was using the latest and greatest version. Let me know if I need to update it with newer code.
 
Thanks again. I updated the attachment (also the one for my Alerts mod) to include this fix. FYI, I used os.path.basename instead of rfind to get the directory name, but otherwise it's pretty much the same as what you did.

TheLopez, you didn't before, but now you need to update. :)
 
Dr Elmer Jiggle said:
... FYI, I used os.path.basename instead of rfind to get the directory name, but otherwise it's pretty much the same as what you did. ...

I'm fine with that, of course. I don' have much experience with those standard python libs, so I somtimes invent the wheel again :)
 
Dr Elmer Jigger,

Before I begin, I want to give credit where credit is due, most of the code did come from Vadus's posting here: http://forums.civfanatics.com/showthread.php?p=3746502#post3746502.

Here is some new code that you might want to fold into your official release. Basically it allows players to define their own custom events and have them handled. The primary reason why I did these changes was to allow generic handling of popup code. Here are the changes:
Code:
class CvCustomEventManager(CvEventManager.CvEventManager, object):

	# < NEW CODE START >
	CustomEvents = {}
	# < NEW CODE END >
	...
	...

and

Code:
	# < NEW CODE START >        
	def beginEvent( self, context, argsList=-1 ):
		"Begin Event"
		if(self.CustomEvents.has_key(context)):
			return self.CustomEvents[context][2](argsList)
		else:
			return CvEventManager.CvEventManager.beginEvent(self, context, argsList)
			
	def applyEvent( self, argsList ):
		'Apply the effects of an event '
		context, playerID, netUserData, popupReturn = argsList
		
		if(self.CustomEvents.has_key(context)):
			entry = self.CustomEvents[context]
			# the apply function
			return entry[1]( playerID, netUserData, popupReturn )   
		else:
			return CvEventManager.CvEventManager.applyEvent(self, argsList)


	def addCustomEventDefinition(self, eventType, eventDefinition):
		self.CustomEvents[eventType] = eventDefinition

	def removeCustomEventDefinition(self, eventType):
		del self.CustomEvents[eventType]

	def setCustomEventDefinition(self, eventType, eventDefinition):
		self.CustomEvents[eventType] = eventDefinition
	# < NEW CODE END >

I have been testing the code all weekend and works for me. Here is an example of how the custom event definition is set up in one of my classes:
Code:
class CvGreatStatesmanEventManager:
	def __init__(self, eventManager):
		# initialize base class

		eventDefinition = ('DiplomaticsRelationsPopupEvent', self.__eventDiplomaticsRelationsPopupApply, self.__eventDiplomaticsRelationsPopupBegin)

		eventManager.addCustomEventDefinition(CvGreatStatesmanGameUtils.DIPLOMATIC_RELATIONS_POPUP, eventDefinition)

Again, I want to give credit where credit is due, most of the code did come from Vadus's posting here: http://forums.civfanatics.com/showthread.php?p=3746502#post3746502.
 
I'm needin' a bit of help, if someone would be so kind... I'm trying to figure out python by doing a simple little (actually overly complicated :D) project using Dr. Jiggle's event manager to instantiate it, however I keep getting a stupid "Failed to load python module CvEventInterface" error message. I'm pretty sure I just missed something simple and stupid, but can someone just point out to me what it is? ;)

thanks
j
 
J_Period said:
I'm needin' a bit of help, if someone would be so kind... I'm trying to figure out python by doing a simple little (actually overly complicated :D) project using Dr. Jiggle's event manager to instantiate it, however I keep getting a stupid "Failed to load python module CvEventInterface" error message. I'm pretty sure I just missed something simple and stupid, but can someone just point out to me what it is? ;)

thanks
j

Come on J, you know better than to just post something like this and not provide the source files or at least a code snippet for use to look at to help you at. Blah, blah, blah... sorry my mind wandered. Anyways, please post your files, or at least look into your log files and let us know what the error is. But from what you are describing you should post your CvEventInterface.py file for us to look at.
 
I get that error when I goofed something in any one of my python files. Check the python log file in the logs folder. It will tell you the exact file that errored out as well as what caused it and what line it happened on.

I have noticed that anything that loads from CvEventInterface.py and fails give you the error "Failed to load python module CvEventInterface". It can be 20 files later with the previous 19 loading fine, and that one line tabed over one extra tab will cause it all to fail. :(

Anyways, check the logs
 
Nexushyper said:
... and that one line tabed over one extra tab will cause it all to fail. :(

Programmers will understand the next statement...

Well that's what you get when you develop a language that depends on spacing in source files to define the scope.
 
Come on J, you know better than to just post something like this and not provide the source files or at least a code snippet for use to look at to help you at. Blah, blah, blah... sorry my mind wandered. Anyways, please post your files, or at least look into your log files and let us know what the error is. But from what you are describing you should post your CvEventInterface.py file for us to look at.

I know, but the reason I just posted that is that if I just put the event manager (unmodified) into its own mod folder and run that, it gets the same error, so I figured I had just done something stupid that everyone knew about :D

thanks for pointing out the logs though. Duh. Looking at them I think I can figure out whats wrong. If I have further difficulties, I'll post AND out up my logs, etc ;)

later guys

I figured it all out BTW...
 
I have followed this word for word and am using the latest download. I can not get my mod to read the ini file and load the options.

Code:
import CvConfigParser
config = CvConfigParser.CvConfigParser("Unit Naming Config.ini")
g_CN = config.getint("Unit Naming", "Custom Name", 2)

In Unit Naming Config.ini if I change Custom Name to = 1 it defaults to 2. I know because I did a print g_CN and the log showed 2 and also the code for option 2 executed.

Any ideas? I am using default install directories.
 
Hmmmm... did you create a CvModName.py file with your mods name in it?

I am assuming that your mod name is "Unit Naming" correct? then you would need a file named "CvModName.py" with:
modName = "Unit Naming"
 
Mods name is "Nexus Mod"

And I thought I had checked that file, but alas, I didn't update it (or I did but saved it to the wrong place). So I did just now. CvModName.py" with:
modName = "Nexus mod"

And it works.


See, always those little things that had I not posted and you not replied, I would be spending days trying to figure out!
 
Well I'm glad it worked :D
 
Hmm. My subscription to this thread seems to have died. I haven't received an email about any of about the last dozen posts. Guess I have a lot to look into, including a bunch of custom event definition code from TheLopez.
 
Top Bottom