|
|
#1126 | |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,216
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
|
|
|
|
|
|
|
#1127 |
|
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,334
Karma: 79528341
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Thanks
|
|
|
|
| Advert | |
|
|
|
|
#1128 |
|
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,334
Karma: 79528341
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.
|
|
|
|
|
|
#1129 | |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Or leave it in a title SFE and return the current title so nothing changes. |
|
|
|
|
|
|
#1130 |
|
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,334
Karma: 79528341
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That did it.
|
|
|
|
| Advert | |
|
|
|
|
#1131 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
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:
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? |
|
|
|
|
|
#1132 | |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,216
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
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. |
|
|
|
|
|
|
#1133 | |||
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
Code:
utils.formatter_functions.function_object_type(template) Quote:
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:||]}')
Code:
python:
def evaluate(book, context):
return '\n'.join(context.globals.get('output', ''))
Code:
program: return globals(has_output) != '' 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'
|
|||
|
|
|
|
|
#1134 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
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. Last edited by chaley; 07-17-2023 at 03:32 PM. |
|
|
|
|
|
#1135 |
|
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,334
Karma: 79528341
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I would use this if implemented.
|
|
|
|
|
|
#1136 | |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,216
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
|
|
|
|
|
|
|
#1137 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
|
|
|
|
|
|
#1138 |
|
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,334
Karma: 79528341
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.
|
|
|
|
|
|
#1139 | |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
icon_groupbox_layout.addStretch(1) Code:
icon_groupbox_layout.addWidget(QLabel(''))
|
|
|
|
|
|
|
#1140 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,216
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
|
|
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Action Chains Resources | capink | Plugins | 80 | 09-18-2025 04:45 AM |
| [Editor Plugin] Editor Chains | capink | Plugins | 106 | 06-17-2025 05:36 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 |