Beispiel #1
0
def setupUnrar(unrar_path):
    global supported_archives
    unrar = which(unrar_path)
    if unrar is None:
        log.warning("UnRAR executable not accessible: %s" % unrar_path)
        log.warning("Disabling rar archive support...")
        rarfile.UNRAR_TOOL = None
        supported_archives = [x for x in supported_archives if x not in rar_like_archives]
    else:
        log.info("UnRAR executable accessible: %s" % unrar)
        log.info("Enabling rar archive support...")
        rarfile.UNRAR_TOOL = unrar
        supported_archives += rar_like_archives
Beispiel #2
0
    def open(self):
        """
        Opens the path the layer was constructed with.
        Handles the type of the path appropriately
            and returns a list of pairs with (path, Layer) entries
            or a PIL.Image if self.path is an image
            or None if it failed to load anything
        """
        entries = None
        if isImage(self.path):
            # got an image, load it!
            if self.archive:
                # load the image from the archive!
                log.info("Open image '%s' in archive '%s'" % (self.path, self.archive.file))
                file = self.archive.open(self.path)
                try:
                    image = Image.open(file).convert("RGB")
                    return image
                except IOError as ex:
                    log.error("Failed loading image '%s' in archive '%s'" % (self.path, self.archive.file))
                    return None
                return image
            else:
                log.info("Open image '%s' from filesystem" % self.path)
                return Image.open(self.path).convert("RGB")

        elif isZip(self.path):
            # got a zipfile, open it!
            if self.archive:
                log.info("Open zip '%s' in archive '%s'" % (self.path, self.archive.file))
                file = self.archive.open(self.path)
                archive = Zip(file)
            else:
                log.info("Open zip '%s' from filesystem" % self.path)
                archive = Zip(self.path)

            name_pairs = [(name, Layer(name, archive)) for name in archive.names if isRar(name) or isZip(name) or isImage(name)]
            entries = dict(name_pairs)

        elif isRar(self.path) and isRARactive():
            # got a rarfile, open it!
            if self.archive:
                log.info("Open rar '%s' in archive '%s'" % (self.path, self.archive.file))
                #file = self.archive.open(self.path)
                #archive = Rar(file)
                log.error("Opening rar archives inside another archive isn't supported!")
                raise RuntimeError("Opening rar archives inside another archive isn't supported!")
            else:
                log.info("Open rar '%s' from filesystem" % self.path)
                archive = Rar(self.path)

            name_pairs = [(name, Layer(name, archive)) for name in archive.names if isRar(name) or isZip(name) or isImage(name)]
            entries = dict(name_pairs)

        elif os.path.isdir(self.path):
            # load names in directory
            log.info("Open directory '%s' from filesystem" % self.path)
            dir = os.listdir(self.path)

            # save all names in directory
            name_pairs = [(d, Layer(os.path.join(self.path, d))) for d in dir if isSupportedArchive(os.path.join(self.path, d)) or isImage(d)]
            entries = dict(name_pairs)

        else:
            log.warning("Unknown file: %s" % self.path)
            return None

        return entries