2021-08-04 18:53:58 +02:00
|
|
|
import functools
|
|
|
|
|
|
|
|
import click
|
2022-10-14 22:11:27 +02:00
|
|
|
import uvicorn
|
2021-12-03 00:06:51 +01:00
|
|
|
from click import pass_context
|
2021-08-04 18:53:58 +02:00
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.cli import safe_execution
|
2021-10-25 13:43:38 +02:00
|
|
|
from mobilizon_reshare.cli.commands.format.format import format_event
|
2022-02-14 21:10:27 +01:00
|
|
|
from mobilizon_reshare.cli.commands.list.list_event import list_events
|
|
|
|
from mobilizon_reshare.cli.commands.list.list_publication import list_publications
|
2022-03-22 21:16:34 +01:00
|
|
|
from mobilizon_reshare.cli.commands.publish.main import publish_command as publish_main
|
2022-10-14 22:11:27 +02:00
|
|
|
from mobilizon_reshare.cli.commands.pull.main import pull_command as pull_main
|
|
|
|
from mobilizon_reshare.cli.commands.recap.main import recap_command as recap_main
|
2022-03-02 08:59:57 +01:00
|
|
|
from mobilizon_reshare.cli.commands.retry.main import (
|
|
|
|
retry_event_command,
|
|
|
|
retry_publication_command,
|
|
|
|
)
|
2022-10-14 22:11:27 +02:00
|
|
|
from mobilizon_reshare.cli.commands.start.main import start_command as start_main
|
|
|
|
from mobilizon_reshare.config.command import CommandConfig
|
|
|
|
from mobilizon_reshare.config.config import current_version, get_settings
|
|
|
|
from mobilizon_reshare.config.publishers import publisher_names
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses.event import _EventPublicationStatus
|
2021-12-01 01:08:37 +01:00
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
2022-05-17 23:14:52 +02:00
|
|
|
from mobilizon_reshare.publishers import get_active_publishers
|
|
|
|
|
|
|
|
|
|
|
|
def test_settings(ctx, param, value):
|
|
|
|
if not value or ctx.resilient_parsing:
|
|
|
|
return
|
|
|
|
get_settings()
|
|
|
|
click.echo("OK!")
|
|
|
|
ctx.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def print_version(ctx, param, value):
|
|
|
|
if not value or ctx.resilient_parsing:
|
|
|
|
return
|
|
|
|
click.echo(current_version())
|
|
|
|
ctx.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def print_platforms(ctx, param, value):
|
|
|
|
if not value or ctx.resilient_parsing:
|
|
|
|
return
|
|
|
|
for platform in get_active_publishers():
|
|
|
|
click.echo(platform)
|
|
|
|
ctx.exit()
|
|
|
|
|
2021-08-04 18:53:58 +02:00
|
|
|
|
2021-10-25 13:43:38 +02:00
|
|
|
status_name_to_enum = {
|
2021-12-01 01:08:37 +01:00
|
|
|
"event": {
|
2022-12-11 14:15:04 +01:00
|
|
|
"waiting": _EventPublicationStatus.WAITING,
|
|
|
|
"completed": _EventPublicationStatus.COMPLETED,
|
|
|
|
"failed": _EventPublicationStatus.FAILED,
|
|
|
|
"partial": _EventPublicationStatus.PARTIAL,
|
2021-12-01 01:08:37 +01:00
|
|
|
"all": None,
|
|
|
|
},
|
|
|
|
"publication": {
|
|
|
|
"completed": PublicationStatus.COMPLETED,
|
|
|
|
"failed": PublicationStatus.FAILED,
|
|
|
|
"all": None,
|
|
|
|
},
|
2021-10-25 13:43:38 +02:00
|
|
|
}
|
2021-08-14 18:23:55 +02:00
|
|
|
from_date_option = click.option(
|
2021-12-03 00:04:36 +01:00
|
|
|
"-b",
|
2021-08-14 18:23:55 +02:00
|
|
|
"--begin",
|
|
|
|
type=click.DateTime(),
|
|
|
|
expose_value=True,
|
2021-12-04 01:20:34 +01:00
|
|
|
help="Include only events that begin after this datetime.",
|
2021-08-14 18:23:55 +02:00
|
|
|
)
|
|
|
|
to_date_option = click.option(
|
2021-12-03 00:04:36 +01:00
|
|
|
"-e",
|
2021-08-14 18:23:55 +02:00
|
|
|
"--end",
|
|
|
|
type=click.DateTime(),
|
|
|
|
expose_value=True,
|
2022-05-17 23:14:52 +02:00
|
|
|
help="Include only events that end before this datetime.",
|
2021-08-14 18:23:55 +02:00
|
|
|
)
|
2022-05-17 23:14:52 +02:00
|
|
|
event_status_argument = click.argument(
|
2022-02-14 21:10:27 +01:00
|
|
|
"status",
|
2021-12-03 00:06:51 +01:00
|
|
|
type=click.Choice(list(status_name_to_enum["event"].keys())),
|
2021-12-01 01:08:37 +01:00
|
|
|
default="all",
|
|
|
|
expose_value=True,
|
2021-12-03 00:06:51 +01:00
|
|
|
)
|
2022-05-17 23:14:52 +02:00
|
|
|
publication_status_argument = click.argument(
|
2022-02-14 21:10:27 +01:00
|
|
|
"status",
|
2021-12-03 00:06:51 +01:00
|
|
|
type=click.Choice(list(status_name_to_enum["publication"].keys())),
|
|
|
|
default="all",
|
|
|
|
expose_value=True,
|
2021-12-01 01:08:37 +01:00
|
|
|
)
|
2022-05-17 23:14:52 +02:00
|
|
|
event_uuid_option = click.option(
|
|
|
|
"-E",
|
|
|
|
"--event",
|
|
|
|
type=click.UUID,
|
|
|
|
expose_value=True,
|
|
|
|
help="Publish the given event.",
|
|
|
|
)
|
|
|
|
publication_uuid_option = click.option(
|
|
|
|
"-P",
|
|
|
|
"--publication",
|
|
|
|
type=click.UUID,
|
|
|
|
expose_value=True,
|
|
|
|
help="Publish the given publication.",
|
|
|
|
)
|
|
|
|
platform_name_option = click.option(
|
|
|
|
"-p",
|
|
|
|
"--platform",
|
|
|
|
type=str,
|
|
|
|
expose_value=True,
|
|
|
|
help="Publish to the given platform. This makes sense only for events.",
|
|
|
|
)
|
|
|
|
list_supported_option = click.option(
|
|
|
|
"--list-platforms",
|
|
|
|
is_flag=True,
|
|
|
|
callback=print_platforms,
|
|
|
|
expose_value=False,
|
|
|
|
is_eager=True,
|
|
|
|
help="Show all active platforms.",
|
|
|
|
)
|
|
|
|
test_configuration = click.option(
|
|
|
|
"-t",
|
|
|
|
"--test-configuration",
|
|
|
|
is_flag=True,
|
|
|
|
callback=test_settings,
|
|
|
|
expose_value=False,
|
|
|
|
is_eager=True,
|
|
|
|
help="Validate the current configuration.",
|
|
|
|
)
|
2021-11-30 00:28:55 +01:00
|
|
|
|
|
|
|
|
2021-08-04 18:53:58 +02:00
|
|
|
@click.group()
|
2022-05-17 23:14:52 +02:00
|
|
|
@test_configuration
|
|
|
|
@list_supported_option
|
2021-11-30 00:28:55 +01:00
|
|
|
@click.option(
|
2022-05-17 23:14:52 +02:00
|
|
|
"--version",
|
|
|
|
is_flag=True,
|
|
|
|
callback=print_version,
|
|
|
|
expose_value=False,
|
|
|
|
is_eager=True,
|
|
|
|
help="Show the current version.",
|
2021-11-30 00:28:55 +01:00
|
|
|
)
|
2021-12-03 00:06:51 +01:00
|
|
|
@pass_context
|
2021-12-14 20:30:08 +01:00
|
|
|
def mobilizon_reshare(obj):
|
2021-12-08 13:32:45 +01:00
|
|
|
pass
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-03-22 21:16:34 +01:00
|
|
|
@mobilizon_reshare.command(
|
|
|
|
help="Synchronize and publish events. It is equivalent to running consecutively pull and then publish."
|
|
|
|
)
|
2022-07-12 07:39:56 +02:00
|
|
|
@click.option(
|
|
|
|
"--dry-run",
|
|
|
|
is_flag=True,
|
|
|
|
help="Prevents data to be published to platforms. WARNING: it will download and write new events to the database",
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
def start(dry_run):
|
|
|
|
|
|
|
|
safe_execution(start_main, CommandConfig(dry_run=dry_run))
|
2021-10-16 01:25:45 +02:00
|
|
|
|
|
|
|
|
2021-12-04 01:20:34 +01:00
|
|
|
@mobilizon_reshare.command(help="Publish a recap of already published events.")
|
2022-07-12 07:39:56 +02:00
|
|
|
@click.option(
|
|
|
|
"--dry-run",
|
|
|
|
"dry_run",
|
|
|
|
is_flag=True,
|
|
|
|
help="Prevents data to be published to platforms. WARNING: it will download and write new events to the database",
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
def recap(dry_run):
|
|
|
|
safe_execution(recap_main, CommandConfig(dry_run=dry_run))
|
2022-03-22 21:16:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
@mobilizon_reshare.command(
|
2022-05-17 23:14:52 +02:00
|
|
|
help="Fetch the latest events from Mobilizon, store them if they are unknown, "
|
|
|
|
"update them if they are known and changed."
|
2022-03-22 21:16:34 +01:00
|
|
|
)
|
|
|
|
def pull():
|
2022-07-12 07:39:56 +02:00
|
|
|
safe_execution(pull_main,)
|
2022-03-22 21:16:34 +01:00
|
|
|
|
|
|
|
|
2022-05-17 23:14:52 +02:00
|
|
|
@mobilizon_reshare.command(
|
|
|
|
help="Select an event with the current configured strategy"
|
|
|
|
" and publish it to all active platforms."
|
|
|
|
)
|
|
|
|
@event_uuid_option
|
|
|
|
@publication_uuid_option
|
|
|
|
@platform_name_option
|
2022-03-22 21:16:34 +01:00
|
|
|
def publish():
|
2022-07-12 07:39:56 +02:00
|
|
|
safe_execution(publish_main,)
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-02-14 21:10:27 +01:00
|
|
|
@mobilizon_reshare.group(help="Operations that pertain to events")
|
|
|
|
def event():
|
|
|
|
pass
|
2021-12-03 00:06:51 +01:00
|
|
|
|
|
|
|
|
2022-02-14 21:10:27 +01:00
|
|
|
@mobilizon_reshare.group(help="Operations that pertain to publications")
|
|
|
|
def publication():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@event.command(help="Query for events in the database.", name="list")
|
2022-05-17 23:14:52 +02:00
|
|
|
@event_status_argument
|
2022-02-14 21:10:27 +01:00
|
|
|
@from_date_option
|
|
|
|
@to_date_option
|
|
|
|
def event_list(status, begin, end):
|
|
|
|
|
2021-12-03 00:06:51 +01:00
|
|
|
safe_execution(
|
|
|
|
functools.partial(
|
2022-07-12 07:39:56 +02:00
|
|
|
list_events, status_name_to_enum["event"][status], frm=begin, to=end,
|
2021-12-03 00:06:51 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-10-25 13:43:38 +02:00
|
|
|
|
2022-02-14 21:10:27 +01:00
|
|
|
@publication.command(help="Query for publications in the database.", name="list")
|
2022-05-17 23:14:52 +02:00
|
|
|
@publication_status_argument
|
2022-02-14 21:10:27 +01:00
|
|
|
@from_date_option
|
|
|
|
@to_date_option
|
|
|
|
def publication_list(status, begin, end):
|
2021-08-04 18:53:58 +02:00
|
|
|
safe_execution(
|
2021-10-25 13:43:38 +02:00
|
|
|
functools.partial(
|
2022-02-14 21:10:27 +01:00
|
|
|
list_publications,
|
2021-12-03 00:06:51 +01:00
|
|
|
status_name_to_enum["publication"][status],
|
2022-02-14 21:10:27 +01:00
|
|
|
frm=begin,
|
|
|
|
to=end,
|
2021-10-25 13:43:38 +02:00
|
|
|
),
|
2021-08-04 18:53:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-14 21:10:27 +01:00
|
|
|
@event.command(
|
2021-12-04 01:20:34 +01:00
|
|
|
help="Format and print event with EVENT-ID using the publisher's format named "
|
|
|
|
"PUBLISHER."
|
2021-10-25 13:43:38 +02:00
|
|
|
)
|
|
|
|
@click.argument("event-id", type=click.UUID)
|
|
|
|
@click.argument("publisher", type=click.Choice(publisher_names))
|
2022-01-08 00:54:27 +01:00
|
|
|
def format(
|
2022-07-12 07:39:56 +02:00
|
|
|
event_id, publisher,
|
2022-01-08 00:54:27 +01:00
|
|
|
):
|
2022-07-12 07:39:56 +02:00
|
|
|
safe_execution(functools.partial(format_event, event_id, publisher),)
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-02-14 21:10:27 +01:00
|
|
|
@event.command(name="retry", help="Retries all the failed publications")
|
|
|
|
@click.argument("event-id", type=click.UUID)
|
|
|
|
def event_retry(event_id):
|
2022-07-12 07:39:56 +02:00
|
|
|
safe_execution(functools.partial(retry_event_command, event_id),)
|
2022-02-14 21:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
@publication.command(name="retry", help="Retries a specific publication")
|
|
|
|
@click.argument("publication-id", type=click.UUID)
|
|
|
|
def publication_retry(publication_id):
|
2022-07-12 07:39:56 +02:00
|
|
|
safe_execution(functools.partial(retry_publication_command, publication_id),)
|
2022-02-14 21:10:27 +01:00
|
|
|
|
|
|
|
|
2022-10-14 22:11:27 +02:00
|
|
|
@mobilizon_reshare.command("web")
|
|
|
|
def web():
|
|
|
|
uvicorn.run(
|
|
|
|
"mobilizon_reshare.web.backend.main:app", host="0.0.0.0", port=8000, reload=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-04 18:53:58 +02:00
|
|
|
if __name__ == "__main__":
|
2021-12-03 00:06:51 +01:00
|
|
|
mobilizon_reshare(obj={})
|