Fix PEP-8 issues in feature_tests, dashboard, about and misc module's python code. Fixes #3082

This commit is contained in:
Murtuza Zabuawala
2018-02-09 12:57:37 +00:00
committed by Dave Page
parent 942ac733a4
commit 6f25f4d175
17 changed files with 634 additions and 361 deletions

View File

@@ -11,7 +11,7 @@
import pgadmin.utils.driver as driver
from flask import url_for, render_template, Response
from flask_babel import gettext as _
from flask_babel import gettext
from pgadmin.utils import PgAdminModule
from pgadmin.utils.preferences import Preferences
@@ -22,18 +22,20 @@ MODULE_NAME = 'misc'
class MiscModule(PgAdminModule):
def get_own_javascripts(self):
return [{
'name': 'pgadmin.misc.explain',
'path': url_for('misc.index') + 'explain/explain',
'preloaded': False
}, {
'name': 'snap.svg',
'path': url_for(
'misc.static', filename='explain/vendor/snap.svg/' + (
'snap.svg' if config.DEBUG else 'snap.svg-min'
)),
'preloaded': False
}]
return [
{
'name': 'pgadmin.misc.explain',
'path': url_for('misc.index') + 'explain/explain',
'preloaded': False
}, {
'name': 'snap.svg',
'path': url_for(
'misc.static', filename='explain/vendor/snap.svg/' + (
'snap.svg' if config.DEBUG else 'snap.svg-min'
)),
'preloaded': False
}
]
def get_own_stylesheets(self):
stylesheets = []
@@ -46,18 +48,24 @@ class MiscModule(PgAdminModule):
"""
Register preferences for this module.
"""
self.misc_preference = Preferences('miscellaneous', _('Miscellaneous'))
self.misc_preference = Preferences(
'miscellaneous', gettext('Miscellaneous')
)
lang_options = []
for lang in config.LANGUAGES:
lang_options.append({'label': config.LANGUAGES[lang],
'value': lang})
lang_options.append(
{
'label': config.LANGUAGES[lang],
'value': lang
}
)
# Register options for the User language settings
self.misc_preference.register(
'miscellaneous', 'user_language',
_("User language"), 'options', 'en',
category_label=_('User language'),
gettext("User language"), 'options', 'en',
category_label=gettext('User language'),
options=lang_options
)
@@ -102,7 +110,8 @@ def explain_js():
"""
return Response(
response=render_template(
"explain/js/explain.js", _=_
"explain/js/explain.js",
_=gettext
),
status=200,
mimetype="application/javascript"

View File

@@ -103,7 +103,6 @@ def acknowledge(pid):
"""
try:
BatchProcess.acknowledge(pid)
return success_return()
except LookupError as lerr:
return gone(errormsg=str(lerr))

View File

@@ -55,11 +55,13 @@ if _IS_PY2:
else:
def _log(msg):
with open(_log_file, 'a') as fp:
fp.write(('INFO:: %s\n' % msg.encode('ascii', 'xmlcharrefreplace')))
fp.write(
('INFO:: %s\n' % msg.encode('ascii', 'xmlcharrefreplace'))
)
def _log_exception():
type_, value_, traceback_ = info=sys.exc_info()
type_, value_, traceback_ = info = sys.exc_info()
with open(_log_file, 'ab') as fp:
from traceback import format_exception
@@ -159,7 +161,7 @@ class ProcessLogger(Thread):
Thread.__init__(self)
self.process = None
self.stream = None
self.logger = open(os.path.join(_out_dir, stream_type), 'wb')
self.logger = open(os.path.join(_out_dir, stream_type), 'wb')
def attach_process_stream(self, process, stream):
"""
@@ -189,9 +191,15 @@ class ProcessLogger(Thread):
# Write into log file
if self.logger:
if msg:
self.logger.write(get_current_time(format='%y%m%d%H%M%S%f').encode('utf-8'))
self.logger.write(
get_current_time(
format='%y%m%d%H%M%S%f'
).encode('utf-8')
)
self.logger.write(b',')
self.logger.write(msg.lstrip(b'\r\n' if _IS_WIN else b'\n'))
self.logger.write(
msg.lstrip(b'\r\n' if _IS_WIN else b'\n')
)
self.logger.write(os.linesep.encode('utf-8'))
return True
@@ -215,7 +223,10 @@ class ProcessLogger(Thread):
get_current_time(
format='%y%m%d%H%M%S%f'
),
msg.lstrip(b'\r\n' if _IS_WIN else b'\n'), os.linesep
msg.lstrip(
b'\r\n' if _IS_WIN else b'\n'
),
os.linesep
)
)
@@ -253,9 +264,8 @@ def update_status(**kw):
if _out_dir:
status = dict(
(k, v) for k, v in kw.items() if k in [
'start_time', 'end_time', 'exit_code', 'pid'
]
(k, v) for k, v in kw.items()
if k in ('start_time', 'end_time', 'exit_code', 'pid')
)
_log('Updating the status:\n{0}'.format(json.dumps(status)))
with open(os.path.join(_out_dir, 'status'), 'w') as fp:
@@ -396,7 +406,7 @@ def convert_environment_variables(env):
if not isinstance(value, str):
value = value.encode(_sys_encoding)
temp_env[key] = value
except Exception as e:
except Exception:
_log_exception()
return temp_env
@@ -411,8 +421,8 @@ if __name__ == '__main__':
_fs_encoding = sys.getfilesystemencoding()
if not _fs_encoding or _fs_encoding == 'ascii':
# Fall back to 'utf-8', if we couldn't determine the file-system encoding,
# or 'ascii'.
# Fall back to 'utf-8', if we couldn't determine the file-system
# encoding or 'ascii'.
_fs_encoding = 'utf-8'
def u(_s, _encoding=_sys_encoding):
@@ -442,14 +452,14 @@ if __name__ == '__main__':
# the child process to run as a daemon. And, it would run without
# depending on the status of the web-server.
if 'PGA_BGP_FOREGROUND' in os.environ and \
os.environ['PGA_BGP_FOREGROUND'] == "1":
os.environ['PGA_BGP_FOREGROUND'] == "1":
_log('[CHILD] Start process execution...')
# This is a child process running as the daemon process.
# Let's do the job assigning to it.
try:
_log('Executing the command now from the detached child...')
execute()
except:
except Exception:
_log_exception()
else:
from subprocess import CREATE_NEW_PROCESS_GROUP
@@ -464,10 +474,11 @@ if __name__ == '__main__':
env['PGA_BGP_FOREGROUND'] = "1"
# We need environment variables & values in string
_log('[PARENT] Converting the environment variable in the bytes format...')
_log('[PARENT] Converting the environment variable in the '
'bytes format...')
try:
env = convert_environment_variables(env)
except Exception as e:
except Exception:
_log_exception()
kwargs = {
@@ -477,7 +488,7 @@ if __name__ == '__main__':
'creationflags': CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS,
'close_fds': False,
'cwd': _out_dir,
'env': env
'env': env
}
cmd = [sys.executable]

View File

@@ -66,7 +66,9 @@ class BatchProcess(object):
if 'id' in kwargs:
self._retrieve_process(kwargs['id'])
else:
self._create_process(kwargs['desc'], kwargs['cmd'], kwargs['args'])
self._create_process(
kwargs['desc'], kwargs['cmd'], kwargs['args']
)
def _retrieve_process(self, _id):
p = Process.query.filter_by(pid=_id, user_id=current_user.id).first()
@@ -159,17 +161,25 @@ class BatchProcess(object):
args_csv_io, delimiter=str(','), quoting=csv.QUOTE_MINIMAL
)
if sys.version_info.major == 2:
csv_writer.writerow([a.encode('utf-8') if isinstance(a, unicode) else a for a in _args])
csv_writer.writerow(
[
a.encode('utf-8')
if isinstance(a, unicode) else a for a in _args
]
)
else:
csv_writer.writerow(_args)
args_val = args_csv_io.getvalue().strip(str('\r\n'))
j = Process(
pid=int(id), command=_cmd,
arguments=args_val.decode('utf-8', 'replace') if IS_PY2 and hasattr(args_val, 'decode') \
else args_val,
logdir=log_dir, desc=dumps(self.desc), user_id=current_user.id
pid=int(id),
command=_cmd,
arguments=args_val.decode('utf-8', 'replace')
if IS_PY2 and hasattr(args_val, 'decode') else args_val,
logdir=log_dir,
desc=dumps(self.desc),
user_id=current_user.id
)
db.session.add(j)
db.session.commit()
@@ -278,7 +288,9 @@ class BatchProcess(object):
if os.name == 'nt' and IS_PY2:
command = []
for c in cmd:
command.append(c.encode('utf-8') if isinstance(c, unicode) else str(c))
command.append(
c.encode('utf-8') if isinstance(c, unicode) else str(c)
)
current_app.logger.info(
u"Executing the process executor with the arguments: %s",
@@ -288,7 +300,8 @@ class BatchProcess(object):
cmd = command
else:
current_app.logger.info(
u"Executing the process executor with the arguments: %s", str(cmd)
u"Executing the process executor with the arguments: %s",
str(cmd)
)
# Make a copy of environment, and add new variables to support
@@ -318,8 +331,12 @@ class BatchProcess(object):
stderr = open(stderr, "a")
p = Popen(
cmd, close_fds=False, env=env, stdout=stdout.fileno(),
stderr=stderr.fileno(), stdin=stdin.fileno(),
cmd,
close_fds=False,
env=env,
stdout=stdout.fileno(),
stderr=stderr.fileno(),
stdin=stdin.fileno(),
creationflags=(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS)
)
else:
@@ -424,10 +441,12 @@ class BatchProcess(object):
execution_time = (etime - stime).total_seconds()
if process_output:
out, out_completed = read_log(self.stdout, stdout, out, ctime,
self.ecode)
err, err_completed = read_log(self.stderr, stderr, err, ctime,
self.ecode)
out, out_completed = read_log(
self.stdout, stdout, out, ctime, self.ecode
)
err, err_completed = read_log(
self.stderr, stderr, err, ctime, self.ecode
)
else:
out_completed = err_completed = False
@@ -439,8 +458,16 @@ class BatchProcess(object):
}
return {
'out': {'pos': out, 'lines': stdout, 'done': out_completed},
'err': {'pos': err, 'lines': stderr, 'done': err_completed},
'out': {
'pos': out,
'lines': stdout,
'done': out_completed
},
'err': {
'pos': err,
'lines': stderr,
'done': err_completed
},
'start_time': self.stime,
'exit_code': self.ecode,
'execution_time': execution_time
@@ -475,9 +502,8 @@ class BatchProcess(object):
except ValueError as e:
current_app.logger.warning(
_("Status for the background process '{0}' could not be loaded.").format(
p.pid
)
_("Status for the background process '{0}' could "
"not be loaded.").format(p.pid)
)
current_app.logger.exception(e)
return False, False
@@ -514,8 +540,8 @@ class BatchProcess(object):
if isinstance(desc, IProcessDesc):
args = []
args_csv = StringIO(
p.arguments.encode('utf-8') \
if hasattr(p.arguments, 'decode') else p.arguments
p.arguments.encode('utf-8')
if hasattr(p.arguments, 'decode') else p.arguments
)
args_reader = csv.reader(args_csv, delimiter=str(','))
for arg in args_reader:

View File

@@ -253,12 +253,12 @@ def file_manager_config(trans_id):
show_hidden_files = pref.preference('show_hidden_files').get()
return Response(response=render_template(
"file_manager/js/file_manager_config.json",
_=gettext,
data=data,
file_dialog_view=file_dialog_view,
show_hidden_files=show_hidden_files
),
"file_manager/js/file_manager_config.json",
_=gettext,
data=data,
file_dialog_view=file_dialog_view,
show_hidden_files=show_hidden_files
),
status=200,
mimetype="application/json"
)
@@ -301,6 +301,7 @@ def save_last_directory_visited(trans_id):
data={'status': True}
)
@blueprint.route(
"/save_file_dialog_view/<int:trans_id>", methods=["POST"],
endpoint='save_file_dialog_view'
@@ -312,6 +313,7 @@ def save_file_dialog_view(trans_id):
data={'status': True}
)
@blueprint.route(
"/save_show_hidden_file_option/<int:trans_id>", methods=["PUT"],
endpoint='save_show_hidden_file_option'
@@ -331,7 +333,9 @@ class Filemanager(object):
self.trans_id = trans_id
self.patherror = encode_json(
{
'Error': gettext('No permission to operate on specified path.'),
'Error': gettext(
'No permission to operate on specified path.'
),
'Code': 0
}
)
@@ -407,7 +411,8 @@ class Filemanager(object):
last_dir = last_dir[:-1]
while last_dir:
if os.path.exists(
(storage_dir if storage_dir is not None else '') + last_dir):
storage_dir
if storage_dir is not None else '' + last_dir):
break
if _platform == 'win32':
index = max(last_dir.rfind('\\'), last_dir.rfind('/'))
@@ -426,7 +431,8 @@ class Filemanager(object):
# create configs using above configs
configs = {
"fileroot": last_dir.replace('\\', '\\\\'), # for JS json compatibility
# for JS json compatibility
"fileroot": last_dir.replace('\\', '\\\\'),
"dialog_type": fm_type,
"title": title,
"upload": {
@@ -516,7 +522,7 @@ class Filemanager(object):
drives.append(letter)
bitmask >>= 1
if (drive_name != '' and drive_name is not None and
drive_name in drives):
drive_name in drives):
return u"{0}{1}".format(drive_name, ':')
else:
return drives # return drives if no argument is passed
@@ -577,7 +583,7 @@ class Filemanager(object):
try:
drive_size = getDriveSize(path)
drive_size_in_units = sizeof_fmt(drive_size)
except:
except Exception:
drive_size = 0
protected = 1 if drive_size == 0 else 0
files[file_name] = {
@@ -606,10 +612,10 @@ class Filemanager(object):
}
user_dir = path
folders_only = trans_data['folders_only'] if 'folders_only' in \
trans_data else ''
files_only = trans_data['files_only'] if 'files_only' in \
trans_data else ''
folders_only = trans_data['folders_only'] \
if 'folders_only' in trans_data else ''
files_only = trans_data['files_only'] \
if 'files_only' in trans_data else ''
supported_types = trans_data['supported_types'] \
if 'supported_types' in trans_data else []
@@ -622,7 +628,7 @@ class Filemanager(object):
# continue if file/folder is hidden (based on user preference)
if not is_show_hidden_files and \
(is_folder_hidden(system_path) or f.startswith('.')):
(is_folder_hidden(system_path) or f.startswith('.')):
continue
user_path = os.path.join(os.path.join(user_dir, f))
@@ -645,8 +651,8 @@ class Filemanager(object):
# filter files based on file_type
if file_type is not None and file_type != "*":
if folders_only or len(supported_types) > 0 and \
file_extension not in supported_types or \
file_type != file_extension:
file_extension not in supported_types or \
file_type != file_extension:
continue
# create a list of files and folders
@@ -790,8 +796,9 @@ class Filemanager(object):
}
if not path_exists(orig_path):
thefile['Error'] = gettext(u"'{0}' file does not exist.".format(
path))
thefile['Error'] = gettext(
u"'{0}' file does not exist.".format(path)
)
thefile['Code'] = -1
return thefile
@@ -822,7 +829,8 @@ class Filemanager(object):
if not dir.endswith('/'):
dir += u'/'
filelist = self.list_filesystem(dir, path, trans_data, file_type, show_hidden)
filelist = self.list_filesystem(
dir, path, trans_data, file_type, show_hidden)
return filelist
def rename(self, old=None, new=None, req=None):
@@ -901,7 +909,8 @@ class Filemanager(object):
}
dir = self.dir if self.dir is not None else ''
path = path.encode('utf-8').decode('utf-8') if hasattr(str, 'decode') else path
path = path.encode(
'utf-8').decode('utf-8') if hasattr(str, 'decode') else path
orig_path = u"{0}{1}".format(dir, path)
try:
@@ -951,20 +960,23 @@ class Filemanager(object):
file_obj = req.files['newfile']
file_name = file_obj.filename
if hasattr(str, 'decode'):
path = req.form.get('currentpath').encode('utf-8').decode('utf-8')
path = req.form.get('currentpath').encode(
'utf-8').decode('utf-8')
file_name = file_obj.filename.encode('utf-8').decode('utf-8')
orig_path = u"{0}{1}".format(dir, path)
newName = u"{0}{1}".format(orig_path, file_name)
with open(newName, 'wb') as f:
while True:
data = file_obj.read(4194304) # 4MB chunk (4 * 1024 * 1024 Bytes)
# 4MB chunk (4 * 1024 * 1024 Bytes)
data = file_obj.read(4194304)
if not data:
break
f.write(data)
except Exception as e:
code = 0
err_msg = u"Error: {0}".format(e.strerror if hasattr(e, 'strerror') else u'Unknown')
err_msg = u"Error: {0}".format(
e.strerror if hasattr(e, 'strerror') else u'Unknown')
try:
Filemanager.check_access_permission(dir, path)
@@ -998,7 +1010,8 @@ class Filemanager(object):
path = path.encode('utf-8').decode('utf-8')
try:
orig_path = u"{0}{1}".format(dir, path)
Filemanager.check_access_permission(dir, u"{}{}".format(path, name))
Filemanager.check_access_permission(
dir, u"{}{}".format(path, name))
newName = u"{0}{1}".format(orig_path, name)
if not os.path.exists(newName):
@@ -1059,14 +1072,14 @@ class Filemanager(object):
# check if file type is text or binary
text_chars = bytearray([7, 8, 9, 10, 12, 13, 27]) \
+ bytearray(range(0x20, 0x7f)) \
+ bytearray(range(0x80, 0x100))
+ bytearray(range(0x20, 0x7f)) \
+ bytearray(range(0x80, 0x100))
def is_binary_string(bytes_data):
"""Checks if string data is binary"""
return bool(
bytes_data.translate(None, text_chars)
)
bytes_data.translate(None, text_chars)
)
# read the file
try:
@@ -1180,11 +1193,13 @@ class Filemanager(object):
orig_path = u"{0}{1}".format(dir, path)
try:
Filemanager.check_access_permission(dir, u"{}{}".format(
path, path))
Filemanager.check_access_permission(
dir, u"{}{}".format(path, path)
)
except Exception as e:
resp = Response(gettext(u"Error: {0}".format(e)))
resp.headers['Content-Disposition'] = 'attachment; filename=' + name
resp.headers['Content-Disposition'] = \
'attachment; filename=' + name
return resp
name = path.split('/')[-1]