Skip to content

Commit

Permalink
Introduce one-line helper for script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed Feb 20, 2021
1 parent d216b81 commit c8b4b0f
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 57 deletions.
7 changes: 7 additions & 0 deletions scripts/_util.py
Expand Up @@ -9,6 +9,7 @@
"""

from contextlib import contextmanager
from typing import Callable

from byceps.application import create_app

Expand All @@ -21,3 +22,9 @@ def app_context():
app = create_app()
with app.app_context():
yield app


def call_with_app_context(func: Callable) -> None:
"""Call a callable inside of an application context."""
with app_context():
func()
5 changes: 2 additions & 3 deletions scripts/add_archived_attendance.py
Expand Up @@ -11,7 +11,7 @@
from byceps.services.ticketing import attendance_service
from byceps.services.user import service as user_service

from _util import app_context
from _util import call_with_app_context
from _validators import validate_party, validate_user_id


Expand All @@ -31,5 +31,4 @@ def execute(user, party):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/announce_orga_birthdays.py
Expand Up @@ -15,7 +15,7 @@
from byceps.services.webhooks import service as webhook_service
from byceps.services.webhooks.transfer.models import OutgoingWebhook, WebhookID

from _util import app_context
from _util import call_with_app_context


def validate_webhook_id(ctx, param, webhook_id: WebhookID) -> OutgoingWebhook:
Expand All @@ -40,5 +40,4 @@ def execute(webhook: OutgoingWebhook):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/clean_up_after_deleted_users.py
Expand Up @@ -39,7 +39,7 @@
)
from byceps.typing import UserID

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand Down Expand Up @@ -171,5 +171,4 @@ def _execute_delete_for_users_query(model, user_ids: Set[UserID]) -> int:


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/copy_snippets.py
Expand Up @@ -11,7 +11,7 @@
from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope, SnippetType

from _util import app_context
from _util import call_with_app_context
from _validators import validate_site


Expand Down Expand Up @@ -91,5 +91,4 @@ def scope_as_string(scope):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/create_database_tables.py
Expand Up @@ -12,7 +12,7 @@

from byceps.database import db

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand All @@ -25,5 +25,4 @@ def execute():


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/create_initial_admin_user.py
Expand Up @@ -12,7 +12,7 @@
from byceps.services.user import command_service as user_command_service
from byceps.services.user import creation_service as user_creation_service

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand Down Expand Up @@ -56,5 +56,4 @@ def _assign_roles_to_user(roles, user_id):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/create_seating_area.py
Expand Up @@ -10,7 +10,7 @@

from byceps.services.seating import area_service

from _util import app_context
from _util import call_with_app_context
from _validators import validate_party


Expand All @@ -24,5 +24,4 @@ def execute(party, slug, title):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/create_terms_version.py
Expand Up @@ -17,7 +17,7 @@
from byceps.services.terms.transfer.models import DocumentID
from byceps.services.terms import version_service as terms_version_service

from _util import app_context
from _util import call_with_app_context
from _validators import validate_brand


Expand Down Expand Up @@ -71,5 +71,4 @@ def _create_consent_subject(brand, title, consent_subject_name_suffix):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/delete_old_user_login_events.py
Expand Up @@ -12,7 +12,7 @@

from byceps.services.user import event_service as user_event_service

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand All @@ -32,5 +32,4 @@ def execute(minimum_age_in_days):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/export_permissions_and_roles.py
Expand Up @@ -10,7 +10,7 @@

from byceps.services.authorization import impex_service

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand All @@ -19,5 +19,4 @@ def execute():


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/export_ticket_user_email_addresses.py
Expand Up @@ -14,7 +14,7 @@
from byceps.services.user import service as user_service
from byceps.typing import PartyID

from _util import app_context
from _util import call_with_app_context
from _validators import validate_party


Expand All @@ -40,5 +40,4 @@ def _get_email_addresses(party_id: PartyID) -> Iterator[str]:


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/find_logins_for_ipaddress.py
Expand Up @@ -15,7 +15,7 @@
from byceps.services.user.transfer.models import User
from byceps.typing import PartyID, UserID

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand Down Expand Up @@ -44,5 +44,4 @@ def get_users_by_id(events: List[UserEvent]) -> Dict[UserID, User]:


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/generate_sql_to_delete_user.py
Expand Up @@ -18,7 +18,7 @@
from byceps.services.user import service as user_service
from byceps.typing import UserID

from _util import app_context
from _util import call_with_app_context
from _validators import validate_user_id_format


Expand Down Expand Up @@ -70,5 +70,4 @@ def generate_delete_statements_for_user(user_id: UserID) -> Iterator[str]:


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/grant_board_access.py
Expand Up @@ -11,7 +11,7 @@
from byceps.services.board import access_control_service, board_service
from byceps.services.board.transfer.models import Board

from _util import app_context
from _util import call_with_app_context
from _validators import validate_user_screen_name


Expand Down Expand Up @@ -48,5 +48,4 @@ def execute(board, user):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/import_permissions_and_roles.py
Expand Up @@ -10,7 +10,7 @@

from byceps.services.authorization import impex_service

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand All @@ -24,5 +24,4 @@ def execute(data_file):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/occupy_seat_group.py
Expand Up @@ -11,7 +11,7 @@
from byceps.services.seating import seat_group_service
from byceps.services.ticketing import ticket_bundle_service

from _util import app_context
from _util import call_with_app_context


def get_seat_group(ctx, param, seat_group_id):
Expand Down Expand Up @@ -44,5 +44,4 @@ def execute(seat_group, ticket_bundle):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/remove_user_sessions.py
Expand Up @@ -16,7 +16,7 @@

from byceps.services.authentication.session import service as session_service

from _util import app_context
from _util import call_with_app_context


@click.command()
Expand All @@ -28,5 +28,4 @@ def execute():


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/search_snippets.py
Expand Up @@ -11,7 +11,7 @@
from byceps.services.snippet import service as snippet_service
from byceps.services.snippet.transfer.models import Scope

from _util import app_context
from _util import call_with_app_context
from _validators import validate_site


Expand Down Expand Up @@ -72,5 +72,4 @@ def format_scope(scope):


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)
5 changes: 2 additions & 3 deletions scripts/set_current_terms_version.py
Expand Up @@ -16,7 +16,7 @@
from byceps.services.terms.transfer.models import DocumentID, VersionID
from byceps.services.terms import document_service, version_service

from _util import app_context
from _util import call_with_app_context


def validate_document_id(ctx, param, value) -> DocumentID:
Expand Down Expand Up @@ -96,5 +96,4 @@ def _get_version_ids_latest_first(


if __name__ == '__main__':
with app_context():
execute()
call_with_app_context(execute)

0 comments on commit c8b4b0f

Please sign in to comment.