Monday, October 16, 2006

MenuTest3 ( Python )

Messy code here.
This is my first 'class' in Python.

The output is the same as MenuTest1 + MenuTest2.


%python test3.py ( input_file ) ( output_file )

(example)
%python test3.py test.csv test.h



%cat test3.py

#! /usr/bin/env python

import re
import os, sys

input_file = sys.argv[ 1 ]
output_file = sys.argv[ 2 ]

class MenuConverter:
def setInputFile( self, fileName ):
self.readFrom = fileName

def setOutputFile( self, fileName ):
self.writeTo = fileName

def writeStrToFile( self, str ):
output = sys.stdout
output = open( self.writeTo, 'w' )
output.write( str )

def setMenuId( self ):
buf = ""
pre = """enum MenuId {"""
post = """TOTALIDS \n};\n\n"""

file = open( self.readFrom )
text = file.read()

buf = re.sub( "[^\w]+", ",\n\tmnu", text )
buf = pre + buf[ 1: ] + post
return buf

def setExeId( self ):
pre = """enum MenuExeId\n{\n\tmnux"""
post = """};\n\n"""
buf = ""
result = []

for line in open( self.readFrom ).readlines():
line = line.replace( '"', "" ).split( "," )
result = result + line[ -1: ]
buf = ''.join( result )
buf = pre + re.sub( "\n(?=.\w)", ",\n\tmnux", buf ) + post
return buf

x = MenuConverter()
x.setInputFile( input_file )
x.setOutputFile( output_file )
x.writeStrToFile( x.setMenuId() + x.setExeId() )

No comments: