Source

yuureinohebi / gsut.py

Full commit
#/usr/bin/env python3
"""Ghostscript utilities toolkit, like GLut, but better!"""
import libgs
import gsu

def std_stream_handler(f):
    """A decorator that wraps the stream handler functions into the apropriate
    ctypes function callback types"""
    return libgs.c_stdstream_t(f)

class GhostScriptInterpreter(object):
    """A high level interface for Ghostscript. To push words, you only have
    to call methods on the object"""
    def __init__(self):
        gs = libgs.gsapi_new_instance()
        libgs.gsapi_init_with_args(gs, [
                    # doesn't matter that much, we use the default name
                    'gs',
                    # quiet out copyright messages and stuff
                    '-q'
                ])
        libgs.gsapi_set_stdio(gs, self._stdin_handler, self._stdout_handler,
                self._stderr_handler)
        # save for later use
        self._instance = gs

    @std_stream_handler
    def _stdout_handler(caller_handle, buf, length):
        print(caller_handle, buf, length)
        return 0

    _stdin_handler = _stdout_handler
    _stderr_handler = _stdout_handler

    def number(self, value):
        gsu.run_code(self._instance, str(value))

    def __getattr__(self, name):
        print("Pushing {0} on stack".format(name))
        return self

def main():
    gsi = GhostScriptInterpreter()
    gsi.number(1)
    print(gsi)

if __name__ == '__main__':
    main()