From 109261bdc01ad8e05b1af744cc044233dfe3bae6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Sep 2020 00:36:25 +0200 Subject: [PATCH 1/5] GitHub Action test on both Python 2 and Python 3 --- .github/workflows/Python_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Python_tests.yml b/.github/workflows/Python_tests.yml index 57b08ce..65215df 100644 --- a/.github/workflows/Python_tests.yml +++ b/.github/workflows/Python_tests.yml @@ -7,7 +7,7 @@ jobs: fail-fast: false matrix: os: [macos-latest] - python-version: [2.7] # , 3.8] + python-version: [2.7, 3.8] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} From 08bdacf476dab2ff2e2ed1eefda109cbf42954a3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Sep 2020 00:39:57 +0200 Subject: [PATCH 2/5] Fix Python syntax error: add a comma Discovered by flake8 running in our GitHub Action --- DeDRM_plugin/erdr2pml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DeDRM_plugin/erdr2pml.py b/DeDRM_plugin/erdr2pml.py index 401fecc..bd3eeff 100644 --- a/DeDRM_plugin/erdr2pml.py +++ b/DeDRM_plugin/erdr2pml.py @@ -516,7 +516,7 @@ def decryptBook(infile, outpath, make_pmlz, user_key): # remove temporary directory shutil.rmtree(outdir, True) print("Output is {0}".format(pmlzname)) - else + else: print("Output is in {0}".format(outdir)) print("done") except ValueError as e: From 3f644ddfd61d5258ad3523fa291c82dcc4e014d3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Sep 2020 00:49:21 +0200 Subject: [PATCH 3/5] print() is a function in Python since 1/1/2020 --- DeDRM_plugin/ignoblepdf.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/DeDRM_plugin/ignoblepdf.py b/DeDRM_plugin/ignoblepdf.py index 1546fa1..33e84b1 100644 --- a/DeDRM_plugin/ignoblepdf.py +++ b/DeDRM_plugin/ignoblepdf.py @@ -19,6 +19,7 @@ """ Decrypts Barnes & Noble encrypted PDF files. """ +from __future__ import print_function __license__ = 'GPL v3' __version__ = "0.1" @@ -428,7 +429,7 @@ class PSBaseParser(object): if not pos: pos = self.bufpos+self.charpos self.fp.seek(pos) - ##print >>sys.stderr, 'poll(%d): %r' % (pos, self.fp.read(n)) + # print('poll(%d): %r' % (pos, self.fp.read(n)), file=sys.stderr) self.fp.seek(pos0) return @@ -753,7 +754,7 @@ class PSStackParser(PSBaseParser): ''' while not self.results: (pos, token) = self.nexttoken() - ##print (pos,token), (self.curtype, self.curstack) + # print((pos, token), (self.curtype, self.curstack)) if (isinstance(token, int) or isinstance(token, Decimal) or isinstance(token, bool) or @@ -778,7 +779,7 @@ class PSStackParser(PSBaseParser): try: (pos, objs) = self.end_type('d') if len(objs) % 2 != 0: - print "Incomplete dictionary construct" + print("Incomplete dictionary construct") objs.append("") # this isn't necessary. # temporary fix. is this due to rental books? # raise PSSyntaxError( @@ -1003,7 +1004,7 @@ class PDFStream(PDFObject): if 'Filter' not in self.dic: self.data = data self.rawdata = None - ##print self.dict + ##print(self.dict) return filters = self.dic['Filter'] if not isinstance(filters, list): @@ -1447,15 +1448,15 @@ class PDFDocument(object): V = ord(bookkey[0]) bookkey = bookkey[1:] else: - print "ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type) - print "length is %d and len(bookkey) is %d" % (length, len(bookkey)) - print "bookkey[0] is %d" % ord(bookkey[0]) + print("ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type)) + print("length is %d and len(bookkey) is %d" % (length, len(bookkey))) + print("bookkey[0] is %d" % ord(bookkey[0])) raise IGNOBLEError('error decrypting book session key - mismatched length') else: # proper length unknown try with whatever you have - print "ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type) - print "length is %d and len(bookkey) is %d" % (length, len(bookkey)) - print "bookkey[0] is %d" % ord(bookkey[0]) + print("ebx_V is %d and ebx_type is %d" % (ebx_V, ebx_type)) + print("length is %d and len(bookkey) is %d" % (length, len(bookkey))) + print("bookkey[0] is %d" % ord(bookkey[0])) if ebx_V == 3: V = 3 else: @@ -1500,7 +1501,7 @@ class PDFDocument(object): plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data) # remove pkcs#5 aes padding cutter = -1 * ord(plaintext[-1]) - #print cutter + #print(cutter) plaintext = plaintext[:cutter] return plaintext @@ -1511,7 +1512,7 @@ class PDFDocument(object): plaintext = AES.new(key,AES.MODE_CBC,ivector).decrypt(data) # remove pkcs#5 aes padding cutter = -1 * ord(plaintext[-1]) - #print cutter + #print(cutter) plaintext = plaintext[:cutter] return plaintext @@ -2010,7 +2011,7 @@ def decryptBook(userkey, inpath, outpath): #try: serializer = PDFSerializer(inf, userkey) #except: - # print "Error serializing pdf {0}. Probably wrong key.".format(os.path.basename(inpath)) + # print("Error serializing pdf {0}. Probably wrong key.".format(os.path.basename(inpath))) # return 2 # hope this will fix the 'bad file descriptor' problem with open(outpath, 'wb') as outf: @@ -2018,7 +2019,7 @@ def decryptBook(userkey, inpath, outpath): try: serializer.dump(outf) except Exception, e: - print "error writing pdf: {0}".format(e.args[0]) + print("error writing pdf: {0}".format(e.args[0])) return 2 return 0 @@ -2029,13 +2030,13 @@ def cli_main(): argv=unicode_argv() progname = os.path.basename(argv[0]) if len(argv) != 4: - print "usage: {0} ".format(progname) + print("usage: {0} ".format(progname)) return 1 keypath, inpath, outpath = argv[1:] userkey = open(keypath,'rb').read() result = decryptBook(userkey, inpath, outpath) if result == 0: - print "Successfully decrypted {0:s} as {1:s}".format(os.path.basename(inpath),os.path.basename(outpath)) + print("Successfully decrypted {0:s} as {1:s}".format(os.path.basename(inpath),os.path.basename(outpath))) return result From 1eaee6a0a8ef75c1ff671775c1c3313b2b7d0e91 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Sep 2020 01:00:21 +0200 Subject: [PATCH 4/5] Old style exceptions are syntax errors in Python 3 Switch to new style exceptions which work on both Python 2 and Python 3. --- DeDRM_plugin/ignoblepdf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DeDRM_plugin/ignoblepdf.py b/DeDRM_plugin/ignoblepdf.py index 33e84b1..2b193cb 100644 --- a/DeDRM_plugin/ignoblepdf.py +++ b/DeDRM_plugin/ignoblepdf.py @@ -2018,7 +2018,7 @@ def decryptBook(userkey, inpath, outpath): # help construct to make sure the method runs to the end try: serializer.dump(outf) - except Exception, e: + except Exception as e: print("error writing pdf: {0}".format(e.args[0])) return 2 return 0 @@ -2138,7 +2138,7 @@ def gui_main(): self.status['text'] = "Decrypting..." try: decrypt_status = decryptBook(userkey, inpath, outpath) - except Exception, e: + except Exception as e: self.status['text'] = "Error; {0}".format(e.args[0]) return if decrypt_status == 0: From 558efebbff2ec591ef47911cca9eb550d6ccd343 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 28 Sep 2020 01:03:30 +0200 Subject: [PATCH 5/5] Update genbook.py --- DeDRM_plugin/genbook.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DeDRM_plugin/genbook.py b/DeDRM_plugin/genbook.py index 791bc5e..fdf3707 100644 --- a/DeDRM_plugin/genbook.py +++ b/DeDRM_plugin/genbook.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # Python 3 for calibre 5.0 +from __future__ import print_function class Unbuffered: def __init__(self, stream): @@ -92,7 +93,7 @@ def getMetaArray(metaFile): tag = readString(fo) value = readString(fo) result[tag] = value - # print tag, value + # print(tag, value) fo.close() return result