Ejemplo n.º 1
1
def edit_rules():
    """Edit rules

    Edit the rules that process inbound events"""
    my_rules = rules.get_all_rules()
    my_rules.append(DEFAULT_RULE)

    selected_rule_id = select(
        label="Existing rules",
        options=[{
            "label": rule["name"],
            "value": rule["id"]
        } for rule in my_rules],
    )
    # Rules have unique IDs from the database:
    logging.info(f"selected_rule: {selected_rule_id}")
    use_rule = [r for r in my_rules if r["id"] == int(selected_rule_id)][0]
    updated_rule = input_group(
        "Rule editing",
        [
            input("name",
                  type=TEXT,
                  name="name",
                  value=use_rule["name"],
                  required=True),  # Need ttextarea(
            textarea(
                "Rule names",
                name="rule",
                rows=10,
                code={
                    "mode": "python",  # code language
                    "theme":
                    "darcula",  # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes
                },
                value=f"""{use_rule['rule']}\n""",
            ),
            actions(
                "actions",
                [
                    # {"label": "test", "value": "test"},
                    {
                        "label": "save",
                        "value": "save"
                    },
                ],
                name="action",
                help_text="Save",
            ),
        ],
    )
    if updated_rule is not None:
        rl = dict(updated_rule)
        if rl["action"] == "save":
            rule_info = rules.save_rule(rl["name"], rl["rule"],
                                        selected_rule_id)
            put_row(put_text("Rule"))
            put_row(put_code(pprint.pformat(rule_info, indent=1)))
            # Use webhook_info's ID to add/update the extractor

    put_text(f"The rule added is: {updated_rule}")
Ejemplo n.º 2
0
    def set_status(self, state: int) -> None:
        """
        Args:
            state (int):
                1 (running)
                2 (not running)
                3 (warning, stop unexpectedly)
                4 (stop for update)
                0 (hide)
                -1 (*state not changed)
        """
        if state == -1:
            return
        clear()

        if state == 1:
            put_row(
                [
                    put_loading(color="success").style("--loading-border--"),
                    None,
                    put_text(t("Gui.Status.Running")),
                ],
                size="auto 2px 1fr",
            )
        elif state == 2:
            put_row(
                [
                    put_loading(color="secondary").style("--loading-border-fill--"),
                    None,
                    put_text(t("Gui.Status.Inactive")),
                ],
                size="auto 2px 1fr",
            )
        elif state == 3:
            put_row(
                [
                    put_loading(shape="grow", color="warning").style(
                        "--loading-grow--"
                    ),
                    None,
                    put_text(t("Gui.Status.Warning")),
                ],
                size="auto 2px 1fr",
            )
        elif state == 4:
            put_row(
                [
                    put_loading(shape="grow", color="success").style(
                        "--loading-grow--"
                    ),
                    None,
                    put_text(t("Gui.Status.Updating")),
                ],
                size="auto 2px 1fr",
            )
Ejemplo n.º 3
0
def index():
    """PyWebIO demos

    PyWebIO的基本demo和数据可视化demo
    """
    style(
        put_row([
            put_markdown('# PyWebIO demos'),
            put_html(
                '<a class="github-button" data-size="large" href="https://github.com/wang0618/PyWebIO" data-show-count="true" aria-label="Star wang0618/PyWebIO on GitHub">Star</a>'
            )
        ],
                size='1fr auto'), 'align-items:center')
    put_html(
        '<script async defer src="https://buttons.github.io/buttons.js"></script>'
    )

    put_markdown(index_md)
Ejemplo n.º 4
0
def edit_webhook():
    """Edit webhook and its extractor

    This should be changed to work with a programattically named
    webhook that has a randmoly named inbound address. For now, we
    name it I guess

    """
    my_hooks = extractions.get_all_webhooks()
    my_hooks.append({
        "id": 0,
        "name": "Your new webhook could go here!",
        "path": "Enter the path that this will be matched on",
        "queue_name": "base name for the queue",
    })

    selected_hook_id = select(
        label="Existing webhooks",
        options=[{
            "label": hook.get("name", "No hook name found"),
            "value": hook.get("id", 0),
        } for hook in my_hooks],
    )
    # Rules have unique IDs from the database:
    logging.info(f"selected_hook: {selected_hook_id}")

    my_hook = dict()
    my_extractor = dict()
    my_hook.update(DEFAULT_HOOK)
    my_extractor.update(DEFAULT_EXTRACTOR)
    if selected_hook_id != 0:
        my_hook = extractions.get_webhook(selected_hook_id)
        my_extractor = extractions.get_hook_extractor(selected_hook_id)

    # TODO: update validator to avoid default hook 0 names
    updated_extractor = input_group(
        "Hook data and hook extractor",
        [
            input("name",
                  type=TEXT,
                  name="name",
                  value=my_hook["name"],
                  required=True),  # Need to get a way to carry around the IDs
            input("path",
                  type=TEXT,
                  name="path",
                  value=my_hook["path"],
                  required=True),  # Need to get a way to carry around the IDs
            input(
                "queue_name",
                type=TEXT,
                name="queue_name",
                value=my_hook["queue_name"],
                required=True,
            ),  # Need to get a way to carry around the IDs
            textarea(
                "Example message",
                name="example",
                rows=10,
                code={
                    "mode": "python",  # code language
                    "theme":
                    "darcula",  # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes
                },
                value=my_extractor["example"],
            ),
            textarea(
                "Edit an extraction rule",
                name="extractor",
                rows=10,
                code={
                    "mode": "python",  # code language
                    "theme":
                    "darcula",  # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes
                },
                value=my_extractor["extractor"],
            ),
            actions(
                "actions",
                [
                    {
                        "label": "test",
                        "value": "test"
                    },
                    {
                        "label": "save",
                        "value": "save"
                    },
                ],
                name="action",
                help_text="Save or test",
            ),
        ],
        validate=test_extractor,
    )
    # Pretty much can't be none, but let's follow the example from
    # https://pywebio.readthedocs.io/en/latest/input.html#pywebio.input.actions
    if updated_extractor is not None:
        uex = dict(updated_extractor)
        if uex["action"] == "save":
            webhook_info = extractions.save_webhook(selected_hook_id,
                                                    uex["name"], uex["path"],
                                                    uex["queue_name"])
            extractor_info = extractions.save_extractor(
                uex["name"], webhook_info["id"], uex["extractor"],
                uex["example"])
            put_row(put_text("Webhook"))
            put_row(put_code(pprint.pformat(webhook_info, indent=1)))
            put_row(put_text("Extractor"))
            put_row(put_code(pprint.pformat(extractor_info, indent=1)))
Ejemplo n.º 5
0
    def dev_update(self) -> None:
        self.init_menu(name='Update')
        self.set_title(t("Gui.MenuDevelop.Update"))

        if not Setting.reload:
            put_warning(t("Gui.Update.DisabledWarn"))

        put_row(content=[
            put_scope('updater_loading'), None,
            put_scope('updater_state')
        ],
                size='auto .25rem 1fr')

        put_scope('updater_btn')
        put_scope('updater_info')

        def update_table():
            with use_scope('updater_info', clear=True):
                local_commit = updater.get_commit(short_sha1=True)
                upstream_commit = updater.get_commit(
                    f'origin/{updater.branch}', short_sha1=True)
                put_table([[t('Gui.Update.Local'), *local_commit],
                           [t('Gui.Update.Upstream'), *upstream_commit]],
                          header=[
                              '', 'SHA1',
                              t('Gui.Update.Author'),
                              t('Gui.Update.Time'),
                              t('Gui.Update.Message')
                          ])

        def u(state):
            if state == -1:
                return
            clear('updater_loading')
            clear('updater_state')
            clear('updater_btn')
            if state == 0:
                put_loading('border', 'secondary',
                            'updater_loading').style("--loading-border-fill--")
                put_text(t('Gui.Update.UpToDate'), scope='updater_state')
                put_button(t('Gui.Button.CheckUpdate'),
                           onclick=updater.check_update,
                           color='info',
                           scope='updater_btn')
                update_table()
            elif state == 1:
                put_loading('grow', 'success',
                            'updater_loading').style("--loading-grow--")
                put_text(t('Gui.Update.HaveUpdate'), scope='updater_state')
                put_button(t('Gui.Button.ClickToUpdate'),
                           onclick=updater.run_update,
                           color='success',
                           scope='updater_btn')
                update_table()
            elif state == 'checking':
                put_loading('border', 'primary',
                            'updater_loading').style("--loading-border--")
                put_text(t('Gui.Update.UpdateChecking'), scope='updater_state')
            elif state == 'failed':
                put_loading('grow', 'danger',
                            'updater_loading').style("--loading-grow--")
                put_text(t('Gui.Update.UpdateFailed'), scope='updater_state')
                put_button(t('Gui.Button.RetryUpdate'),
                           onclick=updater.run_update,
                           color='primary',
                           scope='updater_btn')
            elif state == 'start':
                put_loading('border', 'primary',
                            'updater_loading').style("--loading-border--")
                put_text(t('Gui.Update.UpdateStart'), scope='updater_state')
                put_button(t('Gui.Button.CancelUpdate'),
                           onclick=updater.cancel,
                           color='danger',
                           scope='updater_btn')
            elif state == 'wait':
                put_loading('border', 'primary',
                            'updater_loading').style("--loading-border--")
                put_text(t('Gui.Update.UpdateWait'), scope='updater_state')
                put_button(t('Gui.Button.CancelUpdate'),
                           onclick=updater.cancel,
                           color='danger',
                           scope='updater_btn')
            elif state == 'run update':
                put_loading('border', 'primary',
                            'updater_loading').style("--loading-border--")
                put_text(t('Gui.Update.UpdateRun'), scope='updater_state')
                put_button(t('Gui.Button.CancelUpdate'),
                           onclick=updater.cancel,
                           color='danger',
                           scope='updater_btn',
                           disabled=True)
            elif state == 'reload':
                put_loading('grow', 'success',
                            'updater_loading').style("--loading-grow--")
                put_text(t('Gui.Update.UpdateSuccess'), scope='updater_state')
                update_table()
            elif state == 'finish':
                put_loading('grow', 'success',
                            'updater_loading').style("--loading-grow--")
                put_text(t('Gui.Update.UpdateFinish'), scope='updater_state')
                update_table()
            elif state == 'cancel':
                put_loading('border', 'danger',
                            'updater_loading').style("--loading-border--")
                put_text(t('Gui.Update.UpdateCancel'), scope='updater_state')
                put_button(t('Gui.Button.CancelUpdate'),
                           onclick=updater.cancel,
                           color='danger',
                           scope='updater_btn',
                           disabled=True)
            else:
                put_text("Something went wrong, please contact develops",
                         scope='updater_state')
                put_text(f"state: {state}", scope='updater_state')

        updater_switch = Switch(status=u,
                                get_state=lambda: updater.state,
                                name='updater')

        update_table()
        self.task_handler.add(updater_switch.g(),
                              delay=0.5,
                              pending_delete=True)

        updater.check_update()
Ejemplo n.º 6
0
    def dev_update(self) -> None:
        self.init_menu(name="Update")
        self.set_title(t("Gui.MenuDevelop.Update"))

        if not Setting.reload:
            put_warning(t("Gui.Update.DisabledWarn"))

        put_row(
            content=[
                put_scope("updater_loading"), None,
                put_scope("updater_state")
            ],
            size="auto .25rem 1fr",
        )

        put_scope("updater_btn")
        put_scope("updater_info")

        def update_table():
            with use_scope("updater_info", clear=True):
                local_commit = updater.get_commit(short_sha1=True)
                upstream_commit = updater.get_commit(
                    f"origin/{updater.branch}", short_sha1=True)
                put_table(
                    [
                        [t("Gui.Update.Local"), *local_commit],
                        [t("Gui.Update.Upstream"), *upstream_commit],
                    ],
                    header=[
                        "",
                        "SHA1",
                        t("Gui.Update.Author"),
                        t("Gui.Update.Time"),
                        t("Gui.Update.Message"),
                    ],
                )

        def u(state):
            if state == -1:
                return
            clear("updater_loading")
            clear("updater_state")
            clear("updater_btn")
            if state == 0:
                put_loading("border", "secondary",
                            "updater_loading").style("--loading-border-fill--")
                put_text(t("Gui.Update.UpToDate"), scope="updater_state")
                put_button(
                    t("Gui.Button.CheckUpdate"),
                    onclick=updater.check_update,
                    color="info",
                    scope="updater_btn",
                )
                update_table()
            elif state == 1:
                put_loading("grow", "success",
                            "updater_loading").style("--loading-grow--")
                put_text(t("Gui.Update.HaveUpdate"), scope="updater_state")
                put_button(
                    t("Gui.Button.ClickToUpdate"),
                    onclick=updater.run_update,
                    color="success",
                    scope="updater_btn",
                )
                update_table()
            elif state == "checking":
                put_loading("border", "primary",
                            "updater_loading").style("--loading-border--")
                put_text(t("Gui.Update.UpdateChecking"), scope="updater_state")
            elif state == "failed":
                put_loading("grow", "danger",
                            "updater_loading").style("--loading-grow--")
                put_text(t("Gui.Update.UpdateFailed"), scope="updater_state")
                put_button(
                    t("Gui.Button.RetryUpdate"),
                    onclick=updater.run_update,
                    color="primary",
                    scope="updater_btn",
                )
            elif state == "start":
                put_loading("border", "primary",
                            "updater_loading").style("--loading-border--")
                put_text(t("Gui.Update.UpdateStart"), scope="updater_state")
                put_button(
                    t("Gui.Button.CancelUpdate"),
                    onclick=updater.cancel,
                    color="danger",
                    scope="updater_btn",
                )
            elif state == "wait":
                put_loading("border", "primary",
                            "updater_loading").style("--loading-border--")
                put_text(t("Gui.Update.UpdateWait"), scope="updater_state")
                put_button(
                    t("Gui.Button.CancelUpdate"),
                    onclick=updater.cancel,
                    color="danger",
                    scope="updater_btn",
                )
            elif state == "run update":
                put_loading("border", "primary",
                            "updater_loading").style("--loading-border--")
                put_text(t("Gui.Update.UpdateRun"), scope="updater_state")
                put_button(
                    t("Gui.Button.CancelUpdate"),
                    onclick=updater.cancel,
                    color="danger",
                    scope="updater_btn",
                    disabled=True,
                )
            elif state == "reload":
                put_loading("grow", "success",
                            "updater_loading").style("--loading-grow--")
                put_text(t("Gui.Update.UpdateSuccess"), scope="updater_state")
                update_table()
            elif state == "finish":
                put_loading("grow", "success",
                            "updater_loading").style("--loading-grow--")
                put_text(t("Gui.Update.UpdateFinish"), scope="updater_state")
                update_table()
            elif state == "cancel":
                put_loading("border", "danger",
                            "updater_loading").style("--loading-border--")
                put_text(t("Gui.Update.UpdateCancel"), scope="updater_state")
                put_button(
                    t("Gui.Button.CancelUpdate"),
                    onclick=updater.cancel,
                    color="danger",
                    scope="updater_btn",
                    disabled=True,
                )
            else:
                put_text(
                    "Something went wrong, please contact develops",
                    scope="updater_state",
                )
                put_text(f"state: {state}", scope="updater_state")

        updater_switch = Switch(status=u,
                                get_state=lambda: updater.state,
                                name="updater")

        update_table()
        self.task_handler.add(updater_switch.g(),
                              delay=0.5,
                              pending_delete=True)

        updater.check_update()
Ejemplo n.º 7
0
    def dev_remote(self) -> None:
        self.init_menu(name="Remote")
        self.set_title(t("Gui.MenuDevelop.Remote"))
        put_row(
            content=[put_scope("remote_loading"), None, put_scope("remote_state")],
            size="auto .25rem 1fr",
        )
        put_scope("remote_info")

        def u(state):
            if state == -1:
                return
            clear("remote_loading")
            clear("remote_state")
            clear("remote_info")
            if state in (1, 2):
                put_loading("grow", "success", "remote_loading").style(
                    "--loading-grow--"
                )
                put_text(t("Gui.Remote.Running"), scope="remote_state")
                put_text(t("Gui.Remote.EntryPoint"), scope="remote_info")
                entrypoint = RemoteAccess.get_entry_point()
                if entrypoint:
                    if State.electron:  # Prevent click into url in electron client
                        put_text(entrypoint, scope="remote_info").style(
                            "text-decoration-line: underline"
                        )
                    else:
                        put_link(name=entrypoint, url=entrypoint, scope="remote_info")
                else:
                    put_text("Loading...", scope="remote_info")
            elif state in (0, 3):
                put_loading("border", "secondary", "remote_loading").style(
                    "--loading-border-fill--"
                )
                if (
                    State.deploy_config.EnableRemoteAccess
                    and State.deploy_config.Password
                ):
                    put_text(t("Gui.Remote.NotRunning"), scope="remote_state")
                else:
                    put_text(t("Gui.Remote.NotEnable"), scope="remote_state")
                put_text(t("Gui.Remote.ConfigureHint"), scope="remote_info")
                url = "http://app.azurlane.cloud" + (
                    "" if State.deploy_config.Language.startswith("zh") else "/en.html"
                )
                put_html(
                    f'<a href="{url}" target="_blank">{url}</a>', scope="remote_info"
                )
                if state == 3:
                    put_warning(
                        t("Gui.Remote.SSHNotInstall"),
                        closable=False,
                        scope="remote_info",
                    )

        remote_switch = Switch(
            status=u, get_state=RemoteAccess.get_state, name="remote"
        )

        self.task_handler.add(remote_switch.g(), delay=1, pending_delete=True)