コード例 #1
0
def change_command(
    id_: str,
    queue_id: Tuple[str],
    group: Optional[str],
    locale: Optional[str],
    password: Optional[str],
) -> None:
    if not any([queue_id, group, locale, password]):
        return

    data: Dict[str, Any] = {}
    if password is not None:
        data["password"] = password

    with APIClient() as api_client:
        if queue_id:
            data["queues"] = [
                get_json(api_client.get(f"queues/{queue}"))["url"]
                for queue in queue_id
            ]
        if group is not None:
            data["groups"] = get_groups(api_client, group)
        if locale is not None:
            ui_settings = get_json(
                api_client.get(f"users/{id_}"))["ui_settings"]
            data["ui_settings"] = {**ui_settings, "locale": locale}

        api_client.patch(f"users/{id_}", data)
コード例 #2
0
def _get_organization(api: APIClient, organization_id: Optional[int] = None) -> dict:
    if organization_id is None:
        user_url = get_json(api.get("auth/user"))["url"]
        organziation_url = get_json(api.get_url(user_url))["organization"]
        res = api.get_url(organziation_url)
    else:
        res = api.get(f"organizations/{organization_id}")
    return get_json(res)
コード例 #3
0
ファイル: queue.py プロジェクト: tbots/elisctl
def change_command(
    ctx: click.Context,
    id_: int,
    name: Optional[str],
    schema_content: Optional[List[dict]],
    email_prefix: Optional[str],
    bounce_email: Optional[str],
    connector_id: Optional[int],
    webhook_id: Optional[Tuple[int, ...]],
    locale: Optional[str],
) -> None:

    if not any(
        [name, schema_content, email_prefix, bounce_email, connector_id, locale, webhook_id]
    ):
        return

    data: Dict[str, Any] = {}

    if name is not None:
        data["name"] = name

    if locale is not None:
        data["locale"] = locale

    with ELISClient(context=ctx.obj) as elis:
        if email_prefix or bounce_email:
            queue_dict = elis.get_queue(id_)
            if not queue_dict["inbox"]:
                inbox_dict = _create_inbox(elis, queue_dict, email_prefix, bounce_email, name)
            else:
                inbox_dict = _patch_inbox(elis, queue_dict, email_prefix, bounce_email)
            click.echo(
                f"{inbox_dict['id']}, {inbox_dict['email']}, {inbox_dict['bounce_email_to']}"
            )

        if connector_id is not None:
            data["connector"] = get_json(elis.get(f"connectors/{connector_id}"))["url"]

        if webhook_id:
            webhooks_urls = []
            for webhook in webhook_id:
                webhook_url = get_json(elis.get(f"webhooks/{webhook}"))["url"]
                webhooks_urls.append(webhook_url)
                data["webhooks"] = webhooks_urls

        if schema_content is not None:
            name = name or elis.get_queue(id_)["name"]
            schema_dict = elis.create_schema(f"{name} schema", schema_content)
            data["schema"] = schema_dict["url"]

        if data:
            elis.patch(f"queues/{id_}", data)
コード例 #4
0
ファイル: queue.py プロジェクト: qiq/elisctl
def create_command(
    name: str,
    schema_content_file: IO[bytes],
    email_prefix: Optional[str],
    bounce_email: Optional[str],
    workspace_id: Optional[int],
    connector_id: Optional[int],
    locale: Optional[str],
) -> None:
    schema_content = json.load(schema_content_file)
    if email_prefix is not None and bounce_email is None:
        raise click.ClickException(
            "Inbox cannot be created without specified bounce email.")

    with ELISClient() as elis:
        workspace_url = elis.get_workspace(workspace_id)["url"]
        connector_url = (get_json(
            elis.get(f"connectors/{connector_id}"))["url"]
                         if connector_id is not None else None)

        schema_dict = elis.create_schema(f"{name} schema", schema_content)
        queue_dict = elis.create_queue(name, workspace_url, schema_dict["url"],
                                       connector_url, locale)

        inbox_dict = {"email": "no email-prefix specified"}
        if email_prefix is not None:
            inbox_dict = elis.create_inbox(f"{name} inbox", email_prefix,
                                           bounce_email, queue_dict["url"])
    click.echo(f"{queue_dict['id']}, {inbox_dict['email']}")
コード例 #5
0
ファイル: queue.py プロジェクト: qiq/elisctl
def change_command(
    id_: int,
    name: Optional[str],
    schema_content_file: Optional[IO[bytes]],
    connector_id: Optional[int],
    locale: Optional[str],
) -> None:
    if not any([name, schema_content_file, connector_id, locale]):
        return

    data: Dict[str, Any] = {}

    if name is not None:
        data["name"] = name

    if locale is not None:
        data["locale"] = locale

    with ELISClient() as elis:
        if connector_id is not None:
            data["connector"] = get_json(
                elis.get(f"connectors/{connector_id}"))["url"]

        if schema_content_file is not None:
            name = name or elis.get_queue(id_)["name"]
            schema_content = json.load(schema_content_file)
            schema_dict = elis.create_schema(f"{name} schema", schema_content)
            data["schema"] = schema_dict["url"]

        elis.patch(f"queues/{id_}", data)
コード例 #6
0
ファイル: extract_data.py プロジェクト: tbots/elisctl
def get_data(
    ctx: click.Context,
    queue_id: int,
    files: Tuple[str],
    indent: int,
    ensure_ascii: bool,
    format_: str,
    output_file: Optional[IO[str]],
):
    annotations_to_export = list()

    with ELISClient(context=ctx.obj) as elis:
        for file in files:
            json_response = elis.upload_document(queue_id, file)
            annotation_id = get_id(json_response)
            annotations_to_export.append(annotation_id)
            elis.poll_annotation(annotation_id, _is_done)

        export_data = elis.export_data(queue_id, annotations_to_export,
                                       format_)

        if format_ == "json":
            output = json.dumps(get_json(export_data),
                                indent=indent,
                                ensure_ascii=ensure_ascii)
        else:
            output = get_text(export_data)
        click.echo(output.encode("utf-8"), file=output_file, nl=False)
コード例 #7
0
ファイル: queue.py プロジェクト: JanaPradacova/elisctl
def change_command(
    ctx: click.Context,
    id_: int,
    name: Optional[str],
    schema_content: Optional[List[dict]],
    connector_id: Optional[int],
    locale: Optional[str],
) -> None:
    if not any([name, schema_content, connector_id, locale]):
        return

    data: Dict[str, Any] = {}

    if name is not None:
        data["name"] = name

    if locale is not None:
        data["locale"] = locale

    with ELISClient(context=ctx.obj) as elis:
        if connector_id is not None:
            data["connector"] = get_json(
                elis.get(f"connectors/{connector_id}"))["url"]

        if schema_content is not None:
            name = name or elis.get_queue(id_)["name"]
            schema_dict = elis.create_schema(f"{name} schema", schema_content)
            data["schema"] = schema_dict["url"]

        elis.patch(f"queues/{id_}", data)
コード例 #8
0
ファイル: workspace.py プロジェクト: qiq/elisctl
def create_command(name: str, organization_id: Optional[int]) -> None:
    with ELISClient() as elis:
        organization_url = elis.get_organization(organization_id)["url"]

        res = elis.post("workspaces", {
            "name": name,
            "organization": organization_url
        })
    workspace_dict = get_json(res)
    click.echo(workspace_dict["id"])
コード例 #9
0
def _create_schema(id_: str, schema_content: SchemaContent, elis: ELISClient,
                   name: Optional[str]) -> None:
    original_schema = get_json(elis.get(f"schemas/{id_}"))
    new_schema = elis.create_schema(name or original_schema["name"],
                                    schema_content)

    for queue_url in original_schema["queues"]:
        if queue_url.startswith(elis.url):
            queue_url = queue_url[len(elis.url) + 1:]
        elis.patch(queue_url, data={"schema": new_schema["url"]})
コード例 #10
0
def create_command(
    username: str,
    password: Optional[str],
    queue_id: Tuple[int],
    organization_id: Optional[int],
    group: str,
    locale: str,
) -> None:
    """
    Create user with USERNAME and add him to QUEUES specified by ids.
    """
    password = password or _generate_password()
    with APIClient() as api:
        _check_user_does_not_exists(api, username)
        organization_dict = _get_organization(api, organization_id)

        workspace_urls = {
            w["url"]
            for w in get_json(api.get("workspaces", {"organization": organization_dict["id"]}))[
                "results"
            ]
        }
        queue_urls = []
        for queue in queue_id:
            queue_dict = get_json(api.get(f"queues/{queue}"))
            if queue_dict["workspace"] in workspace_urls:
                queue_urls.append(queue_dict["url"])

        response = api.post(
            "users",
            {
                "username": username,
                "email": username,
                "organization": organization_dict["url"],
                "password": password,
                "groups": get_groups(api, group),
                "queues": queue_urls,
                "ui_settings": {"locale": locale},
            },
        )
        click.echo(f"{get_json(response)['id']}, {password}")
コード例 #11
0
ファイル: queue.py プロジェクト: tbots/elisctl
def create_command(
    ctx: click.Context,
    name: str,
    schema_content: List[dict],
    email_prefix: Optional[str],
    bounce_email: Optional[str],
    workspace_id: Optional[int],
    connector_id: Optional[int],
    webhook_id: Optional[Tuple[int, ...]],
    locale: Optional[str],
) -> None:
    if email_prefix is not None and bounce_email is None:
        raise click.ClickException("Inbox cannot be created without specified bounce email.")

    with ELISClient(context=ctx.obj) as elis:
        workspace_url = elis.get_workspace(workspace_id)["url"]
        connector_url = (
            get_json(elis.get(f"connectors/{connector_id}"))["url"]
            if connector_id is not None
            else None
        )

        webhooks_urls = []
        if webhook_id:
            for webhook in webhook_id:
                webhook_url = get_json(elis.get(f"webhooks/{webhook}"))["url"]
                webhooks_urls.append(webhook_url)

        schema_dict = elis.create_schema(f"{name} schema", schema_content)
        queue_dict = elis.create_queue(
            name, workspace_url, schema_dict["url"], connector_url, webhooks_urls, locale
        )

        inbox_dict = {"email": "no email-prefix specified"}
        if email_prefix is not None:
            inbox_dict = elis.create_inbox(
                f"{name} inbox", email_prefix, bounce_email, queue_dict["url"]
            )
    click.echo(f"{queue_dict['id']}, {inbox_dict['email']}")
コード例 #12
0
ファイル: queue.py プロジェクト: tbots/elisctl
def _patch_inbox(
    elis: ELISClient, queue_dict: dict, email_prefix: Optional[str], bounce_email: Optional[str]
) -> dict:
    inbox_data: Dict[str, Any] = {}

    if email_prefix:
        inbox_data["email_prefix"] = email_prefix

    if bounce_email:
        inbox_data["bounce_email_to"] = bounce_email
        inbox_data["bounce_unprocessable_attachments"] = True

    _, inbox_id = queue_dict["inbox"].rsplit("/", 1)
    return get_json(elis.patch(f"inboxes/{inbox_id}", inbox_data))
コード例 #13
0
ファイル: commands.py プロジェクト: aberndt-hub/elisctl
def download_command(
    ctx: click.Context,
    id_: str,
    indent: int,
    ensure_ascii: bool,
    format_: str,
    output_file: Optional[IO[str]],
):
    with APIClient(context=ctx.obj) as api_client:
        schema_dict = get_json(api_client.get(f"schemas/{id_}"))
    if format_ == "json":
        schema_file = json.dumps(schema_dict["content"],
                                 indent=indent,
                                 ensure_ascii=ensure_ascii,
                                 sort_keys=True).encode("utf-8")
    else:
        schema_file = SchemaToXlsx().convert(schema_dict["content"])

    click.echo(schema_file, file=output_file, nl=False)
コード例 #14
0
def get_groups(api: APIClient, group_name: Optional[str]) -> List[str]:
    if group_name is None:
        return []
    return [g["url"] for g in get_json(api.get("groups", {"name": group_name}))["results"]]
コード例 #15
0
def _check_user_does_not_exists(api: APIClient, username: str) -> None:
    total_users = get_json(api.get(f"users", {"username": username}))["pagination"]["total"]
    if total_users:
        raise click.ClickException(f"User with username {username} already exists.")