Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 09-20-2020, 02:31 PM   #16
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
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.
JimmXinu is offline   Reply With Quote
Old 09-20-2020, 05:12 PM   #17
compurandom
Guru
compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.
 
Posts: 919
Karma: 417282
Join Date: Jun 2015
Device: kobo aura h2o, kobo forma
Quote:
Originally Posted by JimmXinu View Post
compurandom: Is this all about device specific SmartEject searches, or do you have other use cases as well?
Yes and no.

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?
compurandom is offline   Reply With Quote
Old 09-20-2020, 05:36 PM   #18
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
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.
You can do the same thing in a custom template function. Example:
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
Does this answer your question?
chaley is offline   Reply With Quote
Old 09-20-2020, 05:46 PM   #19
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by compurandom View Post
As for more custom columns from templates -- why can't we just test (some subset of) templates in the search directly?
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.
chaley is offline   Reply With Quote
Old 09-20-2020, 06:47 PM   #20
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
Quote:
Originally Posted by chaley View Post
You can do the same thing in a custom template function. Example:
...
Does this answer your question?
Yes, thank you.

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
custom column template:
Code:
{:device_info()}
See attached screen caps for all the GUI settings.

ADDED:
Quote:
Originally Posted by chaley View Post
I could add a function that returns the connected device name, but it wouldn't be guaranteed to be the empty string for a time after a device disconnects.
chaley was right about that. I've updated the function above to also notice when the device is not present.
Attached Thumbnails
Click image for larger version

Name:	custom column.PNG
Views:	55
Size:	36.5 KB
ID:	182048   Click image for larger version

Name:	template function.PNG
Views:	56
Size:	217.9 KB
ID:	182049  

Last edited by JimmXinu; 09-20-2020 at 07:00 PM.
JimmXinu is offline   Reply With Quote
Old 09-20-2020, 07:09 PM   #21
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
It's not polished, obviously, but this does what I wanted:
[...]
chaley was right about that. I've updated the function above to also notice when the device is not present.
If this works for you with the 'is_device_present' check then I can add it as a standard template function.

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.
chaley is offline   Reply With Quote
Old 09-20-2020, 07:16 PM   #22
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
Quote:
Originally Posted by chaley View Post
If this works for you with the 'is_device_present' check then I can add it as a standard template function.

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.
That would be nice, but not just yet, I would suggest. It needs some polish yet.

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(...)
JimmXinu is offline   Reply With Quote
Old 09-21-2020, 04:51 AM   #23
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
That would be nice, but not just yet, I would suggest. It needs some polish yet.

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(...)
As far as I can tell that method is never called in base calibre. I added it in 2011, I think for Reading List.

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".
chaley is offline   Reply With Quote
Old 09-21-2020, 10:02 AM   #24
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: 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')
chaley is offline   Reply With Quote
Old 09-21-2020, 03:18 PM   #25
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
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:
Function: connected_device_info
Argument count: -1

Device Info for currently attached device.

May be passed 'main', 'carda', 'cardb' for info on a specific device location; default is 'main'.

May also (or instead) be passed 'name' or 'uuid' to get device location name or unique identifier; default is 'name'.
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 ''
JimmXinu is offline   Reply With Quote
Old 09-21-2020, 03:31 PM   #26
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
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.
The function breaks no rules that I know of. The most important rule is that the function is read-only vis-a-vis the database. Next is that the function doesn't try to keep static data. Yours doesn't break either of these.

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?
chaley is offline   Reply With Quote
Old 09-21-2020, 03:39 PM   #27
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
Posts: 6,318
Karma: 3966249
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
Quote:
Originally Posted by chaley View Post
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?
Since Calibre doesn't directly offer a way to change those device names, the uuid might be useful? It's unique re: main vs carda vs cardb. I stuck in a Nook with a card in and both main and carda names came up as 'The Nook'.

(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.
JimmXinu is offline   Reply With Quote
Old 09-21-2020, 04:02 PM   #28
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
Since Calibre doesn't directly offer a way to change those device names, the uuid might be useful? It's unique re: main vs carda vs cardb. I stuck in a Nook with a card in and both main and carda names came up as 'The Nook'.
If all else fails you can use the Reading List plugin to set the name. And a plugin to allow setting the name wouldn't be hard.
Quote:
(I wonder what happens if the same SD card shows up in a different Nook? Same uuid or different?)
Same. The information is stored in "driveinfo.calibre" in the root of the storage device.
Quote:
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.
Well, adding template functions that no one will use isn't the best idea. And I also am not convinced that it isn't better to do "whatever" by hand then attempt an automatic solution.

Regarding the smarteject search issue, the plugin can get the current device name (as you know). I see two options: support
  • searches with a replaceable field that is changed to the current device name.
  • different searches selected by device name. The default would be the device name '*' (all devices). This is a more general and probably better solution.
chaley is offline   Reply With Quote
Old 09-21-2020, 04:12 PM   #29
JimmXinu
Plugin Developer
JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.JimmXinu ought to be getting tired of karma fortunes by now.
 
JimmXinu's Avatar
 
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.
JimmXinu is offline   Reply With Quote
Old 10-02-2020, 01:29 PM   #30
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: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by compurandom View Post
As for more custom columns from templates -- why can't we just test (some subset of) templates in the search directly?
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.
chaley is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 11:54 AM.


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