![]() |
#1 |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 78,914
Karma: 143095300
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Conversion to Python 3
There are a number of plugins where thh person who created them is no longer active and in the case of Modify ePub, Rev. Bob has said he doesn't know Python 3.
So is anyone willing step up and help do some conversion so these plugins can still be used come Calibre 4.99/5? I don't know Python so I'm not able to help other then maybe to test. |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,313
Karma: 7957679
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Starting in Feb I have some time to look at this issue. No guarantees: I also don't know P3 (but I trust I can learn what I need to know), have no interest in some plugins, and some others might be too opaque. Of course I must first deal with my own ...
![]() I suggest that people propose here moribund-but-used plugins and see if anyone signs up. |
![]() |
![]() |
![]() |
#3 |
null operator (he/him)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 21,598
Karma: 29709834
Join Date: Mar 2012
Location: Sydney Australia
Device: none
|
|
![]() |
![]() |
![]() |
#4 | |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 78,914
Karma: 143095300
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
|
|
![]() |
![]() |
![]() |
#5 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,998
Karma: 89771343
Join Date: Nov 2011
Location: Charlottesville, VA
Device: Kindles
|
That is probably best discussed on Apprentice Harper's GitHub. Any USA-based developers may want to be cautious about openly violating the DMCA's prohibition on developing DRM removal software.
|
![]() |
![]() |
![]() |
#6 | |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 78,914
Karma: 143095300
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
Besides, DRM violates DMCA. |
|
![]() |
![]() |
![]() |
#7 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,335
Karma: 203719142
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
|
![]() |
![]() |
![]() |
#8 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,998
Karma: 89771343
Join Date: Nov 2011
Location: Charlottesville, VA
Device: Kindles
|
Quote:
Huh? |
|
![]() |
![]() |
![]() |
#9 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
Quote:
I'll admit to not knowing Python 3 as well. I have read a couple of brief things, but, I'm following the BFI* method. Start in debug mode, look at the errors for a plugin, fix them, try again. Repeat until it loads. Then test the function and repeat. Then move onto the next plugin. But, apply the changes I did before. There are a few things I know. For example, all the metadata source plugins I am replacing: Code:
from urllib.parse import quote, unquote from urllib import quote, unquote Code:
try: from urllib.parse import quote, unquote except ImportError: from urllib import quote, unquote try: from queue import Empty, Queue except ImportError: from Queue import Empty, Queue import six from six import text_type as unicode Then, any "prints" have to be changed to wrap parentheses around the argument. So, instead of: Code:
print "The code shouldn't have reached here" Code:
print("The code shouldn't have reached here") Code:
except Exception, e: Code:
except Exception as e:
If anyone can see a problem with these suggestions, or has a good list of other things to look for, I'd be happy to see it. (*Brute force and ignorance) |
|
![]() |
![]() |
![]() |
#10 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,144
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
@davidfor Run 2to3 it will print out a diff of all the problems it finds which is a convenient way to see what to change. After that grep for bytes, encode and decode that should cover most things.
|
![]() |
![]() |
![]() |
#11 |
Deviser
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 2,265
Karma: 2090983
Join Date: Aug 2013
Location: Texas
Device: none
|
Simultaneously Supporting Calibre 4.6 & 4.99: Cheat Sheet
I've ported 14 of my 15 GUI plugins such that they are simultaneously compatible with Calibre 4.6 and 4.99.3. During that tedious, lengthy and sometimes painful journey, I kept a list of frequent solutions to problems I encountered. I hope this helps folks.
Code:
# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals from polyglot.builtins import as_bytes, as_unicode, codepoint_to_chr, is_py3, iteritems, map, only_unicode_recursive, range, unicode_type from polyglot.io import PolyglotStringIO if is_py3: from queue import Queue else: from Queue import Queue for k,v in iteritems(my_dict): from calibre.utils.serialize import json_dumps, json_loads if is_py3: from html.parser import HTMLParser as myhtmlparser else: import HTMLParser myhtmlparser = HTMLParser.HTMLParser if is_py3: from urllib.request import urlopen from urllib.parse import urlencode else: from urllib import urlencode from urllib2 import urlopen if is_py3: my_list = as_unicode(my_list) else: my_list = as_bytes(my_list) my_list = ast.literal_eval(my_list) if not isinstance(text,unicode_type): #unicode is a Py2 type; use Polyglot's unicode_type instead. You will see a lot of these errors: TypeError: a bytes-like object is required, not 'str' TypeError: can only concatenate str (not "bytes") to str Py3 redefined what a bytestring is internally: The bytestring 'A' is this in Python 2: A the bytestring 'A' is this in Python 3: (a 3 digit integer) Python3 is not at all forgiving and not at all helpful in converting anything to anything. Hence, errors such as: 'TypeError: can only concatenate str (not "bytes") to str' will happen if you try to concatenate text with, for example, a bytes version of an integer. Ditto for doing a str.replace(x,y); both x and y must be strings, not bytestrings, since a str in Py3 is an instance of 'str', not 'bytes'. DaltonST Last edited by DaltonST; 01-09-2020 at 12:32 PM. Reason: Tips |
![]() |
![]() |
![]() |
#12 | |
Plugin Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,812
Karma: 4604429
Join Date: Dec 2011
Location: Midwest USA
Device: Kindle Paperwhite(10th)
|
Quote:
![]() Plus I've seen very little response to posting py3 plugin test versions. That said, I do plan to release FanFicFare including the py3 changes in the next week or so. That's going to be my first canary. |
|
![]() |
![]() |
![]() |
#13 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,581
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Thank you very much to davidfor, kovigoyal and DaltonST for their comments about the conversion. I have some work to do myself and it will go a good way to begin.
|
![]() |
![]() |
![]() |
#14 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,581
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
|
![]() |
![]() |
![]() |
#15 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
Quote:
And the above was written with my tongue firmly in my cheek ![]() |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Python 2 to Python 3, Polyglot & Plugins | DaltonST | Calibre | 18 | 04-24-2019 12:10 AM |
Is there any Python API for file conversion? | avin | Development | 0 | 12-27-2016 08:11 AM |
"Python function terminated unexpectedly" on ePub to PDF conversion | zunga | Conversion | 10 | 03-17-2013 07:18 PM |
python based pdf conversion tools | KevinH | Conversion | 1 | 01-23-2011 11:39 AM |
Import ebook conversion in python script | erollisi | Calibre | 1 | 08-19-2010 09:43 PM |