示例#1
0
文件: pisa.py 项目: jjesquea/wms
def execute():    
#    from optparse import OptionParser
#
#    parser = OptionParser()
#    parser.add_option("-f", "--file", dest="filename",
#                      help="write report to FILE", metavar="FILE")
#    parser.add_option("-q", "--quiet",
#                      action="store_false", dest="verbose", default=True,
#                      help="don't print status messages to stdout")
#    (options, args) = parser.parse_args()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "dhqstwcxb", [
            "quiet",
            "help",
            "start-viewer",
            "start",
            "debug=",
            "copyright",
            "version",
            "warn",
            #"booklet=",
            #"multivalent=",
            #"multivalent-path=",            
            "tempdir=",
            "format=",
            "css=",
            "base=",
            "css-dump",
            "xml-dump",
            "xhtml",
            "xml",
            "html",
            "encoding=",
            "system",
            "profile",
            ])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    errors = 0
    startviewer = 0
    quiet = 0    
    debug = 0
    #multivalent_path = ""
    #booklet = ""   
    tempdir = None
    format = "pdf"
    css = None
    xhtml = None
    encoding = None
    xml_output = None
    base_dir = None
    
    log_level = logging.ERROR
    log_format = LOG_FORMAT

    for o, a in opts:
        
        if o in ("-h", "--help"):
            # Hilfe anzeigen
            usage()
            sys.exit()

        if o in ("-s", "--start-viewer", "--start"):
            # Anzeigeprogramm starten
            startviewer = 1

        if o in ("-q", "--quiet"):
            # Output unterdrücken
            quiet = 1

        if o in ("-w", "--warn"):
            # Warnings            
            log_level = min(log_level, logging.WARN) # If also -d ignore -w
            
        if o in ("-d", "--debug"):
            # Debug
            log_level = logging.DEBUG
            log_format = LOG_FORMAT_DEBUG
            # debug = 10
            if a:                
                log_level = int(a)

#        if o in ("--multivalent", "--multivalent-path"):
#            # Multivalent.jar für Booklet
#            multivalent_path = a

#        if o in ("--booklet",):
#            # Booklet
#            booklet = a

        if o in ("--copyright", "--version"):
            print COPYRIGHT
            sys.exit(0)

        if o in ("--system",):
            print COPYRIGHT           
            print
            print "SYSTEM INFORMATIONS"
            print "--------------------------------------------"            
            print "OS:                ", sys.platform
            print "Python:            ", sys.version             
            import html5lib
            print "html5lib:          ", "?"
            import reportlab
            print "Reportlab:         ", reportlab.Version
            #try:
            #    import pyPdf
            #    print "pyPdf:             ", pyPdf.__version__
            #except:
            #    print "pyPdf:             ","-"
            sys.exit(0)
            
#        if o in ("--tempdir",):
#            # Tempdir
#            tempdir = a

        if o in ("-t", "--format"):
            # Format XXX ???
            format = a
            
        if o in ("-b","--base"):
            base_dir = a

        if o in ("--encoding",) and a:
            # Encoding
            encoding = a

        if o in ("-c", "--css"):
            # CSS
            # css = "@import url('%s');" % a
            css = file(a, "r").read()

        if o in ("--css-dump",):
            # CSS dump
            print DEFAULT_CSS
            return 

        if o in ("--xml-dump",):
            xml_output = sys.stdout
        
        if o in ("-x", "--xml", "--xhtml"):
            xhtml = True        
        elif o in ("--html",):
            xhtml = False        

    if not quiet:
        try:
            logging.basicConfig(
                level=log_level, 
                format=log_format)
        except:
            # XXX Logging doesn't work for Python 2.3 
            logging.basicConfig()
            
    if len(args) not in (1, 2):
        usage()
        sys.exit(2)

    if len(args)==2:
        a_src, a_dest = args
    else:
        a_src = args[0]
        a_dest = None
        
    if "*" in a_src:
        a_src = glob.glob(a_src)
        # print a_src
    else:
        a_src = [a_src]
    
    for src in a_src:
    
        # If not forced to parse in a special way have a look 
        # at the filename suffix
        if xhtml is None:
            xhtml = src.lower().endswith(".xml")
    
        lc = None
        wpath = None
        
        if src=="-" or base_dir!=None:
            # Output to console
            fsrc = sys.stdin
            wpath = os.getcwd()
            if base_dir:
                wpath = base_dir
        else:
            # fsrc = open(src, "r")
            if src.startswith("http:") or src.startswith("https:"):                
                wpath = src
                fsrc = getFile(src).getFile()
                # fsrc = urllib2.urlopen(src)
                # lc = pisaLinkLoader(src, quiet=quiet).getFileName                
                src = "".join(urlparse.urlsplit(src)[1:3]).replace("/", "-")                                
            else:
                fsrc = wpath = os.path.abspath(src)
                fsrc = open(fsrc, "rb")

        if a_dest is None:
            dest_part = src            
            if dest_part.lower().endswith(".html") or dest_part.lower().endswith(".htm"):
                dest_part = ".".join(src.split(".")[:-1])
            dest = dest_part + "." + format.lower()
            for i in range(10):
                try:
                    open(dest, "wb").close()
                    break
                except:
                    pass
                dest = dest_part + "-%d.%s" % (i, format.lower())
        else:
            dest = a_dest
                
        fdestclose = 0
        
        if dest=="-" or base_dir:
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
            fdest = sys.stdout
            startviewer = 0
        else:
            dest = os.path.abspath(dest)
            try:
                open(dest, "wb").close()
            except:
                print "File '%s' seems to be in use of another application." % dest
                sys.exit(2)
            fdest = open(dest, "wb")
            fdestclose = 1
    
        if not quiet:
            print "Converting %s to %s..." % (src, dest)          
    
        try:
                                
            pdf = pisaDocument(
                fsrc,
                fdest,
                debug = debug,
                path = wpath,
                errout = sys.stdout,
                #multivalent_path = multivalent_path,
                #booklet = booklet,
                tempdir = tempdir,
                format = format,
                link_callback = lc,
                default_css = css,
                xhtml = xhtml,
                encoding = encoding,
                xml_output = xml_output,
                )

            if xml_output:
                xml_output.getvalue()
        
            if fdestclose:
                fdest.close()
                        
            if (not errors) and startviewer:
                if not quiet:
                    print "Open viewer for file %s" % dest
                startViewer(dest)

        except:
            
            if not quiet: 
                print "*** ERRORS OCCURED" 
    
            sys.exit(1)
示例#2
0
 def addFromURI(self, url, basepath=None):
     obj = getFile(url, basepath)
     if obj and (not obj.notFound()):
         self.files.append(obj.getFile())
示例#3
0
def execute():    
#    from optparse import OptionParser
#
#    parser = OptionParser()
#    parser.add_option("-f", "--file", dest="filename",
#                      help="write report to FILE", metavar="FILE")
#    parser.add_option("-q", "--quiet",
#                      action="store_false", dest="verbose", default=True,
#                      help="don't print status messages to stdout")
#    (options, args) = parser.parse_args()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "dhqstwcxb", [
            "quiet",
            "help",
            "start-viewer",
            "start",
            "debug=",
            "copyright",
            "version",
            "warn",
            #"booklet=",
            #"multivalent=",
            #"multivalent-path=",            
            "tempdir=",
            "format=",
            "css=",
            "base=",
            "css-dump",
            "xml-dump",
            "xhtml",
            "xml",
            "html",
            "encoding=",
            "system",
            "profile",
            ])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    errors = 0
    startviewer = 0
    quiet = 0    
    debug = 0
    #multivalent_path = ""
    #booklet = ""   
    tempdir = None
    format = "pdf"
    css = None
    xhtml = None
    encoding = None
    xml_output = None
    base_dir = None
    
    log_level = logging.ERROR
    log_format = LOG_FORMAT

    for o, a in opts:
        
        if o in ("-h", "--help"):
            # Hilfe anzeigen
            usage()
            sys.exit()

        if o in ("-s", "--start-viewer", "--start"):
            # Anzeigeprogramm starten
            startviewer = 1

        if o in ("-q", "--quiet"):
            # Output unterdrücken
            quiet = 1

        if o in ("-w", "--warn"):
            # Warnings            
            log_level = min(log_level, logging.WARN) # If also -d ignore -w
            
        if o in ("-d", "--debug"):
            # Debug
            log_level = logging.DEBUG
            log_format = LOG_FORMAT_DEBUG
            # debug = 10
            if a:                
                log_level = int(a)

#        if o in ("--multivalent", "--multivalent-path"):
#            # Multivalent.jar für Booklet
#            multivalent_path = a

#        if o in ("--booklet",):
#            # Booklet
#            booklet = a

        if o in ("--copyright", "--version"):
            print COPYRIGHT
            sys.exit(0)

        if o in ("--system",):
            print COPYRIGHT           
            print
            print "SYSTEM INFORMATIONS"
            print "--------------------------------------------"            
            print "OS:                ", sys.platform
            print "Python:            ", sys.version             
            import html5lib
            print "html5lib:          ", "?"
            import reportlab
            print "Reportlab:         ", reportlab.Version
            #try:
            #    import pyPdf
            #    print "pyPdf:             ", pyPdf.__version__
            #except:
            #    print "pyPdf:             ","-"
            sys.exit(0)
            
#        if o in ("--tempdir",):
#            # Tempdir
#            tempdir = a

        if o in ("-t", "--format"):
            # Format XXX ???
            format = a
            
        if o in ("-b","--base"):
            base_dir = a

        if o in ("--encoding",) and a:
            # Encoding
            encoding = a

        if o in ("-c", "--css"):
            # CSS
            # css = "@import url('%s');" % a
            css = file(a, "r").read()

        if o in ("--css-dump",):
            # CSS dump
            print DEFAULT_CSS
            return 

        if o in ("--xml-dump",):
            xml_output = sys.stdout
        
        if o in ("-x", "--xml", "--xhtml"):
            xhtml = True        
        elif o in ("--html",):
            xhtml = False        

    if not quiet:
        try:
            logging.basicConfig(
                level=log_level, 
                format=log_format)
        except:
            # XXX Logging doesn't work for Python 2.3 
            logging.basicConfig()
            
    if len(args) not in (1, 2):
        usage()
        sys.exit(2)

    if len(args)==2:
        a_src, a_dest = args
    else:
        a_src = args[0]
        a_dest = None
        
    if "*" in a_src:
        a_src = glob.glob(a_src)
        # print a_src
    else:
        a_src = [a_src]
    
    for src in a_src:
    
        # If not forced to parse in a special way have a look 
        # at the filename suffix
        if xhtml is None:
            xhtml = src.lower().endswith(".xml")
    
        lc = None
        wpath = None
        
        if src=="-" or base_dir!=None:
            # Output to console
            fsrc = sys.stdin
            wpath = os.getcwd()
            if base_dir:
                wpath = base_dir
        else:
            # fsrc = open(src, "r")
            if src.startswith("http:") or src.startswith("https:"):                
                wpath = src
                fsrc = getFile(src).getFile()
                # fsrc = urllib2.urlopen(src)
                # lc = pisaLinkLoader(src, quiet=quiet).getFileName                
                src = "".join(urlparse.urlsplit(src)[1:3]).replace("/", "-")                                
            else:
                fsrc = wpath = os.path.abspath(src)
                fsrc = open(fsrc, "rb")

        if a_dest is None:
            dest_part = src            
            if dest_part.lower().endswith(".html") or dest_part.lower().endswith(".htm"):
                dest_part = ".".join(src.split(".")[:-1])
            dest = dest_part + "." + format.lower()
            for i in range(10):
                try:
                    open(dest, "wb").close()
                    break
                except:
                    pass
                dest = dest_part + "-%d.%s" % (i, format.lower())
        else:
            dest = a_dest
                
        fdestclose = 0
        
        if dest=="-" or base_dir:
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
            fdest = sys.stdout
            startviewer = 0
        else:
            dest = os.path.abspath(dest)
            try:
                open(dest, "wb").close()
            except:
                print "File '%s' seems to be in use of another application." % dest
                sys.exit(2)
            fdest = open(dest, "wb")
            fdestclose = 1
    
        if not quiet:
            print "Converting %s to %s..." % (src, dest)          
    
        try:
                                
            pdf = pisaDocument(
                fsrc,
                fdest,
                debug = debug,
                path = wpath,
                errout = sys.stdout,
                #multivalent_path = multivalent_path,
                #booklet = booklet,
                tempdir = tempdir,
                format = format,
                link_callback = lc,
                default_css = css,
                xhtml = xhtml,
                encoding = encoding,
                xml_output = xml_output,
                )

            if xml_output:
                xml_output.getvalue()
        
            if fdestclose:
                fdest.close()
                        
            if (not errors) and startviewer:
                if not quiet:
                    print "Open viewer for file %s" % dest
                startViewer(dest)

        except:
            
            if not quiet: 
                print "*** ERRORS OCCURED" 
    
            sys.exit(1)
示例#4
0
 def addFromURI(self, url, basepath=None):
     obj = getFile(url, basepath)
     if obj and (not obj.notFound()):
         self.files.append(obj.getFile())