import os
import re
import csv
import string
import subprocess
class ConfigBuilder(object):
def __init__(self, CSV,basetemplatefile,vrftemplatefile):
self.CSV = CSV
self.CSV_DICT = {}
self.basetemplatefile = basetemplatefile
self.basetemplatecontents = ''
self.vrftemplatefile = vrftemplatefile
self.vrftemplatecontents = ''
# READ IN BASE CONFIG TEMPLATE
with open(self.basetemplatefile) as b:
self.basetemplatecontents = b.read()
self.baseresultstring = self.basetemplatecontents
# READ IN VRF TEMPLATE
with open(self.vrftemplatefile) as v:
self.vrftemplatecontents = v.read()
self.vrfresultstring = ''
# READ IN CSV FILE
with open(self.CSV, mode='r') as csvfile:
Detail_file = csv.reader(csvfile, delimiter=',')
# Cycle through Each ITEM-VALUE pair in the DETAIL FILE
for row in Detail_file:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = row[0]
try:
VALUE = row[1]
except IndexError:
VALUE = ''
print "==== WARNING: ==== " + ITEM + " is missing a comma in the CSV file"
try:
REQUIRED_FIELD = bool(row[2])
except IndexError:
REQUIRED_FIELD = False
if (REQUIRED_FIELD == True and VALUE == ''):
print "\n==== WARNING: ==== " + ITEM + " == IS A REQUIRED FIELD!\n"
# CHECK TO MAKE SURE THE CONFIG_VALUE ISN"T BLANK BEFORE STORING IN DICTIONARY
if VALUE.strip()!='':
self.CSV_DICT[ITEM] = VALUE
def generateBaseConfig(self):
# Cycle through Each ITEM-VALUE pair in the CSV DICTIONARY
for key in self.CSV_DICT:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = key
VALUE = self.CSV_DICT[key]
# CHECK TO MAKE SURE THE CONFIG_VALUE ISN"T BLANK
self.baseresultstring = self.baseresultstring.replace(ITEM,VALUE)
return self.baseresultstring
def generateAgencyVRFs(self):
NumVRFs = self.queryitem('@NUMBER_OF_AGENCY_VRFS')
for x in range(1, int(NumVRFs)+1):
self.vrfresultstring = self.vrfresultstring + self.vrftemplatecontents
# Cycle through Each ITEM-VALUE pair in the CSV DICTIONARY
for key in self.CSV_DICT:
#print key, self.CSV_DICT[key]
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = key
VALUE = self.CSV_DICT[key]
# remove VRF1, VRF2, etc in ITEM key and replace with just VRF so I can use one template for all vrfs
basepattern = "VRF"
iterator = str(x)
fullpattern = basepattern+iterator
regex = re.compile(fullpattern)
ITEM = regex.sub('VRF', ITEM)
#print ITEM, VALUE
self.vrfresultstring = self.vrfresultstring.replace(ITEM,VALUE)
return self.vrfresultstring
def queryitem(self,querystring):
# Cycle through Each ITEM-VALUE pair in the CSV DICTIONARY
for key in self.CSV_DICT:
# DEREFERENCE row ARRAY FOR CLARITY IN READING CODE
ITEM = key
VALUE = self.CSV_DICT[key]
if ITEM == querystring:
queryresult = VALUE
return queryresult
def writeconfig(self,outputfilename,FinalOutput):
if os.path.isfile(outputfilename):
os.remove(outputfilename)
with open(outputfilename + '.txt', 'w') as outputFile:
print '\n==== INFO: ==== WRITING FINAL CONFIG FILE: ' + outputfilename + '.txt'
outputFile.write(FinalOutput)
def fullbuild(self):
baseconfig = self.generateBaseConfig()
vrfconfig = self.generateAgencyVRFs()
outputfilename = self.queryitem('@RTR_NAME_OF_ROUTER')
FinalOutput = baseconfig + vrfconfig
self.writeconfig(outputfilename,FinalOutput)
def OpenOutput(object):
fileName = object.queryitem('@RTR_NAME_OF_ROUTER')+'.txt'
programName = "notepad.exe"
subprocess.Popen([programName, fileName])
def main():
MPLSGen = ConfigBuilder('RouterDetails.csv','BaseMPLSRouter.tmpl','AgencyVRF.tmpl')
MPLSGen.fullbuild()
OpenOutput(MPLSGen)
if __name__ == '__main__':
main()