summaryrefslogtreecommitdiff
path: root/ast2json
diff options
context:
space:
mode:
Diffstat (limited to 'ast2json')
-rw-r--r--ast2json/ast2json.py6
-rwxr-xr-xast2json/rpyc2json.py8
-rwxr-xr-xast2json/settings2json.py22
3 files changed, 23 insertions, 13 deletions
diff --git a/ast2json/ast2json.py b/ast2json/ast2json.py
index 3460969..23b6f04 100644
--- a/ast2json/ast2json.py
+++ b/ast2json/ast2json.py
@@ -25,7 +25,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from _ast import AST
-from ast import parse
+from ast import parse, PyCF_ONLY_AST
def ast2json(node):
@@ -40,8 +40,8 @@ def ast2json(node):
return to_return
-def str2json(string):
- return ast2json(parse(string))
+def str2json(string, mode='exec'):
+ return ast2json(compile(string, '<unknown>', mode, PyCF_ONLY_AST))
def get_value(attr_value):
diff --git a/ast2json/rpyc2json.py b/ast2json/rpyc2json.py
index 7c8518b..ec4f4b8 100755
--- a/ast2json/rpyc2json.py
+++ b/ast2json/rpyc2json.py
@@ -66,16 +66,20 @@ def get_value(attr_value):
return attr_value
if isinstance(attr_value, (int, str, float, complex, bool)):
return attr_value
- if isinstance(attr_value, list) or isinstance(attr_value, tuple):
+ if isinstance(attr_value, (list, tuple)):
return [get_value(x) for x in attr_value]
if isinstance(attr_value, dict):
return attr_value
if isinstance(attr_value, renpy.ast.Node):
return node2json(attr_value)
+ if isinstance(attr_value, (renpy.ast.Python, renpy.ast.EarlyPython)):
+ attr_value.code.mode = 'exec'
+ if isinstance(attr_value, (renpy.ast.Image, renpy.ast.Define)):
+ attr_value.code.mode = 'eval'
if isinstance(attr_value, renpy.ast.PyCode):
return {
"source": attr_value.source,
- "ast": ast2json.str2json(attr_value.source)
+ "ast": ast2json.str2json(attr_value.source, attr_value.mode)
}
if isinstance(attr_value, renpy.ast.ArgumentInfo):
return list(map(lambda x: getattr(attr_value, x), ["arguments", "extrapos", "extrakw"]))
diff --git a/ast2json/settings2json.py b/ast2json/settings2json.py
index a72239d..1e41e2e 100755
--- a/ast2json/settings2json.py
+++ b/ast2json/settings2json.py
@@ -1,16 +1,22 @@
#!/usr/bin/env python3
+import ast
import json
import sys
-def settings2json(ast):
- ret = {}
- if ast[0]['_type'] != 'Label':
- raise TypeError('obj does not start with Label, wrong file?')
- for label in ast:
- if label['parameters'] is not None:
- raise NotImplementedError()
- ret[label['name']] = label['block']
+def settings2json(the_ast):
+ ret = {'images': {}}
+ if the_ast[0]['_type'] != the_ast[3]['_type']:
+ raise TypeError('validation failed, wrong file?')
+ for node in the_ast[1]['block']:
+ if node['_type'] == 'Image':
+ r = node['code']
+ if r: # not atl
+ try:
+ r['eval'] = ast.literal_eval(r['source'])
+ except ValueError:
+ pass
+ ret['images'][' '.join(node['imgname'])] = r
return ret
with open(sys.argv[1], 'r') as f: