Skip to content

Commit

Permalink
Use flask.Config.from_envvar, remove `byceps.util.system.get_config…
Browse files Browse the repository at this point in the history
…_filename_from_env[_or_exit]`

The application config filename is now optional. If it is not specified,
the environment variable `BYCEPS_CONFIG` is expected to be set.
  • Loading branch information
homeworkprod committed Feb 20, 2021
1 parent 1911903 commit 2c36ca1
Show file tree
Hide file tree
Showing 27 changed files with 35 additions and 145 deletions.
5 changes: 1 addition & 4 deletions app.py
Expand Up @@ -21,12 +21,9 @@
from byceps.services.user.dbmodels.detail import UserDetail
from byceps.services.user.dbmodels.user import User
from byceps.services.user.service import find_user_by_screen_name
from byceps.util.system import get_config_filename_from_env_or_exit


config_filename = get_config_filename_from_env_or_exit()

app = create_app(config_filename)
app = create_app()


if app.env == 'development':
Expand Down
7 changes: 5 additions & 2 deletions byceps/application.py
Expand Up @@ -28,15 +28,18 @@


def create_app(
config_filename: Union[Path, str],
*,
config_filename: Optional[Union[Path, str]] = None,
config_overrides: Optional[Dict[str, Any]] = None,
) -> Flask:
"""Create the actual Flask application."""
app = Flask('byceps')

app.config.from_object(config_defaults)
app.config.from_pyfile(str(config_filename))
if config_filename is not None:
app.config.from_pyfile(str(config_filename))
else:
app.config.from_envvar('BYCEPS_CONFIG')
if config_overrides is not None:
app.config.from_mapping(config_overrides)

Expand Down
29 changes: 0 additions & 29 deletions byceps/util/system.py
Expand Up @@ -7,14 +7,10 @@
"""

import os
import sys

from ..config import ConfigurationError


CONFIG_VAR_NAME = 'BYCEPS_CONFIG'


def get_env_value(name: str, error_message: str) -> str:
"""Return the value of the environment variable.
Expand All @@ -26,28 +22,3 @@ def get_env_value(name: str, error_message: str) -> str:
raise ConfigurationError(error_message)

return env


def get_config_filename_from_env() -> str:
"""Return the configuration filename set via environment variable.
Raise an exception if it isn't set.
"""
error_message = (
"No configuration file was specified via the "
f"'{CONFIG_VAR_NAME}' environment variable."
)

return get_env_value(CONFIG_VAR_NAME, error_message)


def get_config_filename_from_env_or_exit() -> str:
"""Return the configuration filename set via environment variable.
Exit if it isn't set.
"""
try:
return get_config_filename_from_env()
except ConfigurationError as e:
sys.stderr.write(f"{e}\n")
sys.exit(1)
4 changes: 2 additions & 2 deletions scripts/_util.py
Expand Up @@ -14,10 +14,10 @@


@contextmanager
def app_context(config_filename):
def app_context():
"""Provide a context in which the application is available with the
specified configuration.
"""
app = create_app(config_filename)
app = create_app()
with app.app_context():
yield app
4 changes: 1 addition & 3 deletions scripts/add_archived_attendance.py
Expand Up @@ -10,7 +10,6 @@

from byceps.services.ticketing import attendance_service
from byceps.services.user import service as user_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_party, validate_user_id
Expand All @@ -32,6 +31,5 @@ def execute(user, party):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/announce_orga_birthdays.py
Expand Up @@ -14,7 +14,6 @@
from byceps.services.orga import birthday_service
from byceps.services.webhooks import service as webhook_service
from byceps.services.webhooks.transfer.models import OutgoingWebhook, WebhookID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -41,6 +40,5 @@ def execute(webhook: OutgoingWebhook):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/clean_up_after_deleted_users.py
Expand Up @@ -38,7 +38,6 @@
Token as VerificationToken,
)
from byceps.typing import UserID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand Down Expand Up @@ -172,6 +171,5 @@ def _execute_delete_for_users_query(model, user_ids: Set[UserID]) -> int:


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/copy_snippets.py
Expand Up @@ -10,7 +10,6 @@

from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope, SnippetType
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_site
Expand Down Expand Up @@ -92,6 +91,5 @@ def scope_as_string(scope):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/create_database_tables.py
Expand Up @@ -11,7 +11,6 @@
import click

from byceps.database import db
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -26,6 +25,5 @@ def execute():


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/create_initial_admin_user.py
Expand Up @@ -11,7 +11,6 @@
from byceps.services.authorization import service as authorization_service
from byceps.services.user import command_service as user_command_service
from byceps.services.user import creation_service as user_creation_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand Down Expand Up @@ -57,6 +56,5 @@ def _assign_roles_to_user(roles, user_id):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/create_seating_area.py
Expand Up @@ -9,7 +9,6 @@
import click

from byceps.services.seating import area_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_party
Expand All @@ -25,6 +24,5 @@ def execute(party, slug, title):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/create_terms_version.py
Expand Up @@ -16,7 +16,6 @@
from byceps.services.terms import document_service as terms_document_service
from byceps.services.terms.transfer.models import DocumentID
from byceps.services.terms import version_service as terms_version_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_brand
Expand Down Expand Up @@ -72,6 +71,5 @@ def _create_consent_subject(brand, title, consent_subject_name_suffix):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/delete_old_user_login_events.py
Expand Up @@ -11,7 +11,6 @@
import click

from byceps.services.user import event_service as user_event_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -33,6 +32,5 @@ def execute(minimum_age_in_days):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/export_permissions_and_roles.py
Expand Up @@ -9,7 +9,6 @@
import click

from byceps.services.authorization import impex_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -20,6 +19,5 @@ def execute():


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/export_ticket_user_email_addresses.py
Expand Up @@ -13,7 +13,6 @@
from byceps.services.ticketing import ticket_service
from byceps.services.user import service as user_service
from byceps.typing import PartyID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_party
Expand Down Expand Up @@ -41,6 +40,5 @@ def _get_email_addresses(party_id: PartyID) -> Iterator[str]:


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/find_logins_for_ipaddress.py
Expand Up @@ -14,7 +14,6 @@
from byceps.services.user import service as user_service
from byceps.services.user.transfer.models import User
from byceps.typing import PartyID, UserID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand Down Expand Up @@ -45,6 +44,5 @@ def get_users_by_id(events: List[UserEvent]) -> Dict[UserID, User]:


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/generate_sql_to_delete_user.py
Expand Up @@ -17,7 +17,6 @@

from byceps.services.user import service as user_service
from byceps.typing import UserID
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_user_id_format
Expand Down Expand Up @@ -71,6 +70,5 @@ def generate_delete_statements_for_user(user_id: UserID) -> Iterator[str]:


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/grant_board_access.py
Expand Up @@ -10,7 +10,6 @@

from byceps.services.board import access_control_service, board_service
from byceps.services.board.transfer.models import Board
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_user_screen_name
Expand Down Expand Up @@ -49,6 +48,5 @@ def execute(board, user):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/import_permissions_and_roles.py
Expand Up @@ -9,7 +9,6 @@
import click

from byceps.services.authorization import impex_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -25,6 +24,5 @@ def execute(data_file):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/occupy_seat_group.py
Expand Up @@ -10,7 +10,6 @@

from byceps.services.seating import seat_group_service
from byceps.services.ticketing import ticket_bundle_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand Down Expand Up @@ -45,6 +44,5 @@ def execute(seat_group, ticket_bundle):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/remove_user_sessions.py
Expand Up @@ -15,7 +15,6 @@
import click

from byceps.services.authentication.session import service as session_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand All @@ -29,6 +28,5 @@ def execute():


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/search_snippets.py
Expand Up @@ -10,7 +10,6 @@

from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context
from _validators import validate_site
Expand Down Expand Up @@ -73,6 +72,5 @@ def format_scope(scope):


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()
4 changes: 1 addition & 3 deletions scripts/set_current_terms_version.py
Expand Up @@ -15,7 +15,6 @@
from byceps.services.terms.dbmodels.version import Version
from byceps.services.terms.transfer.models import DocumentID, VersionID
from byceps.services.terms import document_service, version_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context

Expand Down Expand Up @@ -97,6 +96,5 @@ def _get_version_ids_latest_first(


if __name__ == '__main__':
config_filename = get_config_filename_from_env_or_exit()
with app_context(config_filename):
with app_context():
execute()

0 comments on commit 2c36ca1

Please sign in to comment.