Skip to content

Commit

Permalink
fix imports for Python 2 & 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Apr 10, 2013
1 parent f33ae2e commit 30cd751
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 34 deletions.
6 changes: 5 additions & 1 deletion zinnia/feeds.py
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion zinnia/management/commands/feed2zinnia.py
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion zinnia/management/commands/wp2zinnia.py
@@ -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
Expand Down
19 changes: 13 additions & 6 deletions zinnia/ping.py
@@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion zinnia/templatetags/zinnia_tags.py
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion zinnia/tests/feeds.py
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions zinnia/tests/metaweblog.py
@@ -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
Expand Down
15 changes: 10 additions & 5 deletions zinnia/tests/ping.py
@@ -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
Expand Down Expand Up @@ -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')
Expand Down
16 changes: 11 additions & 5 deletions zinnia/tests/pingback.py
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions zinnia/tests/utils.py
@@ -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
Expand All @@ -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'):
Expand Down
5 changes: 4 additions & 1 deletion zinnia/views/quick_entry.py
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions zinnia/xmlrpc/metaweblog.py
@@ -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
Expand Down
14 changes: 10 additions & 4 deletions zinnia/xmlrpc/pingback.py
@@ -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
Expand Down

0 comments on commit 30cd751

Please sign in to comment.