forked from dmitrii-eremin/python-lua
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__main__.py
64 lines (48 loc) · 2.11 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
"""The main entry point to the translator"""
from argparse import ArgumentParser
from pathlib import Path
import sys
from pythonlua.config import Config
from pythonlua.translator import Translator
def create_arg_parser():
"""Create and initialize an argument parser object"""
parser = ArgumentParser(description="Python to lua translator.")
parser.add_argument("inputfilename", metavar="IF", type=str,
help="A python script filename to translate it.",
nargs="?", default="")
parser.add_argument("configfilename", metavar="CONFIG", type=str,
help="Translator configuration file in yaml format.",
nargs="?", default=".pyluaconf.yaml")
parser.add_argument("--show-ast", help="Print python ast tree before code.",
dest="show_ast", action="store_true")
parser.add_argument("--only-lua-init", help="Print only lua initialization code.",
dest="only_lua_init", action="store_true")
parser.add_argument("--no-lua-init", help="Print lua code without lua init code.",
dest="no_lua_init", action="store_true")
return parser
def main():
"""Entry point function to the translator"""
parser = create_arg_parser()
argv = parser.parse_args()
if not argv.no_lua_init and not argv.show_ast:
print(Translator.get_luainit())
if argv.only_lua_init:
return 0
input_filename = argv.inputfilename
if not Path(input_filename).is_file():
raise RuntimeError(
"The given filename ('{}') is not a file.".format(input_filename))
content = None
with open(input_filename, "r") as file:
content = file.read()
if not content:
raise RuntimeError("The input file is empty.")
translator = Translator(Config(argv.configfilename),
show_ast=argv.show_ast)
lua_code = translator.translate(content)
if not argv.only_lua_init and not argv.show_ast:
print(lua_code)
return 0
if __name__ == "__main__":
sys.exit(main())