Quote:
Originally Posted by ownedbycats
Question: Is there a method to add a file to the extra data folder similar to SFE on 'formats'?
Context: I have a chain that copies the cover path to clipboard then follows it with the 'add data file' Calibre action where I paste it in. It's a little clunky though.
|
You can do this with a python template. It would probably be better as an Action Chains python action but I didn't feel like doing that.
Code:
python:
def evaluate(book, context):
import os
from shutil import copy
from calibre.db.constants import DATA_DIR_NAME
db = context.db
cache = db.new_api
# Get the normalized library path
library_path = cache.backend.library_path
# Construct the full path to the book folder
path = os.path.join(library_path, cache.field_for('path', book.id).replace('/', os.sep))
# Ensure the data directory exists
data_dir = os.path.join(path, DATA_DIR_NAME)
if not os.path.exists(data_dir):
os.mkdir(data_dir)
cover_file = os.path.join(path, 'cover.jpg')
if os.path.exists(cover_file):
# It does. Copy it to the data directory. The 'copy' method takes a
# directory as a target.
copy(cover_file, data_dir)
return 'copied'
return 'not copied'