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 ''