Exemplo n.º 1
0
def register_command1():
    """Remote command registration service.
    This has to be enabled by liquer.commands.enable_remote_registration()

    WARNING: Remote command registration allows to deploy arbitrary python code on LiQuer server,
    therefore it is a HUGE SECURITY RISK and it only should be used if other security measures are taken
    (e.g. on localhost or intranet where only trusted users have access).
    This is on by default on Jupyter server extension.
    """
    data = request.get_data()
    return jsonify(command_registry().register_remote_serialized(data))
Exemplo n.º 2
0
    def test_command(self):
        reset_command_registry()

        @command
        def test_callable(state, a: int, b=123):  # has state as a first argument
            return a + b

        ns, _command, command_metadata = command_registry().resolve_command(State(),"test_callable")
        m = Dependencies()
        m.add_command_dependency(ns, command_metadata)
        assert "ns-root/test_callable" in m.as_dict()["commands"]
Exemplo n.º 3
0
def help(state, command_name, ns="root"):
    """Returns a description of the command"""
    crd = command_registry().as_dict()
    make_link = ("py" in crd) and ("pygments" in crd)
    try:
        r = crd[ns]
        try:
            c = r[command_name]
            html = f"<html><head><title>{command_name}</title></head>"
            html = "<body\n>"
            html = f"<h1>{command_name} <em>({ns})</em></h1>\n"
            html += f"<pre>{c['doc']}</pre>\n"
            html += "<h3>Arguments</h3>\n"
            html += "  <ul>\n"
            for a in c["arguments"]:
                html += f"    <li><b>{a['name']}</b> - type: {a['type']}</li>\n"
            html += "  </ul>"
            html += "<h3>Attributes</h3>\n"
            html += "  <ul>\n"
            for key, value in sorted(c["attributes"].items()):
                if key == "example_link":
                    q = value
                    url = (state.vars.get("server", "http://localhost") +
                           state.vars.get("api_path", "/q/") + q)
                    html += f'    <li><b>{key}</b>:<a href="{url}">{value}</a></li>\n'
                else:
                    html += f"    <li><b>{key}</b>:{value}</li>\n"
            html += "  </ul>"
            html += f"<h3>Python module</h3>\n"
            module = c["module"]
            if make_link:
                modfile = module.replace(".", "_")
                html += evaluate_template(
                    f'<a href="$ns-py-pygments/module_source-{module}/highlight/link-url-html$/{modfile}.html">{module}</a>\n'
                )
            else:
                html += module

            html += "</body>"
            html += "</html>"
            return state.with_data(html).with_filename("help.html")
        except KeyError:
            html = f"<h1>Command <em>{command_name}</em> not registered in namespace <em>{ns}</em></h1>"
            candidates = ", ".join(key for key, value in crd.items()
                                   if command_name in value)
            if len(candidates):
                html += f"Command can be found in: {candidates}"
            else:
                html += "There is no such command in available namespaces"
            return state.with_data(html).with_filename("help.html")
    except KeyError:
        return state.with_data(f"<h1>Unknown namespace <em>{ns}</em></h1>"
                               ).with_filename("help.html")
Exemplo n.º 4
0
def flat_commands_nodoc(*pairs):
    """Returns a list of commands in json format"""
    filters = [(pairs[i - 1], pairs[i]) for i in range(1, len(pairs), 2)]
    d = []
    for rl in command_registry().as_dict().values():
        for r in rl.values():
            if len(filters) == 0 or all(r["attributes"].get(key) == value
                                        for key, value in filters):
                c = {key: r.get(key) for key in ["name", "label", "module"]}
                for key, value in r["attributes"].items():
                    if key not in c and key != "context_menu":
                        c[key] = value
                d.append(c)
    return d
Exemplo n.º 5
0
def evaluate_ql_on(ql, state=None, cache=None):
    """This is equivalent to evaluate_query_on, but accepts decoded query
    (list of lists of strings)."""
    if cache is None:
        cache = get_cache()
    if state is None:
        state = State()
    elif not isinstance(state, State):
        state = State().with_data(state)

    cr = command_registry()
    for i, qcommand in enumerate(ql):
        if i == len(ql) - 1:
            if len(qcommand) == 1 and '.' in qcommand[0]:
                state.with_filename(qcommand[0])
                break
        state = cr.evaluate_command(state, qcommand)
        if state.caching and not state.is_error and not state.is_volatile():
            cache.store(state)

    return state
Exemplo n.º 6
0
def commands():
    """Returns a list of commands in json format"""
    return jsonify(command_registry().as_dict())
Exemplo n.º 7
0
 def post(self, param):
     self.write(
         json.dumps(command_registry().register_remote_serialized(
             self.request.body)))
Exemplo n.º 8
0
 def get(self):
     """Returns a list of commands in json format"""
     self.write(json.dumps(command_registry().as_dict()))
Exemplo n.º 9
0
 def get(self, param):
     self.write(
         json.dumps(command_registry().register_remote_serialized(
             param.encode("ascii"))))
Exemplo n.º 10
0
 def get(self):
     mimetype = "application/json"
     header = "Content-Type"
     body = mimetype
     self.set_header(header, body)
     self.write(json.dumps(command_registry().as_dict()))
Exemplo n.º 11
0
 def command_registry(self):
     return command_registry()