View Single Post
Old 04-24-2024, 07:45 AM   #1326
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,106
Karma: 1954138
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
IIRC this can produce a value that isn't the "real" path generated by Save to Disk. The Save to Disk function will truncate segments of the path so the path length fits within the 250 character limit calibre enforces on the path length. The resulting path is also "sanitized" to remove any characters that shouldn't be in paths, for example colons, control characters, question marks, asterisks, etc.

The only way I know to get the "real" path is to include the calibre ID somewhere in the path, walk the resulting book folders to generate the full path to a book, then use the ID to insert that path into the book's metadata.
Ahhh. I totally missed that. Using the id would work so long as the user includes it in the template in a pre-defined format that allows for parsing. Also the default template does not include the id.

One other way is to follow calibre's own way of sanitizing and shortening paths. Something like this:

Code:
        from calibre.library.save_to_disk import sanitize_args, get_path_components
        db = gui.current_db
        for book_id in chain.scope().get_book_ids():
            fmt_paths = []
            root, opts, length = sanitize_args(path, opts)
            mi = db.get_metadata(book_id, index_is_id=True)
            components = get_path_components(opts, mi, book_id, length)
            extensionless_path = os.path.sep.join(components)
            if single_dir:
                extensionless_path = components[-1]
            if opts.formats == 'all':
                formats = db.formats(book_id, index_is_id=True)
            else:
                formats = settings['formats']
            for fmt in [fmt.strip().lower() for fmt in formats.split(',')]:
                fmt_paths.append(os.path.join(root, f'{extensionless_path}.{fmt.lower()}'))
            chain.set_book_vars(book_id, 'save_to_disk_path', ','.join(fmt_paths))

Last edited by capink; 05-08-2024 at 08:23 AM.
capink is offline   Reply With Quote