SHARE
TWEET

Untitled

a guest Aug 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2. from subprocess import run, PIPE, DEVNULL
  3. from num2words import num2words
  4. from jinja2 import Environment, PackageLoader, select_autoescape
  5.  
  6. env = Environment(
  7.     loader=PackageLoader('__main__', 'templates'),
  8.     autoescape=select_autoescape([])
  9. )
  10.  
  11.  
  12. def gen_gosrc(desc, templf):
  13.     args = {
  14.         "desc": desc,
  15.         "args": re.findall('[A-Z][^A-Z]*', desc.split("R")[0]),
  16.         "return": re.findall('[A-Z][^A-Z]*', desc.split("R")[1]),
  17.         "enumerate": enumerate,
  18.         "re": re,
  19.         "len": len,
  20.         "num2words": num2words,
  21.         "types": {
  22.             "I64": "int64",
  23.             "I": "int",
  24.             "B": "bool",
  25.             "S": "string",
  26.             "E": "error",
  27.             "Y": "[]byte",
  28.             "F": "float64",
  29.         },
  30.         "type_funcs": {
  31.             "Y": "ByteSlice",
  32.             "I64": "Int",
  33.         },
  34.         "tengo_type": {
  35.             "I64": "int",
  36.             "I": "int",
  37.             "B": "bool",
  38.             "S": "string",
  39.             "E": "error",
  40.             "Y": "bytes",
  41.             "F": "float",
  42.         }
  43.     }
  44.  
  45.     for k in args["types"].keys():
  46.         try:
  47.             args["type_funcs"][k]
  48.         except KeyError:
  49.             args["type_funcs"][k] = args["types"][k].capitalize()
  50.  
  51.     for k in args["types"].keys():
  52.         try:
  53.             args["type_funcs"][k]
  54.         except KeyError:
  55.             args["type_funcs"][k] = args["types"][k].lower()
  56.  
  57.     gosrc = env.get_template(templf).render(**args)
  58.     proc = run(
  59.         ["gofmt"],
  60.         input=gosrc.encode(),
  61.         stdout=PIPE,
  62.     )
  63.     if proc.returncode == 0:
  64.         print(proc.stdout.decode())
  65.     else:
  66.         print(gosrc)
  67.  
  68.  
  69. def gen(desc):
  70.     if desc.startswith("Func"):
  71.         desc = desc[4:]
  72.  
  73.     if desc[0] == "A":
  74.         desc = desc[1:]
  75.  
  76.     gen_gosrc(desc, "func_wrapper.go.jinja2")
  77.     gen_gosrc(desc, "func_wrapper_test.go.jinja2")
  78.  
  79.  
  80. gen("FuncASRYE")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top