Esempio n. 1
0
    def _add_formats(self, paths):
        added = False
        if not paths:
            return added
        bad_perms = []
        for _file in paths:
            _file = os.path.abspath(_file)
            if not os.access(_file, os.R_OK):
                bad_perms.append(_file)
                continue

            nfile = run_plugins_on_import(_file)
            if nfile is not None:
                _file = nfile
            stat = os.stat(_file)
            size = stat.st_size
            ext = os.path.splitext(_file)[1].lower().replace('.', '')
            timestamp = utcfromtimestamp(stat.st_mtime)
            for row in range(self.formats.count()):
                fmt = self.formats.item(row)
                if fmt.ext.lower() == ext:
                    self.formats.takeItem(row)
                    break
            Format(self.formats, ext, size, path=_file, timestamp=timestamp)
            self.changed = True
            added = True
        if bad_perms:
            error_dialog(self, _('No permission'),
                    _('You do not have '
                'permission to read the following files:'),
                det_msg='\n'.join(bad_perms), show=True)

        return added
Esempio n. 2
0
def run_import_plugins(paths, group_id, tdir):
    final_paths = []
    for path in paths:
        if not os.access(path, os.R_OK):
            continue
        try:
            nfp = run_plugins_on_import(path)
        except Exception:
            nfp = None
            import traceback
            traceback.print_exc()
        if nfp and os.access(nfp, os.R_OK) and not samefile(nfp, path):
            # Ensure that the filename is preserved so that
            # reading metadata from filename is not broken
            name = os.path.splitext(os.path.basename(path))[0]
            ext = os.path.splitext(nfp)[1]
            path = os.path.join(tdir, '%s' % group_id, name + ext)
            try:
                os.mkdir(os.path.dirname(path))
            except EnvironmentError as err:
                if err.errno != errno.EEXIST:
                    raise
            try:
                os.rename(nfp, path)
            except EnvironmentError:
                shutil.copyfile(nfp, path)
        final_paths.append(path)
    return final_paths
Esempio n. 3
0
def run_import_plugins(paths, group_id, tdir):
    final_paths = []
    for path in paths:
        if not os.access(path, os.R_OK):
            continue
        try:
            nfp = run_plugins_on_import(path)
        except Exception:
            nfp = None
            import traceback
            traceback.print_exc()
        if nfp and os.access(nfp, os.R_OK) and not samefile(nfp, path):
            # Ensure that the filename is preserved so that
            # reading metadata from filename is not broken
            name = os.path.splitext(os.path.basename(path))[0]
            ext = os.path.splitext(nfp)[1]
            path = os.path.join(tdir, '%s' % group_id, name + ext)
            try:
                os.mkdir(os.path.dirname(path))
            except EnvironmentError as err:
                if err.errno != errno.EEXIST:
                    raise
            try:
                os.rename(nfp, path)
            except EnvironmentError:
                shutil.copyfile(nfp, path)
        final_paths.append(path)
    return final_paths
Esempio n. 4
0
    def _add_formats(self, paths):
        added = False
        if not paths:
            return added
        bad_perms = []
        for _file in paths:
            _file = os.path.abspath(_file)
            if not os.access(_file, os.R_OK):
                bad_perms.append(_file)
                continue

            nfile = run_plugins_on_import(_file)
            if nfile is not None:
                _file = nfile
            stat = os.stat(_file)
            size = stat.st_size
            ext = os.path.splitext(_file)[1].lower().replace('.', '')
            timestamp = utcfromtimestamp(stat.st_mtime)
            for row in range(self.formats.count()):
                fmt = self.formats.item(row)
                if fmt.ext.lower() == ext:
                    self.formats.takeItem(row)
                    break
            Format(self.formats, ext, size, path=_file, timestamp=timestamp)
            self.changed = True
            added = True
        if bad_perms:
            error_dialog(self,
                         _('No permission'),
                         _('You do not have '
                           'permission to read the following files:'),
                         det_msg='\n'.join(bad_perms),
                         show=True)

        return added
Esempio n. 5
0
def run_import_plugins(path_or_stream, fmt):
    fmt = fmt.lower()
    if hasattr(path_or_stream, 'seek'):
        path_or_stream.seek(0)
        pt = PersistentTemporaryFile('_import_plugin.'+fmt)
        shutil.copyfileobj(path_or_stream, pt, 1024**2)
        pt.close()
        path = pt.name
    else:
        path = path_or_stream
    return run_plugins_on_import(path, fmt)
Esempio n. 6
0
def do_read_metadata(task, tdir, mdir, notification):
    from calibre.customize.ui import run_plugins_on_import
    for x in task:
        try:
            id_, formats = x
        except:
            continue
        try:
            if isinstance(formats, basestring): formats = [formats]
            import_map = {}
            fmts, metadata_fmts = [], []
            for format in formats:
                mfmt = format
                name, ext = os.path.splitext(os.path.basename(format))
                nfp = run_plugins_on_import(format)
                if not nfp or nfp == format or not os.access(nfp, os.R_OK):
                    nfp = None
                else:
                    # Ensure that the filename is preserved so that
                    # reading metadata from filename is not broken
                    nfp = os.path.abspath(nfp)
                    nfext = os.path.splitext(nfp)[1]
                    mfmt = os.path.join(mdir, name + nfext)
                    shutil.copyfile(nfp, mfmt)
                metadata_fmts.append(mfmt)
                fmts.append(nfp)

            serialize_metadata_for(metadata_fmts, tdir, id_)

            for format, nfp in zip(formats, fmts):
                if not nfp:
                    continue
                if isinstance(nfp, unicode):
                    nfp.encode(filesystem_encoding)
                x = lambda j: os.path.abspath(
                    os.path.normpath(os.path.normcase(j)))
                if x(nfp) != x(format) and os.access(nfp, os.R_OK | os.W_OK):
                    fmt = os.path.splitext(format)[1].replace('.', '').lower()
                    nfmt = os.path.splitext(nfp)[1].replace('.', '').lower()
                    dest = os.path.join(tdir, '%s.%s' % (id_, nfmt))
                    shutil.copyfile(nfp, dest)
                    import_map[fmt] = dest
            if import_map:
                with open(os.path.join(tdir, str(id_) + '.import'), 'wb') as f:
                    for fmt, nfp in import_map.items():
                        f.write(fmt + ':' + nfp + '\n')
            notification(0.5, id_)
        except:
            import traceback
            with open(os.path.join(tdir, '%s.error' % id_), 'wb') as f:
                f.write(traceback.format_exc())
Esempio n. 7
0
def do_read_metadata(task, tdir, mdir, notification):
    from calibre.customize.ui import run_plugins_on_import
    for x in task:
        try:
            id_, formats = x
        except:
            continue
        try:
            if isinstance(formats, basestring): formats = [formats]
            import_map = {}
            fmts, metadata_fmts = [], []
            for format in formats:
                mfmt = format
                name, ext = os.path.splitext(os.path.basename(format))
                nfp = run_plugins_on_import(format)
                if not nfp or nfp == format or not os.access(nfp, os.R_OK):
                    nfp = None
                else:
                    # Ensure that the filename is preserved so that
                    # reading metadata from filename is not broken
                    nfp = os.path.abspath(nfp)
                    nfext = os.path.splitext(nfp)[1]
                    mfmt = os.path.join(mdir, name + nfext)
                    shutil.copyfile(nfp, mfmt)
                metadata_fmts.append(mfmt)
                fmts.append(nfp)

            serialize_metadata_for(metadata_fmts, tdir, id_)

            for format, nfp in zip(formats, fmts):
                if not nfp:
                    continue
                if isinstance(nfp, unicode):
                    nfp.encode(filesystem_encoding)
                x = lambda j : os.path.abspath(os.path.normpath(os.path.normcase(j)))
                if x(nfp) != x(format) and os.access(nfp, os.R_OK|os.W_OK):
                    fmt = os.path.splitext(format)[1].replace('.', '').lower()
                    nfmt = os.path.splitext(nfp)[1].replace('.', '').lower()
                    dest = os.path.join(tdir, '%s.%s'%(id_, nfmt))
                    shutil.copyfile(nfp, dest)
                    import_map[fmt] = dest
            if import_map:
                with open(os.path.join(tdir, str(id_)+'.import'), 'wb') as f:
                    for fmt, nfp in import_map.items():
                        f.write(fmt+':'+nfp+'\n')
            notification(0.5, id_)
        except:
            import traceback
            with open(os.path.join(tdir, '%s.error'%id_), 'wb') as f:
                f.write(traceback.format_exc())
Esempio n. 8
0
def run_import_plugins(paths, group_id, tdir):
    final_paths = []
    for path in paths:
        if not os.access(path, os.R_OK):
            continue
        try:
            nfp = run_plugins_on_import(path)
        except Exception:
            nfp = None
            import traceback
            traceback.print_exc()
        if nfp and os.access(nfp, os.R_OK) and not samefile(nfp, path):
            # Ensure that the filename is preserved so that
            # reading metadata from filename is not broken
            name = os.path.splitext(os.path.basename(path))[0]
            ext = os.path.splitext(nfp)[1]
            path = os.path.join(tdir, str(group_id), name + ext)
            os.makedirs(os.path.dirname(path), exist_ok=True)
            try:
                os.replace(nfp, path)
            except OSError:
                shutil.copyfile(nfp, path)
        final_paths.append(path)
    return final_paths