Confusion About Classes

flamesrock flamesrock at gmail.com
Sat Jan 1 20:24:39 EST 2005


Well, I took a short excursion into xml for a different part of the
program, but the class is now finished!! (source below) I have a few
more questions for you guys.

1)If I set
self.serverConfig = file(os.path.join('configuration','score.conf'),
'w')
and then go self.serverConfig.close(), is there a way to open it in
read mode without calling the filename directly? Like
self.serverConfig.open('r') -which doesn't work BTW- instead of
open(os.path.join('configuration','score.conf'), 'r')? Using
open(self.ServerConfig, 'r') doesn't seem to work either.

2) A problem arose when converting the encoded file back into a string
for decoding; the \n's seemed to have dissapeared so it returned an
error when I tried to decode. Is there a way to stop python from
disposing of those those characters before writing to file?

3) Is there way to retrieve the timestamp of a file via ftp?

>>There are NAMES, thats why there are NAMESPACES.
>>A name is just a reference or 'handle' to an object, because.....
>>everything in python is an OBJECT,(except for names, they are just
>>names).
And
>>That being said, understanding this part of Python can aid enormously
in
>>working with the language. If you'd like to think of it in C terms,
>>basically every 'name' in Python is a pointer to a PyObject, and
'names'
>>(as pointers) are always passed by value -- that is, you get a copy
of
>>the pointer, but not a copy of the object.

Ahhhhh...now *that* makes sense. So dynamically typed variables aren't
variables at all...but just another term for pointers? cool, I like
that. And thanks for the examples, guys. Its coming together and I'm
starting to actually 'get it' :)

So name mangling basically follows the rules of private and public in
Java? (I have limited experience in java so don't quote me on that.)

(Google groups isn't displaying the spaces correctly, so I added four
*'s = 1 four space indentation..)
import ftplib, ezPyCrypto, ConfigParser
import traceback, sys, os
#-------------------------------------------------------------------------------
class create_server:
****'''Create a create_server object whenever a new server is to be
added to
****the SCORE network.'''
****def __init__(self, absCFGpath, domain, servername, master_ftpUSER,
****master_ftpPASS, score_ftpUSER='anonymous',
score_ftpPASS='anonymous',
****scorecontact='admin at simcitysphere.com', maxusers='unlimited',
httpport='80',
****ftpport='21', passivemode=True):
********self.domain             = domain
********self.httpport           = httpport
********self.ftpport            = ftpport
********self.servername         = servername
********self.maxusers           = maxusers
********self.scorecontact       = scorecontact
********try:
************self.serverConfig   =
file(os.path.join('configuration','score.conf'), 'w')
********except:
************os.mkdir('configuration')
************self.serverConfig   =
file(os.path.join('configuration','score.conf'), 'w')
********self.absCFGpath        = absCFGpath
********self.master_ftpUSER    = master_ftpUSER
********self.master_ftpPASS    = master_ftpPASS
********self.score_ftpUSER     = score_ftpUSER
********self.score_ftpPASS     = score_ftpPASS
********self.passivemode       = passivemode
********self.parser            = ConfigParser.ConfigParser()

#-------------------------------------------------------------------------------
****def createUniversalConfig(self):
********'''Creates the SCORE servers main configuration file.'''
********self.parser.add_section('score')
********self.parser.set('score', 'domain', self.domain)
********self.parser.set('score', 'servername', self.servername)
********self.parser.set('score', 'httpport', self.httpport)
********self.parser.set('score', 'ftpport', self.ftpport)
********self.parser.set('score', 'maxusers', self.maxusers)
********self.parser.set('score', 'scorecontact', self.scorecontact)
********self.parser.write(self.serverConfig)
********self.serverConfig.close()
********return open(os.path.join('configuration','score.conf'), 'r')
#-------------------------------------------------------------------------------
****def editUniversalConfig(self, section, key, value):
********'''edit the SCORE servers main configuration file.'''
********self.serverConfig =
open(os.path.join('configuration','score.conf'), 'w')
********self.parser.set(section, key, value)
********self.serverConfig.close
#-------------------------------------------------------------------------------
****def createUSERfile(self):
********'''Creates an encrypted ascii public ftp password file that
clients use
********to decode after logging in'''
********crypt = ezPyCrypto.key()
********userFile = file(os.path.join('configuration','uFile.enc'), 'w')
********userFile.write(crypt.encStringToAscii(self.score_ftpUSER))
********userFile.close()
********userFile = open(os.path.join('configuration','uFile.enc'), 'r')
********return userFile
#-------------------------------------------------------------------------------
****def createPASSfile(self):
********'''Creates an encrypted ascii public ftp password file that
clients use
********to decode after logging in'''
********crypt = ezPyCrypto.key()
********passFile = file(os.path.join('configuration','pFile.enc'), 'w')
********passFile.write(crypt.encStringToAscii(self.score_ftpPASS))
********passFile.close()
********passFile = open(os.path.join('configuration','pFile.enc'), 'r')
********return passFile
#-------------------------------------------------------------------------------
****def uploadConfigs(self):
********'''Uploads the universal config/ftpuser/pass files to the
correct directories on the site'''
********##print "Files:", sys.argv[1:] relic..

********print "Logging in..."
********ftp = ftplib.FTP()
********ftp.set_pasv(self.passivemode) #true if passive, false if
active
********ftp.connect(self.domain, self.ftpport)
********print ftp.getwelcome()
********try:
************try:
****************ftp.login(self.master_ftpUSER, self.master_ftpPASS)
****************ftp.cwd(self.absCFGpath) #*the SCORE root directory
****************print "Currently in:", ftp.pwd()
****************print "Uploading..."
****************sConfig = self.createUniversalConfig()
****************uFile   = self.createUSERfile()
****************pFile   = self.createPASSfile()
****************ftp.storlines('STOR score.conf', sConfig)
****************try:
********************ftp.cwd(self.absCFGpath + '/files')
****************except ftplib.error_perm: #*if dir doesn't exist
********************ftp.mkd('files')
********************ftp.cwd(self.absCFGpath + '/files') #change to
config directory
****************ftp.storlines('STOR uFile.enc', uFile)
****************ftp.storlines('STOR pFile.enc', pFile)
****************sConfig.close()
****************uFile.close()
****************pFile.close()
****************print "OK"

****************print "Files:"
****************print ftp.retrlines('LIST')
************finally:
****************print "Quitting..."
************ftp.quit()
********except:
************traceback.print_exc()
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
-thanks,
flamesrock




More information about the Python-list mailing list