Giter Club home page Giter Club logo

ipostscript's People

Contributors

kts avatar

Stargazers

 avatar

Watchers

 avatar

ipostscript's Issues

added prompt

This works on both Windows,
2023-11-05 20_05_48-cmd

and Linux,
2023-11-05 20_14_44-Ubuntu  Running  - Oracle VM VirtualBox

from ipykernel.kernelbase import Kernel
from subprocess import check_output
import os.path
import re
import signal

from pexpect import popen_spawn
import pexpect
import platform

def iswindows():
    return platform.system() == "Windows"

__version__ = "0.1.0"

class GSWrapper:
    """
    Wrap Ghostscript executable using pexpect
    """
    def __init__(self):
        """
        todo: config cmd can be 'gs', 'gs-X11', etc.
        """
        if iswindows():
            self.cmd = 'gswin64c -dNOSAFER' 
            self.p = popen_spawn.PopenSpawn(self.cmd)
        else:
            self.cmd = 'gs -dNOSAFER' 
            self.p = pexpect.spawn(self.cmd)

        # maybe input/output to gs is always 'ascii'?
        self.encoding = "utf8"

        pattern1 = b"GS>"
        self.p.expect(re.compile(pattern1))

        assert(self.p.after == b"GS>")

        
    def run(self, code):
        """
        Execute 'code' in session.

        newline is appended
        """
        prompt = re.compile(b'GS(>|<\d+>)')
        code = code + "\n"

        self.p.send(code)
        
        self.p.expect(prompt)

        before = self.p.before.decode(self.encoding)
        after = self.p.after.decode(self.encoding)
        
        if not iswindows():
            code = code.strip()
            before = before[len(code):]

        if len(before)==0:
            r = after
        else:
            r = before + after
            
        # .after is always the prompt itself. ?
        # eg:  b'GS>'
        #
        # could parse to get stack size
        # 'GS>', 'GS<1>', ...
        #

        return r.strip()

Note that I have added the -dNOSAFER option to cmd to allow scripts to run other scripts.

e.g.

(utils.ps) run

added Windows support

Hi,
Thanks for the kernel.

I made some minimal changes to add windows support as pexpect.spawn() doesn't exist on windows.
No changes were made to the Linux functionality.

Only requirement is that gswin64c.exe is on the path.

from pexpect import popen_spawn
import pexpect
import platform

def iswindows():
    return platform.system() == "Windows"

__version__ = "0.1.0"

class GSWrapper:
    """
    Wrap Ghostscript executable using pexpect
    """
    def __init__(self,
                 cmd = 'gs'
                 ):
        """
        todo: config cmd can be 'gs', 'gs-X11', etc.
        """
        if iswindows():
            cmd = 'gswin64c.exe -dNOSAFER' 
            self.p = popen_spawn.PopenSpawn(cmd)
        else:
            self.p = pexpect.spawn(cmd)

        # maybe input/output to gs is always 'ascii'?
        self.encoding = "utf8"

        pattern1 = b"GS>"
        self.p.expect(re.compile(pattern1))

        assert(self.p.after == b"GS>")

        
    def run(self, code):
        """
        Execute 'code' in session.

        newline is appended
        """
        rx = re.compile(b'GS(>|<\d+>)')
        cc = code + "\n"

        # sendline uses "\r\n"?
        #p.sendline(c)
        self.p.send(cc)
        
        self.p.expect(rx)

        c1 = self.p.before.decode(self.encoding)

        code1 = code.strip()
        
        if iswindows():
            r = c1
        else:
            assert(c1.startswith(code1))
            r = c1[len(code1):]

        # .after is always the prompt itself. ?
        # eg:  b'GS>'
        #
        # could parse to get stack size
        # 'GS>', 'GS<1>', ...
        #

        return r.strip()

example of it running in a jupyter console - python 3.11.5, Windows 10 x64,

2023-11-05 18_30_16-cmd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.