# Name:		CIntegrate.py
# Author:	Dave Renner		dmrenne@comcast.net	
# Date:		09/01/11	1145
# Rev:		01/09/13	0315
# Note:		this is a called module of 'pyDemo.py'
#
# Note:		'CIntegrate.py' demos how to call a compiled
#			'C' object to do work, in this case, to print a phrase.
#			Usually, a called C module will be used for some
#			fast number-crunching or time-sensitive job
#
#
# ########################### BOILERPLATE ##############################
import pickle

# version has been stored in our calling module 'pyDemo.py'
PythonVersFile = 'version.obj'
versfile = open(PythonVersFile, 'rb')
PythonVers = pickle.load(versfile)
versfile.close()

if PythonVers == '30':
    import configparser
    config = configparser.ConfigParser()
    input_cmd = 'input'
    import C30PyIntegrate
else:
    import ConfigParser
    config = ConfigParser.ConfigParser()
    input_cmd = 'raw_input'
    import C26PyIntegrate

DEBUG = 'True'
myConfigFile = 'pyDemoConfig.dat'
config.read(myConfigFile)
interactive = config.get('Mode', 'interactive')
continue_cmd = config.get('Strings', 'continue_cmd')

# easier just to paste this boilerplate function into all our modules
# than to import it
def printf(arg):
    cmdText = "print "
    arg_nonSlash = arg.strip('\\')
    if PythonVers =='30':
        cmdFull = cmdText + '("' + arg_nonSlash + '")'
    else:
        cmdFull = cmdText + '"' + arg_nonSlash + '"'
    exec( cmdFull )

# ########################################################################

printf("IMPORTANT. You must have run 'setup.py'\
\\nbefore you continue with this program.\
\\nHave you run 'setup.py'?")
printf('\a')

if PythonVers == '30':
    resp = input('Enter Y for Yes, N for No: \n')
else:
    resp = raw_input('Enter Y for Yes, N for No: \n')

if resp == 'y':
    pass
elif resp == 'Y':
    pass
else:
    exit()
	
printf("")
printf("")
printf("We are in 'CIntegrate.py', and will call function\
\\n'insult_the_English', located in the C-module 'C26PyIntegrate',\
\\nor 'C30PyIntegrate'\
\\nwhich has been compiled as a shared C object.\
\\n\
\\n'insult the English' will accept the parm 'French',\
\\nconcatenate it to the phrase 'We are', and return\
\\nthe full phrase back to the Python caller.\
\\n\
\\nNot too complicated, but it does show that we can call\
\\na 'C' program from Python, which can be very useful\
\\nif time-sensitive work has to be done.\
\\n\
\\n\
\\nBTW, I'm currently working on Windows.\
\\nNot perhaps the most-tested Python platform.")

if interactive == '1' == '1':
	printf("")
	full_cmd = input_cmd + '("' + continue_cmd + '")'
	eval(full_cmd)
	printf("")

printf("To create the shared library object, I didn't use\
\\nGNU or MS Visual C++.\
\\nI tried GNU 'make', but it didn't work.\
\\nIt said it compiled, but Python couldn't use it somehow.\
\\nSo I used Python's 'distutils' package, and it\
\\nworked fine, without a hitch.\
\\n\
\\nI suspect that creating an shared object works better on Linux.\
\\n\
\\nIn any event, all you need to do is to code up a file\
\\n called 'setup.py' in the format prescribed\
\\nin a number of textbooks. Take a look at mine here;\
\\nit's in your current directory.\
\\nYou'll see that it actually compiles two 'C' objects,\
\\none for Python 3.0, another for 2.6.\
\\nYou'll only need one of them, obviously,\
\\nbut it would be difficult and cumbersome to try to make a\
\\none-size-fits-all for this.\
\\n\
\\n\
\\nOK, enough of this silliness. Back to the program.\
")

if PythonVers == '30':
    phrase = C30PyIntegrate.insult_the_English('French')
else:
    phrase = C26PyIntegrate.insult_the_English('French')

printf("")
printf("We have returned from the C program\
\\nand will display its value. Here it is:")

printf( phrase )
