Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: use openttd-helpers library for common functions #35

Merged
merged 1 commit into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 6 additions & 17 deletions bananas_server/__main__.py
Expand Up @@ -4,14 +4,12 @@

from aiohttp import web
from aiohttp.web_log import AccessLogger
from openttd_helpers import click_helper
from openttd_helpers.logging_helper import click_logging
from openttd_helpers.sentry_helper import click_sentry

from . import web_routes
from .application.bananas_server import Application
from .helpers.click import (
click_additional_options,
import_module,
)
from .helpers.sentry import click_sentry
from .index.github import click_index_github
from .index.local import click_index_local
from .storage.local import click_storage_local
Expand All @@ -21,8 +19,6 @@

log = logging.getLogger(__name__)

CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}


class ErrorOnlyAccessLogger(AccessLogger):
def log(self, request, response, time):
Expand All @@ -46,14 +42,7 @@ async def run_server(application, bind, port):
return server


@click_additional_options
def click_logging():
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO
)


@click.command(context_settings=CONTEXT_SETTINGS)
@click_helper.command()
@click_logging # Should always be on top, as it initializes the logging
@click_sentry
@click.option(
Expand All @@ -65,15 +54,15 @@ def click_logging():
"--storage",
type=click.Choice(["local", "s3"], case_sensitive=False),
required=True,
callback=import_module("bananas_server.storage", "Storage"),
callback=click_helper.import_module("bananas_server.storage", "Storage"),
)
@click_storage_local
@click_storage_s3
@click.option(
"--index",
type=click.Choice(["local", "github"], case_sensitive=False),
required=True,
callback=import_module("bananas_server.index", "Index"),
callback=click_helper.import_module("bananas_server.index", "Index"),
)
@click_index_local
@click_index_github
Expand Down
41 changes: 0 additions & 41 deletions bananas_server/helpers/click.py

This file was deleted.

30 changes: 0 additions & 30 deletions bananas_server/helpers/sentry.py

This file was deleted.

5 changes: 3 additions & 2 deletions bananas_server/index/github.py
Expand Up @@ -5,8 +5,9 @@
import tempfile
import os

from openttd_helpers import click_helper

from .local import Index as LocalIndex
from ..helpers.click import click_additional_options

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,7 +86,7 @@ def reload(self, application):
return super().reload(application)


@click_additional_options
@click_helper.extend
@click.option(
"--index-github-url",
help="Repository URL on GitHub. (index=github only)",
Expand Down
4 changes: 2 additions & 2 deletions bananas_server/index/local.py
Expand Up @@ -4,9 +4,9 @@
import yaml

from collections import defaultdict
from openttd_helpers import click_helper

from .schema import ContentEntry as ContentEntryTest
from ..helpers.click import click_additional_options
from ..helpers.content_type import get_content_type_from_name
from ..helpers.content_type import get_folder_name_from_content_type
from ..openttd.protocol.enums import ContentType
Expand Down Expand Up @@ -294,7 +294,7 @@ def reload(self, md5sum_mapping):
)


@click_additional_options
@click_helper.extend
@click.option(
"--index-local-folder",
help="Folder to use for index storage. (index=local only)",
Expand Down
5 changes: 3 additions & 2 deletions bananas_server/openttd/tcp_content.py
Expand Up @@ -2,6 +2,8 @@
import click
import logging

from openttd_helpers import click_helper

from .protocol.exceptions import (
PacketInvalid,
SocketClosed,
Expand All @@ -10,7 +12,6 @@
from .protocol.write import SEND_MTU
from .receive import OpenTTDProtocolReceive
from .send import OpenTTDProtocolSend
from ..helpers.click import click_additional_options

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,7 +135,7 @@ async def send_packet(self, data):
self.transport.write(data)


@click_additional_options
@click_helper.extend
@click.option(
"--proxy-protocol",
help="Enable Proxy Protocol (v1), and expect all incoming package to have this header.",
Expand Down
5 changes: 3 additions & 2 deletions bananas_server/storage/local.py
@@ -1,7 +1,8 @@
import click
import os

from ..helpers.click import click_additional_options
from openttd_helpers import click_helper

from ..helpers.content_type import get_folder_name_from_content_type

_folder = None
Expand Down Expand Up @@ -60,7 +61,7 @@ def get_stream(self, content_entry):
return Stream(filename, content_entry.filesize)


@click_additional_options
@click_helper.extend
@click.option(
"--storage-local-folder",
help="Folder to use for storage. (storage=local only)",
Expand Down
4 changes: 2 additions & 2 deletions bananas_server/storage/s3.py
@@ -1,10 +1,10 @@
import boto3
import click

from openttd_helpers import click_helper
from urllib3.exceptions import ProtocolError

from .exceptions import StreamReadError
from ..helpers.click import click_additional_options
from ..helpers.content_type import get_folder_name_from_content_type

_bucket_name = None
Expand Down Expand Up @@ -115,7 +115,7 @@ def get_stream(self, content_entry):
return Stream(response["Body"], response["ContentLength"])


@click_additional_options
@click_helper.extend
@click.option(
"--storage-s3-bucket",
help="Name of the bucket to upload the files. (storage=s3 only)",
Expand Down
4 changes: 2 additions & 2 deletions bananas_server/web_routes.py
Expand Up @@ -2,8 +2,8 @@
import logging

from aiohttp import web
from openttd_helpers import click_helper

from .helpers.click import click_additional_options
from .helpers.content_type import get_folder_name_from_content_type
from .helpers.safe_filename import safe_filename

Expand Down Expand Up @@ -79,7 +79,7 @@ async def fallback(request):
return web.HTTPNotFound()


@click_additional_options
@click_helper.extend
@click.option(
"--reload-secret",
help="Secret to allow an index reload. Always use this via an environment variable!",
Expand Down
1 change: 1 addition & 0 deletions requirements.base
Expand Up @@ -4,5 +4,6 @@ click
gitpython
marshmallow
marshmallow-enum
openttd-helpers
PyYAML
sentry-sdk
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -14,6 +14,7 @@ jmespath==0.10.0
marshmallow==3.8.0
marshmallow-enum==1.5.1
multidict==4.7.6
openttd-helpers==1.0.0
python-dateutil==2.8.1
PyYAML==5.3.1
s3transfer==0.3.3
Expand Down