示例#1
0
def _run_ghostscript( args ) :
    """Run Ghostscript.

    We have to do a bit of stuffing around to stop Ghostscript from printing warnings to the console.
    This code was adapted from ghostscript's _gsprint.py.
    """
    # allocate a new Ghostscript instance
    # NOTE: We only import the ghostscript stuff if it's needed (i.e. when we get here), so that people
    # can run this program without needing Ghostscript to be installed, if they already have a database.
    import ghostscript
    import ghostscript._gsprint as gsp
    inst = gsp.new_instance()
    # wrap stdin/stdout/stderr with dummy buffers
    def wrap( stdin ) :
        return gsp.c_stdstream_call_t(
            lambda inst,buf,count: 0 if stdin else count
        )
    stdin_buf = wrap( True )
    stdout_buf = wrap( False )
    stderr_buf = wrap( False )
    gsp.set_stdio( inst , stdin_buf , stdout_buf , stderr_buf )
    try :
        # run Ghostscript
        args = [ s.encode(locale.getpreferredencoding()) for s in args ]
        __Ghostscript = getattr( ghostscript , "__Ghostscript" )
        __Ghostscript( inst , args )
    finally :
        # clean up
        gsp.delete_instance( inst )
        del inst
示例#2
0
def main(argv):
    code = 1
    use_gui, _ = Gtk.init_check(argv)
    
    # insert display device parameters as first arguments
    # this controls the format of the pixbuf that ghostscript will deliver
    # see display_sync for details
    
    # fast
    CAIRO_FORMAT_RGB24  = gs.DISPLAY_COLORS_RGB | gs.DISPLAY_UNUSED_LAST | \
                          gs.DISPLAY_DEPTH_8 | gs.DISPLAY_LITTLEENDIAN
    
    # interesting
    SEPARATION_FORMAT = gs.DISPLAY_COLORS_SEPARATION | gs.DISPLAY_ALPHA_NONE | \
                        gs.DISPLAY_DEPTH_8 | gs.DISPLAY_BIGENDIAN
    
    # if there are spot colors they are mixed into the cmyk values
    CMYK_FORMAT = gs.DISPLAY_COLORS_CMYK | gs.DISPLAY_ALPHA_NONE | \
                  gs.DISPLAY_DEPTH_8 | gs.DISPLAY_BIGENDIAN
    
    dformat = "-dDisplayFormat=%d" % \
            ( CAIRO_FORMAT_RGB24 | gs.DISPLAY_TOPFIRST )
    
    nargv = [argv[0], dformat] + argv[1:]
    
    #run Ghostscript 
    try:
        instance = gs.new_instance()
        gs.set_stdio(instance, gsdll_stdin, gsdll_stdout, gsdll_stderr)
        if use_gui:
            gs.set_display_callback(instance, c.byref(display))
        code = gs.init_with_args(instance, nargv)
        if code == 0:
            code = gs.run_string(instance, start_string)
        code1 = gs.exit(instance)
        if code == 0 or code == gs.e_Quit:
            code = code1
        if code == gs.e_Quit:
            code = 0 # user executed 'quit'
        gs.delete_instance(instance)
    except gs.GhostscriptError as e:
        code = e.code
        sys.stderr.write(e.message)
    finally:
        exit_status = 0;
        if code in [0, gs.e_Info, gs.e_Quit]:
            pass
        elif code == gs.e_Fatal:
            exit_status = 1
        else:
            exit_status = 255
    return exit_status
示例#3
0
__author__ = "Hartmut Goebel <*****@*****.**>"
__copyright__ = "Copyright 2010 by Hartmut Goebel <*****@*****.**>"
__licence__ = "GNU General Public License version 3 (GPL v3)"
__credits__ = "Based on an example from http://www.ghostscript.com/doc/8.63/API.htm"

import sys
from ghostscript import _gsprint as gs

command = "1 2 add == flush\n"

instance = gs.new_instance()

# set_stdio() is not yet implemented
#gs.set_stdio(isntance, gsdll_stdin, gsdll_stdout, gsdll_stderr)

code = gs.init_with_args(instance, sys.argv)
if code == 0:
    gs.run_string_begin(instance)
    gs.run_string_continue(instance, command)
    gs.run_string_continue(instance, "qu")
    gs.run_string_continue(instance, "it")
    gs.run_string_end(instance)

code1 = gs.exit(instance)
if code == 0 or code == gs.e_Quit:
    code = code1
gs.delete_instance(instance)
if not (code == 0 or code == gs.e_Quit):
    sys.exit(1)
示例#4
0
__author__ = "Hartmut Goebel <*****@*****.**>"
__copyright__ = "Copyright 2010 by Hartmut Goebel <*****@*****.**>"
__licence__ = "GNU General Public License version 3 (GPL v3)"
__credits__ = "Based on an example from http://www.ghostscript.com/doc/8.63/API.htm"

import sys
from ghostscript import _gsprint as gs

command = "1 2 add == flush\n"

instance = gs.new_instance()

# set_stdio() is not yet implemented
#gs.set_stdio(isntance, gsdll_stdin, gsdll_stdout, gsdll_stderr)

code = gs.init_with_args(instance, sys.argv)
if code == 0:
    gs.run_string_begin(instance);
    gs.run_string_continue(instance, command);
    gs.run_string_continue(instance, "qu");
    gs.run_string_continue(instance, "it");
    gs.run_string_end(instance);
    
code1 = gs.exit(instance)
if code == 0 or code == gs.e_Quit:
    code = code1
gs.delete_instance(instance)
if not (code == 0 or code == gs.e_Quit):
    sys.exit(1)
示例#5
0
 def cleanup(self):
     """ Purge the ghostscript instance """
     gs.delete_instance(self.instance)
     self.instance = None