Skip to content

Commit

Permalink
Code improvements (tableau#431)
Browse files Browse the repository at this point in the history
* Rename tabpy_server and tabpy_tools to server and tools (breaking change)

* fix flake8 warnings

* Clean up code to reduce number of conditions
  • Loading branch information
0golovatyi authored Jul 20, 2020
1 parent 5539afd commit c43d9b1
Show file tree
Hide file tree
Showing 66 changed files with 329 additions and 339 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ package-lock.json
.idea/

# TabPy server artifacts
tabpy/tabpy_server/state.ini
tabpy/tabpy_server/query_objects
tabpy/tabpy_server/staging
tabpy/server/state.ini
tabpy/server/query_objects
tabpy/server/staging

# VS Code
*.code-workspace
Expand All @@ -128,3 +128,4 @@ tabpy/tabpy_server/staging
# etc
setup.bat
*~
tabpy_log.log.1
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"**/*.pyc": true
},
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.pytestArgs": [
Expand All @@ -18,5 +18,6 @@
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.linting.pycodestyleEnabled": true
"python.linting.pycodestyleEnabled": false,
"python.pythonPath": "C:\\Users\\ogolovatyi\\Anaconda3\\envs\\Python 37\\python.exe"
}
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v2.0.0

### Improvements

- Minor code cleanup.

### Breaking changes

- tabpy_server and tabpy_tools are renamed to server and tools.


## v1.1.0

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ and run it locally.
These are prerequisites for an environment required for a contributor to
be able to work on TabPy changes:

- Python 3.6 or 3.7:
- Python 3.6, 3.7 or 3.8:
- To see which version of Python you have installed, run `python --version`.
- git
- Node.js for npm packages - install from <https://nodejs.org>.
Expand Down
8 changes: 4 additions & 4 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
exclude \
tabpy/tabpy_server/state.ini
tabpy/server/state.ini

include \
CHANGELOG \
LICENSE \
tabpy/VERSION \
tabpy/tabpy_server/state.ini.template \
tabpy/tabpy_server/static/* \
tabpy/tabpy_server/common/default.conf
tabpy/server/state.ini.template \
tabpy/server/static/* \
tabpy/server/common/default.conf
2 changes: 1 addition & 1 deletion docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ you may want to consider the following as you use TabPy:
- The REST server and Python execution share the same Python session,
meaning that HTTP requests and user scripts are evaluated in the
same addressable memory and processor threads.
- The tabpy_tools client does not perform client-side validation of the
- The tabpy.tools client does not perform client-side validation of the
SSL certificate on TabPy Server.
- Python scripts can contain code which can harm security on the server
where the TabPy is running. For example, Python scripts can:
Expand Down
10 changes: 5 additions & 5 deletions docs/server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ at [`logging.config` documentation page](https://docs.python.org/3.6/library/log
[TabPy Tools documentation](tabpy-tools.md) for details. Default value -
`/tmp/query_objects`.
- `TABPY_STATE_PATH` - state folder location (absolute path) for Tornado web
server. Default value - `tabpy/tabpy_server` subfolder in TabPy package
server. Default value - `tabpy/server` subfolder in TabPy package
folder.
- `TABPY_STATIC_PATH` - absolute path for location of static files (index.html
page) for TabPy instance. Default value - `tabpy/tabpy_server/static`
page) for TabPy instance. Default value - `tabpy/server/static`
subfolder in TabPy package folder.
- `TABPY_PWD_FILE` - absolute path to password file. Setting up this parameter
makes TabPy require credentials with HTTP(S) requests. More details about
Expand Down Expand Up @@ -101,10 +101,10 @@ settings._
[TabPy]
# TABPY_QUERY_OBJECT_PATH = /tmp/query_objects
# TABPY_PORT = 9004
# TABPY_STATE_PATH = <package-path>/tabpy/tabpy_server
# TABPY_STATE_PATH = <package-path>/tabpy/server

# Where static pages live
# TABPY_STATIC_PATH = <package-path>/tabpy/tabpy_server/static
# TABPY_STATIC_PATH = <package-path>/tabpy/server/static

# For how to configure TabPy authentication read
# docs/server-config.md.
Expand Down Expand Up @@ -256,7 +256,7 @@ as explained in Python documentation at
[Logging Configuration page](https://docs.python.org/3.6/library/logging.config.html).

A default config provided with TabPy is at
[`tabpy-server/tabpy_server/common/default.conf`](tabpy-server/tabpy_server/common/default.conf)
[`tabpy-server/server/common/default.conf`](tabpy-server/server/common/default.conf)
and has a configuration for console and file loggers. Changing the config file
allows the user to modify the log level, format of the logged messages and
add or remove loggers.
Expand Down
14 changes: 7 additions & 7 deletions docs/tabpy-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ to specify the service location for all subsequent operations:

```python

from tabpy.tabpy_tools.client import Client
from tabpy.tools.client import Client

client = Client('http://localhost:9004/')

Expand Down Expand Up @@ -351,7 +351,7 @@ method provided in this tools package:

```python

from tabpy_tools.schema import generate_schema
from tabpy.tools.schema import generate_schema

schema = generate_schema(
input={'x': 3, 'y': 2},
Expand All @@ -368,7 +368,7 @@ To describe more complex input, like arrays, you would use the following syntax:

```python

from tabpy_tools.schema import generate_schema
from tabpy.tools.schema import generate_schema

schema = generate_schema(
input={'x': [6.35, 6.40, 6.65, 8.60],
Expand Down Expand Up @@ -413,10 +413,10 @@ Response:

```json
{
'model': 'clustering',
'response': [0, 0, 0, 1, 1, 1, 1],
'uuid': '1ca01e46-733c-4a77-b3da-3ded84dff4cd',
'version': 2
"model": "clustering",
"response": [0, 0, 0, 1, 1, 1, 1],
"uuid": "1ca01e46-733c-4a77-b3da-3ded84dff4cd",
"version": 2
}

```
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def read(fname):
package_data={
"tabpy": [
"VERSION",
"tabpy_server/state.ini.template",
"tabpy_server/static/*",
"tabpy_server/common/default.conf",
"server/state.ini.template",
"server/static/*",
"server/common/default.conf",
]
},
python_requires=">=3.6",
Expand Down
2 changes: 1 addition & 1 deletion tabpy/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
2.0.0
23 changes: 7 additions & 16 deletions tabpy/models/deploy_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from pathlib import Path
import pip
import platform
import subprocess
import sys
Expand All @@ -9,26 +8,18 @@

def main():
# Determine if we run python or python3
if platform.system() == "Windows":
py = "python"
else:
py = "python3"
py = "python" if platform.system() == "Windows" else "python3"

if len(sys.argv) > 1:
config_file_path = sys.argv[1]
else:
config_file_path = setup_utils.get_default_config_file_path()
print(f"Using config file at {config_file_path}")
port, auth_on, prefix = setup_utils.parse_config(config_file_path)
if auth_on:
auth_args = setup_utils.get_creds()
else:
auth_args = []
file_path = sys.argv[1] if len(sys.argv) > 1 else setup_utils.get_default_config_file_path()
print(f"Using config file at {file_path}")

port, auth_on, prefix = setup_utils.parse_config(file_path)
auth_args = setup_utils.get_creds() if auth_on else []

directory = str(Path(__file__).resolve().parent / "scripts")
# Deploy each model in the scripts directory
for filename in os.listdir(directory):
subprocess.run([py, f"{directory}/{filename}", config_file_path] + auth_args)
subprocess.run([py, f"{directory}/{filename}", file_path] + auth_args)


if __name__ == "__main__":
Expand Down
15 changes: 4 additions & 11 deletions tabpy/models/utils/setup_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import getpass
import os
import sys
from tabpy.tabpy_tools.client import Client
from tabpy.tools.client import Client


def get_default_config_file_path():
import tabpy

pkg_path = os.path.dirname(tabpy.__file__)
config_file_path = os.path.join(pkg_path, "tabpy_server", "common", "default.conf")
config_file_path = os.path.join(pkg_path, "server", "common", "default.conf")
return config_file_path


Expand Down Expand Up @@ -44,21 +44,14 @@ def get_creds():

def deploy_model(funcName, func, funcDescription):
# running from deploy_models.py
if len(sys.argv) > 1:
config_file_path = sys.argv[1]
else:
config_file_path = get_default_config_file_path()
config_file_path = sys.argv[1] if len(sys.argv) > 1 else get_default_config_file_path()
port, auth_on, prefix = parse_config(config_file_path)

connection = Client(f"{prefix}://localhost:{port}/")

if auth_on:
# credentials are passed in from setup.py
if len(sys.argv) == 4:
user, passwd = sys.argv[2], sys.argv[3]
# running Sentiment Analysis independently
else:
user, passwd = get_creds()
user, passwd = sys.argv[2], sys.argv[3] if len(sys.argv) == 4 else get_creds()
connection.set_credentials(user, passwd)

connection.deploy(funcName, func, funcDescription, override=True)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 15 additions & 15 deletions tabpy/tabpy_server/app/app.py → tabpy/server/app/app.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import concurrent.futures
import configparser
import logging
from logging import config
import multiprocessing
import os
import shutil
import signal
import sys
import tabpy.tabpy_server
import tabpy
from tabpy.tabpy import __version__
from tabpy.tabpy_server.app.ConfigParameters import ConfigParameters
from tabpy.tabpy_server.app.SettingsParameters import SettingsParameters
from tabpy.tabpy_server.app.util import parse_pwd_file
from tabpy.tabpy_server.management.state import TabPyState
from tabpy.tabpy_server.management.util import _get_state_from_file
from tabpy.tabpy_server.psws.callbacks import init_model_evaluator, init_ps_server
from tabpy.tabpy_server.psws.python_service import PythonService, PythonServiceHandler
from tabpy.tabpy_server.handlers import (
from tabpy.server.app.ConfigParameters import ConfigParameters
from tabpy.server.app.SettingsParameters import SettingsParameters
from tabpy.server.app.util import parse_pwd_file
from tabpy.server.management.state import TabPyState
from tabpy.server.management.util import _get_state_from_file
from tabpy.server.psws.callbacks import init_model_evaluator, init_ps_server
from tabpy.server.psws.python_service import PythonService, PythonServiceHandler
from tabpy.server.handlers import (
EndpointHandler,
EndpointsHandler,
EvaluationPlaneHandler,
Expand Down Expand Up @@ -69,7 +68,8 @@ def __init__(self, config_file=None):

if os.path.isfile(config_file):
try:
logging.config.fileConfig(config_file, disable_existing_loggers=False)
from logging import config
config.fileConfig(config_file, disable_existing_loggers=False)
except KeyError:
logging.basicConfig(level=logging.DEBUG)

Expand Down Expand Up @@ -266,9 +266,9 @@ def _parse_config(self, config_file):
None, None),
(SettingsParameters.KeyFile, ConfigParameters.TABPY_KEY_FILE, None, None),
(SettingsParameters.StateFilePath, ConfigParameters.TABPY_STATE_PATH,
os.path.join(pkg_path, "tabpy_server"), None),
os.path.join(pkg_path, "server"), None),
(SettingsParameters.StaticPath, ConfigParameters.TABPY_STATIC_PATH,
os.path.join(pkg_path, "tabpy_server", "static"), None),
os.path.join(pkg_path, "server", "static"), None),
(ConfigParameters.TABPY_PWD_FILE, ConfigParameters.TABPY_PWD_FILE, None, None),
(SettingsParameters.LogRequestContext, ConfigParameters.TABPY_LOG_DETAILS,
"false", None),
Expand Down Expand Up @@ -367,7 +367,7 @@ def _validate_transfer_protocol_settings(self):
os.path.isfile(cert),
os.path.isfile(self.settings[SettingsParameters.KeyFile]),
)
tabpy.tabpy_server.app.util.validate_cert(cert)
tabpy.server.app.util.validate_cert(cert)

@staticmethod
def _validate_cert_key_state(msg, cert_valid, key_valid):
Expand Down Expand Up @@ -417,7 +417,7 @@ def _build_tabpy_state(self):
state_file_path = os.path.join(state_file_dir, "state.ini")
if not os.path.isfile(state_file_path):
state_file_template_path = os.path.join(
pkg_path, "tabpy_server", "state.ini.template"
pkg_path, "server", "state.ini.template"
)
logger.debug(
f"File {state_file_path} not found, creating from "
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tabpy/tabpy_server/common/default.conf → tabpy/server/common/default.conf
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[TabPy]
# TABPY_QUERY_OBJECT_PATH = /tmp/query_objects
# TABPY_PORT = 9004
# TABPY_STATE_PATH = ./tabpy/tabpy_server
# TABPY_STATE_PATH = ./tabpy/server

# Where static pages live
# TABPY_STATIC_PATH = ./tabpy/tabpy_server/static
# TABPY_STATIC_PATH = ./tabpy/server/static

# For how to configure TabPy authentication read
# Authentication section in docs/server-config.md.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions tabpy/server/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tabpy.server.handlers.base_handler import BaseHandler
from tabpy.server.handlers.main_handler import MainHandler
from tabpy.server.handlers.management_handler import ManagementHandler

from tabpy.server.handlers.endpoint_handler import EndpointHandler
from tabpy.server.handlers.endpoints_handler import EndpointsHandler
from tabpy.server.handlers.evaluation_plane_handler import EvaluationPlaneHandler
from tabpy.server.handlers.query_plane_handler import QueryPlaneHandler
from tabpy.server.handlers.service_info_handler import ServiceInfoHandler
from tabpy.server.handlers.status_handler import StatusHandler
from tabpy.server.handlers.upload_destination_handler import (
UploadDestinationHandler,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import json
import logging
import tornado.web
from tabpy.tabpy_server.app.SettingsParameters import SettingsParameters
from tabpy.tabpy_server.handlers.util import hash_password
from tabpy.server.app.SettingsParameters import SettingsParameters
from tabpy.server.handlers.util import hash_password
import uuid


Expand Down Expand Up @@ -38,15 +38,8 @@ def set_request(self, request: tornado.httputil.HTTPServerRequest):
self.method = request.method
self.url = request.full_url()

if "TabPy-Client" in request.headers:
self.client = request.headers["TabPy-Client"]
else:
self.client = None

if "TabPy-User" in request.headers:
self.tableau_username = request.headers["TabPy-User"]
else:
self.tableau_username = None
self.client = request.headers.get("TabPy-Client", None)
self.tableau_username = request.headers.get("TabPy-User", None)

def set_tabpy_username(self, tabpy_username: str):
self.tabpy_username = tabpy_username
Expand Down
Loading

0 comments on commit c43d9b1

Please sign in to comment.