Fixed the errors, and warnings reported by pylama.

TODO::
- Don't use unicode in Python 3 for removing the support of Python 2 (
  psycopg2 driver - __init__.py, server_manager.py).
- Merge the lastest pgcli (version: 3.0.0) for SQL autocompletion.
This commit is contained in:
Ashesh Vashi 2020-05-08 14:13:32 +05:30
parent 9338bdcb3a
commit e73e2d2502
10 changed files with 43 additions and 49 deletions

View File

@ -13,7 +13,7 @@ import simplejson as json
from functools import wraps from functools import wraps
import pgadmin.browser.server_groups.servers.databases as database import pgadmin.browser.server_groups.servers.databases as database
from flask import render_template, make_response, request, jsonify from flask import render_template, request, jsonify
from flask_babelex import gettext from flask_babelex import gettext
from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ from pgadmin.browser.server_groups.servers.databases.schemas.tables.\
constraints.type import ConstraintRegistry, ConstraintTypeModule constraints.type import ConstraintRegistry, ConstraintTypeModule
@ -517,7 +517,7 @@ class ForeignKeyConstraintView(PGChildNodeView):
return make_json_response( return make_json_response(
status=400, status=400,
success=0, success=0,
errormsg=_( errormsg=gettext(
"Could not find required parameter ({})." "Could not find required parameter ({})."
).format(arg) ).format(arg)
) )
@ -525,7 +525,7 @@ class ForeignKeyConstraintView(PGChildNodeView):
return make_json_response( return make_json_response(
status=400, status=400,
success=0, success=0,
errormsg=_( errormsg=gettext(
"Could not find required parameter ({})." "Could not find required parameter ({})."
).format(arg) ).format(arg)
) )

View File

@ -11,10 +11,11 @@ import os
from pgadmin.utils.driver import DriverRegistry from pgadmin.utils.driver import DriverRegistry
from regression.python_test_utils.template_helper import file_as_template from regression.python_test_utils.template_helper import file_as_template
DriverRegistry.load_drivers()
from pgadmin.utils.route import BaseTestGenerator from pgadmin.utils.route import BaseTestGenerator
from regression.python_test_utils import test_utils from regression.python_test_utils import test_utils
DriverRegistry.load_drivers()
class TestColumnForeignKeyGetConstraintCols(BaseTestGenerator): class TestColumnForeignKeyGetConstraintCols(BaseTestGenerator):
scenarios = [ scenarios = [

View File

@ -72,7 +72,7 @@ def unescape_dquotes_process_arg(arg):
def _log_exception(): def _log_exception():
type_, value_, traceback_ = info = sys.exc_info() type_, value_, traceback_ = sys.exc_info()
with open(_log_file, 'a') as fp: with open(_log_file, 'a') as fp:
from traceback import format_exception from traceback import format_exception
@ -352,7 +352,7 @@ def execute(argv):
args.update({'exit_code': e.errno}) args.update({'exit_code': e.errno})
# Unknown errors # Unknown errors
except Exception: except Exception as e:
info = _log_exception() info = _log_exception()
args.update({'exit_code': 501}) args.update({'exit_code': 501})
if process_stderr: if process_stderr:

View File

@ -15,11 +15,6 @@ import re
import getpass import getpass
if 'raw_input' in __builtins__:
input = raw_input
range = xrange
def user_info(): def user_info():
if config.SERVER_MODE is False: if config.SERVER_MODE is False:
print(u"NOTE: Configuring authentication for DESKTOP mode.") print(u"NOTE: Configuring authentication for DESKTOP mode.")
@ -48,9 +43,10 @@ def user_info():
) )
email_filter = re.compile( email_filter = re.compile(
"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]" "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9]"
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]" "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]"
"(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
)
email = input("Email address: ") email = input("Email address: ")
while email == '' or not email_filter.match(email): while email == '' or not email_filter.match(email):

View File

@ -31,7 +31,7 @@ def run_backup_job(tester, job_id, expected_params, assert_in, assert_not_in,
if cnt >= 5: if cnt >= 5:
break break
# Check the process list # Check the process list
response1 = tester.get('/misc/bgprocess/?_='.format( response1 = tester.get('/misc/bgprocess/?_={0}'.format(
random.randint(1, 9999999))) random.randint(1, 9999999)))
assert_equal(response1.status_code, 200) assert_equal(response1.status_code, 200)
process_list = json.loads(response1.data.decode('utf-8')) process_list = json.loads(response1.data.decode('utf-8'))
@ -39,7 +39,7 @@ def run_backup_job(tester, job_id, expected_params, assert_in, assert_not_in,
try: try:
the_process = next( the_process = next(
p for p in process_list if p['id'] == job_id) p for p in process_list if p['id'] == job_id)
except Exception as _: except Exception:
the_process = None the_process = None
if the_process and 'execution_time' in the_process: if the_process and 'execution_time' in the_process:
@ -68,12 +68,12 @@ def run_backup_job(tester, job_id, expected_params, assert_in, assert_not_in,
assert_not_in(opt, the_process['details']) assert_not_in(opt, the_process['details'])
# Check the process details # Check the process details
p_details = tester.get('/misc/bgprocess/{0}?_='.format( p_details = tester.get('/misc/bgprocess/{0}?_={1}'.format(
job_id, random.randint(1, 9999999)) job_id, random.randint(1, 9999999))
) )
assert_equal(p_details.status_code, 200) assert_equal(p_details.status_code, 200)
p_details = tester.get('/misc/bgprocess/{0}/{1}/{2}/?_='.format( p_details = tester.get('/misc/bgprocess/{0}/{1}/{2}/?_={3}'.format(
job_id, 0, 0, random.randint(1, 9999999)) job_id, 0, 0, random.randint(1, 9999999))
) )
assert_equal(p_details.status_code, 200) assert_equal(p_details.status_code, 200)

View File

@ -8,7 +8,6 @@
########################################################################## ##########################################################################
"""A blueprint module implementing the datagrid frame.""" """A blueprint module implementing the datagrid frame."""
MODULE_NAME = 'datagrid'
import simplejson as json import simplejson as json
import pickle import pickle
@ -17,9 +16,10 @@ import random
from threading import Lock from threading import Lock
from flask import Response, url_for, session, request, make_response from flask import Response, url_for, session, request, make_response
from werkzeug.useragents import UserAgent from werkzeug.useragents import UserAgent
from flask import current_app as app from flask import current_app as app, render_template
from flask_babelex import gettext
from flask_security import login_required from flask_security import login_required
from pgadmin.tools.sqleditor.command import * from pgadmin.tools.sqleditor.command import ObjectRegistry, SQLFilter
from pgadmin.utils import PgAdminModule from pgadmin.utils import PgAdminModule
from pgadmin.utils.ajax import make_json_response, bad_request, \ from pgadmin.utils.ajax import make_json_response, bad_request, \
internal_server_error internal_server_error
@ -32,6 +32,7 @@ from pgadmin.utils.preferences import Preferences
from pgadmin.settings import get_setting from pgadmin.settings import get_setting
from pgadmin.browser.utils import underscore_unescape from pgadmin.browser.utils import underscore_unescape
MODULE_NAME = 'datagrid'
query_tool_close_session_lock = Lock() query_tool_close_session_lock = Lock()
@ -153,7 +154,7 @@ def initialize_datagrid(trans_id, cmd_type, obj_type, sgid, sid, did, obj_id):
auto_reconnect=False, auto_reconnect=False,
use_binary_placeholder=True, use_binary_placeholder=True,
array_to_string=True) array_to_string=True)
except (ConnectionLost, SSHTunnelConnectionLost) as e: except (ConnectionLost, SSHTunnelConnectionLost):
raise raise
except Exception as e: except Exception as e:
app.logger.error(e) app.logger.error(e)

View File

@ -9,13 +9,11 @@
"""A blueprint module implementing the debugger""" """A blueprint module implementing the debugger"""
MODULE_NAME = 'debugger'
import simplejson as json import simplejson as json
import random import random
import re import re
from flask import url_for, Response, render_template, request, session, \ from flask import url_for, Response, render_template, request, \
current_app current_app
from flask_babelex import gettext from flask_babelex import gettext
from flask_security import login_required from flask_security import login_required
@ -34,6 +32,8 @@ from config import PG_DEFAULT_DRIVER
from pgadmin.model import db, DebuggerFunctionArguments from pgadmin.model import db, DebuggerFunctionArguments
from pgadmin.tools.debugger.utils.debugger_instance import DebuggerInstance from pgadmin.tools.debugger.utils.debugger_instance import DebuggerInstance
MODULE_NAME = 'debugger'
# Constants # Constants
ASYNC_OK = 1 ASYNC_OK = 1
@ -344,7 +344,6 @@ def init_function(node_type, sid, did, scid, fid, trid=None):
# Get the server version, server type and user information # Get the server version, server type and user information
server_type = manager.server_type server_type = manager.server_type
user = manager.user_info
# Check server type is ppas or not # Check server type is ppas or not
ppas_server = False ppas_server = False
@ -2122,7 +2121,7 @@ def close_debugger_session(_trans_id, close_all=False):
dbg_obj['exe_conn_id'], dbg_obj['exe_conn_id'],
dbg_obj['database_id']) dbg_obj['database_id'])
manager.release(conn_id=dbg_obj['exe_conn_id']) manager.release(conn_id=dbg_obj['exe_conn_id'])
except Exception as _: except Exception:
raise raise
finally: finally:
de_inst.clear() de_inst.clear()

View File

@ -79,7 +79,7 @@ class MaintenanceJobTest(BaseTestGenerator):
if cnt >= 10: if cnt >= 10:
break break
# Check the process list # Check the process list
response1 = self.tester.get('/misc/bgprocess/?_='.format( response1 = self.tester.get('/misc/bgprocess/?_={0}'.format(
random.randint(1, 9999999))) random.randint(1, 9999999)))
self.assertEqual(response1.status_code, 200) self.assertEqual(response1.status_code, 200)
process_list = json.loads(response1.data.decode('utf-8')) process_list = json.loads(response1.data.decode('utf-8'))
@ -87,7 +87,7 @@ class MaintenanceJobTest(BaseTestGenerator):
try: try:
the_process = next( the_process = next(
p for p in process_list if p['id'] == job_id) p for p in process_list if p['id'] == job_id)
except Exception as _: except Exception:
the_process = None the_process = None
if the_process and 'execution_time' in the_process: if the_process and 'execution_time' in the_process:
@ -104,13 +104,15 @@ class MaintenanceJobTest(BaseTestGenerator):
self.assertIn(self.expected_cmd, the_process['details']) self.assertIn(self.expected_cmd, the_process['details'])
# Check the process details # Check the process details
p_details = self.tester.get('/misc/bgprocess/{0}?_='.format( p_details = self.tester.get('/misc/bgprocess/{0}?_={1}'.format(
job_id, random.randint(1, 9999999)) job_id, random.randint(1, 9999999))
) )
self.assertEqual(p_details.status_code, 200) self.assertEqual(p_details.status_code, 200)
p_details = self.tester.get('/misc/bgprocess/{0}/{1}/{2}/?_='.format( p_details = self.tester.get(
job_id, 0, 0, random.randint(1, 9999999)) '/misc/bgprocess/{0}/{1}/{2}/?_={3}'.format(
job_id, 0, 0, random.randint(1, 9999999)
)
) )
self.assertEqual(p_details.status_code, 200) self.assertEqual(p_details.status_code, 200)
p_details_data = json.loads(p_details.data.decode('utf-8')) p_details_data = json.loads(p_details.data.decode('utf-8'))

View File

@ -7,7 +7,6 @@
# #
########################################################################## ##########################################################################
import sys
import time import time
import random import random
import os import os
@ -120,7 +119,7 @@ class RestoreJobTest(BaseTestGenerator):
if cnt >= 5: if cnt >= 5:
break break
# Check the process list # Check the process list
response1 = self.tester.get('/misc/bgprocess/?_='.format( response1 = self.tester.get('/misc/bgprocess/?_={0}'.format(
random.randint(1, 9999999))) random.randint(1, 9999999)))
self.assertEqual(response1.status_code, 200) self.assertEqual(response1.status_code, 200)
process_list = json.loads(response1.data.decode('utf-8')) process_list = json.loads(response1.data.decode('utf-8'))
@ -128,7 +127,7 @@ class RestoreJobTest(BaseTestGenerator):
try: try:
the_process = next( the_process = next(
p for p in process_list if p['id'] == job_id) p for p in process_list if p['id'] == job_id)
except Exception as _: except Exception:
the_process = None the_process = None
if the_process and 'execution_time' in the_process: if the_process and 'execution_time' in the_process:
@ -150,14 +149,16 @@ class RestoreJobTest(BaseTestGenerator):
self.assertNotIn(opt, the_process['details']) self.assertNotIn(opt, the_process['details'])
# Check the process details # Check the process details
p_details = self.tester.get('/misc/bgprocess/{0}?_='.format( p_details = self.tester.get('/misc/bgprocess/{0}?_={1}'.format(
job_id, random.randint(1, 9999999)) job_id, random.randint(1, 9999999))
) )
self.assertEqual(p_details.status_code, 200) self.assertEqual(p_details.status_code, 200)
json.loads(p_details.data.decode('utf-8')) json.loads(p_details.data.decode('utf-8'))
p_details = self.tester.get('/misc/bgprocess/{0}/{1}/{2}/?_='.format( p_details = self.tester.get(
job_id, 0, 0, random.randint(1, 9999999)) '/misc/bgprocess/{0}/{1}/{2}/?_={3}'.format(
job_id, 0, 0, random.randint(1, 9999999)
)
) )
self.assertEqual(p_details.status_code, 200) self.assertEqual(p_details.status_code, 200)
p_details_data = json.loads(p_details.data.decode('utf-8')) p_details_data = json.loads(p_details.data.decode('utf-8'))

View File

@ -18,13 +18,12 @@ import select
import six import six
import datetime import datetime
from collections import deque from collections import deque
import simplejson as json
import psycopg2 import psycopg2
from flask import g, current_app from flask import g, current_app
from flask_babelex import gettext from flask_babelex import gettext
from flask_security import current_user from flask_security import current_user
from pgadmin.utils.crypto import decrypt from pgadmin.utils.crypto import decrypt
from psycopg2.extensions import adapt, encodings from psycopg2.extensions import encodings
import config import config
from pgadmin.model import User from pgadmin.model import User
@ -697,10 +696,10 @@ WHERE
current_app.logger.error( current_app.logger.error(
u"failed to execute query ((with server cursor) " u"failed to execute query ((with server cursor) "
u"for the server #{server_id} - {conn_id} " u"for the server #{server_id} - {conn_id} "
u"(query-id: {query_id}):\nerror message:{errmsg}".format( u"(query-id: {query_id}):\n"
u"error message:{errmsg}".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id, conn_id=self.conn_id,
query=query,
errmsg=errmsg, errmsg=errmsg,
query_id=query_id query_id=query_id
) )
@ -769,7 +768,6 @@ WHERE
header = [] header = []
json_columns = [] json_columns = []
conn_encoding = encodings[cur.connection.encoding]
for c in cur.ordered_description(): for c in cur.ordered_description():
# This is to handle the case in which column name is non-ascii # This is to handle the case in which column name is non-ascii
@ -880,7 +878,6 @@ WHERE
u"Error Message:{errmsg}".format( u"Error Message:{errmsg}".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id, conn_id=self.conn_id,
query=query,
errmsg=errmsg, errmsg=errmsg,
query_id=query_id query_id=query_id
) )
@ -946,7 +943,6 @@ WHERE
u"Error Message:{errmsg}".format( u"Error Message:{errmsg}".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id, conn_id=self.conn_id,
query=query.decode(encoding),
errmsg=errmsg, errmsg=errmsg,
query_id=query_id query_id=query_id
) )
@ -1018,7 +1014,6 @@ WHERE
u"Error Message:{errmsg}".format( u"Error Message:{errmsg}".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id, conn_id=self.conn_id,
query=query,
errmsg=errmsg, errmsg=errmsg,
query_id=query_id query_id=query_id
) )
@ -1048,7 +1043,7 @@ WHERE
current_app.logger.warning( current_app.logger.warning(
"Failed to reconnect the database server " "Failed to reconnect the database server "
"(#{server_id})".format( "(Server #{server_id}, Connection #{conn_id})".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id conn_id=self.conn_id
) )
@ -1097,7 +1092,6 @@ WHERE
u"Error Message:{errmsg}".format( u"Error Message:{errmsg}".format(
server_id=self.manager.sid, server_id=self.manager.sid,
conn_id=self.conn_id, conn_id=self.conn_id,
query=query,
errmsg=errmsg, errmsg=errmsg,
query_id=query_id query_id=query_id
) )
@ -1215,7 +1209,7 @@ WHERE
for col in self.column_info: for col in self.column_info:
new_row.append(row[col['name']]) new_row.append(row[col['name']])
result.append(new_row) result.append(new_row)
except psycopg2.ProgrammingError as e: except psycopg2.ProgrammingError:
result = None result = None
else: else:
# User performed operation which dose not produce record/s as # User performed operation which dose not produce record/s as