Skip to content

Commit 93fd913

Browse files
Added translator config file (.pyluaconf.yaml)
1 parent ba78e34 commit 93fd913

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# User-defined
22
.vscode/
3+
.pyluaconf.yaml
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/

__main__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
import sys
66

7+
from pythonlua.config import Config
78
from pythonlua.translator import Translator
89

910

@@ -13,6 +14,9 @@ def create_arg_parser():
1314
parser.add_argument("inputfilename", metavar="IF", type=str,
1415
help="A python script filename to translate it.",
1516
nargs="?", default="")
17+
parser.add_argument("configfilename", metavar="CONFIG", type=str,
18+
help="Translator configuration file in yaml format.",
19+
nargs="?", default=".pyluaconf.yaml")
1620

1721
parser.add_argument("--show-ast", help="Print python ast tree before code.",
1822
dest="show_ast", action="store_true")
@@ -47,8 +51,9 @@ def main():
4751
if not content:
4852
raise RuntimeError("The input file is empty.")
4953

50-
translator = Translator(show_ast=argv.show_ast)
51-
lua_code = translator.translate(content)
54+
translator = Translator(Config(argv.configfilename),
55+
show_ast=argv.show_ast)
56+
lua_code = translator.translate(content)
5257

5358
if not argv.only_lua_init:
5459
print(lua_code)

pythonlua/config.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
"""Python to lua translator config class"""
2-
2+
import sys
3+
import yaml
34

45
class Config:
56
"""Translator config."""
6-
def __init__(self):
7-
pass
7+
def __init__(self, filename=None):
8+
self.data = {
9+
"class": {
10+
"return_at_the_end": False,
11+
},
12+
}
13+
14+
if filename is not None:
15+
self.load(filename)
16+
17+
def load(self, filename):
18+
"""Load config from the file"""
19+
try:
20+
with open(filename, "r") as stream:
21+
data = yaml.load(stream)
22+
self.data.update(data)
23+
except FileNotFoundError:
24+
pass # Use a default config if the file not found
25+
except yaml.YAMLError as ex:
26+
print(ex)
27+
28+
def __getitem__(self, key):
29+
"""Get data values"""
30+
return self.data[key]

pythonlua/nodevisitor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class NodeVisitor(ast.NodeVisitor):
1616
LUACODE = "[[luacode]]"
1717

1818
"""Node visitor"""
19-
def __init__(self, context=None):
19+
def __init__(self, context=None, config=None):
2020
self.context = context if context is not None else Context()
21+
self.config = config
2122
self.last_end_mode = TokenEndMode.LINE_FEED
2223
self.output = []
2324

@@ -135,6 +136,11 @@ def visit_ClassDef(self, node):
135136

136137
self.emit("end, {{{}}})".format(", ".join(bases)))
137138

139+
# Return class object only in the top-level classes.
140+
# Not in the nested classes.
141+
if self.config["class"]["return_at_the_end"] and not last_ctx["class_name"]:
142+
self.emit("return {}".format(name))
143+
138144
def visit_Compare(self, node):
139145
"""Visit compare"""
140146

@@ -556,7 +562,7 @@ def visit_all(self, nodes, inline=False):
556562
last_ctx = self.context.last()
557563
last_ctx["locals"].push()
558564

559-
visitor = NodeVisitor(self.context)
565+
visitor = NodeVisitor(context=self.context, config=self.config)
560566

561567
if isinstance(nodes, list):
562568
for node in nodes:

pythonlua/translator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Translator:
1010
"""Python to lua main class translator"""
1111
def __init__(self, config=None, show_ast=False):
12-
config = config if config is not None else Config()
12+
self.config = config if config is not None else Config()
1313
self.show_ast = show_ast
1414

1515
self.output = []
@@ -18,7 +18,7 @@ def translate(self, pycode):
1818
"""Translate python code to lua code"""
1919
py_ast_tree = ast.parse(pycode)
2020

21-
visitor = NodeVisitor()
21+
visitor = NodeVisitor(config=self.config)
2222

2323
if self.show_ast:
2424
print(ast.dump(py_ast_tree))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
colorama==0.3.7
2+
PyYAML==3.12

0 commit comments

Comments
 (0)