FileReader.py :  » Network » Grail-Internet-Browser » grail-0.6 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Grail Internet Browser 
Grail Internet Browser » grail 0.6 » FileReader.py
"""File reader class -- read from a URL to a file in the background."""

from BaseReader import BaseReader

class FileReader(BaseReader):

    """File reader class -- read from a URL to a file in the background.

    Derived classes are supposed to override handle_error() and
    handle_done() to specify what should happen next, and possibly
    handle_meta() to decide whether to continue based on the data
    type.

    The methods handle_data() and handle_eof() are implemented at this
    level and should normally be left alone (or extended, not
    overridden).

    Class or instance variable filemode may be set to override the
    file writing mode (default 'wb' -- make sure it's a writing
    mode!).

    """

    filemode = "wb"

    def __init__(self, context, api, filename):
        self.filename = filename
        self.fp = None
        BaseReader.__init__(self, context, api)

    def handle_data(self, data):
        try:
            if self.fp is None:
                self.fp = self.open_file()
            self.fp.write(data)
        except IOError, msg:
            self.stop()
            self.handle_error(-1, "IOError", {'detail': msg})
            return
 
    def open_file(self):
        return open(self.filename, "wb")

    def handle_eof(self):
        if self.fp:
            self.fp.close()
        self.handle_done()

    def handle_done(self):
        pass


class TempFileReader(FileReader):

    """Derived class of FileReader that chooses a temporary file.

    This also supports inserting a filtering pipeline.
    """

    def __init__(self, context, api):
        self.pipeline = None
        import tempfile
        filename = tempfile.mktemp()
        FileReader.__init__(self, context, api, filename)

    def set_pipeline(self, pipeline):
        """New method to select the filter pipeline."""
        self.pipeline = pipeline

    def getfilename(self):
        """New method to return the file name chosen."""
        return self.filename

    def open_file(self):
        if not self.pipeline:
            return FileReader.open_file(self)
        else:
            import os, sys
            if not hasattr(os, 'popen'):
                raise IOError, "pipelines not supported"
            try:
                return os.popen(self.pipeline + ">" + self.filename, "wb")
            except os.error, msg:
                raise IOError, msg, sys.exc_traceback
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.