Skip to content

Commit 247b2ad

Browse files
committed
Merge pull request scrapy#431 from alexanderlukanin13/syntax
Python 3 compatible syntax: print, except, raise, octal numbers; removed...
2 parents 3d4904b + 6b59847 commit 247b2ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+164
-141
lines changed

extras/qps-bench-server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import print_function
23
from time import time
34
from collections import deque
45
from twisted.web.server import Site, NOT_DONE_YET

extras/scrapy-ws.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
"""
1313

14+
from __future__ import print_function
1415
import sys, optparse, urllib, json
1516
from urlparse import urljoin
1617

@@ -29,9 +30,9 @@ def get_commands():
2930

3031
def cmd_help(args, opts):
3132
"""help - list available commands"""
32-
print "Available commands:"
33+
print("Available commands:")
3334
for _, func in sorted(get_commands().items()):
34-
print " ", func.__doc__
35+
print(" ", func.__doc__)
3536

3637
def cmd_stop(args, opts):
3738
"""stop <spider> - stop a running spider"""
@@ -40,29 +41,29 @@ def cmd_stop(args, opts):
4041
def cmd_list_running(args, opts):
4142
"""list-running - list running spiders"""
4243
for x in json_get(opts, 'crawler/engine/open_spiders'):
43-
print x
44+
print(x)
4445

4546
def cmd_list_available(args, opts):
4647
"""list-available - list name of available spiders"""
4748
for x in jsonrpc_call(opts, 'crawler/spiders', 'list'):
48-
print x
49+
print(x)
4950

5051
def cmd_list_resources(args, opts):
5152
"""list-resources - list available web service resources"""
5253
for x in json_get(opts, '')['resources']:
53-
print x
54+
print(x)
5455

5556
def cmd_get_spider_stats(args, opts):
5657
"""get-spider-stats <spider> - get stats of a running spider"""
5758
stats = jsonrpc_call(opts, 'stats', 'get_stats', args[0])
5859
for name, value in stats.items():
59-
print "%-40s %s" % (name, value)
60+
print("%-40s %s" % (name, value))
6061

6162
def cmd_get_global_stats(args, opts):
6263
"""get-global-stats - get global stats"""
6364
stats = jsonrpc_call(opts, 'stats', 'get_stats')
6465
for name, value in stats.items():
65-
print "%-40s %s" % (name, value)
66+
print("%-40s %s" % (name, value))
6667

6768
def get_wsurl(opts, path):
6869
return urljoin("http://%s:%s/"% (opts.host, opts.port), path)
@@ -101,12 +102,12 @@ def main():
101102
try:
102103
cmd(args, opts)
103104
except IndexError:
104-
print cmd.__doc__
105-
except JsonRpcError, e:
106-
print str(e)
105+
print(cmd.__doc__)
106+
except JsonRpcError as e:
107+
print(str(e))
107108
if e.data:
108-
print "Server Traceback below:"
109-
print e.data
109+
print("Server Traceback below:")
110+
print(e.data)
110111

111112

112113
if __name__ == '__main__':

scrapy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"""
22
Scrapy - a screen scraping framework written in Python
33
"""
4+
from __future__ import print_function
45
import pkgutil
56
__version__ = pkgutil.get_data(__package__, 'VERSION').strip()
67
version_info = tuple(__version__.split('.')[:3])
78

89
import sys, os, warnings
910

1011
if sys.version_info < (2, 6):
11-
print "Scrapy %s requires Python 2.6 or above" % __version__
12+
print("Scrapy %s requires Python 2.6 or above" % __version__)
1213
sys.exit(1)
1314

1415
# ignore noisy twisted deprecation warnings

scrapy/cmdline.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import sys
23
import optparse
34
import cProfile
@@ -59,34 +60,34 @@ def _pop_command_name(argv):
5960

6061
def _print_header(settings, inproject):
6162
if inproject:
62-
print "Scrapy %s - project: %s\n" % (scrapy.__version__, \
63-
settings['BOT_NAME'])
63+
print("Scrapy %s - project: %s\n" % (scrapy.__version__, \
64+
settings['BOT_NAME']))
6465
else:
65-
print "Scrapy %s - no active project\n" % scrapy.__version__
66+
print("Scrapy %s - no active project\n" % scrapy.__version__)
6667

6768
def _print_commands(settings, inproject):
6869
_print_header(settings, inproject)
69-
print "Usage:"
70-
print " scrapy <command> [options] [args]\n"
71-
print "Available commands:"
70+
print("Usage:")
71+
print(" scrapy <command> [options] [args]\n")
72+
print("Available commands:")
7273
cmds = _get_commands_dict(settings, inproject)
7374
for cmdname, cmdclass in sorted(cmds.iteritems()):
74-
print " %-13s %s" % (cmdname, cmdclass.short_desc())
75+
print(" %-13s %s" % (cmdname, cmdclass.short_desc()))
7576
if not inproject:
76-
print
77-
print " [ more ] More commands available when run from project directory"
78-
print
79-
print 'Use "scrapy <command> -h" to see more info about a command'
77+
print()
78+
print(" [ more ] More commands available when run from project directory")
79+
print()
80+
print('Use "scrapy <command> -h" to see more info about a command')
8081

8182
def _print_unknown_command(settings, cmdname, inproject):
8283
_print_header(settings, inproject)
83-
print "Unknown command: %s\n" % cmdname
84-
print 'Use "scrapy" to see available commands'
84+
print("Unknown command: %s\n" % cmdname)
85+
print('Use "scrapy" to see available commands')
8586

8687
def _run_print_help(parser, func, *a, **kw):
8788
try:
8889
func(*a, **kw)
89-
except UsageError, e:
90+
except UsageError as e:
9091
if str(e):
9192
parser.error(str(e))
9293
if e.print_help:

scrapy/commands/check.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from collections import defaultdict
23
from functools import wraps
34
from unittest import TextTestRunner
@@ -64,9 +65,9 @@ def run(self, args, opts):
6465
# start checks
6566
if opts.list:
6667
for spider, methods in sorted(contract_reqs.iteritems()):
67-
print spider
68+
print(spider)
6869
for method in sorted(methods):
69-
print ' * %s' % method
70+
print(' * %s' % method)
7071
else:
7172
self.crawler_process.start()
7273
self.results.printErrors()

scrapy/commands/deploy.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import sys
23
import os
34
import glob
@@ -72,7 +73,7 @@ def run(self, args, opts):
7273

7374
if opts.list_targets:
7475
for name, target in _get_targets().items():
75-
print "%-20s %s" % (name, target['url'])
76+
print("%-20s %s" % (name, target['url']))
7677
return
7778

7879
if opts.list_projects:
@@ -81,7 +82,7 @@ def run(self, args, opts):
8182
_add_auth_header(req, target)
8283
f = urllib2.urlopen(req)
8384
projects = json.loads(f.read())['projects']
84-
print os.linesep.join(projects)
85+
print(os.linesep.join(projects))
8586
return
8687

8788
tmpdir = None
@@ -208,12 +209,12 @@ def _http_post(request):
208209
try:
209210
f = urllib2.urlopen(request)
210211
_log("Server response (%s):" % f.code)
211-
print f.read()
212+
print(f.read())
212213
return True
213-
except urllib2.HTTPError, e:
214+
except urllib2.HTTPError as e:
214215
_log("Deploy failed (%s):" % e.code)
215-
print e.read()
216-
except urllib2.URLError, e:
216+
print(e.read())
217+
except urllib2.URLError as e:
217218
_log("Deploy failed: %s" % e)
218219

219220
def _build_egg():

scrapy/commands/fetch.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from w3lib.url import is_url
23

34
from scrapy.command import ScrapyCommand
@@ -30,15 +31,15 @@ def add_options(self, parser):
3031
def _print_headers(self, headers, prefix):
3132
for key, values in headers.items():
3233
for value in values:
33-
print '%s %s: %s' % (prefix, key, value)
34+
print('%s %s: %s' % (prefix, key, value))
3435

3536
def _print_response(self, response, opts):
3637
if opts.headers:
3738
self._print_headers(response.request.headers, '>')
38-
print '>'
39+
print('>')
3940
self._print_headers(response.headers, '<')
4041
else:
41-
print response.body
42+
print(response.body)
4243

4344
def run(self, args, opts):
4445
if len(args) != 1 or not is_url(args[0]):

scrapy/commands/genspider.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import os
23
import shutil
34
import string
@@ -49,7 +50,7 @@ def run(self, args, opts):
4950
if opts.dump:
5051
template_file = self._find_template(opts.dump)
5152
if template_file:
52-
print open(template_file, 'r').read()
53+
print(open(template_file, 'r').read())
5354
return
5455
if len(args) != 2:
5556
raise UsageError()
@@ -58,7 +59,7 @@ def run(self, args, opts):
5859
module = sanitize_module_name(name)
5960

6061
if self.settings.get('BOT_NAME') == module:
61-
print "Cannot create a spider with the same name as your project"
62+
print("Cannot create a spider with the same name as your project")
6263
return
6364

6465
try:
@@ -69,8 +70,8 @@ def run(self, args, opts):
6970
else:
7071
# if spider already exists and not --force then halt
7172
if not opts.force:
72-
print "Spider %r already exists in module:" % name
73-
print " %s" % spider.__module__
73+
print("Spider %r already exists in module:" % name)
74+
print(" %s" % spider.__module__)
7475
return
7576
template_file = self._find_template(opts.template)
7677
if template_file:
@@ -94,22 +95,22 @@ def _genspider(self, module, name, domain, template_name, template_file):
9495
spider_file = "%s.py" % join(spiders_dir, module)
9596
shutil.copyfile(template_file, spider_file)
9697
render_templatefile(spider_file, **tvars)
97-
print "Created spider %r using template %r in module:" % (name, \
98-
template_name)
99-
print " %s.%s" % (spiders_module.__name__, module)
98+
print("Created spider %r using template %r in module:" % (name, \
99+
template_name))
100+
print(" %s.%s" % (spiders_module.__name__, module))
100101

101102
def _find_template(self, template):
102103
template_file = join(self.templates_dir, '%s.tmpl' % template)
103104
if exists(template_file):
104105
return template_file
105-
print "Unable to find template: %s\n" % template
106-
print 'Use "scrapy genspider --list" to see all available templates.'
106+
print("Unable to find template: %s\n" % template)
107+
print('Use "scrapy genspider --list" to see all available templates.')
107108

108109
def _list_templates(self):
109-
print "Available templates:"
110+
print("Available templates:")
110111
for filename in sorted(os.listdir(self.templates_dir)):
111112
if filename.endswith('.tmpl'):
112-
print " %s" % splitext(filename)[0]
113+
print(" %s" % splitext(filename)[0])
113114

114115
@property
115116
def templates_dir(self):

scrapy/commands/list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from scrapy.command import ScrapyCommand
23

34
class Command(ScrapyCommand):
@@ -11,4 +12,4 @@ def short_desc(self):
1112
def run(self, args, opts):
1213
crawler = self.crawler_process.create_crawler()
1314
for s in crawler.spiders.list():
14-
print s
15+
print(s)

scrapy/commands/parse.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from w3lib.url import is_url
23
from scrapy.command import ScrapyCommand
34
from scrapy.http import Request
@@ -68,7 +69,7 @@ def print_items(self, lvl=None, colour=True):
6869
else:
6970
items = self.items.get(lvl, [])
7071

71-
print "# Scraped Items ", "-"*60
72+
print("# Scraped Items ", "-"*60)
7273
display.pprint([dict(x) for x in items], colorize=colour)
7374

7475
def print_requests(self, lvl=None, colour=True):
@@ -81,21 +82,21 @@ def print_requests(self, lvl=None, colour=True):
8182
else:
8283
requests = self.requests.get(lvl, [])
8384

84-
print "# Requests ", "-"*65
85+
print("# Requests ", "-"*65)
8586
display.pprint(requests, colorize=colour)
8687

8788
def print_results(self, opts):
8889
colour = not opts.nocolour
8990

9091
if opts.verbose:
9192
for level in xrange(1, self.max_level+1):
92-
print '\n>>> DEPTH LEVEL: %s <<<' % level
93+
print('\n>>> DEPTH LEVEL: %s <<<' % level)
9394
if not opts.noitems:
9495
self.print_items(level, colour)
9596
if not opts.nolinks:
9697
self.print_requests(level, colour)
9798
else:
98-
print '\n>>> STATUS DEPTH LEVEL %s <<<' % self.max_level
99+
print('\n>>> STATUS DEPTH LEVEL %s <<<' % self.max_level)
99100
if not opts.noitems:
100101
self.print_items(colour=colour)
101102
if not opts.nolinks:

scrapy/commands/runspider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run(self, args, opts):
6767
raise UsageError("File not found: %s\n" % filename)
6868
try:
6969
module = _import_file(filename)
70-
except (ImportError, ValueError), e:
70+
except (ImportError, ValueError) as e:
7171
raise UsageError("Unable to load %r: %s\n" % (filename, e))
7272
spclasses = list(iter_spider_classes(module))
7373
if not spclasses:

scrapy/commands/settings.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from scrapy.command import ScrapyCommand
23

34
class Command(ScrapyCommand):
@@ -27,12 +28,12 @@ def add_options(self, parser):
2728
def run(self, args, opts):
2829
settings = self.crawler_process.settings
2930
if opts.get:
30-
print settings.get(opts.get)
31+
print(settings.get(opts.get))
3132
elif opts.getbool:
32-
print settings.getbool(opts.getbool)
33+
print(settings.getbool(opts.getbool))
3334
elif opts.getint:
34-
print settings.getint(opts.getint)
35+
print(settings.getint(opts.getint))
3536
elif opts.getfloat:
36-
print settings.getfloat(opts.getfloat)
37+
print(settings.getfloat(opts.getfloat))
3738
elif opts.getlist:
38-
print settings.getlist(opts.getlist)
39+
print(settings.getlist(opts.getlist))

scrapy/commands/startproject.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import sys
23
import string
34
import re
@@ -36,11 +37,11 @@ def run(self, args, opts):
3637
raise UsageError()
3738
project_name = args[0]
3839
if not re.search(r'^[_a-zA-Z]\w*$', project_name):
39-
print 'Error: Project names must begin with a letter and contain only\n' \
40-
'letters, numbers and underscores'
40+
print('Error: Project names must begin with a letter and contain only\n' \
41+
'letters, numbers and underscores')
4142
sys.exit(1)
4243
elif exists(project_name):
43-
print "Error: directory %r already exists" % project_name
44+
print("Error: directory %r already exists" % project_name)
4445
sys.exit(1)
4546

4647
moduletpl = join(TEMPLATES_PATH, 'module')

0 commit comments

Comments
 (0)