Created
April 23, 2013 06:55
-
-
Save tangentstorm/5441370 to your computer and use it in GitHub Desktop.
Ugly python + xpath code to translate the UML digaram for gamesketchlib to pascal. see http://i.imgur.com/LZtWxH7.png for the diagram.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
IGridMember = interface | |
end; | |
IInteractive = interface | |
procedure click( mx, my : int ); | |
procedure press( mx, my : int ); | |
procedure drag( mx, my : int ); | |
end; | |
IPlayable = interface | |
procedure render; | |
procedure update; | |
end; | |
ISpatial = interface | |
function containsPoint( x, y : int ): boolean; | |
function overlaps( other : ISpatial ): boolean; | |
function surrounds( other : ISpatial ): boolean; | |
procedure onOverlap( other : ISpatial ); | |
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
generate pascal code from visual paradigm community edition xml export | |
""" | |
from lxml import etree | |
repo = etree.parse(open('/home/michal/tmp/gsl/project.xml')) | |
classTags = repo.xpath('/Project/Models//ModelChildren/Class') | |
pathTo = lambda node : '/'.join(n.tag for n in reversed(list(node.iterancestors()))) | |
package = lambda node : node.xpath('../../@Name')[0] | |
stereos = lambda node : ' '.join(node.xpath('.//Stereotype/@Name')) | |
enlist = lambda f : lambda *a, **kw : list(f(*a,**kw)) | |
@enlist | |
def methods(node): | |
""" | |
:: ( meth:str, params:[ ( name:str, type:str )* ], rtype:str? ) | |
""" | |
return (( op.xpath('@Name')[0], | |
[( param.xpath('@Name')[0], | |
''.join(param.xpath('@Type') + | |
param.xpath('.//Type//@Name'))) | |
for param in op.xpath('.//Parameter')], | |
op.xpath('./ReturnType/DataType/@Name')) | |
for op in node.xpath('.//Operation')) | |
print 'type' | |
for e in classTags: | |
if not stereos(e) : continue | |
if package(e) != 'GameSketchLib' : continue | |
print ' '.join([' ', e.get('Name'), '=', stereos(e).lower() or 'class']) | |
for meth, args, ret in methods(e): | |
keywd = 'function' if ret else 'procedure' | |
if args: | |
takes = '( %s )' \ | |
% ', '.join(n + (' : ' + t if t else '') for (n,t) in args) | |
else: takes = '' | |
gives = ': {0}'.format(ret[0]) if ret else '' | |
print " {keywd} {meth}{takes}{gives};".format(**locals()) | |
print ' end;' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment