Example #1
0
 def openers(self, filename, names, extensions, mode):
     for extension in extensions:
         full_filenames = pycompat.popen("locate \"%s\"" % (filename+extension)).read()
         if full_filenames:
             break
     else:
         return []
     full_filename = full_filenames.split("\n")[0]
     def _opener():
         try:
             return builtinopen(full_filenames, mode)
         except IOError:
             warnings.warn("'%s' should be available at '%s' according to the locate, "
                           "but the file is not available at this location; "
                           "update your locate database" % (filename, self.full_filenames[filename]))
     return [_opener]
Example #2
0
    def writeGSfile(self, filename=None, device=None, input="eps", **kwargs):
        """
        convert EPS or PDF output to a file via Ghostscript

        If filename is None it is auto-guessed from the script name. If
        filename is "-", the output is written to stdout. In both cases, a
        device needs to be specified to define the format.

        If device is None, but a filename with suffix is given, PNG files will
        be written using the png16m device and JPG files using the jpeg device.
        """
        if filename is None:
            if not sys.argv[0].endswith(".py"):
                raise RuntimeError("could not auto-guess filename")
            if device.startswith("png"):
                filename = sys.argv[0][:-2] + "png"
            elif device.startswith("jpeg"):
                filename = sys.argv[0][:-2] + "jpg"
            else:
                filename = sys.argv[0][:-2] + device
        if device is None:
            if filename.endswith(".png"):
                device = "png16m"
            elif filename.endswith(".jpg"):
                device = "jpeg"
            else:
                raise RuntimeError("could not auto-guess device")

        gscmd, kwargs = self._gscmd(device, filename, **kwargs)

        if input == "eps":
            gscmd += " -"
            stdin = pycompat.popen(gscmd, "wb")
            self.writeEPSfile(stdin, **kwargs)
            stdin.close()
        elif input == "pdf":
            # PDF files need to be accesible by random access and thus we need to create
            # a temporary file
            fd, fname = tempfile.mkstemp()
            f = os.fdopen(fd, "wb")
            gscmd += " %s" % fname
            self.writePDFfile(f, **kwargs)
            f.close()
            os.system(gscmd)
            os.unlink(fname)
        else:
            raise RuntimeError("input 'eps' or 'pdf' expected")
Example #3
0
    def writeGSfile(self, filename=None, device=None, input="eps", **kwargs):
        """
        convert EPS or PDF output to a file via Ghostscript

        If filename is None it is auto-guessed from the script name. If
        filename is "-", the output is written to stdout. In both cases, a
        device needs to be specified to define the format.

        If device is None, but a filename with suffix is given, PNG files will
        be written using the png16m device and JPG files using the jpeg device.
        """
        if filename is None:
            if not sys.argv[0].endswith(".py"):
                raise RuntimeError("could not auto-guess filename")
            if device.startswith("png"):
                filename = sys.argv[0][:-2] + "png"
            elif device.startswith("jpeg"):
                filename = sys.argv[0][:-2] + "jpg"
            else:
                filename = sys.argv[0][:-2] + device
        if device is None:
            if filename.endswith(".png"):
                device = "png16m"
            elif filename.endswith(".jpg"):
                device = "jpeg"
            else:
                raise RuntimeError("could not auto-guess device")

        gscmd, kwargs = self._gscmd(device, filename, **kwargs)

        if input == "eps":
            gscmd += " -"
            stdin = pycompat.popen(gscmd, "wb")
            self.writeEPSfile(stdin, **kwargs)
            stdin.close()
        elif input == "pdf":
            # PDF files need to be accesible by random access and thus we need to create
            # a temporary file
            fd, fname = tempfile.mkstemp()
            f = os.fdopen(fd, "wb")
            gscmd += " %s" % fname
            self.writePDFfile(f, **kwargs)
            f.close()
            os.system(gscmd)
            os.unlink(fname)
        else:
            raise RuntimeError("input 'eps' or 'pdf' expected")
Example #4
0
 def pipeGS(
     self,
     filename="-",
     device=None,
     resolution=100,
     gscommand="gs",
     gsoptions="",
     textalphabits=4,
     graphicsalphabits=4,
     ciecolor=False,
     input="eps",
     **kwargs
 ):
     if device is None:
         if filename.endswith(".png"):
             device = "png16m"
         if filename.endswith(".jpg"):
             device = "jpeg"
     gscommand += " -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -r%i -sDEVICE=%s -sOutputFile=%s" % (
         resolution,
         device,
         filename,
     )
     if gsoptions:
         gscommand += " %s" % gsoptions
     if textalphabits is not None:
         gscommand += " -dTextAlphaBits=%i" % textalphabits
     if graphicsalphabits is not None:
         gscommand += " -dGraphicsAlphaBits=%i" % graphicsalphabits
     if ciecolor:
         gscommand += " -dUseCIEColor"
     if input == "eps":
         gscommand += " -"
         pipe = pycompat.popen(gscommand, "wb")
         self.writeEPSfile(pipe, **kwargs)
     elif input == "pdf":
         fd, fname = tempfile.mkstemp()
         f = os.fdopen(fd, "wb")
         gscommand += " %s" % fname
         self.writePDFfile(f, **kwargs)
         f.close()
         os.system(gscommand)
         os.unlink(fname)
     else:
         raise RuntimeError("input 'eps' or 'pdf' expected")
Example #5
0
 def openers(self, filename, names, extensions, mode):
     for name in names:
         try:
             full_filenames = pycompat.popen('kpsewhich --format="%s" "%s"' % (name, filename)).read()
         except OSError:
             return []
         if full_filenames:
             break
     else:
         return []
     full_filename = full_filenames.split("\n")[0]
     def _opener():
         try:
             return builtinopen(full_filename, mode)
         except IOError:
             warnings.warn("'%s' should be available at '%s' according to kpsewhich, "
                           "but the file is not available at this location; "
                           "update your kpsewhich database" % (filename, full_filename))
     return [_opener]
Example #6
0
    def pipeGS(self, device, input="eps", seekable=False, **kwargs):
        """
        returns a pipe with the Ghostscript output of the EPS or PDF of the canvas

        If seekable is True, a StringIO instance will be returned instead of a
        pipe to allow random access.
        """

        gscmd, kwargs = self._gscmd(device, "-", **kwargs)

        if input == "eps":
            gscmd += " -"
            # we can safely ignore that the input and output pipes could block each other,
            # because Ghostscript has to read the full input before writing the output
            stdin, stdout = pycompat.popen2(gscmd)
            self.writeEPSfile(stdin, **kwargs)
            stdin.close()
        elif input == "pdf":
            # PDF files need to be accesible by random access and thus we need to create
            # a temporary file
            fd, fname = tempfile.mkstemp()
            f = os.fdopen(fd, "wb")
            gscmd += " %s" % fname
            self.writePDFfile(f, **kwargs)
            f.close()
            stdout = pycompat.popen(gscmd, "rb")
            os.unlink(fname)
        else:
            raise RuntimeError("input 'eps' or 'pdf' expected")

        if seekable:
            # the read method of a pipe object may not return the full content
            f = cStringIO.StringIO()
            while True:
                data = stdout.read()
                if not data:
                   break
                f.write(data)
            stdout.close()
            f.seek(0)
            return f
        else:
            return stdout
Example #7
0
    def pipeGS(self, device, input="eps", seekable=False, **kwargs):
        """
        returns a pipe with the Ghostscript output of the EPS or PDF of the canvas

        If seekable is True, a StringIO instance will be returned instead of a
        pipe to allow random access.
        """

        gscmd, kwargs = self._gscmd(device, "-", **kwargs)

        if input == "eps":
            gscmd += " -"
            # we can safely ignore that the input and output pipes could block each other,
            # because Ghostscript has to read the full input before writing the output
            stdin, stdout = pycompat.popen2(gscmd)
            self.writeEPSfile(stdin, **kwargs)
            stdin.close()
        elif input == "pdf":
            # PDF files need to be accesible by random access and thus we need to create
            # a temporary file
            fd, fname = tempfile.mkstemp()
            f = os.fdopen(fd, "wb")
            gscmd += " %s" % fname
            self.writePDFfile(f, **kwargs)
            f.close()
            stdout = pycompat.popen(gscmd, "rb")
            os.unlink(fname)
        else:
            raise RuntimeError("input 'eps' or 'pdf' expected")

        if seekable:
            # the read method of a pipe object may not return the full content
            f = cStringIO.StringIO()
            while True:
                data = stdout.read()
                if not data:
                    break
                f.write(data)
            stdout.close()
            f.seek(0)
            return f
        else:
            return stdout
Example #8
0
    def openers(self, filename, names, extensions, mode):
        for extension in extensions:
            full_filenames = pycompat.popen("locate \"%s\"" %
                                            (filename + extension)).read()
            if full_filenames:
                break
        else:
            return []
        full_filename = full_filenames.split("\n")[0].rstrip("\r")

        def _opener():
            try:
                return builtinopen(full_filenames, mode)
            except IOError:
                warnings.warn(
                    "'%s' should be available at '%s' according to the locate, "
                    "but the file is not available at this location; "
                    "update your locate database" %
                    (filename, self.full_filenames[filename]))

        return [_opener]
Example #9
0
 def pipeGS(self,
            filename="-",
            device=None,
            resolution=100,
            gscommand="gs",
            gsoptions="",
            textalphabits=4,
            graphicsalphabits=4,
            ciecolor=False,
            input="eps",
            **kwargs):
     if device is None:
         if filename.endswith(".png"):
             device = "png16m"
         if filename.endswith(".jpg"):
             device = "jpeg"
     gscommand += " -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -r%i -sDEVICE=%s -sOutputFile=%s" % (
         resolution, device, filename)
     if gsoptions:
         gscommand += " %s" % gsoptions
     if textalphabits is not None:
         gscommand += " -dTextAlphaBits=%i" % textalphabits
     if graphicsalphabits is not None:
         gscommand += " -dGraphicsAlphaBits=%i" % graphicsalphabits
     if ciecolor:
         gscommand += " -dUseCIEColor"
     if input == "eps":
         gscommand += " -"
         pipe = pycompat.popen(gscommand, "wb")
         self.writeEPSfile(pipe, **kwargs)
     elif input == "pdf":
         fd, fname = tempfile.mkstemp()
         f = os.fdopen(fd, "wb")
         gscommand += " %s" % fname
         self.writePDFfile(f, **kwargs)
         f.close()
         os.system(gscommand)
         os.unlink(fname)
     else:
         raise RuntimeError("input 'eps' or 'pdf' expected")
Example #10
0
    def openers(self, filename, names, extensions, mode):
        for name in names:
            try:
                full_filenames = pycompat.popen(
                    'kpsewhich --format="%s" "%s"' % (name, filename)).read()
            except OSError:
                return []
            if full_filenames:
                break
        else:
            return []
        full_filename = full_filenames.split("\n")[0].rstrip("\r")

        def _opener():
            try:
                return builtinopen(full_filename, mode)
            except IOError:
                warnings.warn(
                    "'%s' should be available at '%s' according to kpsewhich, "
                    "but the file is not available at this location; "
                    "update your kpsewhich database" %
                    (filename, full_filename))

        return [_opener]