Skip to content

Commit 95dcb40

Browse files
committed
Python 3 preparation: update urllib / urllib2 / urlparse imports.
The existing roundup/anypy/urllib_.py is extended to cover more imports and used in more places. Manual patch.
1 parent 1c34c06 commit 95dcb40

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed

frontends/ZRoundup/ZRoundup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
advantages over regular CGI :)
2727
'''
2828

29-
import urlparse
29+
from roundup.anypy import urllib_
3030

3131
from Globals import InitializeClass, HTMLFile
3232
from OFS.SimpleItem import Item
@@ -141,7 +141,7 @@ def roundup_opendb(self):
141141
env = self.REQUEST.environ
142142

143143
# figure out the path components to set
144-
url = urlparse.urlparse( self.absolute_url() )
144+
url = urllib_.urlparse( self.absolute_url() )
145145
path = url[2]
146146
path_components = path.split( '/' )
147147

@@ -168,10 +168,10 @@ def index_html(self):
168168
# PATH_INFO, as defined by the CGI spec, has the *real* request path
169169
orig_path = self.REQUEST.environ['PATH_INFO']
170170
if orig_path[-1] != '/' :
171-
url = urlparse.urlparse( self.absolute_url() )
171+
url = urllib_.urlparse( self.absolute_url() )
172172
url = list( url ) # make mutable
173173
url[2] = url[2]+'/' # patch
174-
url = urlparse.urlunparse( url ) # reassemble
174+
url = urllib_.urlunparse( url ) # reassemble
175175
RESPONSE = self.REQUEST.RESPONSE
176176
RESPONSE.setStatus( "MovedPermanently" ) # 301
177177
RESPONSE.setHeader( "Location" , url )

roundup/anypy/urllib_.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
try:
33
# Python 3+
4-
from urllib.parse import quote, urlencode, urlparse, parse_qs, urlunparse
4+
from urllib.parse import quote, unquote, urlencode, urlparse, parse_qs, \
5+
urlunparse
6+
from urllib.request import urlopen
57
except:
68
# Python 2.5-2.7
7-
from urllib import quote, urlencode
9+
from urllib import quote, unquote, urlencode
10+
from urllib2 import urlopen
811
from urlparse import urlparse, parse_qs, urlunparse

roundup/cgi/TAL/XMLParser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
##############################################################################
1414
# Modifications for Roundup:
1515
# 1. commented out zLOG references
16+
# 2. use roundup.anypy.urllib_
1617
"""
1718
Generic expat-based XML parser base class.
1819
"""
@@ -83,8 +84,8 @@ def parseString(self, s):
8384
self.parser.Parse(s, 1)
8485

8586
def parseURL(self, url):
86-
import urllib
87-
self.parseStream(urllib.urlopen(url))
87+
import roundup.anypy.urllib_
88+
self.parseStream(roundup.anypy.urllib_.urlopen(url))
8889

8990
def parseStream(self, stream):
9091
self.parser.ParseFile(stream)

roundup/cgi/templating.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
__docformat__ = 'restructuredtext'
2121

2222

23-
import cgi, urllib, re, os.path, mimetypes, csv, string
23+
import cgi, re, os.path, mimetypes, csv, string
2424
import calendar
2525
import textwrap
2626
import time, hashlib
2727

28+
from roundup.anypy import urllib_
2829
from roundup import hyperdb, date, support
2930
from roundup import i18n
3031
from roundup.i18n import _
@@ -763,7 +764,7 @@ def classhelp(self, properties=None, label=''"(list)", width='500',
763764
for x in filterprops:
764765
(name, values) = x.split('=')
765766
names.append(name)
766-
filtervalues.append('&%s=%s' % (name, urllib.quote(values)))
767+
filtervalues.append('&%s=%s' % (name, urllib_.quote(values)))
767768
filter = '&@filter=%s%s' % (','.join(names), ''.join(filtervalues))
768769
else:
769770
filter = ''
@@ -1199,7 +1200,7 @@ def renderQueryForm(self):
11991200
req.classname = self._klass.get(self._nodeid, 'klass')
12001201
name = self._klass.get(self._nodeid, 'name')
12011202
req.updateFromURL(self._klass.get(self._nodeid, 'url') +
1202-
'&@queryname=%s'%urllib.quote(name))
1203+
'&@queryname=%s'%urllib_.quote(name))
12031204

12041205
# new template, using the specified classname and request
12051206
# [ ] the custom logic for search page doesn't belong to
@@ -1219,7 +1220,7 @@ def download_url(self):
12191220
"""
12201221
name = self._klass.get(self._nodeid, 'name')
12211222
url = '%s%s/%s'%(self._classname, self._nodeid, name)
1222-
return urllib.quote(url)
1223+
return urllib_.quote(url)
12231224

12241225
def copy_url(self, exclude=("messages", "files")):
12251226
"""Construct a URL for creating a copy of this item
@@ -1246,7 +1247,7 @@ def copy_url(self, exclude=("messages", "files")):
12461247
query[name] = ",".join(self._klass.get(self._nodeid, name))
12471248

12481249
return self._classname + "?" + "&".join(
1249-
["%s=%s" % (key, urllib.quote(value))
1250+
["%s=%s" % (key, urllib_.quote(value))
12501251
for key, value in query.items()])
12511252

12521253
class _HTMLUser(_HTMLItem):
@@ -1463,7 +1464,7 @@ def _hyper_repl_rst(self, match):
14631464

14641465
def url_quote(self):
14651466
""" Return the string in plain format but escaped for use in a url """
1466-
return urllib.quote(self.plain())
1467+
return urllib_.quote(self.plain())
14671468

14681469
def hyperlinked(self):
14691470
""" Render a "hyperlinked" version of the text """
@@ -2855,7 +2856,7 @@ def indexargs_url(self, url, args):
28552856
dispname otherwise the parameter will be omitted
28562857
from the url.
28572858
"""
2858-
q = urllib.quote
2859+
q = urllib_.quote
28592860
sc = self.special_char
28602861
l = ['%s=%s'%(k,isinstance(v, basestring) and q(v) or v)
28612862
for k,v in args.items() if v != None ]
@@ -3069,7 +3070,7 @@ def anti_csrf_nonce(self, lifetime=None):
30693070

30703071
def url_quote(self, url):
30713072
"""URL-quote the supplied text."""
3072-
return urllib.quote(url)
3073+
return urllib_.quote(url)
30733074

30743075
def html_quote(self, html):
30753076
"""HTML-quote the supplied text."""

roundup/scripts/roundup_server.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# --/
3737

3838

39-
import errno, cgi, getopt, os, socket, sys, traceback, urllib, time
39+
import errno, cgi, getopt, os, socket, sys, traceback, time
4040
import ConfigParser, BaseHTTPServer, SocketServer, StringIO
4141

4242
try:
@@ -49,6 +49,7 @@
4949
from roundup import __version__ as roundup_version
5050

5151
# Roundup modules of use here
52+
from roundup.anypy import urllib_
5253
from roundup.cgi import cgitb, client
5354
from roundup.cgi.PageTemplates.PageTemplate import PageTemplate
5455
import roundup.instance
@@ -248,7 +249,7 @@ def index(self):
248249
keys = list(self.TRACKER_HOMES.keys())
249250
if len(keys) == 1:
250251
self.send_response(302)
251-
self.send_header('Location', urllib.quote(keys[0]) + '/index')
252+
self.send_header('Location', urllib_.quote(keys[0]) + '/index')
252253
self.end_headers()
253254
else:
254255
self.send_response(200)
@@ -273,7 +274,7 @@ def index(self):
273274
keys.sort()
274275
for tracker in keys:
275276
w('<li><a href="%(tracker_url)s/index">%(tracker_name)s</a>\n'%{
276-
'tracker_url': urllib.quote(tracker),
277+
'tracker_url': urllib_.quote(tracker),
277278
'tracker_name': cgi.escape(tracker)})
278279
w('</ol></body></html>')
279280

@@ -332,7 +333,7 @@ def inner_run_cgi(self):
332333

333334
# figure the tracker
334335
l_path = rest.split('/')
335-
tracker_name = urllib.unquote(l_path[1]).lower()
336+
tracker_name = urllib_.unquote(l_path[1]).lower()
336337

337338
# handle missing trailing '/'
338339
if len(l_path) == 2:
@@ -357,7 +358,7 @@ def inner_run_cgi(self):
357358
env = {}
358359
env['TRACKER_NAME'] = tracker_name
359360
env['REQUEST_METHOD'] = self.command
360-
env['PATH_INFO'] = urllib.unquote(rest)
361+
env['PATH_INFO'] = urllib_.unquote(rest)
361362
if query:
362363
env['QUERY_STRING'] = query
363364
if self.headers.typeheader is None:

roundup/scripts/roundup_xmlrpc_server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
# --/
2121

2222

23-
import base64, getopt, os, sys, socket, urllib
23+
import base64, getopt, os, sys, socket
24+
from roundup.anypy import urllib_
2425
from roundup.xmlrpc import translate
2526
from roundup.xmlrpc import RoundupInstance
2627
import roundup.instance
@@ -39,7 +40,7 @@ class RequestHandler(SimpleXMLRPCRequestHandler):
3940

4041
def is_rpc_path_valid(self):
4142
path = self.path.split('/')
42-
name = urllib.unquote(path[1]).lower()
43+
name = urllib_.unquote(path[1]).lower()
4344
return name in self.TRACKER_HOMES
4445

4546
def get_tracker(self, name):
@@ -93,7 +94,7 @@ def do_POST(self):
9394
db = None
9495
try:
9596
path = self.path.split('/')
96-
tracker_name = urllib.unquote(path[1]).lower()
97+
tracker_name = urllib_.unquote(path[1]).lower()
9798
tracker = self.get_tracker(tracker_name)
9899
db = self.authenticate(tracker)
99100

scripts/import_sf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
And you're done!
2222
"""
2323

24-
import sys, os, csv, time, urllib2, httplib, mimetypes, urlparse
24+
import sys, os, csv, time, httplib, mimetypes
2525

2626
try:
2727
import cElementTree as ElementTree
2828
except ImportError:
2929
from elementtree import ElementTree
3030

3131
from roundup import instance, hyperdb, date, support, password
32+
from roundup.anypy import urllib_
3233

3334
today = date.Date('.')
3435

@@ -46,7 +47,7 @@ def get_url(aid):
4647
# magic "group_id" and "atid" values in it that we need
4748
assert response.status == 302, 'response code was %s'%response.status
4849
location = response.getheader('location')
49-
query = urlparse.urlparse(response.getheader('location'))[-2]
50+
query = urllib_.urlparse(response.getheader('location'))[-2]
5051
info = dict([param.split('=') for param in query.split('&')])
5152
return DL_URL%info
5253

@@ -91,7 +92,7 @@ def fetch_files(xml_file, file_dir):
9192
f.write('%s %s\n'%(aid, urls[aid]))
9293
f.close()
9394
url = urls[aid] + '&file_id=' + fid
94-
f = urllib2.urlopen(url)
95+
f = urllib_.urlopen(url)
9596
data = f.read()
9697
n = open(os.path.join(file_dir, fid), 'w')
9798
n.write(data)

0 commit comments

Comments
 (0)