09-20-2020, 02:31 PM | #16 |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
The use case I'm looking at is SmartEject plugin searches.
SmartEject runs 3 configurable searches: 1) look for duplicates, 2) books on device not in library and 3) books in library not on device. If I can include device name in the search, I can customize the searches differently for different devices with search logic. I, personally, don't care about remember what's on which device when not connected. I assume, perhaps incorrectly, that compurandom brought this up for the same reason: they asked for device specific searches in the SmartEject thread. Regardless, I think it is something I'd use myself. Can I have a hint on how I'd get device manager/info in a custom template function? The only way I know now is self.gui.device_manager in a plugin. |
09-20-2020, 05:12 PM | #17 | |
Guru
Posts: 919
Karma: 417282
Join Date: Jun 2015
Device: kobo aura h2o, kobo forma
|
Quote:
It's all about book lifecycle, including when it is created/imported, copied to the device, read by me, deleted on the device, and then either deleted from the library or marked as read and left in the library. Obviously, that last step needs to be done by hand when it turns up in a search. I've had this working for a while for one device, but now I want to do it with two devices. Calibre's automatic copy of news to the device is also getting in the way. I need to either turn it off or make it copy to a specific device, rather than the first device that connects, but that's another issue. So with two devices, I have a search for each device to pick what documents to add. But once the document is added, I don't want to re-add it if I delete it from the device. Instead, I need to review it and manually decide on its next state. I think there's a bug in Reading List where it clears the column even if it's only suppose to add to it, which makes it hard to determine if the book was previously sent. It would help if I could set a tag when a book is sent via a "auto populated from search" list. Right now, I have a search for each device that gives the list of books that should be on the device, but aren't; but I can't use that with smarteject because I can't pick which search to use, so I end up with a list of missing books from both devices. As for a test that is true after a device is (recently) disconnected -- that would be OK for me. I think I can get about 80% of what I want by playing with reading list and searches, but it's all so contorted and would be easier and shorter if there was some kind of device specific test either for connectivity (isconnected:devicename) or a book (ondevice:devicename). As for more custom columns from templates -- why can't we just test (some subset of) templates in the search directly? |
|
09-20-2020, 05:36 PM | #18 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
def evaluate(self, formatter, kwargs, mi, locals): from calibre.gui2.ui import get_gui info = get_gui().device_manager.get_current_device_information() v = '' if info is not None: print(info) v = "yes" return v |
|
09-20-2020, 05:46 PM | #19 |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
That won't happen. Search by design looks for data in columns. There is no mechanism in search to evaluate a template during the search, and I am not interested in adding such a thing. First, the performance would be awful because of how calibre caches column values. Second, you can get the same thing by creating and hiding a custom column. And third, it would be hard to do for what appears to be minimal value.
|
09-20-2020, 06:47 PM | #20 | |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
Quote:
It's not polished, obviously, but this does what I wanted: template function: Code:
def evaluate(self, formatter, kwargs, mi, locals, *fnargs): from calibre.gui2.ui import get_gui dm = get_gui().device_manager info = dm.get_current_device_information() v = '' if dm.is_device_present and info is not None: print(info) v = info['info'][4]['main']['device_name'] return v Code:
{:device_info()} ADDED: chaley was right about that. I've updated the function above to also notice when the device is not present. Last edited by JimmXinu; 09-20-2020 at 07:00 PM. |
|
09-20-2020, 07:09 PM | #21 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
One issue I see is that if someone has multiple devices of the same kind, e.g., multiple kindles, then there is no way to disambiguate them using the device name. |
|
09-20-2020, 07:16 PM | #22 | |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
Quote:
That device name is configurable; although I confess I don't know where in vanilla Calibre. ReadingList plugin lets you change it and calls gui.device_manager.set_driveinfo_name(...) |
|
09-21-2020, 04:51 AM | #23 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I have no problem having the template function return that name. The function would need to take a "location code" telling it which of the three possible names to return. I suspect the code will mostly be "main". |
|
09-21-2020, 10:02 AM | #24 |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
I have a version of the 'connected_device_name' built-in template function that returns the configured device name. I also found the problem causing the incorrect value (a value) displayed in the selected book when the device disconnects. There is a race condition in gui2.device.
FYI: here is the source of the function. Code:
def evaluate(self, formatter, kwargs, mi, locals, storage_location): if hasattr(mi, '_proxy_metadata'): # Do the import here so that we don't entangle the GUI when using # command line functions from calibre.gui2.ui import get_gui info = get_gui().device_manager.get_current_device_information() if info is None: return '' try: if storage_location not in {'main', 'carda', 'cardb'}: raise ValueError( _('connected_device_name: invalid storage location "{0}"' .format(storage_location))) info = info['info'][4] if storage_location not in info: return '' return info[storage_location]['device_name'] except: traceback.print_exc() raise return _('This function can be used only in the GUI') |
09-21-2020, 03:18 PM | #25 | |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
I've never done a template function before, so it's entirely possible I'm breaking rules for one or over-complicating it, but this is my version that can optionally return name or uuid for main, carda or cardb; defaulting to main and name.
Quote:
Code:
def evaluate(self, formatter, kwargs, mi, locals, *fnargs): from calibre.gui2.ui import get_gui dm = get_gui().device_manager info = dm.get_current_device_information() loc = 'main' val = 'device_name' for arg in fnargs: arg = arg.lower() if not arg: pass # ignore empty arg -- usually would be column. elif arg in ('main','carda','cardb','a','b'): loc = arg # for some reason, they are A and B in info, not carda and cardb. loc = loc.replace('card','') if loc in ('a','b'): loc = loc.upper() elif arg == 'name': # default is already device_name. pass elif arg == 'uuid': # default is already device_name. val = 'device_store_uuid' else: raise ValueError( _('connected_device_info: invalid argument "{0}"' .format(arg))) if not info or not dm.is_device_present: return '' else: dev_info = info['info'][4] if loc in dev_info and val in dev_info[loc]: return dev_info[loc][val] else: return '' |
|
09-21-2020, 03:31 PM | #26 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
The "not dm.is_device_present" doesn't prevent the bug where stale information is shown. The 'real' fix requires a change to gui2/device.py What would someone do with the UUID in a template? In my original function I added it to the list, but after reflection I don't see use for it. And if you provide that, why not the path, the prefix, or the free space? |
|
09-21-2020, 03:39 PM | #27 | |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
Quote:
(I wonder what happens if the same SD card shows up in a different Nook? Same uuid or different?) To be fair, now that I know how to do it for my own use, I'm relatively indifferent to what ends up in default Calibre--or if it does at all. There seems to be a grand total of two of us who think it's useful at all. |
|
09-21-2020, 04:02 PM | #28 | |||
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
Quote:
Regarding the smarteject search issue, the plugin can get the current device name (as you know). I see two options: support
|
|||
09-21-2020, 04:12 PM | #29 |
Plugin Developer
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
The template function, 'displayed' in a custom column so I can use it in a search, is all I personally needed for SmartEject.
I don't know if it's enough for compurandom's original request. |
10-02-2020, 01:29 PM | #30 |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
I decided to take this on as a challenge and see if I could make templates work efficiently in searches. The answer is yes. it is available in calibre 5.1.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Multiple same book on device: which search expression? | un_pogaz | Library Management | 3 | 12-29-2018 11:50 AM |
Aura Can't search bookstore on my device | MV89 | Kobo Reader | 9 | 11-29-2013 05:21 PM |
Search for Books on Device | random43743 | Calibre | 3 | 04-23-2011 10:44 AM |
search for books not on device | alansplace | Calibre | 2 | 01-05-2011 01:07 PM |
Search restriction bug when device is plugged in | Manichean | Calibre | 6 | 09-11-2010 04:52 PM |