From 1a7dc8973d1c9e204a2591fe2b15db1134d6bd23 Mon Sep 17 00:00:00 2001 From: "Br. Samuel Springuel" Date: Tue, 26 Aug 2014 20:33:05 -0400 Subject: [PATCH 1/3] New option to indicate whether an existing apnx file on the kindle should be overwritten. This option makes explicit the existing behavior and allows for it to be turned off so that if a book being sent to the kindle already has an apnx file on the kindle, that apnx file is preserved instead of a new one being generated. --- src/calibre/devices/kindle/driver.py | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 3e8fdac86f..fa2cd447fd 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -323,16 +323,29 @@ class KINDLE2(KINDLE): ' store the page count of books, you can have calibre use that' ' information, instead of calculating a page count. Specify the' ' name of the custom column here, for example, #pages.'), + ### New option to indicate whether an existing apnx file on the kindle should be overwritten when sending to the device. + _('Overwrite existing apnx on device') + ':::' + _( + 'Uncheck this option to allow an apnx file existing on the device' + ' to have priority over the version which calibre would send.' + ' Since apnx files are usually deleted when a book is removed from' + ' the Kindle, this is mostly useful when resending a book to the' + ' device which is already on the device (e.g. after making a' + ' modification.)'), + ### ] EXTRA_CUSTOMIZATION_DEFAULT = [ True, 'fast', '', + True, ] OPT_APNX = 0 OPT_APNX_METHOD = 1 OPT_APNX_CUST_COL = 2 + ### New option + OPT_APNX_OVERWRITE = 3 + ### EXTRA_CUSTOMIZATION_CHOICES = {OPT_APNX_METHOD:{'fast', 'accurate', 'pagebreak'}} # x330 on the PaperWhite @@ -456,13 +469,21 @@ class KINDLE2(KINDLE): apnx_path = '%s.apnx' % os.path.join(path, filename) apnx_builder = APNXBuilder() - try: - method = opts.extra_customization[self.OPT_APNX_METHOD] - apnx_builder.write_apnx(filepath, apnx_path, method=method, page_count=custom_page_count) - except: - print 'Failed to generate APNX' - import traceback - traceback.print_exc() + ### Check to see if there is an existing apnx file on Kindle we should keep. + apnx_file = '%s.apnx' % os.path.splitext(filepath.lower())[0] + if not opts.extra_customization[self.OPT_APNX_OVERWRITE] and os.path.isfile(apnx_path): + pass #do nothing, existing apnx file on the kindle is kept + else: + ### This was existing code just dedented one level + try: + method = opts.extra_customization[self.OPT_APNX_METHOD] + apnx_builder.write_apnx(filepath, apnx_path, method=method, page_count=custom_page_count) + except: + print 'Failed to generate APNX' + import traceback + traceback.print_exc() + ### + ### class KINDLE_DX(KINDLE2): From f293a76543565c88b55afda862dc865ac6e8aaab Mon Sep 17 00:00:00 2001 From: "Br. Samuel Springuel" Date: Fri, 29 Aug 2014 13:39:04 -0400 Subject: [PATCH 2/3] New option to override page count calculation method on book-by-book basis A new option is provided which allows the user to create a custom column which specifies which apnx calculation method to use for a particular book. The original method option is treated as a default which is overridden in cases where a valid method is given in the specified column. This is useful, for example, in cases where the user has a small number of books which are setup for the pagebreak method and wants to use the accurate method most of the rest of the time. Because this option was introduced into the option list before the overwrite option, I've also included code which should shift the options around accordingly in cases where the overwrite option has been implemented and used prior to applying this change. --- src/calibre/devices/kindle/driver.py | 32 ++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index fa2cd447fd..ab504eea0a 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -323,6 +323,14 @@ class KINDLE2(KINDLE): ' store the page count of books, you can have calibre use that' ' information, instead of calculating a page count. Specify the' ' name of the custom column here, for example, #pages.'), + ### New option to override page count calculation method on book-by-book basis + _('Custom column name to retrieve calculation method from') + ':::' + _( + 'If you have a custom column in your library that you use to' + ' store the preferred method for calculating the number of pages' + ' for a book, you can have calibre use that method instead of the' + ' default one selected above. Specify the name of the custom' + ' column here, for example, #pagemethod.'), + ### ### New option to indicate whether an existing apnx file on the kindle should be overwritten when sending to the device. _('Overwrite existing apnx on device') + ':::' + _( 'Uncheck this option to allow an apnx file existing on the device' @@ -338,13 +346,15 @@ class KINDLE2(KINDLE): True, 'fast', '', + '', True, ] OPT_APNX = 0 OPT_APNX_METHOD = 1 OPT_APNX_CUST_COL = 2 - ### New option - OPT_APNX_OVERWRITE = 3 + ### New options + OPT_APNX_METHOD_COL = 3 + OPT_APNX_OVERWRITE = 4 ### EXTRA_CUSTOMIZATION_CHOICES = {OPT_APNX_METHOD:{'fast', 'accurate', 'pagebreak'}} @@ -357,6 +367,10 @@ class KINDLE2(KINDLE): if isinstance(vals[cls.OPT_APNX_METHOD], bool): # Previously this option used to be a bool vals[cls.OPT_APNX_METHOD] = 'accurate' if vals[cls.OPT_APNX_METHOD] else 'fast' + if isinstance(vals[cls.OPT_APNX_METHOD_COL], bool): + # The method column option was added before the overwrite option, so we need to shift the options around a bit + vals[cls.OPT_APNX_OVERWRITE] = vals[cls.OPT_APNX_METHOD_COL] + vals[cls.OPT_APNX_METHOD_COL] = '' return vals def formats_to_scan_for(self): @@ -477,6 +491,20 @@ class KINDLE2(KINDLE): ### This was existing code just dedented one level try: method = opts.extra_customization[self.OPT_APNX_METHOD] + ### New code to retreive override method, if available + cust_col_name = opts.extra_customization[self.OPT_APNX_METHOD_COL] + if cust_col_name: + try: + temp = str(metadata.get(cust_col_name, 0)) + if temp not in self.EXTRA_CUSTOMIZATION_CHOICES[self.OPT_APNX_METHOD]: + print "Invalid method choice for this book, reverting to default." + else: + method = temp + except: + print 'Could not retreive override method choice, reverting to default.' + pass + print 'Generating apnx with', method + ### apnx_builder.write_apnx(filepath, apnx_path, method=method, page_count=custom_page_count) except: print 'Failed to generate APNX' From 9cf8c41b1ca108537e0351752b8b5bd7a8324107 Mon Sep 17 00:00:00 2001 From: "Br. Samuel Springuel" Date: Sat, 30 Aug 2014 09:56:10 -0400 Subject: [PATCH 3/3] Removing superfluous code. --- src/calibre/devices/kindle/driver.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index ab504eea0a..bf31ee0836 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -484,7 +484,6 @@ class KINDLE2(KINDLE): apnx_path = '%s.apnx' % os.path.join(path, filename) apnx_builder = APNXBuilder() ### Check to see if there is an existing apnx file on Kindle we should keep. - apnx_file = '%s.apnx' % os.path.splitext(filepath.lower())[0] if not opts.extra_customization[self.OPT_APNX_OVERWRITE] and os.path.isfile(apnx_path): pass #do nothing, existing apnx file on the kindle is kept else: