From cfa401046d2ee1872bfbd16851f010abd837b82e Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Tue, 1 Apr 2014 18:35:31 -0400 Subject: stuff --- ast2json/imachine2json.py | 19 +++++++++++++++++++ ast2json/rpyc2json.py | 5 ++++- ast2json/strings2json.py | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100755 ast2json/imachine2json.py create mode 100755 ast2json/strings2json.py (limited to 'ast2json') diff --git a/ast2json/imachine2json.py b/ast2json/imachine2json.py new file mode 100755 index 0000000..db1878c --- /dev/null +++ b/ast2json/imachine2json.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +import json +import sys + +def imachine2json(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 or label['hide']: + raise NotImplementedError() + ret[label['name']] = label + return ret + +with open(sys.argv[1], 'r') as f: + output = imachine2json(json.load(f)) + +json.dump(output, open(sys.argv[2], 'w'), separators=(',', ':')) diff --git a/ast2json/rpyc2json.py b/ast2json/rpyc2json.py index 2caf53e..1bf9b73 100755 --- a/ast2json/rpyc2json.py +++ b/ast2json/rpyc2json.py @@ -67,7 +67,10 @@ def get_value(attr_value): if isinstance(attr_value, renpy.ast.Node): return node2json(attr_value) if isinstance(attr_value, renpy.ast.PyCode): - return ast2json.str2json(attr_value.source) + return { + "source": attr_value.source, + "ast": ast2json.str2json(attr_value.source) + } if isinstance(attr_value, renpy.ast.ArgumentInfo): return list(map(lambda x: getattr(attr_value, x), ["arguments", "extrapos", "extrakw"])) if isinstance(attr_value, renpy.atl.RawBlock): diff --git a/ast2json/strings2json.py b/ast2json/strings2json.py new file mode 100755 index 0000000..0736a10 --- /dev/null +++ b/ast2json/strings2json.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import json +import sys + +def strings2json(ast): + ret = {} + if ast[0]['_type'] != 'Init': raise TypeError('obj does not start with Init, wrong file?') + for string in ast[0]['block'][0]['code']['ast']['body'][1:]: + target = string['targets'][0] + if target['_type'] == 'Attribute': + name = string['targets'][0]['attr'] + value = string['value'] + vtype = value['_type'] + if vtype == 'Str': + ret[name] = value['s'] + return ret + +with open(sys.argv[1], 'r') as f: + output = strings2json(json.load(f)) + +json.dump(output, open(sys.argv[2], 'w'), separators=(',', ':')) -- cgit v1.2.3-70-g09d2