diff sat/plugins/plugin_blog_import_dokuwiki.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_blog_import_dokuwiki.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_blog_import_dokuwiki.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SàT plugin to import dokuwiki blogs
@@ -28,8 +28,8 @@
 from twisted.internet import threads
 from collections import OrderedDict
 import calendar
-import urllib
-import urlparse
+import urllib.request, urllib.parse, urllib.error
+import urllib.parse
 import tempfile
 import re
 import time
@@ -39,13 +39,13 @@
     from dokuwiki import DokuWiki, DokuWikiError  # this is a new dependency
 except ImportError:
     raise exceptions.MissingModule(
-        u'Missing module dokuwiki, please install it with "pip install dokuwiki"'
+        'Missing module dokuwiki, please install it with "pip install dokuwiki"'
     )
 try:
     from PIL import Image  # this is already needed by plugin XEP-0054
 except:
     raise exceptions.MissingModule(
-        u"Missing module pillow, please download/install it from https://python-pillow.github.io"
+        "Missing module pillow, please download/install it from https://python-pillow.github.io"
     )
 
 PLUGIN_INFO = {
@@ -58,10 +58,10 @@
     C.PI_DESCRIPTION: _("""Blog importer for Dokuwiki blog engine."""),
 }
 
-SHORT_DESC = D_(u"import posts from Dokuwiki blog engine")
+SHORT_DESC = D_("import posts from Dokuwiki blog engine")
 
 LONG_DESC = D_(
-    u"""This importer handle Dokuwiki blog engine.
+    """This importer handle Dokuwiki blog engine.
 
 To use it, you need an admin access to a running Dokuwiki website
 (local or on the Internet). The importer retrieves the data using
@@ -129,7 +129,7 @@
         @param post(dict): parsed post data
         @return (unicode): post unique item id
         """
-        return unicode(post["id"])
+        return str(post["id"])
 
     def getPostUpdated(self, post):
         """Return the update date.
@@ -137,7 +137,7 @@
         @param post(dict): parsed post data
         @return (unicode): update date
         """
-        return unicode(post["mtime"])
+        return str(post["mtime"])
 
     def getPostPublished(self, post):
         """Try to parse the date from the message ID, else use "mtime".
@@ -148,7 +148,7 @@
         @param post (dict):  parsed post data
         @return (unicode): publication date
         """
-        id_, default = unicode(post["id"]), unicode(post["mtime"])
+        id_, default = str(post["id"]), str(post["mtime"])
         try:
             date = id_.split(":")[-1].split("_")[0]
         except KeyError:
@@ -160,7 +160,7 @@
                 time_struct = time.strptime(date, "%Y%m%d")
             except ValueError:
                 return default
-        return unicode(calendar.timegm(time_struct))
+        return str(calendar.timegm(time_struct))
 
     def processPost(self, post, profile_jid):
         """Process a single page.
@@ -235,7 +235,7 @@
             if count >= self.limit:
                 break
 
-        return (self.posts_data.itervalues(), len(self.posts_data))
+        return (iter(self.posts_data.values()), len(self.posts_data))
 
     def processContent(self, text, backlinks, profile_jid):
         """Do text substitutions and file copy.
@@ -243,7 +243,7 @@
         @param text (unicode): message content
         @param backlinks (list[unicode]): list of backlinks
         """
-        text = text.strip(u"\ufeff")  # this is at the beginning of the file (BOM)
+        text = text.strip("\ufeff")  # this is at the beginning of the file (BOM)
 
         for backlink in backlinks:
             src = '/doku.php?id=%s"' % backlink
@@ -261,9 +261,9 @@
             if self.media_repo:
                 self.moveMedia(link, subs)
             elif link not in subs:
-                subs[link] = urlparse.urljoin(self.url, link)
+                subs[link] = urllib.parse.urljoin(self.url, link)
 
-        for url, new_url in subs.iteritems():
+        for url, new_url in subs.items():
             text = text.replace(url, new_url)
         return text
 
@@ -274,12 +274,12 @@
         @param link (unicode): media link
         @param subs (dict): substitutions data
         """
-        url = urlparse.urljoin(self.url, link)
+        url = urllib.parse.urljoin(self.url, link)
         user_media = re.match(r"(/lib/exe/\w+.php\?)(.*)", link)
         thumb_width = None
 
         if user_media:  # media that has been added by the user
-            params = urlparse.parse_qs(urlparse.urlparse(url).query)
+            params = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)
             try:
                 media = params["media"][0]
             except KeyError:
@@ -295,7 +295,7 @@
 
             filename = media.replace(":", "/")
             # XXX: avoid "precondition failed" error (only keep the media parameter)
-            url = urlparse.urljoin(self.url, "/lib/exe/fetch.php?media=%s" % media)
+            url = urllib.parse.urljoin(self.url, "/lib/exe/fetch.php?media=%s" % media)
 
         elif link.startswith("/lib/plugins/"):
             # other link added by a plugin or something else
@@ -324,7 +324,7 @@
         if not os.path.exists(dest):
             if not os.path.exists(dirname):
                 os.makedirs(dirname)
-            urllib.urlretrieve(source, dest)
+            urllib.request.urlretrieve(source, dest)
             log.debug("DokuWiki media file copied to %s" % dest)
 
     def createThumbnail(self, source, dest, width):