"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.5/xhtml2pdf/wsgi.py" (25 Sep 2020, 2868 Bytes) of package /linux/www/xhtml2pdf-0.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "wsgi.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 
    3 # Copyright 2010 Dirk Holtwick, holtwick.it
    4 #
    5 # Licensed under the Apache License, Version 2.0 (the "License");
    6 # you may not use this file except in compliance with the License.
    7 # You may obtain a copy of the License at
    8 #
    9 #     http://www.apache.org/licenses/LICENSE-2.0
   10 #
   11 # Unless required by applicable law or agreed to in writing, software
   12 # distributed under the License is distributed on an "AS IS" BASIS,
   13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14 # See the License for the specific language governing permissions and
   15 # limitations under the License.
   16 
   17 import xhtml2pdf.pisa as pisa
   18 from six import StringIO
   19 
   20 import logging
   21 
   22 
   23 log = logging.getLogger("xhtml2pdf.wsgi")
   24 
   25 
   26 class Filter(object):
   27     def __init__(self, app):
   28         self.app = app
   29 
   30     def __call__(self, environ, start_response):
   31         script_name = environ.get('SCRIPT_NAME', '')
   32         path_info = environ.get('PATH_INFO', '')
   33         sent = []
   34         written_response = StringIO.StringIO()
   35 
   36         def replacement_start_response(status, headers, exc_info=None):
   37             if not self.should_filter(status, headers):
   38                 return start_response(status, headers, exc_info)
   39             else:
   40                 sent[:] = [status, headers, exc_info]
   41                 return written_response.write
   42 
   43         app_iter = self.app(environ, replacement_start_response)
   44         if not sent:
   45             return app_iter
   46         status, headers, exc_info = sent
   47         try:
   48             for chunk in app_iter:
   49                 written_response.write(chunk)
   50         finally:
   51             if hasattr(app_iter, 'close'):
   52                 app_iter.close()
   53         body = written_response.getvalue()
   54         status, headers, body = self.filter(
   55             script_name, path_info, environ, status, headers, body)
   56         start_response(status, headers, exc_info)
   57         return [body]
   58 
   59     def should_filter(self, status, headers):
   60         print (headers)
   61 
   62     def filter(self, status, headers, body):
   63         raise NotImplementedError
   64 
   65 
   66 class HTMLFilter(Filter):
   67     def should_filter(self, status, headers):
   68         if not status.startswith('200'):
   69             return False
   70         for name, value in headers:
   71             if name.lower() == 'content-type':
   72                 return value.startswith('text/html')
   73         return False
   74 
   75 
   76 class PisaMiddleware(HTMLFilter):
   77     def filter(self, script_name, path_info, environ, status, headers, body):
   78         topdf = environ.get("pisa.topdf", "")
   79         if topdf:
   80             dst = StringIO.StringIO()
   81             pisa.CreatePDF(body, dst, show_error_as_pdf=True)
   82             headers = [
   83                 ("content-type", "application/pdf"),
   84                 ("content-disposition", "attachment; filename=" + topdf)
   85             ]
   86             body = dst.getvalue()
   87         return status, headers, body