Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 07-12-2023, 06:41 PM   #1126
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
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'
I added a link to this in the templates section in the Action Chains Resources thread.
capink is offline   Reply With Quote
Old 07-13-2023, 11:06 AM   #1127
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,977
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Thanks
ownedbycats is offline   Reply With Quote
Advert
Old 07-13-2023, 05:33 PM   #1128
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,977
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Question: Where should I put it? I tried a SFE and ended up accidentally changing the title of a book to 'copied.' Oops.
ownedbycats is offline   Reply With Quote
Old 07-13-2023, 05:35 PM   #1129
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Question: Where should I put it? I tried a SFE and ended up accidentally changing the title of a book to 'copied.' Oops.
I would put it in an SFE cover action. Return the empty string so the cover isn't changed.

Or leave it in a title SFE and return the current title so nothing changes.
chaley is offline   Reply With Quote
Old 07-13-2023, 05:56 PM   #1130
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,977
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
That did it.
ownedbycats is offline   Reply With Quote
Advert
Old 07-14-2023, 04:41 AM   #1131
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
@capink: A question ...

I sometimes want to run a (python) template over the selected books. This isn't the easiest thing to do.

Solutions I have found:
  • Select the books then use the template tester. Although this works, it isn't an obvious or particularly efficient solution. For example it doesn't support keyboard shortcuts. It does have the advantage that I can see error output.
  • Write a Python code action. This works, but I must implement in the template iteration over the currently selected books. In addition, there isn't a good way to show errors.
  • Write a Formulas action. Here I again must implement my own selection iteration. However, it has the advantage that I can display errors. However, I can't find a way to show nothing if there is no output.
  • Use a Single Field Edit. This solution takes care of iteration. However, it really wants to modify some column, which in this case I don't want to do. The work-around: pick a column like Title and return that value so nothing changes. In addition, it isn't easy to see errors without using an "error column" (long comments) and writing the errors into that.
To solve this problem I have considered making a calibre action that runs a template over the selected books. If the template produces no output then the action doesn't show a dialog. If the template does produce output for any/all books then it opens a dialog like the middle part of the template tester, showing the output per book.

My problem with this solution is that it feels like I am reinventing a part of Action Chains, which seems a bit silly to me. A Formulas-like action that iterates over the selection and shows output only if there is any is more-or-less the same thing.

What are your thoughts? How would you solve this problem?
chaley is offline   Reply With Quote
Old 07-14-2023, 04:29 PM   #1132
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
@capink: A question ...

I sometimes want to run a (python) template over the selected books. This isn't the easiest thing to do.

Solutions I have found:
  • Select the books then use the template tester. Although this works, it isn't an obvious or particularly efficient solution. For example it doesn't support keyboard shortcuts. It does have the advantage that I can see error output.
  • Write a Python code action. This works, but I must implement in the template iteration over the currently selected books. In addition, there isn't a good way to show errors.
  • Write a Formulas action. Here I again must implement my own selection iteration. However, it has the advantage that I can display errors. However, I can't find a way to show nothing if there is no output.
  • Use a Single Field Edit. This solution takes care of iteration. However, it really wants to modify some column, which in this case I don't want to do. The work-around: pick a column like Title and return that value so nothing changes. In addition, it isn't easy to see errors without using an "error column" (long comments) and writing the errors into that.
To solve this problem I have considered making a calibre action that runs a template over the selected books. If the template produces no output then the action doesn't show a dialog. If the template does produce output for any/all books then it opens a dialog like the middle part of the template tester, showing the output per book.

My problem with this solution is that it feels like I am reinventing a part of Action Chains, which seems a bit silly to me. A Formulas-like action that iterates over the selection and shows output only if there is any is more-or-less the same thing.

What are your thoughts? How would you solve this problem?
If you don't want to modify the values it is better to steer away from SFE. Two solutions spring to mind:
  1. An iterate checkbox was introduced to the Chains Variables Action. This will run the template for all the books in the current scope (default is selected books).
  2. The Second solution is using a Formulas Action with a combination of from_selection() and book_field()
    Code:
    program:
        for book_id in from_selection('id'):
            title = book_field(book_id, 'title')
        rof;
    This might not be convenient as it does not allow the usage of a short form like $title.

If either of these solutions not sufficient, please let me know why, so we can work out a solution together.

A solution is not necessarily a new action, it might be a matter of a new template function. While we are at it, is there for a way for a function to know which template mode it is running in, to modify the output depending on the mode. e.g. from_selection() could return a python list instead of a string if running in a python mode.

Edit: As for errors, you can save them in a global variable, and display them using a Formula Action at the end of the chain. This works for any kind of solution you choose, whether a Formula Action, a Chains Variables Action or even a "Run Python Code" Action.

Last edited by capink; 07-14-2023 at 06:49 PM.
capink is offline   Reply With Quote
Old 07-15-2023, 04:39 AM   #1133
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
If you don't want to modify the values it is better to steer away from SFE. Two solutions spring to mind:
[snip]
I ended up using chain variables and a formula to show output, as you suggested.
Quote:
A solution is not necessarily a new action, it might be a matter of a new template function. While we are at it, is there for a way for a function to know which template mode it is running in, to modify the output depending on the mode. e.g. from_selection() could return a python list instead of a string if running in a python mode.
Code:
utils.formatter_functions.function_object_type(template)
This accepts template text, a stored template, or a template function.
Quote:
Edit: As for errors, you can save them in a global variable, and display them using a Formula Action at the end of the chain. This works for any kind of solution you choose, whether a Formula Action, a Chains Variables Action or even a "Run Python Code" Action.
I ended up building an "example" action chain that I can use as the starting point. It contains two actions: a chain variables action that is run over the selected books, and a formulas action that displays any output.

The chain variables action puts its output into a list stored in globals. It also sets the chain variable "has_output" if there is anything in the list. The formulas action has a condition ensuring the action is run only if "has_output" is set to the non-empty string. The export of the prototype action chain is attached.

Here is the prototype chain variables action:
Code:
python:
def evaluate(book, context):
	# Run the desired template. It must produce a string
	output = run_template(book, context)

	# if the template produced some output, store it in globals to be printed later
	all_output = context.globals.get('output', [])
	if output:
		all_output.append(f'{book.title} ({book.id}): {output}')
		context.globals['output'] = all_output
	return 'yes' if all_output else ''

def run_template(book, context):
	# you can directly enter a python template here, or alternatively call
	# a stored template (GPM or python) or formatter function using
	# context.funcs.name.
	# Example: context.funcs.template('{series:|| [}{series_index:||]}')
	# runs the SFM template to return the series and series index
	return context.funcs.template('{series:|| [}{series_index:||]}')
Here is the formula action. It formats the list of "errors" from the chain vars action into a single string.
Code:
python:
def evaluate(book, context):
	return '\n'.join(context.globals.get('output', ''))
The condition check is equally simple:
Code:
program:
	return globals(has_output) != ''
Here is an example that uses @ownedbycats request, to copy the cover into the data directory for all selected books. At the end it shows a dialog listing the books and whether or not a cover was copied.
Code:
python:
def evaluate(book, context):
	# Run the desired template. It must produce a string
	output = run_template(book, context)

	# if the template produced some output, store it in globals to be printed later
	all_output = context.globals.get('output', [])
	if output:
		all_output.append(f'{book.title} ({book.id}): {output}')
		context.globals['output'] = all_output
	return 'yes' if all_output else ''

def run_template(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 'cover copied'
	return 'cover not copied'
Attached Files
File Type: zip Run template over books prototype.zip (883 Bytes, 516 views)
chaley is offline   Reply With Quote
Old 07-17-2023, 07:27 AM   #1134
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
@capink: I noticed that when I run a chain with several quick-acting actions I get a "screen flash" because of the action chains progress bar. Looking at the code I saw that you already didn't show the progress bar for Chain Caller.

To avoid that flash, using the Chain Caller code as an example I added a checkbox to the chain dialog to explicitly enable/disable the progress bar. This eliminated the flash. The changes are compatible with existing chains and the import/export process.

In case you are interested in seeing or incorporating these changes I am attaching the plugin zip and a patch file. I didn't change the version number.

EDIT: removed the plugin zip because it has been incorporated into a release.
Attached Files
File Type: txt ac_patch.txt (2.5 KB, 355 views)

Last edited by chaley; 07-17-2023 at 03:32 PM.
chaley is offline   Reply With Quote
Old 07-17-2023, 09:22 AM   #1135
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,977
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
I would use this if implemented.
ownedbycats is offline   Reply With Quote
Old 07-17-2023, 02:12 PM   #1136
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
@capink: I noticed that when I run a chain with several quick-acting actions I get a "screen flash" because of the action chains progress bar. Looking at the code I saw that you already didn't show the progress bar for Chain Caller.

To avoid that flash, using the Chain Caller code as an example I added a checkbox to the chain dialog to explicitly enable/disable the progress bar. This eliminated the flash. The changes are compatible with existing chains and the import/export process.

In case you are interested in seeing or incorporating these changes I am attaching the plugin zip and a patch file. I didn't change the version number.
Thanks for the patch. Changes are now in the new version of the plugin.
capink is offline   Reply With Quote
Old 07-17-2023, 03:42 PM   #1137
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Thanks for the patch. Changes are now in the new version of the plugin.
chaley is offline   Reply With Quote
Old 07-17-2023, 04:00 PM   #1138
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,977
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
@chaley: Small issue, resizing the dialog now only resizes the top half.
Attached Thumbnails
Click image for larger version

Name:	2023-07-17 17_00_21-Add actions.png
Views:	724
Size:	30.7 KB
ID:	202693   Click image for larger version

Name:	2023-07-17 17_00_39-Add actions.png
Views:	767
Size:	32.5 KB
ID:	202694  
ownedbycats is offline   Reply With Quote
Old 07-17-2023, 04:15 PM   #1139
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
@chaley: Small issue, resizing the dialog now only resizes the top half.
@capink: personally I don't care because the odd stretching only happens once you get 9 or more actions in the chain, but if you want to "fix" this then change line 71 in action_dialogs.py from
Code:
        icon_groupbox_layout.addStretch(1)
to
Code:
        icon_groupbox_layout.addWidget(QLabel(''))
chaley is offline   Reply With Quote
Old 07-18-2023, 07:02 AM   #1140
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
if you want to "fix" this then change line 71 in action_dialogs.py from
Fixed.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Editor Plugin] Editor Chains capink Plugins 106 06-17-2025 05:36 PM
Action Chains Resources capink Plugins 77 06-16-2025 12:45 PM
[GUI Plugin] Noosfere_util, a companion plugin to noosfere DB lrpirlet Plugins 2 08-18-2022 03:15 PM
[GUI Plugin] Save Virtual Libraries To Column (GUI) chaley Plugins 14 04-04-2021 05:25 AM


All times are GMT -4. The time now is 04:24 PM.


MobileRead.com is a privately owned, operated and funded community.