File: generator.py

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (109 lines) | stat: -rwxr-xr-x 3,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
#
#	Copyright 2010  Andreas Löscher
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#	@author Andreas Löscher
#	@author Matthias Ableitner <spam@abma.de>
#

import sys
import os

from callback_parser import getcallback_functions
from event_parser import getevents, getcommands, parse_enums
from interface_builder import buildclasses, commandfuncs

from helper import converter

from template import Template


PATH = os.path.join("ExternalAI","Interface")

CALLBACKFILE = os.path.join(PATH,"SSkirmishAICallback.h")
EVENTFILE = os.path.join(PATH,"AISEvents.h")
COMMANDFILE = os.path.join(PATH, "AISCommands.h")

TEMPLATEDIR = None

class Generator(object):
  def __init__(self, templatedir, springdir, outputdir, options=()):
    self.templatedir=templatedir
    self.springdir=springdir
    self.outputdir=outputdir
    
    files = [CALLBACKFILE, EVENTFILE, COMMANDFILE]
    for i, f in enumerate(files):
      files[i]=os.path.join(springdir,f)

    self.clbfuncs, plainfuncs = getcallback_functions(files[0])
    self.classes= buildclasses(plainfuncs)
    self.events = getevents(files[1])
    self.commands = getcommands(files[2])
    self.command_types = parse_enums(files[2])
    self.event_types = parse_enums(files[1])
    self.commandfuncs = commandfuncs(self.commands)
    
    
    self.options = set(options)
    
  def render(self):
    # render wrapper for handleEvent and callback functions
    out = os.path.join(self.outputdir, "ai.c")
    tempfile = os.path.join(self.templatedir, "wrappai.c")
    template = open(tempfile, "r")
    
    
    stream = Template(template.read()).render({'clbfuncs'    : self.clbfuncs,
                                               'events'      : self.events,
                                               'commands'    : self.commands,
                                               'templatedir' : self.templatedir,
                                               'converter'   : converter,
                                               'options'     : self.options
                                              })
    template.close()
    outfile = open(out, "w")
    outfile.write(stream)
    outfile.close()
    
    # render interface
    out = os.path.join(self.outputdir, "PyAI", "interface.py")
    tempfile = os.path.join(self.templatedir, "aiInterface.py")
    template = open(tempfile, "r")
    
    stream = Template(template.read()).render({'classes'     : self.classes,
                                               'commands'    : self.commandfuncs,
                                               'cmd_types'   : self.command_types,
                                               'evt_types'   : self.event_types,
                                               'options'     : self.options
                                              })
    outfile = open(out, "w")
    outfile.write(stream)
    outfile.close()

if __name__=='__main__':
  if (len(sys.argv) != 4):
    print 'Usage: '+sys.argv[0]+' springdir templatedir outputdir '
    sys.exit(-1)
  springdir=os.path.expanduser(sys.argv[1]+'/rts')
  templatedir=os.path.expanduser(sys.argv[2])
  outputdir=os.path.expanduser(sys.argv[3])
  fullpath=outputdir+"/PyAI"
  if (not os.path.exists(fullpath)):
    os.makedirs(fullpath)
  print "Generating Sources..."
  Generator(templatedir,springdir,outputdir).render()
  print "done"