Permalink
Browse files

fix imports for Python 2 & 3

  • Loading branch information...
1 parent f33ae2e commit 30cd751c1e17a463771b6f2f2df8805620147d8c @Fantomas42 committed Apr 10, 2013
View
@@ -1,5 +1,9 @@
"""Feeds for Zinnia"""
-from urlparse import urljoin
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
+
from BeautifulSoup import BeautifulSoup
from django.contrib import comments
@@ -1,9 +1,12 @@
"""Feed to Zinnia command module"""
import os
import sys
-from urllib2 import urlopen
from datetime import datetime
from optparse import make_option
+try:
+ from urllib.request import urlopen
+except ImportError: # Python 2
+ from urllib2 import urlopen
from django.conf import settings
from django.utils import timezone
@@ -1,10 +1,13 @@
"""WordPress to Zinnia command module"""
import os
import sys
-from urllib2 import urlopen
from datetime import datetime
from optparse import make_option
from xml.etree import ElementTree as ET
+try:
+ from urllib.request import urlopen
+except ImportError: # Python 2
+ from urllib2 import urlopen
from django.conf import settings
from django.utils import timezone
View
@@ -1,10 +1,17 @@
"""Pings utilities for Zinnia"""
import socket
-import xmlrpclib
import threading
-from urllib2 import urlopen
-from urlparse import urlsplit
from logging import getLogger
+try:
+ from urllib.request import urlopen
+ from urllib.parse import urlsplit
+ from xmlrpc.client import Error
+ from xmlrpc.client import ServerProxy
+except ImportError: # Python 2
+ from urllib2 import urlopen
+ from urlparse import urlsplit
+ from xmlrpclib import Error
+ from xmlrpclib import ServerProxy
from BeautifulSoup import BeautifulSoup
@@ -35,7 +42,7 @@ def __init__(self, server_name, entries, timeout=10, start_now=True):
self.timeout = timeout
self.entries = entries
self.server_name = server_name
- self.server = xmlrpclib.ServerProxy(self.server_name)
+ self.server = ServerProxy(self.server_name)
self.ressources = URLRessources()
threading.Thread.__init__(self)
@@ -161,8 +168,8 @@ def find_pingback_urls(self, urls):
def pingback_url(self, server_name, target_url):
"""Do a pingback call for the target url"""
try:
- server = xmlrpclib.ServerProxy(server_name)
+ server = ServerProxy(server_name)
reply = server.pingback.ping(self.entry_url, target_url)
- except (xmlrpclib.Error, socket.error):
+ except (Error, socket.error):
reply = '%s cannot be pinged.' % target_url
return reply
@@ -1,7 +1,10 @@
"""Template tags and filters for Zinnia"""
from hashlib import md5
-from urllib import urlencode
from datetime import datetime
+try:
+ from urllib.parse import urlencode
+except ImportError: # Python 2
+ from urllib import urlencode
from django.db.models import Q
from django.db.models import Count
View
@@ -1,5 +1,8 @@
"""Test cases for Zinnia's feeds"""
-from urlparse import urljoin
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
from django.test import TestCase
from django.contrib import comments
View
@@ -1,7 +1,12 @@
"""Test cases for Zinnia's MetaWeblog API"""
-from xmlrpclib import Binary
-from xmlrpclib import Fault
-from xmlrpclib import ServerProxy
+try:
+ from xmlrpc.client import Binary
+ from xmlrpc.client import Fault
+ from xmlrpc.client import ServerProxy
+except ImportError: # Python 2
+ from xmlrpclib import Binary
+ from xmlrpclib import Fault
+ from xmlrpclib import ServerProxy
from tempfile import TemporaryFile
from django.test import TestCase
View
@@ -1,7 +1,12 @@
"""Test cases for Zinnia's ping"""
-import cStringIO
-from urllib2 import URLError
from urllib import addinfourl
+try:
+ from io import StringIO
+ from urllib.error import URLError
+except ImportError: # Python 2
+ from cStringIO import StringIO
+ from urllib2 import URLError
+
from django.test import TestCase
from zinnia.models.entry import Entry
@@ -89,15 +94,15 @@ def test_find_pingback_href(self):
def fake_urlopen(self, url):
"""Fake urlopen using test client"""
if 'example' in url:
- response = cStringIO.StringIO('')
+ response = StringIO('')
return addinfourl(response, {'X-Pingback': '/xmlrpc.php',
'Content-Type': 'text/html'}, url)
elif 'localhost' in url:
- response = cStringIO.StringIO(
+ response = StringIO(
'<link rel="pingback" href="/xmlrpc/">')
return addinfourl(response, {'Content-Type': 'text/xhtml'}, url)
elif 'google' in url:
- response = cStringIO.StringIO('PNG CONTENT')
+ response = StringIO('PNG CONTENT')
return addinfourl(response, {'content-type': 'image/png'}, url)
elif 'error' in url:
raise URLError('Invalid ressource')
View
@@ -1,8 +1,14 @@
"""Test cases for Zinnia's PingBack API"""
-import cStringIO
-from urlparse import urlsplit
-from urllib2 import HTTPError
-from xmlrpclib import ServerProxy
+try:
+ from io import StringIO
+ from urllib.error import HTTPError
+ from urllib.parse import urlsplit
+ from xmlrpc.client import ServerProxy
+except ImportError: # Python 2
+ from cStringIO import StringIO
+ from urllib2 import HTTPError
+ from urlparse import urlsplit
+ from xmlrpclib import ServerProxy
from django.test import TestCase
from django.contrib import comments
@@ -39,7 +45,7 @@ def fake_urlopen(self, url):
if not netloc:
raise
if self.site.domain == netloc:
- response = cStringIO.StringIO(self.client.get(url).content)
+ response = StringIO(self.client.get(url).content)
return response
raise HTTPError(url, 404, 'unavailable url', {}, None)
View
@@ -1,6 +1,10 @@
"""Utils for Zinnia's tests"""
-import StringIO
-from xmlrpclib import Transport
+try:
+ from io import StringIO
+ from xmlrpc.client import Transport
+except ImportError: # Python 2
+ from StringIO import StringIO
+ from xmlrpclib import Transport
from datetime import datetime as original_datetime
from django.conf import settings
@@ -21,7 +25,7 @@ def request(self, host, handler, request_body, verbose=0):
response = self.client.post(handler,
request_body,
content_type="text/xml")
- res = StringIO.StringIO(response.content)
+ res = StringIO(response.content)
setattr(res, 'getheader', lambda *args: '') # For Python >= 2.7
res.seek(0)
if not hasattr(res, 'getheader'):
@@ -1,5 +1,8 @@
"""Views for Zinnia quick entry"""
-from urllib import urlencode
+try:
+ from urllib.parse import urlencode
+except: # Python 2
+ from urllib import urlencode
from django import forms
from django.shortcuts import redirect
@@ -1,8 +1,12 @@
"""XML-RPC methods of Zinnia metaWeblog API"""
import os
from datetime import datetime
-from xmlrpclib import Fault
-from xmlrpclib import DateTime
+try:
+ from xmlrpc.client import Fault
+ from xmlrpc.client import DateTime
+except ImportError: # Python 2
+ from xmlrpclib import Fault
+ from xmlrpclib import DateTime
from django.utils import six
from django.conf import settings
View
@@ -1,8 +1,14 @@
"""XML-RPC methods of Zinnia Pingback"""
-from urllib2 import urlopen
-from urllib2 import URLError
-from urllib2 import HTTPError
-from urlparse import urlsplit
+try:
+ from urllib.error import URLError
+ from urllib.error import HTTPError
+ from urllib.request import urlopen
+ from urllib.parse import urlsplit
+except ImportError: # Python 2
+ from urllib2 import urlopen
+ from urllib2 import URLError
+ from urllib2 import HTTPError
+ from urlparse import urlsplit
from django.utils import six
from django.contrib import comments

0 comments on commit 30cd751

Please sign in to comment.