[Ironpython-users] Manipulating/Reversing the Ironpython AST

Jay Riley super24bitsound at hotmail.com
Fri Oct 28 17:38:47 CEST 2011


Hi all,
 
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 * 
 
import copy 
 
class ScriptedItem(Item): 
    def __init__(self, name, description, itemtypes, primarytype, flags, usability, value, throwpower): 
        Item.__init__(self, name, description, itemtypes, primarytype, flags, usability, value, throwpower, Item.GetNextItemID()) 
    def Clone(self): 
        return copy.deepcopy(self) 
 
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:
 

Find and list all the class types (in this example, it'd be only Scripted Item)
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
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,
 
Thanks 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20111028/fbaf3666/attachment.html>


More information about the Ironpython-users mailing list