Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Updated code to work with py2 and py3
Browse files Browse the repository at this point in the history
  • Loading branch information
trollfot committed Sep 20, 2018
1 parent b51db3e commit ee1f81e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
27 changes: 15 additions & 12 deletions src/grokui/admin/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
"""
import grok
import time
import urllib.request
import urllib.parse as urlparse

try:
from html import escape
except ImportError:
from cgi import escape

import sys

from persistent import Persistent
from grokui.admin.interfaces import ISecurityNotifier
from grokui.admin.utilities import getVersion, TimeoutableHTTPHandler
from grokui.base import Messages, IGrokUIRealm

if sys.version_info >= (3, 3):
from html import escape
import urllib.request as urllib
import urllib.parse as urlparse
import urllib.urljoin as urljoin
else:
import urllib2 as urllib
from cgi import escape
from urlparse import urlparse, urljoin


MSG_DISABLED = u'Security notifications are disabled.'

Expand Down Expand Up @@ -112,16 +115,16 @@ def fetchMessage(self):
return
version = getVersion('grok')
filename = 'grok-%s.security.txt' % version
url = urlparse.urljoin(self.lookup_url, filename)
url = urljoin(self.lookup_url, filename)
# We create a HTTP handler with a timeout.
http_handler = TimeoutableHTTPHandler(timeout=self.lookup_timeout)
opener = urllib.request.build_opener(http_handler)
req = urllib.request.Request(url)
opener = urllib.build_opener(http_handler)
req = urllib.Request(url)
try:
message = opener.open(req).read()
self._message = escape(message.decode())
self._warningstate = True
except urllib.request.HTTPError as e:
except urllib.HTTPError as e:
if e.code == 404:
# No security warning found, good message.
self._message = u''
Expand Down
2 changes: 1 addition & 1 deletion src/grokui/admin/tests/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
>>> sn.last_lookup = None
>>> sn.getNotification()
'You better smash ...'
'You better smash 3.1'
To decide, whether the delivered string is actually a warning, we can
call the `isWarning` method::
Expand Down
2 changes: 1 addition & 1 deletion src/grokui/admin/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_suite():
doctest.NORMALIZE_WHITESPACE +
doctest.REPORT_NDIFF)
globs = dict(getRootFolder=functional_layer.getRootFolder)

tests = [
'apps',
'brokenapps',
Expand Down
18 changes: 12 additions & 6 deletions src/grokui/admin/utilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import pkg_resources
import socket
import urllib
import urllib.request
import http.client
import sys

if sys.version_info >= (3, 3):
from http.client import HTTPConnection
from urllib.request import HTTPHandler
else:
from urllib2 import HTTPHandler
from httplib import HTTPConnection


def getURLWithParams(url, data=None):
Expand Down Expand Up @@ -31,13 +37,13 @@ def getVersion(pkgname):
return None


class TimeoutableHTTPConnection(http.client.HTTPConnection):
class TimeoutableHTTPConnection(HTTPConnection):
"""A customised HTTPConnection allowing a per-connection
timeout, specified at construction.
"""

def __init__(self, host, port=None, strict=None, timeout=None):
http.client.HTTPConnection.__init__(self, host, port,
HTTPConnection.__init__(self, host, port,
strict)
self.timeout = timeout

Expand All @@ -64,13 +70,13 @@ def connect(self):
raise socket.error(msg)


class TimeoutableHTTPHandler(urllib.request.HTTPHandler):
class TimeoutableHTTPHandler(HTTPHandler):
"""A customised HTTPHandler which times out connection
after the duration specified at construction.
"""

def __init__(self, timeout=None):
urllib.request.HTTPHandler.__init__(self)
HTTPHandler.__init__(self)
self.timeout = timeout

def http_open(self, req):
Expand Down

0 comments on commit ee1f81e

Please sign in to comment.