def _bool_pladder_to_py(string): if string == "true": return True elif string == "false": return False else: raise ScriptError(f'Expected "true" or "false", got "{string}"')
def let(context, *args): if len(args) % 2 != 1: raise ScriptError("Let accepts an odd number of arguments (name-value pairs and a body)") new_env = dict(context.environment) for variable, value in _pairs(args[:-1]): new_env[variable] = value subcontext = context._replace(environment=new_env) result, _display_name = interpret(subcontext, args[-1]) return result
def exec_command(context: Context, *args: str) -> str: if len(command.params) != len(args): raise ScriptError( f"{command.name} takes {len(command.params)} arguments, got {len(args)}" ) new_env = dict(zip(command.params, args)) subcontext = context._replace(environment=new_env) result, _display_name = interpret(subcontext, command.script) return result
def _apply(context, words): if not words: return "" command_name, arguments = words[0], words[1:] command = context.commands.lookup_command(command_name) if command is None: raise ScriptError(f"Unknown command name: {command_name}") command_context = context._replace(command_name=command_name) return apply_call(command_context, command, command_name, arguments)
def rest_post_simple(url, message): """ Do a POST to a simple REST API, sending plain text and returning the result """ headers = {"Content-Type": "text/plain; charset=utf-8"} r = requests.post(url, headers=headers, data=message.encode("utf-8")) if r.status_code != 200: raise ScriptError("Unexpected error code: %d" % r.status_code) else: return r.text
def add_command(self, name: str, params: List[str], script: str) -> None: for param in params: if " " in param or "{" in param or "}" in param: raise ScriptError(f'Invalid parameter name: "{param}"') params_string = " ".join(params) with self._transaction() as c: c.execute( """ INSERT INTO commands(name, params, script) VALUES (?, ?, ?); """, (name, params_string, script))
def nth(index, *args): index = int(index) if index < 0 or index >= len(args): raise ScriptError("nth: index out of range") return args[index]
def last(*args): if not args: raise ScriptError("last: no arguments given") return args[-1]
def first(*args): if not args: raise ScriptError("first: no arguments given") return args[0]
def unicode(char_name): try: return unicodedata.lookup(char_name) except KeyError: raise ScriptError("Uknown Unicode character name: " + char_name)