misc: use print as function (#36515)

This commit is contained in:
Frédéric Péters 2019-11-12 08:59:27 +01:00
parent c586e8262d
commit 78c4a5f640
11 changed files with 77 additions and 54 deletions

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import datetime
import os
import random
@ -1760,7 +1762,7 @@ def test_view_performances():
formdef.store()
formdefs.append(formdef)
print 'create formdatas'
print('create formdatas')
# create formdatas
for i in range(nb_formdatas):
data_class = random.choice(formdefs).data_class()
@ -1775,7 +1777,7 @@ def test_view_performances():
formdata.jump_status('st%s' % (j+2))
if random.random() < 0.5:
break
print 'done'
print('done')
t0 = time.time()
user_roles = [random.choice(roles).id, random.choice(roles).id]
@ -1784,7 +1786,7 @@ def test_view_performances():
criterias.append(st.Equal('is_at_endpoint', False))
criterias.append(st.Intersects('actions_roles_array', user_roles))
formdatas = sql.AnyFormData.select(criterias, order_by='receipt_time', limit=20, offset=0)
print time.time() - t0
print(time.time() - t0)
assert (time.time() - t0) < 0.5

View File

@ -16,6 +16,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import time
from subprocess import Popen, PIPE
import textwrap
@ -142,23 +144,23 @@ def graphviz(workflow, url_prefix='', select=None, svg=True,
colours = {}
revert_colours = {}
print >>out, 'digraph main {'
print('digraph main {', file=out)
# print >>out, 'graph [ rankdir=LR ];'
print >>out, 'node [shape=box,style=filled];'
print >>out, 'edge [];'
print('node [shape=box,style=filled];', file=out)
print('edge [];', file=out)
for status in workflow.possible_status:
i = status.id
print >>out, 'status%s' % i,
print >>out, '[label="%s"' % status.name.replace('"', "'"),
print('status%s' % i, end=' ', file=out)
print('[label="%s"' % status.name.replace('"', "'"), end= ' ', file=out)
if select == str(i):
print >>out, ',id=current_status'
print(',id=current_status', file=out)
if status.colour:
if status.colour not in colours:
colours[status.colour] = graphviz_colours.pop()
revert_colours[colours[status.colour]] = status.colour
print >>out, ',color=%s' % colours[status.colour]
print >>out, ',class=%s' % colours[status.colour]
print >>out, ' URL="%sstatus/%s/"];' % (url_prefix, i)
print(',color=%s' % colours[status.colour], file=out)
print(',class=%s' % colours[status.colour], file=out)
print(' URL="%sstatus/%s/"];' % (url_prefix, i), file=out)
for status in workflow.possible_status:
i = status.id
@ -173,7 +175,7 @@ def graphviz(workflow, url_prefix='', select=None, svg=True,
# don't display multiple arrows for same action and target
# status
continue
print >>out, 'status%s -> status%s' % (i, next_id)
print('status%s -> status%s' % (i, next_id), file=out)
done[next_id] = True
label = item.get_jump_label(target_id=next_id)
label = label.replace('"', '\\"')
@ -181,10 +183,10 @@ def graphviz(workflow, url_prefix='', select=None, svg=True,
label = textwrap.fill(label, 20, break_long_words=False)
label = label.encode('utf8')
label = label.replace('\n', '\\n')
print >>out, '[label="%s"' % label,
print >>out, ',URL="%s%s"]' % (url_prefix, url)
print('[label="%s"' % label, end=' ', file=out)
print(',URL="%s%s"]' % (url_prefix, url), file=out)
print >>out, '}'
print('}', file=out)
out = out.getvalue()
if svg:
try:

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import ConfigParser
import json
import os
@ -118,7 +120,7 @@ class CmdCheckHobos(Command):
pub.app_dir = os.path.join(global_app_dir,
self.get_instance_path(service))
if not os.path.exists(pub.app_dir):
print 'initializing instance in', pub.app_dir
print('initializing instance in', pub.app_dir)
os.mkdir(pub.app_dir)
pub.initialize_app_dir()
@ -129,14 +131,14 @@ class CmdCheckHobos(Command):
pub.import_zip(file(skeleton_filepath))
new_site = True
else:
print 'updating instance in', pub.app_dir
print('updating instance in', pub.app_dir)
new_site = False
try:
self.configure_site_options(service, pub,
ignore_timestamp=sub_options.ignore_timestamp)
except NoChange:
print ' skipping'
print(' skipping')
return
pub.set_config(skip_sql=True)
@ -324,7 +326,7 @@ class CmdCheckHobos(Command):
try:
rfd = misc.urlopen(metadata_url)
except misc.ConnectionError as e:
print >> sys.stderr, 'failed to get metadata URL', metadata_url, e
print('failed to get metadata URL', metadata_url, e, file=sys.stderr)
continue
s = rfd.read()
@ -507,8 +509,8 @@ class CmdCheckHobos(Command):
try:
pgconn = psycopg2.connect(**createdb_cfg)
except psycopg2.Error as e:
print >> sys.stderr, 'failed to connect to postgresql (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode)
print('failed to connect to postgresql (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode), file=sys.stderr)
return
pgconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
@ -527,8 +529,8 @@ class CmdCheckHobos(Command):
if cur.fetchall():
new_database = False
else:
print >> sys.stderr, 'failed to create database (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode)
print('failed to create database (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode), file=sys.stderr)
return
else:
cur.close()

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
import sys
import psycopg2
@ -69,7 +71,8 @@ class CmdDeleteTenant(Command):
try:
pgconn = psycopg2.connect(**createdb_cfg)
except psycopg2.Error as e:
print >> sys.stderr, 'failed to connect to postgresql (%s)' % psycopg2.errorcodes.lookup(e.pgcode)
print('failed to connect to postgresql (%s)' % psycopg2.errorcodes.lookup(e.pgcode),
file=sys.stderr)
return
pgconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
@ -103,8 +106,9 @@ class CmdDeleteTenant(Command):
(table_name, schema_name[:63]))
except psycopg2.Error as e:
print >> sys.stderr, 'failed to alter database %s: (%s)' % (createdb_cfg['database'],
psycopg2.errorcodes.lookup(e.pgcode))
print('failed to alter database %s: (%s)' % (
createdb_cfg['database'], psycopg2.errorcodes.lookup(e.pgcode)),
file=sys.stderr)
return
cur.close()

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
from ..qommon.ctl import Command, make_option
@ -35,6 +37,6 @@ class CmdExportSettings(Command):
register_tld_names=False)
pub.app_dir = os.path.join(pub.app_dir, sub_options.vhost)
pub.reload_cfg()
print pub.export_cfg()
print(pub.export_cfg())
CmdExportSettings.register()

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import json
import os
@ -116,7 +118,7 @@ def jump_and_perform(formdata, action, workflow_data=None):
get_publisher().substitutions.feed(get_publisher())
get_publisher().substitutions.feed(formdata.formdef)
get_publisher().substitutions.feed(formdata)
print 'formdata %s jumps to status %s' % (formdata, action.status)
print('formdata %s jumps to status %s' % (formdata, action.status))
wcs_jump_and_perform(formdata, action, workflow_data=workflow_data)
def select_and_jump_formdata(formdef, trigger, rows, status_ids=None):

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import sys
import tarfile
import os
@ -41,12 +43,12 @@ class CmdRestore(Command):
hostname = args[0]
pub.app_dir = os.path.join(pub.app_dir, hostname)
if os.path.exists(pub.app_dir):
print >> sys.stderr, 'cannot overwrite site'
print('cannot overwrite site', file=sys.stderr)
return 1
os.mkdir(pub.app_dir)
if not sub_options.filename:
print >> sys.stderr, 'missing --file parameter'
print('missing --file parameter', file=sys.stderr)
return 1
backup_filepath = sub_options.filename

View File

@ -19,6 +19,8 @@
#
# [1]: file django/core/management/commands/shell.py
from __future__ import print_function
import os.path
from ..qommon.ctl import Command, make_option
@ -40,7 +42,7 @@ class CmdShell(Command):
register_tld_names=False)
publisher.app_dir = os.path.join(publisher.APP_DIR, args[0])
if not os.path.exists(publisher.app_dir):
print 'Application directory %r does not exist.' % publisher.app_dir
print('Application directory %r does not exist.' % publisher.app_dir)
raise SystemExit(1)
publisher.set_config()
try:

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import os
import sys
@ -32,7 +34,7 @@ class CmdWipeData(Command):
def execute(self, base_options, sub_options, args):
if not sub_options.vhost:
print >> sys.stderr, 'you must specify --vhost'
print('you must specify --vhost', file=sys.stderr)
sys.exit(1)
from .. import publisher

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import ConfigParser
import optparse
from optparse import make_option
@ -47,13 +49,13 @@ class Command(object):
def run(self, args, base_options):
if base_options.configfile:
if not os.path.exists(base_options.configfile):
print >> sys.stderr, 'Missing configuration file %s' % base_options.configfile
print('Missing configuration file %s' % base_options.configfile, file=sys.stderr)
sys.exit(1)
try:
self.config.read(base_options.configfile)
except ConfigParser.ParsingError as e:
print >> sys.stderr, 'Invalid configuration file %s' % base_options.configfile
print >> sys.stderr, e
print('Invalid configuration file %s' % base_options.configfile, file=sys.stderr)
print(e, file=sys.stderr)
sys.exit(1)
if not self.config.has_section('main'):
self.config.add_section('main')
@ -127,13 +129,13 @@ class Ctl(object):
def print_help(self, *args):
self.parser.print_help()
self.load_all_commands()
print
print()
commands = [(x.name, x.doc) for x in self.get_commands().values()]
commands.sort()
print 'Available commands:'
print('Available commands:')
for name, description in commands:
print ' %-15s %s' % (name, description)
print(' %-15s %s' % (name, description))
sys.exit(0)
def run(self, args):

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import collections
from django.utils.six.moves import builtins
from django.utils.six.moves import configparser as ConfigParser
@ -195,12 +197,12 @@ class QommonPublisher(Publisher, object):
if limit is None:
if hasattr(sys, 'tracebacklimit'):
limit = sys.tracebacklimit
print >>error_file, "Exception:"
print >>error_file, " type = '%s', value = '%s'" % (exc_type, exc_value)
print >>error_file
print("Exception:", file=error_file)
print(" type = '%s', value = '%s'" % (exc_type, exc_value), file=error_file)
print('', file=error_file)
# format the traceback
print >>error_file, 'Stack trace (most recent call first):'
print('Stack trace (most recent call first):', file=error_file)
while tb.tb_next:
tb = tb.tb_next
frame = tb.tb_frame
@ -211,29 +213,28 @@ class QommonPublisher(Publisher, object):
exclineno = frame.f_lineno
locals = sorted(frame.f_locals.items(), key=lambda item: item[0])
print >>error_file, ' File "%s", line %s, in %s' % (filename, exclineno, function)
print(' File "%s", line %s, in %s' % (filename, exclineno, function), file=error_file)
linecache.checkcache(filename)
for lineno in range(exclineno-2,exclineno+3):
line = linecache.getline(filename, lineno, frame.f_globals)
if line:
if lineno == exclineno:
print >>error_file, '>%5s %s' % (lineno, line.rstrip())
print('>%5s %s' % (lineno, line.rstrip()), file=error_file)
else:
print >>error_file, ' %5s %s' % (lineno, line.rstrip())
print >>error_file
print(' %5s %s' % (lineno, line.rstrip()), file=error_file)
print('', file=error_file)
if locals:
print >>error_file, " locals: "
print(" locals: ", file=error_file)
for key, value in locals:
print >>error_file, " %s =" % key,
print(" %s =" % key, end=' ', file=error_file)
try:
repr_value = repr(value)
if len(repr_value) > 10000:
repr_value = repr_value[:10000] + ' [...]'
print >>error_file, repr_value,
print(repr_value, file=error_file)
except:
print >>error_file, "<ERROR WHILE PRINTING VALUE>",
print >>error_file
print >>error_file
print("<ERROR WHILE PRINTING VALUE>", file=error_file)
print('', file=error_file)
frame = frame.f_back
n = n + 1
@ -679,7 +680,7 @@ class QommonPublisher(Publisher, object):
try:
imp.load_module(modulename, fp, pathname, description)
except Exception as e:
print >> sys.stderr, 'failed to load extra module: %s (%s)' % (modulename, e)
print('failed to load extra module: %s (%s)' % (modulename, e), file=sys.stderr)
if fp:
fp.close()