Skip to content

Commit

Permalink
Dev remove dead code (tableau#285)
Browse files Browse the repository at this point in the history
* Delete dead code

* Remove Python 3 workarounds for Unicode

* Remove checks for Python 3

* Replace %s with f-strings

* Replace %r with f-strings

* Replace .format() with f-strings

* Fix pycodestyle warnings

* Add configuration for coverage reporting

* Add configuration for coverage reporting

* Add configuration for coverage reporting
  • Loading branch information
0golovatyi authored May 6, 2019
1 parent 1602a33 commit 0b3d315
Show file tree
Hide file tree
Showing 28 changed files with 267 additions and 320 deletions.
7 changes: 7 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[report]
# Exclude lines that match patterns from coverage report.
exclude_lines =
if __name__ == .__main__.:

# Only show one number after decimal point in report.
precision = 1
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ details are on
## Documentation Updates

For any process, scripts or API changes documentation needs to be updated accordingly.
Please use markdown validation tools like web-based[markdownlint](https://dlaa.me/markdownlint/)
Please use markdown validation tools like web-based [markdownlint](https://dlaa.me/markdownlint/)
or npm [markdownlint-cli](https://github.com/igorshubovych/markdownlint-cli).

TOC for markdown file is built with [markdown-toc](https://www.npmjs.com/package/markdown-toc):
Expand Down
26 changes: 14 additions & 12 deletions tabpy-server/tabpy_server/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ def run(self):
logger.critical(msg)
raise RuntimeError(msg)

logger.info('Web service listening on port {}'.format(
str(self.settings[SettingsParameters.Port])))
logger.info(
'Web service listening on port '
f'{str(self.settings[SettingsParameters.Port])}')
tornado.ioloop.IOLoop.instance().start()

def _create_tornado_web_app(self):
Expand Down Expand Up @@ -164,8 +165,8 @@ def _parse_config(self, config_file):
parser.read_string(f.read())
else:
logger.warning(
"Unable to find config file at '{}', "
"using default settings.".format(config_file))
f'Unable to find config file at {config_file}, '
'using default settings.')

def set_parameter(settings_key,
config_key,
Expand Down Expand Up @@ -215,8 +216,8 @@ def set_parameter(settings_key,
os.path.expanduser(
self.settings[SettingsParameters.StateFilePath])))
state_file_path = self.settings[SettingsParameters.StateFilePath]
logger.info("Loading state from state file %s" %
os.path.join(state_file_path, "state.ini"))
logger.info('Loading state from state file '
f'{os.path.join(state_file_path, "state.ini")}')
tabpy_state = _get_state_from_file(state_file_path)
self.tabpy_state = TabPyState(
config=tabpy_state, settings=self.settings)
Expand Down Expand Up @@ -262,9 +263,10 @@ def set_parameter(settings_key,
self.settings[SettingsParameters.LogRequestContext] = (
self.settings[SettingsParameters.LogRequestContext].lower() !=
'false')
logger.info('Call context logging is {}'.format(
'enabled' if self.settings[SettingsParameters.LogRequestContext]
else 'disabled'))
call_context_state =\
'enabled' if self.settings[SettingsParameters.LogRequestContext]\
else 'disabled'
logger.info(f'Call context logging is {call_context_state}')

def _validate_transfer_protocol_settings(self):
if SettingsParameters.TransferProtocol not in self.settings:
Expand Down Expand Up @@ -297,9 +299,9 @@ def _validate_transfer_protocol_settings(self):

@staticmethod
def _validate_cert_key_state(msg, cert_valid, key_valid):
cert_and_key_param = '{} and {}'.format(
ConfigParameters.TABPY_CERTIFICATE_FILE,
ConfigParameters.TABPY_KEY_FILE)
cert_and_key_param = (
f'{ConfigParameters.TABPY_CERTIFICATE_FILE} and '
f'{ConfigParameters.TABPY_KEY_FILE}')
https_error = 'Error using HTTPS: '
err = None
if not cert_valid and not key_valid:
Expand Down
16 changes: 7 additions & 9 deletions tabpy-server/tabpy_server/app/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import csv
from datetime import datetime
import logging
from OpenSSL import crypto
import os

from datetime import datetime
from OpenSSL import crypto

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,23 +72,21 @@ def parse_pwd_file(pwd_file_name):

if len(row) != 2:
logger.error(
'Incorrect entry "{}" '
'in password file'.format(row))
f'Incorrect entry "{row}" in password file')
return False, {}

login = row[0].lower()
if login in credentials:
logger.error(
'Multiple entries for username {} '
'in password file'.format(login))
f'Multiple entries for username {login} '
'in password file')
return False, {}

if(len(row[1]) > 0):
credentials[login] = row[1]
logger.debug('Found username {}'.format(login))
logger.debug(f'Found username {login}')
else:
logger.warning('Found username {} but no password'
.format(row[0]))
logger.warning(f'Found username {row[0]} but no password')
return False, {}

logger.info("Authentication is enabled")
Expand Down
3 changes: 1 addition & 2 deletions tabpy-server/tabpy_server/common/endpoint_file_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _check_endpoint_name(name, logger=logging.getLogger(__name__)):
"""Checks that the endpoint name is valid by comparing it with an RE and
checking that it is not reserved."""
if not isinstance(name, str):
msg = 'Endpoint name must be a string or unicode'
msg = 'Endpoint name must be a string'
logger.log(logging.CRITICAL, msg)
raise TypeError(msg)

Expand All @@ -43,7 +43,6 @@ def grab_files(directory):
'''
if not os.path.isdir(directory):
return
yield
else:
for name in os.listdir(directory):
full_path = os.path.join(directory, name)
Expand Down
6 changes: 1 addition & 5 deletions tabpy-server/tabpy_server/common/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import traceback


def format_exception(e, context):
err_msg = "%s : " % e.__class__.__name__
err_msg += "%s" % str(e)
err_msg = f'{e.__class__.__name__} : {str(e)}'
return err_msg
2 changes: 1 addition & 1 deletion tabpy-server/tabpy_server/handlers/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def fail_with_not_authorized(self):
'Failing with 401 for unauthorized request')
self.set_status(401)
self.set_header('WWW-Authenticate',
'Basic realm="{}"'.format(self.tabpy_state.name))
f'Basic realm="{self.tabpy_state.name}"')
self.error_out(
401,
info="Unauthorized request.",
Expand Down
10 changes: 5 additions & 5 deletions tabpy-server/tabpy_server/handlers/endpoint_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get(self, endpoint_name):
self.tabpy_state.get_endpoints()[endpoint_name]))
else:
self.error_out(404, 'Unknown endpoint',
info='Endpoint %s is not found' % endpoint_name)
info=f'Endpoint {endpoint_name} is not found')

@tornado.web.asynchronous
@gen.coroutine
Expand Down Expand Up @@ -72,7 +72,7 @@ def put(self, name):
endpoints = self.tabpy_state.get_endpoints(name)
if len(endpoints) == 0:
self.error_out(404,
"endpoint %s does not exist." % name)
f'endpoint {name} does not exist.')
self.finish()
return

Expand Down Expand Up @@ -109,7 +109,7 @@ def delete(self, name):
endpoints = self.tabpy_state.get_endpoints(name)
if len(endpoints) == 0:
self.error_out(404,
"endpoint %s does not exist." % name)
f'endpoint {name} does not exist.')
self.finish()
return

Expand All @@ -118,7 +118,7 @@ def delete(self, name):
endpoint_info = self.tabpy_state.delete_endpoint(name)
except Exception as e:
self.error_out(400,
"Error when removing endpoint: %s" % e.message)
f'Error when removing endpoint: {e.message}')
self.finish()
return

Expand All @@ -130,7 +130,7 @@ def delete(self, name):
yield self._delete_po_future(delete_path)
except Exception as e:
self.error_out(400,
"Error while deleting: %s" % e)
f'Error while deleting: {e}')
self.finish()
return

Expand Down
2 changes: 1 addition & 1 deletion tabpy-server/tabpy_server/handlers/endpoints_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def post(self):

# check if endpoint already exist
if name in self.tabpy_state.get_endpoints():
self.error_out(400, "endpoint %s already exists." % name)
self.error_out(400, f'endpoint {name} already exists.')
self.finish()
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def post(self):
self.finish()

except Exception as e:
err_msg = "%s : " % e.__class__.__name__
err_msg += "%s" % str(e)
err_msg = f'{e.__class__.__name__} : {str(e)}'
if err_msg != "KeyError : 'response'":
err_msg = format_exception(e, 'POST /evaluate')
self.error_out(500, 'Error processing script', info=err_msg)
Expand All @@ -106,10 +105,7 @@ def post(self):
def call_subprocess(self, function_to_evaluate, arguments):
restricted_tabpy = RestrictedTabPy(self.port, self)
# Exec does not run the function, so it does not block.
if sys.version_info > (3, 0):
exec(function_to_evaluate, globals())
else:
exec(function_to_evaluate)
exec(function_to_evaluate, globals())

if arguments is None:
future = self.executor.submit(_user_script, restricted_tabpy)
Expand Down
22 changes: 7 additions & 15 deletions tabpy-server/tabpy_server/handlers/management_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
from tabpy_server.psws.callbacks import on_state_change


if sys.version_info.major == 3:
unicode = str


def copy_from_local(localpath, remotepath, is_dir=False):
if is_dir:
if not os.path.exists(remotepath):
Expand Down Expand Up @@ -53,10 +49,10 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
'''
Add or update an endpoint
'''
logging.debug("Adding/updating model {}...".format(name))
logging.debug(f'Adding/updating model {name}...')
_name_checker = _compile('^[a-zA-Z0-9-_\\s]+$')
if not isinstance(name, (str, unicode)):
msg = 'Endpoint name must be a string or unicode'
if not isinstance(name, str):
msg = 'Endpoint name must be a string'
self.logger.log(logging.CRITICAL, msg)
raise TypeError(msg)

Expand All @@ -76,12 +72,8 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
description = (request_data['description']
if 'description' in request_data else None)
if 'docstring' in request_data:
if sys.version_info > (3, 0):
docstring = str(bytes(request_data['docstring'],
"utf-8").decode('unicode_escape'))
else:
docstring = request_data['docstring'].decode(
'string_escape')
docstring = str(bytes(request_data['docstring'],
"utf-8").decode('unicode_escape'))
else:
docstring = None
endpoint_type = (request_data['type'] if 'type' in request_data
Expand All @@ -102,7 +94,7 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
_path_checker = _compile('^[\\a-zA-Z0-9-_\\s/]+$')
# copy from staging
if src_path:
if not isinstance(request_data['src_path'], (str, unicode)):
if not isinstance(request_data['src_path'], str):
raise gen.Return("src_path must be a string.")
if not _path_checker.match(src_path):
raise gen.Return('Endpoint name can only contain: a-z, A-'
Expand Down Expand Up @@ -144,7 +136,7 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
version=version)

except Exception as e:
raise gen.Return("Error when changing TabPy state: %s" % e)
raise gen.Return(f'Error when changing TabPy state: {e}')

on_state_change(self.settings,
self.tabpy_state,
Expand Down
34 changes: 15 additions & 19 deletions tabpy-server/tabpy_server/handlers/query_plane_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def _query(self, po_name, data, uid, qry):

if isinstance(response, QuerySuccessful):
response_json = response.to_json()
self.set_header("Etag", '"%s"' % md5(response_json.encode(
'utf-8')).hexdigest())
md5_tag = md5(response_json.encode('utf-8')).hexdigest()
self.set_header("Etag", f'"{md5_tag}"')
return (QuerySuccessful, response.for_json(), gls_time)
else:
self.logger.log(
Expand Down Expand Up @@ -93,8 +93,8 @@ def _handle_result(self, po_name, data, qry, uid):
else:
if response_type == UnknownURI:
self.error_out(404, 'UnknownURI',
info="No query object has been registered"
" with the name '%s'" % po_name)
info=('No query object has been registered'
f' with the name "{po_name}"'))
elif response_type == QueryError:
self.error_out(400, 'QueryError', info=response)
else:
Expand Down Expand Up @@ -141,16 +141,17 @@ def _process_query(self, endpoint_name, start):
# po_name is None if self.python_service.ps.query_objects.get(
# endpoint_name) is None
if not po_name:
self.error_out(404, 'UnknownURI',
info="Endpoint '%s' does not exist"
% endpoint_name)
self.error_out(
404,
'UnknownURI',
info=f'Endpoint "{endpoint_name}" does not exist')
return

po_obj = self.python_service.ps.query_objects.get(po_name)

if not po_obj:
self.error_out(404, 'UnknownURI',
info="Endpoint '%s' does not exist" % po_name)
info=f'Endpoint "{po_name}" does not exist')
return

if po_name != endpoint_name:
Expand Down Expand Up @@ -194,9 +195,10 @@ def _get_actual_model(self, endpoint_name):
elif endpoint_type == 'model':
break
else:
self.error_out(500, 'Unknown endpoint type',
info="Endpoint type '%s' does not exist"
% endpoint_type)
self.error_out(
500,
'Unknown endpoint type',
info=f'Endpoint type "{endpoint_type}" does not exist')
return

return (endpoint_name, all_endpoint_names)
Expand All @@ -208,10 +210,7 @@ def get(self, endpoint_name):
return

start = time.time()
if sys.version_info > (3, 0):
endpoint_name = urllib.parse.unquote(endpoint_name)
else:
endpoint_name = urllib.unquote(endpoint_name)
endpoint_name = urllib.parse.unquote(endpoint_name)
self._process_query(endpoint_name, start)

@tornado.web.asynchronous
Expand All @@ -221,8 +220,5 @@ def post(self, endpoint_name):
return

start = time.time()
if sys.version_info > (3, 0):
endpoint_name = urllib.parse.unquote(endpoint_name)
else:
endpoint_name = urllib.unquote(endpoint_name)
endpoint_name = urllib.parse.unquote(endpoint_name)
self._process_query(endpoint_name, start)
2 changes: 1 addition & 1 deletion tabpy-server/tabpy_server/handlers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def hash_password(username, pwd):
Sting representation (hexidecimal) for PBKDF2 hash
for the password.
'''
salt = '_$salt@tabpy:%s$_' % username.lower()
salt = f'_$salt@tabpy:{username.lower()}$_'

hash = pbkdf2_hmac(hash_name='sha512',
password=pwd.encode(),
Expand Down
Loading

0 comments on commit 0b3d315

Please sign in to comment.