cryptonas-branches/pythonrewrite/bin2/filehandling.py
lars 84028f4a92 added python version checks
added startup test to CryptoBox
integrate CryptoBoxRootActions into CryptoBox (see example-super.tab for details)
implemented "mount" and "umount" in CryptoBoxRootActions
adapted CryptoBoxRootActions checks to 'super'
2006-08-23 13:27:25 +00:00

71 lines
1.6 KiB
Python
Executable file

import os,tempfile,dircache,string,commands
"""I suppose this stuff should be inside some class?"""
# RFC: which of these methods do we need more than once?
# or: these actions are too short for methods - or not? [l]
def read_file(filename):
"""
read from the file whose name is given
@param filename String : name of file to read from
@return String: content of given file or None
"""
try:
f = open(filename,"r")
filecontent = f.read()
f.close()
except:
#TODO:if debug >=1:
print "(EE)[%s]: \"%s\" is not readable!"%(__name__, filename)
return ""
return filecontent
def is_dir_readable(path):
"""
Gets the name of a directory.
returns True if dir is readable, else False.
"""
return os.access(path,os.R_OK)
def write_file(filename,content):
"""
Write content to the given filename.
gets: filename,content.
"""
try:
f = open(filename,"w")#oeffnen und schliessen =>
f.close() #datei ist jetzt genullt
f = open(filename,"a") #anhaengend oeffnen
f.write(content)
f.close()
return ""
except:
#TODO: debug
#if self.debug >=1:
print "(EE)[%s]: \"%s\" is not writeable!"%(__name__, filename)
return filename
def basename(filename):
return os.path.basename(filename)
def gen_temp_dir(prefix='cryptobox--'):
'''returns the name of a secure temporary directory
with optional prefix as parameter'''
dirname=tempfile.mkdtemp("",prefix)
dirname+="/"
return dirname
def gen_temp_file(suffix="--cryptobox"):
"""
returns the name of a generated temporay file.
optionally gets a suffix for the filename.
"""
return tempfile.mkstemp(suffix)[1]