1#!/usr/local/bin/python3.8 2# vim:fileencoding=utf-8 3# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> 4 5 6import sys 7from time import monotonic 8 9from calibre.constants import DEBUG 10 11 12def get_current_book_data(set_val=False): 13 if set_val is not False: 14 setattr(get_current_book_data, 'ans', set_val) 15 return getattr(get_current_book_data, 'ans', {}) 16 17 18def link_prefix_for_location_links(add_open_at=True): 19 cbd = get_current_book_data() 20 link_prefix = library_id = None 21 if 'calibre_library_id' in cbd: 22 library_id = cbd['calibre_library_id'] 23 book_id = cbd['calibre_book_id'] 24 book_fmt = cbd['calibre_book_fmt'] 25 elif cbd.get('book_library_details'): 26 bld = cbd['book_library_details'] 27 book_id = bld['book_id'] 28 book_fmt = bld['fmt'].upper() 29 library_id = bld['library_id'] 30 if library_id: 31 library_id = '_hex_-' + library_id.encode('utf-8').hex() 32 link_prefix = f'calibre://view-book/{library_id}/{book_id}/{book_fmt}' 33 if add_open_at: 34 link_prefix += '?open_at=' 35 return link_prefix 36 37 38class PerformanceMonitor: 39 40 def __init__(self): 41 self.start_time = monotonic() 42 43 def __call__(self, desc='', reset=False): 44 if DEBUG: 45 at = monotonic() 46 if reset: 47 self.start_time = at 48 if desc: 49 ts = at - self.start_time 50 print(f'[{ts:.3f}] {desc}', file=sys.stderr) 51 52 53performance_monitor = PerformanceMonitor() 54