Skip to content

Commit

Permalink
Converting to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
isagalaev committed Nov 4, 2013
1 parent f5150c8 commit 8073906
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
10 changes: 5 additions & 5 deletions pingdjack/__init__.py
Expand Up @@ -6,8 +6,8 @@
and author name.
'''

from client import external_urls, ping, ping_external_urls
from server import received, parse_data, server_view
from errors import SourceNotFound, TargetNotFoundUnderSource, \
TargetDoesNotExist, UnpingableTarget, DuplicatePing, \
AccessDenied, UpstreamError
from .client import external_urls, ping, ping_external_urls
from .server import received, parse_data, server_view
from .errors import SourceNotFound, TargetNotFoundUnderSource, \
TargetDoesNotExist, UnpingableTarget, DuplicatePing, \
AccessDenied, UpstreamError
12 changes: 6 additions & 6 deletions pingdjack/client.py
@@ -1,8 +1,8 @@
# -*- coding:utf-8 -*-
import re
from urllib2 import urlopen
from urlparse import urlsplit
import xmlrpclib
from urllib.request import urlopen
from urllib.parse import urlsplit
import xmlrpc.client
from xml.parsers.expat import ExpatError

from html5lib import HTMLParser
Expand All @@ -22,7 +22,7 @@ def is_external(url):
(host != root_host or not path.startswith(root_path))

doc = HTMLParser().parseFragment(html)
urls = (n.attributes.get('href', '') for n in doc if n.name == u'a')
urls = (n.attributes.get('href', '') for n in doc if n.name == 'a')
return (u.encode('utf-8') for u in urls if is_external(u))

def ping(source_url, target_url):
Expand All @@ -43,7 +43,7 @@ def search_link(content):
server_url = info.get('X-Pingback', '') or \
search_link(f.read(512 * 1024))
if server_url:
server = xmlrpclib.ServerProxy(server_url)
server = xmlrpc.client.ServerProxy(server_url)
server.pingback.ping(source_url, target_url)
finally:
f.close()
Expand All @@ -58,6 +58,6 @@ def ping_external_urls(source_url, html, root_url):
for url in external_urls(html, root_url):
try:
ping(source_url, url)
except (IOError, xmlrpclib.Error, ExpatError):
except (IOError, xmlrpc.client.Error, ExpatError):
# One failed URL shouldn't block others
pass
4 changes: 2 additions & 2 deletions pingdjack/errors.py
@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
import xmlrpclib
import xmlrpc.client

class Error(xmlrpclib.Fault):
class Error(xmlrpc.client.Fault):
code = 0
message = 'Unknown error'

Expand Down
28 changes: 14 additions & 14 deletions pingdjack/server.py
@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
from urllib2 import urlopen
from urlparse import urlsplit
import xmlrpclib
from urllib.request import urlopen
from urllib.parse import urlsplit
import xmlrpc.client
import cgi

from html5lib import HTMLParser
Expand All @@ -10,7 +10,7 @@
from django import dispatch
from django.core.urlresolvers import resolve, Resolver404

import errors
from . import errors

# Connect a handler to the "received" signal that will handle pingback
# requests made to your server. The signal provides the following keyword
Expand Down Expand Up @@ -53,15 +53,15 @@ def parse_data(source_url, target_url):
charset = params.get('charset', 'utf-8').replace("'", '')
doc = HTMLParser().parse(f.read().decode(charset))
for node in doc:
if node.name == u'a' and node.attributes.get('href') == target_url:
if node.name == 'a' and node.attributes.get('href') == target_url:
link = node
break
else:
raise errors.TargetNotFoundUnderSource

def text(node):
result = u''.join(s.value for s in node if s.type == 4)
return result.replace(u'\n', u' ').strip()
result = ''.join(s.value for s in node if s.type == 4)
return result.replace('\n', ' ').strip()

def find(node, name, exclude):
childNodes = (n for n in node.childNodes if n != exclude)
Expand All @@ -88,7 +88,7 @@ def find(node, name, exclude):
container, node = container.parent, container
else:
title = find(doc, 'title', None)
author = title and text(title) or unicode(source_url)
author = title and text(title) or source_url
return author, excerpt

def _handle_pingback(request, root, source_url, target_url):
Expand Down Expand Up @@ -133,13 +133,13 @@ def server_view(request, root='/'):
consider incoming target URLs as its own.
'''
try:
args, method = xmlrpclib.loads(request.raw_post_data)
args, method = xmlrpc.client.loads(request.raw_post_data)
if method != 'pingback.ping':
raise errors.Error('Unknown method "%s"' % method)
_handle_pingback(request, root, *args)
result = xmlrpclib.dumps(('OK',), methodresponse=True)
except xmlrpclib.Fault, fault:
result = xmlrpclib.dumps(fault)
except Exception, e:
result = xmlrpclib.dumps(errors.Error(str(e)))
result = xmlrpc.client.dumps(('OK',), methodresponse=True)
except xmlrpc.client.Fault as fault:
result = xmlrpc.client.dumps(fault)
except Exception as e:
result = xmlrpc.client.dumps(errors.Error(str(e)))
return http.HttpResponse(result)

0 comments on commit 8073906

Please sign in to comment.