home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2059 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.5 KB  |  74 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. try:
  6.     from cStringIO import StringIO
  7. except ImportError:
  8.     from StringIO import StringIO
  9.  
  10. import mimetools
  11. import os
  12. import socket
  13. import urllib
  14. from urllib2 import BaseHandler, URLError
  15.  
  16. class FileHandler(BaseHandler):
  17.     
  18.     def file_open(self, req):
  19.         url = req.get_selector()
  20.         if url[:2] == '//' and url[2:3] != '/':
  21.             req.type = 'ftp'
  22.             return self.parent.open(req)
  23.         return self.open_local_file(req)
  24.  
  25.     names = None
  26.     
  27.     def get_names(self):
  28.         if FileHandler.names is None:
  29.             
  30.             try:
  31.                 FileHandler.names = (socket.gethostbyname('localhost'), socket.gethostbyname(socket.gethostname()))
  32.             except socket.gaierror:
  33.                 FileHandler.names = (socket.gethostbyname('localhost'),)
  34.             except:
  35.                 None<EXCEPTION MATCH>socket.gaierror
  36.             
  37.  
  38.         None<EXCEPTION MATCH>socket.gaierror
  39.         return FileHandler.names
  40.  
  41.     
  42.     def open_local_file(self, req):
  43.         
  44.         try:
  45.             import email.utils as emailutils
  46.         except ImportError:
  47.             import email.Utils as emailutils
  48.  
  49.         import mimetypes
  50.         host = req.get_host()
  51.         file = req.get_selector()
  52.         localfile = urllib.url2pathname(file)
  53.         
  54.         try:
  55.             stats = os.stat(localfile)
  56.             size = stats.st_size
  57.             modified = emailutils.formatdate(stats.st_mtime, usegmt = True)
  58.             mtype = mimetypes.guess_type(file)[0]
  59.             if not mtype:
  60.                 pass
  61.             headers = mimetools.Message(StringIO('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' % ('text/plain', size, modified)))
  62.             if host:
  63.                 (host, port) = urllib.splitport(host)
  64.             
  65.             if (not host or not port) and socket.gethostbyname(host) in self.get_names():
  66.                 return urllib.addinfourl(open(localfile, 'rb'), headers, 'file:' + file)
  67.         except OSError:
  68.             msg = None
  69.             raise URLError(msg)
  70.  
  71.         raise URLError('file not on local host')
  72.  
  73.  
  74.