generate.py revision 226031
11558Srgrimes#!/usr/local/bin/python
21558Srgrimes# -*- coding: iso-8859-1 -*-
31558Srgrimes
41558Srgrimes# $Id$
51558Srgrimes
61558Srgrimes# Copyright (c) 2004 Kungliga Tekniska H��gskolan
71558Srgrimes# (Royal Institute of Technology, Stockholm, Sweden).
81558Srgrimes# All rights reserved.
91558Srgrimes#
101558Srgrimes# Redistribution and use in source and binary forms, with or without
111558Srgrimes# modification, are permitted provided that the following conditions
121558Srgrimes# are met:
131558Srgrimes#
141558Srgrimes# 1. Redistributions of source code must retain the above copyright
151558Srgrimes#    notice, this list of conditions and the following disclaimer.
161558Srgrimes#
171558Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
181558Srgrimes#    notice, this list of conditions and the following disclaimer in the
191558Srgrimes#    documentation and/or other materials provided with the distribution.
201558Srgrimes#
211558Srgrimes# 3. Neither the name of the Institute nor the names of its contributors
221558Srgrimes#    may be used to endorse or promote products derived from this software
231558Srgrimes#    without specific prior written permission.
241558Srgrimes#
251558Srgrimes# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
261558Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271558Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281558Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
291558Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301558Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311558Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321558Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331558Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341558Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351558Srgrimes# SUCH DAMAGE.
361558Srgrimes
371558Srgrimesimport datetime
381558Srgrimesimport string
391558Srgrimesimport os
401558Srgrimes
411558Srgrimesclass GeneratedFile :
421558Srgrimes    "Represents a generated file"
431558Srgrimes    def __init__(self, name) :
441558Srgrimes        "Create a new GeneratedFile with name"
451558Srgrimes        self.name  = os.path.basename(name)
461558Srgrimes        self.file  = open(name, 'w')
471558Srgrimes        self.file.write('/* ' + name + ' */\n')
481558Srgrimes        self.file.write('/* Automatically generated at ' +
491558Srgrimes                        datetime.datetime.now().isoformat() +
501558Srgrimes                        ' */\n\n')
511558Srgrimes
521558Srgrimes    def close(self) :
531558Srgrimes        """End and close the file header"""
541558Srgrimes        self.file.close()
551558Srgrimes
561558Srgrimes
571558Srgrimesclass Header(GeneratedFile) :
581558Srgrimes    "Represents a generated header file"
591558Srgrimes    guardTrans = string.maketrans('-.', '__')
601558Srgrimes    def makeGuard(self) :
611558Srgrimes        """Return a name to be used as ifdef guard"""
621558Srgrimes        return string.upper(string.translate(self.name, self.guardTrans))
631558Srgrimes
641558Srgrimes    def __init__(self, name) :
651558Srgrimes        "Create a new Header with name"
661558Srgrimes        GeneratedFile.__init__(self, name)
671558Srgrimes        self.guard = self.makeGuard()
681558Srgrimes        self.file.write('#ifndef ' + self.guard + '\n')
691558Srgrimes        self.file.write('#define ' + self.guard + ' 1\n')
701558Srgrimes
711558Srgrimes    def close(self) :
721558Srgrimes        """End and close the file header"""
731558Srgrimes        self.file.write('#endif /* ' + self.guard + ' */\n')
741558Srgrimes        GeneratedFile.close(self)
751558Srgrimes
761558Srgrimes
771558Srgrimesclass Implementation(GeneratedFile) :
781558Srgrimes    "Represents a generated implementation file"
791558Srgrimes    def __init__(self, name) :
801558Srgrimes        "Create a new Implementation with name"
811558Srgrimes        GeneratedFile.__init__(self, name)
821558Srgrimes