def _compile_sqlserver(string): """ TOXIC compile function for SQL Server """ foundproc = False foundargs = [] foundvars = [] foundsql = [] foundtype = "varchar(max)" for (t, s) in misc.tokenizepi(string): if t is None: foundsql.append((-1, s)) elif t == "code": foundsql.append((0, s)) elif t == "expr": foundsql.append((1, s)) elif t == "args": foundargs.append(s) elif t == "vars": foundvars.append(s) elif t == "type": foundtype = s elif t == "proc": foundproc = True else: # Treat unknown PIs as text foundsql.append((-1, f"<?{t} {s}?>")) result = [] if foundargs: foundargs = ",\n\t".join(foundargs) result.append(f"(\n\t{foundargs}\n)\n") if not foundproc: result.append(f"returns {foundtype}\n") result.append("as\n") result.append("begin\n") if not foundproc: result.append(f"\tdeclare @c_out {foundtype};\n") result.append("\tset @c_out = '';\n") if foundvars: foundvars = "".join(foundvars) result.append(f"\t{foundvars}\n") for (mode, string) in foundsql: if mode == -1: string = string.replace("'", "''") string = f"'{string}'" result.append(f"\tset @c_out = @c_out + {string};\n") elif mode == 0: result.append(string) result.append("\n") else: # mode == 1 result.append( f"\tset @c_out = @c_out + set @c_out = @c_out + convert(varchar, isnull({string}, ''));\n" ) if not foundproc: result.append("\treturn @c_out;\n") result.append("end;\n") return "".join(result)
def _compile_sqlserver(string): """ TOXIC compile function for SQL Server """ foundproc = False foundargs = [] foundvars = [] foundsql = [] foundtype = "varchar(max)" for (t, s) in misc.tokenizepi(string): if t is None: foundsql.append((-1, s)) elif t == "code": foundsql.append((0, s)) elif t == "expr": foundsql.append((1, s)) elif t == "args": foundargs.append(s) elif t == "vars": foundvars.append(s) elif t == "type": foundtype = s elif t == "proc": foundproc = True else: # Treat unknown PIs as text foundsql.append((-1, f"<?{t} {s}?>")) result = [] if foundargs: foundargs = ",\n\t".join(foundargs) result.append(f"(\n\t{foundargs}\n)\n") if not foundproc: result.append(f"returns {foundtype}\n") result.append("as\n") result.append("begin\n") if not foundproc: result.append(f"\tdeclare @c_out {foundtype};\n") result.append("\tset @c_out = '';\n") if foundvars: foundvars = "".join(foundvars) result.append(f"\t{foundvars}\n") for (mode, string) in foundsql: if mode == -1: string = string.replace("'", "''") string = f"'{string}'" result.append(f"\tset @c_out = @c_out + {string};\n") elif mode == 0: result.append(string) result.append("\n") else: # mode == 1 result.append(f"\tset @c_out = @c_out + set @c_out = @c_out + convert(varchar, isnull({string}, ''));\n") if not foundproc: result.append("\treturn @c_out;\n") result.append("end;\n") return "".join(result)
def xml2py(source): stack = [] stackoutput = [] # stack containing only True for def and False for class lines = [ f"# generated by {__file__} on {datetime.datetime.utcnow()} UTC", "", "from ll.misc import xmlescape as __detox_xmlescape__", "", ] def endscope(action): if not stack: raise SyntaxError( f"can't end {action or 'unnamed'} scope: no active scope") if action and action != stack[-1][0]: raise SyntaxError( f"can't end {action} scope: active scope is: {stack[-1][0]} {stack[-1][1]}" ) return stack.pop() for (t, s) in misc.tokenizepi(source): if t is None: # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield {s!r}") elif t == "expr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield {s}") elif t == "textexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append( f"{len(stack)*indent}yield __detox_xmlescape__({s})") elif t == "attrexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append( f"{len(stack)*indent}yield __detox_xmlescape__({s})") elif t == "code": lines.append(f"{len(stack)*indent}{s}") elif t == "def": lines.append("") lines.append(f"{len(stack)*indent}def {s}:") stack.append((t, s)) stackoutput.append(True) elif t == "class": lines.append("") lines.append(f"{len(stack)*indent}class {s}:") stack.append((t, s)) stackoutput.append(False) elif t == "for": lines.append(f"{len(stack)*indent}for {s}:") stack.append((t, s)) elif t == "while": lines.append(f"{len(stack)*indent}while {s}:") stack.append((t, s)) elif t == "if": lines.append(f"{len(stack)*indent}if {s}:") stack.append((t, s)) elif t == "else": lines.append(f"{(len(stack)-1)*indent}else:") elif t == "elif": lines.append(f"{(len(stack)-1)*indent}elif {s}:") elif t == "end": scope = endscope(s) if scope in ("def", "class"): stackoutput.pop() else: # unknown PI target => treat as text # ignore output outside of functions if stackoutput and stackoutput[-1]: s = f"<?{t} {s}?>" lines.append(f"{len(stack)*indent}yield {s!r}") if stack: scopes = ", ".join(scope[0] for scope in stack) raise SyntaxError(f"unclosed scopes remaining: {scopes}") return "\n".join(lines)
def xml2py(source): stack = [] stackoutput = [] # stack containing only True for def and False for class lines = [ "# generated by {} on {} UTC".format(__file__, datetime.datetime.utcnow()), "", "from ll.misc import xmlescape as __detox_xmlescape__", "", ] def endscope(action): if not stack: raise SyntaxError("can't end {} scope: no active scope".format( action or "unnamed")) if action and action != stack[-1][0]: raise SyntaxError( "can't end {} scope: active scope is: {} {}".format( action, stack[-1][0], stack[-1][1])) return stack.pop() for (t, s) in misc.tokenizepi(source): if t is None: # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append("{}yield {!r}".format(len(stack) * indent, s)) elif t == "expr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append("{}yield {}".format(len(stack) * indent, s)) elif t == "textexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append("{}yield __detox_xmlescape__({})".format( len(stack) * indent, s)) elif t == "attrexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append("{}yield __detox_xmlescape__({})".format( len(stack) * indent, s)) elif t == "code": lines.append("{}{}".format(len(stack) * indent, s)) elif t == "def": lines.append("") lines.append("{}def {}:".format(len(stack) * indent, s)) stack.append((t, s)) stackoutput.append(True) elif t == "class": lines.append("") lines.append("{}class {}:".format(len(stack) * indent, s)) stack.append((t, s)) stackoutput.append(False) elif t == "for": lines.append("{}for {}:".format(len(stack) * indent, s)) stack.append((t, s)) elif t == "while": lines.append("{}while {}:".format(len(stack) * indent, s)) stack.append((t, s)) elif t == "if": lines.append("{}if {}:".format(len(stack) * indent, s)) stack.append((t, s)) elif t == "else": lines.append("{}else:".format((len(stack) - 1) * indent)) elif t == "elif": lines.append("{}elif {}:".format((len(stack) - 1) * indent, s)) elif t == "end": scope = endscope(s) if scope in ("def", "class"): stackoutput.pop() else: # unknown PI target => treat as text # ignore output outside of functions if stackoutput and stackoutput[-1]: s = "<?{} {}?>".format(t, s) lines.append("{}yield {!r}".format(len(stack) * indent, s)) if stack: raise SyntaxError("unclosed scopes remaining: {}".format(", ".join( scope[0] for scope in stack))) return "\n".join(lines)
def _compile_oracle(string): """ TOXIC compile function for Oracle """ foundproc = False foundargs = [] foundvars = [] foundsql = [] foundtype = "clob" for (t, s) in misc.tokenizepi(string): if t is None: foundsql.append((-1, s)) elif t == "code": foundsql.append((0, s)) elif t == "expr": foundsql.append((1, s)) elif t == "args": foundargs.append(s) elif t == "vars": foundvars.append(s) elif t == "type": foundtype = s elif t == "proc": foundproc = True else: # Treat unknown PIs as text foundsql.append((-1, f"<?{t} {s}?>")) result = [] if foundargs: foundargs = ",\n\t".join(foundargs) result.append(f"(\n\t{foundargs}\n)\n") plaintype = foundtype if "(" in plaintype: plaintype = plaintype[:plaintype.find("(")] isclob = plaintype.lower() in ("clob", "nclob") if not foundproc: result.append(f"return {plaintype}\n") result.append("as\n") if not foundproc: result.append(f"\tc_out {foundtype};\n") if foundvars: foundvars = "".join(foundvars) result.append("\t{foundvars}\n") nchar = foundtype.lower().startswith("n") if isclob: argtype = plaintype.rstrip("clob") for arg in ("clob", "varchar2"): result.append(f"\tprocedure write(p_text in {argtype}{arg})\n") result.append("\tas\n") result.append("\t\tbegin\n") if arg == "clob": result.append("\t\t\tif p_text is not null and length(p_text) != 0 then\n") result.append("\t\t\t\tdbms_lob.append(c_out, p_text);\n") else: result.append("\t\t\tif p_text is not null then\n") result.append("\t\t\t\tdbms_lob.writeappend(c_out, length(p_text), p_text);\n") result.append("\t\tend if;\n") result.append("\tend;\n") result.append("begin\n") if isclob: result.append("\tdbms_lob.createtemporary(c_out, true);\n") for (mode, string) in foundsql: if mode == -1: for s in _stringifyoracle(string, nchar): if isclob: result.append(f"\twrite({s});\n") else: result.append(f"\tc_out := c_out || {s};\n") elif mode == 0: result.append(string) result.append("\n") else: # mode == 1 if isclob: result.append(f"\twrite({string});\n") else: result.append(f"\tc_out := c_out || {string};\n") if not foundproc: result.append("\treturn c_out;\n") result.append("end;\n") return "".join(result)
def _compile_oracle(string): """ TOXIC compile function for Oracle """ foundproc = False foundargs = [] foundvars = [] foundsql = [] foundtype = "clob" for (t, s) in misc.tokenizepi(string): if t is None: foundsql.append((-1, s)) elif t == "code": foundsql.append((0, s)) elif t == "expr": foundsql.append((1, s)) elif t == "args": foundargs.append(s) elif t == "vars": foundvars.append(s) elif t == "type": foundtype = s elif t == "proc": foundproc = True else: # Treat unknown PIs as text foundsql.append((-1, f"<?{t} {s}?>")) result = [] if foundargs: foundargs = ",\n\t".join(foundargs) result.append(f"(\n\t{foundargs}\n)\n") plaintype = foundtype if "(" in plaintype: plaintype = plaintype[:plaintype.find("(")] isclob = plaintype.lower() in ("clob", "nclob") if not foundproc: result.append(f"return {plaintype}\n") result.append("as\n") if not foundproc: result.append(f"\tc_out {foundtype};\n") if foundvars: foundvars = "".join(foundvars) result.append("\t{foundvars}\n") nchar = foundtype.lower().startswith("n") if isclob: argtype = plaintype.rstrip("clob") for arg in ("clob", "varchar2"): result.append(f"\tprocedure write(p_text in {argtype}{arg})\n") result.append("\tas\n") result.append("\t\tbegin\n") if arg == "clob": result.append( "\t\t\tif p_text is not null and length(p_text) != 0 then\n" ) result.append("\t\t\t\tdbms_lob.append(c_out, p_text);\n") else: result.append("\t\t\tif p_text is not null then\n") result.append( "\t\t\t\tdbms_lob.writeappend(c_out, length(p_text), p_text);\n" ) result.append("\t\tend if;\n") result.append("\tend;\n") result.append("begin\n") if isclob: result.append("\tdbms_lob.createtemporary(c_out, true);\n") for (mode, string) in foundsql: if mode == -1: for s in _stringifyoracle(string, nchar): if isclob: result.append(f"\twrite({s});\n") else: result.append(f"\tc_out := c_out || {s};\n") elif mode == 0: result.append(string) result.append("\n") else: # mode == 1 if isclob: result.append(f"\twrite({string});\n") else: result.append(f"\tc_out := c_out || {string};\n") if not foundproc: result.append("\treturn c_out;\n") result.append("end;\n") return "".join(result)
def xml2py(source): stack = [] stackoutput = [] # stack containing only True for def and False for class lines = [ f"# generated by {__file__} on {datetime.datetime.utcnow()} UTC", "", "from ll.misc import xmlescape as __detox_xmlescape__", "", ] def endscope(action): if not stack: raise SyntaxError(f"can't end {action or 'unnamed'} scope: no active scope") if action and action != stack[-1][0]: raise SyntaxError(f"can't end {action} scope: active scope is: {stack[-1][0]} {stack[-1][1]}") return stack.pop() for (t, s) in misc.tokenizepi(source): if t is None: # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield {s!r}") elif t == "expr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield {s}") elif t == "textexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield __detox_xmlescape__({s})") elif t == "attrexpr": # ignore output outside of functions if stackoutput and stackoutput[-1]: lines.append(f"{len(stack)*indent}yield __detox_xmlescape__({s})") elif t == "code": lines.append(f"{len(stack)*indent}{s}") elif t == "def": lines.append("") lines.append(f"{len(stack)*indent}def {s}:") stack.append((t, s)) stackoutput.append(True) elif t == "class": lines.append("") lines.append(f"{len(stack)*indent}class {s}:") stack.append((t, s)) stackoutput.append(False) elif t == "for": lines.append(f"{len(stack)*indent}for {s}:") stack.append((t, s)) elif t == "while": lines.append(f"{len(stack)*indent}while {s}:") stack.append((t, s)) elif t == "if": lines.append(f"{len(stack)*indent}if {s}:") stack.append((t, s)) elif t == "else": lines.append(f"{(len(stack)-1)*indent}else:") elif t == "elif": lines.append(f"{(len(stack)-1)*indent}elif {s}:") elif t == "end": scope = endscope(s) if scope in ("def", "class"): stackoutput.pop() else: # unknown PI target => treat as text # ignore output outside of functions if stackoutput and stackoutput[-1]: s = f"<?{t} {s}?>" lines.append(f"{len(stack)*indent}yield {s!r}") if stack: scopes = ", ".join(scope[0] for scope in stack) raise SyntaxError(f"unclosed scopes remaining: {scopes}") return "\n".join(lines)