[Ironpython-users] Manipulating/Reversing the Ironpython AST
rome at Wintellect.com
Fri Oct 28 18:31:27 CEST 2011
If that is the entirety of your solution's requirements, then if I were in your shoes I would just cheat...
I presume ItemModule is a CLR namespace, sourced from an assembly you have loaded via sys.AddReference (or similar technique). In that case, I would create a mock version of that assembly that your tool uses. It would define all of the same interfaces, classes, enums, etc that your script is using from the real one; however the ItemLibrary.AddItem() method would just capture the incoming values for the tool to work with.
The tool would simply host IronPython, load your mocked assembly instead of the real one, create a new ScriptScope, and run the script within it while capturing those incoming values. This would give the tool data to work with, I assume displaying it in some friendly UI format. After making some changes using the UI tool, it should have no problem writing out a new python file that includes the modified parameters / items.
Hack? Yes. But far simpler than dealing with AST and generic code emit.
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | krome at wintellect.com<mailto:rome at wintellect.com>
From: ironpython-users-bounces+rome=wintellect.com at python.org [mailto:ironpython-users-bounces+rome=wintellect.com at python.org] On Behalf Of Jay Riley
Sent: Friday, October 28, 2011 11:39 AM
To: ironpython-users at python.org
Subject: [Ironpython-users] Manipulating/Reversing the Ironpython AST
I was wondering if IronPython has the ability to reverse/unparse the AST back into a ource file? Or if someone has perhaps built something to do so?
Here's what I'm trying to do:
I have a game I'm working on, and I'm currently drafting up tools for it. The tools are being written in C# and are meant to make changing and editing game files easier. Several of the game files are written in python and are used to extend objects from the main game source. For instace, I have an Items.py file that contains the following (minimalized for example):
from ItemModule import *
def __init__(self, name, description, itemtypes, primarytype, flags, usability, value, throwpower):
Item.__init__(self, name, description, itemtypes, primarytype, flags, usability, value, throwpower, Item.GetNextItemID())
ItemLibrary.AddItem(ScriptedItem("Abounding Crystal", "A colourful crystal composed of many smaller crystals. It gives off a warm glow.", ItemType.SynthesisMaterial, ItemType.SynthesisMaterial, 0, ItemUsage.Unusable, 0, 50))
In this case, I'd like to provide a convenient front-end to allow editors to modify/add/delete items from the list. To do this, my editor need to be able to:
1. Find and list all the class types (in this example, it'd be only Scripted Item)
2. Find and list all created items (in this case there'd only be one, Abounding Crystal). I'd need to find the type (in this caseScriptedItem) and all the parameter values
3. Allow editing of parameters and the creation/removal of items.
I tried writing my own parser, but that became increasingly difficult as the complexity of the Item constructors went up. When I found IronPython and its ability to generate a walkable AST, I thought I'd found my solution, and indeed the AST makes it easy to accomplish 1 and 2 of my requirements. However, I'm currently stuck on how to write back changes made in my editor to the source file. My initial idea was to preserve the AST and modify values on the existing nodes for edited items and inject new nodes when new items were created. However even if I could get this to work correctly, I have no idea how to reconvert the AST back into a source file. When I asked on stackoverflow, I was told this is usually done using "prettyprinting" and had some suggestions to use python's "inspect" property.
I'm not sure how to use inspect to do what I want, and have some concerns over the amount of effort require to get "prettyprinting" correct, so i wanted to ask if anyone here has written a prettyprinter for ironpython or perhaps knows some other way to accomplish my three goals? Any help would be appreciated,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Ironpython-users