![]() |
#1216 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
My suggestion for you is to save the above command as a batch file (I suppose that is what you mean by cmd file), and add the path to the batch file using the "choose binary" button. This way you don't need to set the cmd file as the default app. Edit: If the above solution fails (maybe batch files cannot be executed the same way bash files are in linux), you can use the following code in "Run Python Code Action" Code:
import subprocess def run(gui, settings, chain): db = gui.current_db output_dir = 'D:\Temp' for book_id in chain.scope().get_book_ids(): path_to_txtz = db.format_abspath(book_id, 'TXTZ', index_is_id=True) title = db.title(book_id, index_is_id=True) if not path_to_txtz: print(f'Book {title} does not have format: TXTZ') continue cmd = f'"C:\Program Files\7-Zip\7z.exe" e {path_to_txtz} -o"{output_dir}" file-to-extract -y' result = subprocess.run(cmd) if result.returncode != 0: prints(f'Command error: Return code is {result.returncode} when running command for file: {path_to_txtz}\ncmd: {cmd}') sys.stdout.buffer.write(b'stderr: ' + result.stderr) prints() Last edited by capink; 01-10-2024 at 02:47 AM. |
|
![]() |
![]() |
![]() |
#1217 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
Quote:
1) the path to the batch file (yes, cmd file is the batch file) was entered to the "Choose binary" box. It was my first choice. But it doesn't run. I think the path of the file is not sent to the cmd file. 2) Thank for your code, but it returns error (with Run Python Code action) Code:
calibre 7.3 embedded-python: True Windows-10-10.0.19045-SP0 Windows ('64bit', 'WindowsPE') ('Windows', '10', '10.0.19045') Python 3.11.5 Windows: ('10', '10.0.19045', 'SP0', 'Multiprocessor Free') Interface language: None Successfully initialized third party plugins: Action Chains (1, 18, 20) && Count Pages (1, 13, 4) && Editor Chains (1, 1, 0) && EpubMerge (2, 17, 0) && Goodreads (1, 7, 9) && Goodreads Sync (1, 16, 4) && Resize Cover (1, 2, 0) Traceback (most recent call last): File "calibre_plugins.action_chains.action", line 451, in run_chain File "calibre_plugins.action_chains.chains", line 403, in run File "calibre_plugins.action_chains.chains", line 208, in _run_loop File "calibre_plugins.action_chains.chains", line 181, in _run_loop File "calibre_plugins.action_chains.actions.code", line 174, in run File "module", line 13, in run File "subprocess.py", line 548, in run File "subprocess.py", line 1026, in __init__ File "subprocess.py", line 1538, in _execute_child FileNotFoundError: [WinError 2] The system cannot find the file specified |
|
![]() |
![]() |
![]() |
#1218 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
OK. Try this:
Code:
import subprocess import os from calibre.constants import iswindows, isosx def run(gui, settings, chain): fmt = 'TXTZ' db = gui.current_db output_dir = 'D:\Temp' path_to_binary = 'C:\Program Files\7-Zip\7z.exe' clean_env = dict(os.environ) kw = {'env': clean_env, 'shell':True, 'capture_output': True} if iswindows: kw['DETACHED_PROCESS'] = 0x00000008 del clean_env['PATH'] elif isosx: if path_to_binary.lower().endswith(".app"): path_to_binary = 'open -a ' + path_to_binary else: #Linux clean_env['LD_LIBRARY_PATH'] = '' for book_id in chain.scope().get_book_ids(): path_to_format = db.format_abspath(book_id, fmt, index_is_id=True) title = db.title(book_id, index_is_id=True) if not path_to_format: print(f'Book {title} does not have format: {fmt}') continue cmd = f'"{path_to_binary}" e {path_to_format} -o"{output_dir}" file-to-extract -y' result = subprocess.run(cmd, **kw) if result.returncode != 0: prints(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}') sys.stdout.buffer.write(b'stderr: ' + result.stderr) prints() Last edited by capink; 01-10-2024 at 04:26 AM. |
![]() |
![]() |
![]() |
#1219 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
Sorry to have bugged you this much.
But it returns new error Code:
File "subprocess.py", line 548, in run TypeError: Popen.__init__() got an unexpected keyword argument 'DETACHED_PROCESS' |
![]() |
![]() |
![]() |
#1220 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
Anyway, try this: Code:
import subprocess import os from calibre.constants import iswindows, isosx def run(gui, settings, chain): fmt = 'TXTZ' db = gui.current_db output_dir = 'D:\Temp' path_to_binary = 'C:\Program Files\7-Zip\7z.exe' clean_env = dict(os.environ) kw = {'env': clean_env, 'shell':True, 'capture_output': True} if iswindows: del clean_env['PATH'] elif isosx: if path_to_binary.lower().endswith(".app"): path_to_binary = 'open -a ' + path_to_binary else: #Linux clean_env['LD_LIBRARY_PATH'] = '' for book_id in chain.scope().get_book_ids(): path_to_format = db.format_abspath(book_id, fmt, index_is_id=True) title = db.title(book_id, index_is_id=True) if not path_to_format: print(f'Book {title} does not have format: {fmt}') continue cmd = f'"{path_to_binary}" e {path_to_format} -o"{output_dir}" file-to-extract -y' result = subprocess.run(cmd, **kw) if result.returncode != 0: prints(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}') sys.stdout.buffer.write(b'stderr: ' + result.stderr) prints() |
|
![]() |
![]() |
![]() |
#1221 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
I have a feeling that it's almost there. Because it has now come to the print command.
Code:
NameError: name 'prints' is not defined Code:
File "module", line 29, in run NameError: name 'sys' is not defined Last edited by nqk; 01-10-2024 at 05:57 AM. |
![]() |
![]() |
![]() |
#1222 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
Code:
import subprocess import os from calibre.constants import iswindows, isosx import sys def run(gui, settings, chain): fmt = 'TXTZ' db = gui.current_db output_dir = 'D:\Temp' path_to_binary = 'C:\Program Files\7-Zip\7z.exe' clean_env = dict(os.environ) kw = {'env': clean_env, 'shell':True, 'capture_output': True} if iswindows: del clean_env['PATH'] elif isosx: if path_to_binary.lower().endswith(".app"): path_to_binary = 'open -a ' + path_to_binary else: #Linux clean_env['LD_LIBRARY_PATH'] = '' for book_id in chain.scope().get_book_ids(): path_to_format = db.format_abspath(book_id, fmt, index_is_id=True) title = db.title(book_id, index_is_id=True) if not path_to_format: print(f'Book {title} does not have format: {fmt}') continue cmd = f'"{path_to_binary}" e {path_to_format} -o"{output_dir}" file-to-extract -y' result = subprocess.run(cmd, **kw) if result.returncode != 0: print(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}') sys.stdout.buffer.write(b'stderr: ' + result.stderr) print() |
|
![]() |
![]() |
![]() |
#1223 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
With this piece of codes, it went through but did not do the job. No extraction was made. And if I run this on a book without TXTZ format, no error message pops up.
|
![]() |
![]() |
![]() |
#1224 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
In both cases it does print an output, but you have to use calibre-debug -g
|
![]() |
![]() |
![]() |
#1225 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
Quote:
Code:
cmd: "C:\Program Files-Zipz.exe" e C:\Users\my path\to\xyz (12248)\xyz- adbc.txtz -o"D:\Temp" images/abc.jpg -y stderr: The filename, directory name, or volume label syntax is incorrect. Last edited by nqk; 01-10-2024 at 08:09 AM. |
|
![]() |
![]() |
![]() |
#1226 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
This should solve both issues, hopefully:
Code:
import subprocess import os import sys from calibre.constants import iswindows, isosx #import pathlib #exec = pathlib.PureWindowsPath(path).as_posix() def run(gui, settings, chain): fmt = 'TXTZ' db = gui.current_db output_dir = 'D:\Temp' path_to_binary = 'C:\Program Files\\7-Zip\\7z.exe' clean_env = dict(os.environ) kw = {'env': clean_env, 'shell':True, 'capture_output': True} if iswindows: del clean_env['PATH'] path_to_binary = os.path.normpath(path_to_binary) elif isosx: if path_to_binary.lower().endswith(".app"): path_to_binary = 'open -a ' + path_to_binary else: #Linux clean_env['LD_LIBRARY_PATH'] = '' for book_id in chain.scope().get_book_ids(): path_to_format = db.format_abspath(book_id, fmt, index_is_id=True) title = db.title(book_id, index_is_id=True) if not path_to_format: print(f'Book {title} does not have format: {fmt}') continue if iswindows: path_to_format = os.path.normpath(path_to_format) cmd = f'"{path_to_binary}" e "{path_to_format}" -o"{output_dir}" file-to-extract -y' result = subprocess.run(cmd, **kw) if result.returncode != 0: print(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}') sys.stdout.buffer.write(b'stderr: ' + result.stderr) print() Last edited by capink; 01-10-2024 at 11:18 PM. |
![]() |
![]() |
![]() |
#1227 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
Thank you for your work.
I have tested and it could extract the file successfully. The purpose is fulfilled. ![]() For testing purpose, I ran it on a book without txtz format, it returned: Code:
File "ntpath.py", line 540, in normpath TypeError: expected str, bytes or os.PathLike object, not NoneType ![]() and {path_to_format} looks like a built-in variable. Where can I find the list of built-in variables like this? I'm interested in path to current book folder [current book data folder]. I realize that if this code works out and if I can get necessary variables, I can have external apps (command line interface) do a lot of things. Last edited by nqk; 01-10-2024 at 10:12 PM. |
![]() |
![]() |
![]() |
#1228 | ||
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
Quote:
You can help test by trying out these three scenarios:
Last edited by capink; 01-13-2024 at 01:51 AM. Reason: Remove zip attachment |
||
![]() |
![]() |
![]() |
#1229 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 564
Karma: 32228
Join Date: Feb 2012
Device: Onyx Boox Leaf
|
1) and 2) don't work. Debug window doesn't show any error, but saying "Action Chains: chain (test Open With) finished in: 0:00:00.078117"
3) works. And I personally prefer this. Looks pretty much easy to understand. Even if 1 and 2 worked, I would use 3) as it doesn't require external setup. If "containing folder" is selected, the variable name is still {inputfile}? Last edited by nqk; 01-11-2024 at 10:31 AM. |
![]() |
![]() |
![]() |
#1230 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
Yes. It passes for any option you choose in the path groupbox including "containing folder", covers and templates. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
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 |