Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
xpcom.file Namespace Reference

Classes

class  _File
class  URIFile
class  LocalFile

Functions

def LocalFileToURL
def _DoTestRead
 Test Code.
def _DoTestBufferRead
def _TestLocalFile
def _TestAll
def _TestURI

Variables

int NS_RDONLY = 0x01
int NS_WRONLY = 0x02
int NS_RDWR = 0x04
int NS_CREATE_FILE = 0x08
int NS_APPEND = 0x10
int NS_TRUNCATE = 0x20
int NS_SYNC = 0x40
int NS_EXCL = 0x80

Function Documentation

def xpcom.file._DoTestBufferRead (   file,
  expected 
) [private]

Definition at line 230 of file file.py.

00230 
00231 def _DoTestBufferRead(file, expected):
00232     # read in a couple of chunks, just to test that our various arg combinations work.
00233     buffer = _xpcom.AllocateBuffer(50)
00234     got = ''
00235     while 1:
00236         # Note - we need to reach into the file object so we
00237         # can get at the native buffer supported function.
00238         num = file.inputStream.read(buffer)
00239         if num == 0:
00240             break
00241         got = got + str(buffer[:num])
00242     if got != expected:
00243         raise RuntimeError, "Reading '%s' failed - got %d bytes, but expected %d bytes" % (file, len(got), len(expected))

Here is the caller graph for this function:

def xpcom.file._DoTestRead (   file,
  expected 
) [private]

Test Code.

Definition at line 221 of file file.py.

00221 
00222 def _DoTestRead(file, expected):
00223     # read in a couple of chunks, just to test that our various arg combinations work.
00224     got = file.read(3)
00225     got = got + file.read(300)
00226     got = got + file.read(0)
00227     got = got + file.read()
00228     if got != expected:
00229         raise RuntimeError, "Reading '%s' failed - got %d bytes, but expected %d bytes" % (file, len(got), len(expected))

Here is the caller graph for this function:

def xpcom.file._TestAll ( ) [private]

Definition at line 292 of file file.py.

00292 
00293 def _TestAll():
00294     # A mini test suite.
00295     # Get a test file, and convert it to a file:// URI.
00296     # check what we read is the same as when
00297     # we read this file "normally"
00298     fname = components.__file__
00299     if fname[-1] in "cCoO": # fix .pyc/.pyo
00300             fname = fname[:-1]
00301     expected = open(fname, "rb").read()
00302     # convert the fname to a URI.
00303     url = LocalFileToURL(fname)
00304     # First try passing a URL as a string.
00305     _DoTestRead( URIFile( url.spec), expected)
00306     print "Open as string test worked."
00307     # Now with a URL object.
00308     _DoTestRead( URIFile( url ), expected)
00309     print "Open as URL test worked."
00310 
00311     _DoTestBufferRead( URIFile( url ), expected)
00312     print "File test using buffers worked."
00313 
00314     # For the sake of testing, do our pointless, demo object!
00315     _DoTestRead( LocalFile(fname), expected )
00316     print "Local file read test worked."
00317 
00318     # Now do the full test of our pointless, demo object!
00319     _TestLocalFile()

Here is the call graph for this function:

Here is the caller graph for this function:

def xpcom.file._TestLocalFile ( ) [private]

Definition at line 244 of file file.py.

00244 
00245 def _TestLocalFile():
00246     import tempfile, os
00247     fname = tempfile.mktemp()
00248     data = "Hello from Python"
00249     test_file = LocalFile(fname, "w")
00250     try:
00251         test_file.write(data)
00252         test_file.close()
00253         # Make sure Python can read it OK.
00254         f = open(fname, "r")
00255         if f.read() != data:
00256                 print "Eeek - Python could not read the data back correctly!"
00257         f.close()
00258         # For the sake of the test, try a re-init.
00259         test_file.init(fname, "r")
00260         got = str(test_file.read())
00261         if got != data:
00262             print "Read the wrong data back - %r" % (got,)
00263         else:
00264             print "Read the correct data."
00265         test_file.close()
00266         # Try reading in chunks.
00267         test_file = LocalFile(fname, "r")
00268         got = test_file.read(10) + test_file.read()
00269         if got != data:
00270             print "Chunks the wrong data back - %r" % (got,)
00271         else:
00272             print "Chunks read the correct data."
00273         test_file.close()
00274         # Open the same file again for writing - this should delete the old one.
00275         if not os.path.isfile(fname):
00276             raise RuntimeError, "The file '%s' does not exist, but we are explicitly testing create semantics when it does" % (fname,)
00277         test_file = LocalFile(fname, "w")
00278         test_file.write(data)
00279         test_file.close()
00280         # Make sure Python can read it OK.
00281         f = open(fname, "r")
00282         if f.read() != data:
00283                 print "Eeek - Python could not read the data back correctly after recreating an existing file!"
00284         f.close()
00285 
00286         # XXX - todo - test "a" mode!
00287     finally:
00288         try:
00289             os.unlink(fname)
00290         except OSError, details:
00291             print "Error removing temp test file:", details

Here is the caller graph for this function:

def xpcom.file._TestURI (   url) [private]

Definition at line 320 of file file.py.

00320 
00321 def _TestURI(url):
00322     test_file = URIFile(url)
00323     print "Opened file is", test_file
00324     got = test_file.read()
00325     print "Read %d bytes of data from %r" % (len(got), url)
00326     test_file.close()

Here is the call graph for this function:

def xpcom.file.LocalFileToURL (   localFileName)

Definition at line 78 of file file.py.

00078 
00079 def LocalFileToURL(localFileName):
00080     "Convert a filename to an XPCOM nsIFileURL object."
00081     # Create an nsILocalFile
00082     localFile = components.classes["@mozilla.org/file/local;1"] \
00083           .createInstance(components.interfaces.nsILocalFile)
00084     localFile.initWithPath(localFileName)
00085 
00086     # Use the IO Service to create the interface, then QI for a FileURL
00087     io_service = components.classes["@mozilla.org/network/io-service;1"] \
00088                     .getService(components.interfaces.nsIIOService)
00089     url = io_service.newFileURI(localFile).queryInterface(components.interfaces.nsIFileURL)
00090     # Setting the "file" attribute causes initialization...
00091     url.file = localFile
00092     return url
00093 
# A base class for file objects.

Here is the caller graph for this function:


Variable Documentation

Definition at line 72 of file file.py.

Definition at line 71 of file file.py.

Definition at line 75 of file file.py.

Definition at line 68 of file file.py.

Definition at line 70 of file file.py.

Definition at line 74 of file file.py.

Definition at line 73 of file file.py.

Definition at line 69 of file file.py.