# Name:		ftpPyDemo.py
# Author:	Dave Renner	dmrenne@comcast.net
# Date:		08/30/11q		1200
# Rev:		01/09/112		0315
# Note:		this is a called module of 'pyDemo.py'
#
# Note:		'ftpPyDemo.ftp' demos ftp client processing to a real website, 
#			some tricky print formatting, and OS system calls 
#
#
import os
from ftplib import FTP

# ############################ BOILERPLATE ##########################
import pickle

myConfigFile = 'pyDemoConfig.dat'

# 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'
else:
    import ConfigParser
    config = ConfigParser.ConfigParser()
    input_cmd = 'raw_input'

config.read(myConfigFile)
interactive = config.get('Mode', 'interactive')
continue_cmd = config.get('Strings', 'continue_cmd')

# easier to just include this boilerplate function into every module 
# 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 )

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

user = ''
pw = ''
mySite = 'www.maklisco.com'
file_to_download = 'myPyTest.html'
file_to_upload_name = 'myPyTest.html'
file_now_here_name = 'myTiny.html'
file_now_here = open(file_now_here_name, 'wb')
file_to_upload = open(file_to_upload_name, 'rb')
del_cmd = 'remove ' + file_now_here_name
DEBUG1 = 1
myConfigFile = 'pyDemoConfig.dat'
#curdir = '.'
curdir = 'public_html'

printf("")
printf("")	
printf("Now we are in the 'ftpPyDemo.py' module.\
\\nWe have only gotten here because I have given\
\\nyou permission to access my website. In real life,\
\\nthere would be no permission problems with this module\
\\nbecause you would be using your own website.\
\\nNow for the fun stuff. The real stuff.\
\\nWe will connect to the Internet and transfer files to/from\
\\nmy website. We will use Python's 'ftp' module for this.")
if interactive == '1':
	printf("")
	full_cmd = input_cmd + '("' + continue_cmd + '")'
	eval(full_cmd)
	printf("")

printf("Here's the code:\
\\nOK, I had to cheat a little bit with the backslashes\
\\nand quotes and stuff to make it easily printable.\
\\nThat's why you should NEVER consider commented-out\
\\nor textbook code as Gospel. ALWAYS look at the real code\
\\nif you can.\
\\n'nuff said. Right, then.\
\\n\
\\nconn = FTP(mySite)\
\\nconn.login( user, pw )\
\\nconn.cwd(curdir)\
\\nprintf('')\
\\nprintf('Let us list what's on the website first:')\
\\nprintf('')\
\\nconn.retrlines('LIST')\
\\nprintf('')\
\\nprintf('Now, to upload a file...')")
if interactive == '1':
	printf("")
	full_cmd = input_cmd + '("' + continue_cmd + '")'
	eval(full_cmd)
	printf("")

printf("nprintf('')\
\\nconn.storbinary('STOR ' + file_to_upload_name, file_to_upload)\
\\nprintf('Dun.')\
\\nprintf('')\
\\nconn.retrbinary('RETR ' + file_to_download, file_now_here.write)\
\\nconn.quit")

printf("")
printf("")
if interactive == '1':
	printf("")
	full_cmd = input_cmd + '("' + continue_cmd + '")'
	eval(full_cmd)	
	printf("")
	
conn = FTP(mySite)
conn.login( user, pw )
#conn.login()
conn.cwd(curdir)
printf("")
printf("Let's list what's on the website first:")
printf("")
conn.retrlines('LIST')
printf("")
printf("Now, to upload a file...")
printf("")
conn.storbinary('STOR ' + file_to_upload_name, file_to_upload)
printf('Dun. Uploaded my.html')
printf("")
conn.retrbinary('RETR ' + file_to_download, file_now_here.write)
conn.quit

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

printf("We have just downloaded the file 'myPyTest.html'\
\\nfrom my website, 'www.maklisco.com',\
\\nsubdirectory 'public_html'.\
\\n Then we've named it to 'myTiny.html'.\
\\nIf you don't believe me, check your current directory\
\\nfor 'myTiny.html'.\
\\n(You'll probably want to erase it afterwards.) \
\\nThen log on to 'www.maklisco.com'\
\\nto see the file 'myPyTest.html'.")

file_now_here.close()
#os.system(del_cmd)

printf("")
printf("Let's take a break from ftp, and show how to use\
\\nthe operating system itself to do something outside of Python.\
\\nWe'll just have the OS print out text to the screen.\
\\nHere's the syntax of the command:\
\\n    os.system(echo 'your text here')")
printf("")
os.system('echo "You tiny-brained food-trough whappers!"')
