Skip to content

Commit

Permalink
Merge pull request #4 from galbwe/code-formatting
Browse files Browse the repository at this point in the history
Add black code style check to ci pipeline
  • Loading branch information
galbwe authored Feb 6, 2022
2 parents bcd10a5 + 16ddccd commit b1392ed
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max_line_length = 120
10 changes: 9 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ jobs:
python -m pip install --upgrade pip
python -m pip install -e .
python -m pip install pytest
python -m pip install black
python -m pip install flake8
- name: Check code style
run: |
black ./flask_opensearch/ ./tests ./scripts -l 120 --check
- name: Lint
run: |
flake8 ./flask_opensearch/ ./tests ./scripts
- name: Test with pytest
env:
OPENSEARCH_HOST: localhost
Expand All @@ -43,5 +51,5 @@ jobs:
OPENSEARCH_PORT: 9200
PYTHONPATH: ./flask_opensearch
run: |
python ./scripts/wait_for_opensearch.py --skip-prompts
python ./scripts/wait_for_opensearch.py --skip-prompts --number-tries=10
pytest -vvv
2 changes: 1 addition & 1 deletion flask_opensearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .flask_opensearch import FlaskOpenSearch


__all__ = ['FlaskOpenSearch']
__all__ = ["FlaskOpenSearch"]
19 changes: 10 additions & 9 deletions flask_opensearch/flask_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# TODO: switch to current_app instead of self.app to support multiple applications running in the same process
# TODO: instantiate AWSRequestsAuth if OPENSEARCH_HTTP_AUTH is not set


class FlaskOpenSearch:
def __init__(self, app=None, **kwargs):
self.app = app
Expand All @@ -21,9 +22,9 @@ def __init__(self, app=None, **kwargs):
self.init_app(app, **kwargs)

def init_app(self, app, **kwargs):
host = app.config.get('OPENSEARCH_HOST', 'localhost')
host = app.config.get("OPENSEARCH_HOST", "localhost")
port = app.config.setdefault("OPENSEARCH_PORT", 9200)
app.config.setdefault("OPENSEARCH_HOST_OBJECTS", [{'host': host, 'port': port}])
app.config.setdefault("OPENSEARCH_HOST_OBJECTS", [{"host": host, "port": port}])
user = app.config.get("OPENSEARCH_USERNAME", "admin")
password = app.config.get("OPENSEARCH_PASSWORD", "admin")
http_auth = (user, password)
Expand All @@ -34,23 +35,23 @@ def init_app(self, app, **kwargs):

# Use the newstyle teardown_appcontext if it's available,
# otherwise fall back to the request context
if hasattr(app, 'teardown_appcontext'):
if hasattr(app, "teardown_appcontext"):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)

def __getattr__(self, item):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'opensearch'):
hosts = ctx.app.config.get('OPENSEARCH_HOST_OBJECTS')
ctx.opensearch = OpenSearch(hosts=hosts,
http_auth=ctx.app.config.get('OPENSEARCH_HTTP_AUTH'),
**self.opensearch_options)
if not hasattr(ctx, "opensearch"):
hosts = ctx.app.config.get("OPENSEARCH_HOST_OBJECTS")
ctx.opensearch = OpenSearch(
hosts=hosts, http_auth=ctx.app.config.get("OPENSEARCH_HTTP_AUTH"), **self.opensearch_options
)

return getattr(ctx.opensearch, item)

def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'opensearch'):
if hasattr(ctx, "opensearch"):
ctx.opensearch = None
2 changes: 2 additions & 0 deletions scripts/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
black ./flask_opensearch/ ./tests ./scripts -l 120
2 changes: 2 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
flake8 ./flask_opensearch ./scripts ./tests
53 changes: 42 additions & 11 deletions scripts/wait_for_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,42 @@


@click.command()
@click.option("-h", "--host", default="localhost", type=str, help="The host name of the openesearch cluster. Can be set with OPENSEARCH_HOST environment variable.")
@click.option("-p", "--port", default=9200, type=int, help="The port opensearch accepts connections on. Can be set with OPENSEARCH_PORT environment variable.")
@click.option("-u", "--user", default="admin", type=str, help="The opensearch user to authenticate with. Can be set with OPENSEARCH_USER environment variable.")
@click.option("-w", "--password", default="", type=str, help="The opensearch password to authenticate with. Can be set with OPENSEARCH_PASSWORD environment variable.")
@click.option("-n", "--number-tries", "tries", default=5, type=int, help="The maximum number of times to retry connecting to the opensearch cluster.")
@click.option(
"-h",
"--host",
default="localhost",
type=str,
help="The host name of the openesearch cluster. Can be set with OPENSEARCH_HOST environment variable.",
)
@click.option(
"-p",
"--port",
default=9200,
type=int,
help="The port opensearch accepts connections on. Can be set with OPENSEARCH_PORT environment variable.",
)
@click.option(
"-u",
"--user",
default="admin",
type=str,
help="The opensearch user to authenticate with. Can be set with OPENSEARCH_USER environment variable.",
)
@click.option(
"-w",
"--password",
default="",
type=str,
help="The opensearch password to authenticate with. Can be set with OPENSEARCH_PASSWORD environment variable.",
)
@click.option(
"-n",
"--number-tries",
"tries",
default=5,
type=int,
help="The maximum number of times to retry connecting to the opensearch cluster.",
)
@click.option("-s", "--seconds", default=5, type=int, help="The time in seconds between connection attempts.")
@click.option("-y", "--skip-prompts", "skip_prompts", is_flag=True, help="Skip password prompts.")
def wait_for_opensearch(host, port, user, password, tries, seconds, skip_prompts):
Expand All @@ -23,12 +54,12 @@ def wait_for_opensearch(host, port, user, password, tries, seconds, skip_prompts

auth = (user, password)
opensearch = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_auth = auth,
use_ssl = True,
verify_certs = False,
ssl_assert_hostname = False,
ssl_show_warn = False,
hosts=[{"host": host, "port": port}],
http_auth=auth,
use_ssl=True,
verify_certs=False,
ssl_assert_hostname=False,
ssl_show_warn=False,
)

while t < tries and not ready:
Expand Down
32 changes: 17 additions & 15 deletions tests/test_flask_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def test_local_docker_opensearch_connection():
# check that a connection can be made to the local cluster running in docker compose
host = os.environ.get("OPENSEARCH_HOST", "localhost")
port = os.environ.get("OPENSEARCH_PORT", 9200)
auth = ('admin', 'admin')
auth = ("admin", "admin")
opensearch = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_auth = auth,
use_ssl = True,
verify_certs = False,
ssl_assert_hostname = False,
ssl_show_warn = False,
hosts=[{"host": host, "port": port}],
http_auth=auth,
use_ssl=True,
verify_certs=False,
ssl_assert_hostname=False,
ssl_show_warn=False,
)
assert opensearch.ping() is True

Expand Down Expand Up @@ -73,12 +73,12 @@ def test_init_app_overwrites_provided_opensearch_options_only(self, create_app):
use_ssl=False,
verify_certs=False,
)
assert opensearch.opensearch_options['use_ssl'] is False
assert opensearch.opensearch_options['verify_certs'] is False
assert opensearch.opensearch_options["use_ssl"] is False
assert opensearch.opensearch_options["verify_certs"] is False
with pytest.raises(KeyError):
opensearch.opensearch_options['ssl_assert_hostname']
opensearch.opensearch_options["ssl_assert_hostname"]
with pytest.raises(KeyError):
opensearch.opensearch_options['ssl_show_warn']
opensearch.opensearch_options["ssl_show_warn"]
with app.app_context():
opensearch.init_app(
app,
Expand All @@ -87,10 +87,12 @@ def test_init_app_overwrites_provided_opensearch_options_only(self, create_app):
ssl_assert_hostname=False,
ssl_show_warn=False,
)
assert opensearch.opensearch_options['use_ssl'] is True
assert opensearch.opensearch_options['verify_certs'] is False
assert opensearch.opensearch_options['ssl_assert_hostname'] is False
assert opensearch.opensearch_options['ssl_show_warn'] is False
assert opensearch.opensearch_options["use_ssl"] is True
assert opensearch.opensearch_options["verify_certs"] is False
assert opensearch.opensearch_options["ssl_assert_hostname"] is False
assert opensearch.opensearch_options["ssl_show_warn"] is False

assert opensearch.ping() is True


# TODO: test against an AWS opensearch instance

0 comments on commit b1392ed

Please sign in to comment.