mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix id rebasing and add helpful method for test scripts
This commit is contained in:
parent
6b9696867f
commit
a1971fdfda
@ -27,6 +27,34 @@ def strftime(epoch, zone=time.gmtime):
|
|||||||
src[2] = INVERSE_MONTH_MAP[int(src[2])]
|
src[2] = INVERSE_MONTH_MAP[int(src[2])]
|
||||||
return ' '.join(src)
|
return ' '.join(src)
|
||||||
|
|
||||||
|
def get_connected_device():
|
||||||
|
from calibre.customize.ui import device_plugins
|
||||||
|
from calibre.devices.scanner import DeviceScanner
|
||||||
|
dev = None
|
||||||
|
scanner = DeviceScanner()
|
||||||
|
scanner.scan()
|
||||||
|
connected_devices = []
|
||||||
|
for d in device_plugins():
|
||||||
|
ok, det = scanner.is_device_connected(d)
|
||||||
|
if ok:
|
||||||
|
dev = d
|
||||||
|
dev.reset(log_packets=False, detected_device=det)
|
||||||
|
connected_devices.append(dev)
|
||||||
|
|
||||||
|
if dev is None:
|
||||||
|
print >>sys.stderr, 'Unable to find a connected ebook reader.'
|
||||||
|
return
|
||||||
|
|
||||||
|
for d in connected_devices:
|
||||||
|
try:
|
||||||
|
d.open()
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
dev = d
|
||||||
|
break
|
||||||
|
return dev
|
||||||
|
|
||||||
def debug(ioreg_to_tmp=False, buf=None):
|
def debug(ioreg_to_tmp=False, buf=None):
|
||||||
from calibre.customize.ui import device_plugins
|
from calibre.customize.ui import device_plugins
|
||||||
from calibre.devices.scanner import DeviceScanner, win_pnp_drives
|
from calibre.devices.scanner import DeviceScanner, win_pnp_drives
|
||||||
|
@ -173,7 +173,7 @@ class XMLCache(object):
|
|||||||
|
|
||||||
def ensure_numeric_ids(root):
|
def ensure_numeric_ids(root):
|
||||||
idmap = {}
|
idmap = {}
|
||||||
for x in root.xpath('//*[@id]'):
|
for x in root.xpath('child::*[@id]'):
|
||||||
id_ = x.get('id')
|
id_ = x.get('id')
|
||||||
try:
|
try:
|
||||||
id_ = int(id_)
|
id_ = int(id_)
|
||||||
@ -206,7 +206,9 @@ class XMLCache(object):
|
|||||||
for item in root.xpath('//*[@sourceid]'):
|
for item in root.xpath('//*[@sourceid]'):
|
||||||
sid = pl_sourceid if item.tag.endswith('playlist') else sourceid
|
sid = pl_sourceid if item.tag.endswith('playlist') else sourceid
|
||||||
item.set('sourceid', str(sid))
|
item.set('sourceid', str(sid))
|
||||||
items = root.xpath('//*[@id]')
|
# Only rebase ids of nodes that are immediate children of the
|
||||||
|
# record root (that way playlist/itemnodes are unaffected
|
||||||
|
items = root.xpath('child::*[@id]')
|
||||||
items.sort(cmp=lambda x,y:cmp(int(x.get('id')), int(y.get('id'))))
|
items.sort(cmp=lambda x,y:cmp(int(x.get('id')), int(y.get('id'))))
|
||||||
idmap = {}
|
idmap = {}
|
||||||
for i, item in enumerate(items):
|
for i, item in enumerate(items):
|
||||||
@ -214,13 +216,13 @@ class XMLCache(object):
|
|||||||
new = base + i
|
new = base + i
|
||||||
if old != new:
|
if old != new:
|
||||||
item.set('id', str(new))
|
item.set('id', str(new))
|
||||||
idmap[old] = str(new)
|
idmap[old] = str(new)
|
||||||
return idmap
|
return idmap
|
||||||
|
|
||||||
self.prune_empty_playlists()
|
self.prune_empty_playlists()
|
||||||
|
|
||||||
for i in sorted(self.roots.keys()):
|
for i in sorted(self.roots.keys()):
|
||||||
root = self.roots[i]
|
root = self.record_roots[i]
|
||||||
if i == 0:
|
if i == 0:
|
||||||
ensure_media_xml_base_ids(root)
|
ensure_media_xml_base_ids(root)
|
||||||
|
|
||||||
@ -281,6 +283,8 @@ class XMLCache(object):
|
|||||||
playlist_map = self.get_playlist_map()
|
playlist_map = self.get_playlist_map()
|
||||||
|
|
||||||
for i, booklist in booklists.items():
|
for i, booklist in booklists.items():
|
||||||
|
if DEBUG:
|
||||||
|
prints('Updating booklist:', i)
|
||||||
root = self.record_roots[i]
|
root = self.record_roots[i]
|
||||||
for book in booklist:
|
for book in booklist:
|
||||||
path = os.path.join(self.prefixes[i], *(book.lpath.split('/')))
|
path = os.path.join(self.prefixes[i], *(book.lpath.split('/')))
|
||||||
@ -378,7 +382,7 @@ class XMLCache(object):
|
|||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
for i, path in self.paths.items():
|
for i, path in self.paths.items():
|
||||||
raw = etree.tostring(self.roots[i], encoding='utf-8',
|
raw = etree.tostring(self.roots[i], encoding='UTF-8',
|
||||||
xml_declaration=True)
|
xml_declaration=True)
|
||||||
with open(path, 'wb') as f:
|
with open(path, 'wb') as f:
|
||||||
f.write(raw)
|
f.write(raw)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user