Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The Itsy Bitsey Config Parser
- import glob
- import os
- def parse(fname, extension, useFname, combineResults=False):
- # parses a config file, returns the result as a dict
- # if useFname is true, it will parse any file with the name
- # fname, and will ignore file extensions
- # if useFname is false, it will ignore the name of each file
- # and parse all files with a matching extension
- ### syntax ###
- # sect - begins a section
- # endsect - ends a section
- # [foo]=[bar] sets foo equal to bar
- ### example of syntax ###
- # file foo.txt
- """
- sect example
- str,cheese=yes
- spinach=nope!
- int,lbsOfCheese=417
- endsect
- float,uncatagorizedThing=42
- """
- # this will return a dict like this:
- # {name:"foo", sects:{example:{cheese:"yes", spinach:"nope!", lbsOfCheese:417}},
- # nosect:{uncatagorizedThing:42.0}}
- # combine results will cause the program to return all of the results form all files in a single
- # dict if set to true, otherwise, it will return a dict where each key corrosponds to the results of each file
- fList = [] # list of all files to be parsed
- retVal = {} # setup return value
- # decide how glob should get files
- if useFname == True:
- globString = fname + ".*"
- elif useFname == False:
- globString = "*." + extension
- else:
- print "ERROR: IBCP encountered an unhandled useFname argument: ", useFname
- return "ERROR"
- # compile list of files to parse
- for files in glob.glob(globString):
- fList.append(files)
- if combineResults == True:
- for files in fList:
- f = open(files, 'r')
- foo = doParse(f) # I know this is bad practice, IDGAF
- for key in foo:
- if key not in retVal:
- retVal[key] = {}
- retVal[key] = dict(retVal[key].items() + foo[key].items())
- return retVal
- else:
- for files in fList:
- f = open(files, 'r')
- foo = doParse(f)
- thisKey = files.split('.')[0]
- if thisKey not in retVal:
- retVal[thisKey] = {}
- for key in foo:
- if key not in retVal[thisKey]:
- retVal[thisKey][key] = {}
- retVal[thisKey][key] = dict(retVal[thisKey][key].items() + foo[key].items())
- return retVal
- def doParse(f):
- # doParse actually does the parsing, and is spun off for modularity
- # f should actuall be the contents of the file
- # doParse shouldbe called like this:
- # f=open('something','flag')
- # doParse(f)
- inSect = False # currently parsing a section?
- thisSect = '' # name of the current section
- retVal = {} # value to be returned
- retVal['nosect'] = {} # add a value for sectionless items
- for line in f:
- line = line.rstrip('\n')
- if line != '':
- if line.split(' ')[0] == 'sect':
- inSect = True
- thisSect = line.split(' ')[1]
- retVal[thisSect] = {}
- elif line == 'endsect':
- inSect = False
- thisSect = ''
- else:
- # store the value (line[1]) at the key (line[0])
- line = lineParser(line)
- if inSect == True:
- retVal[thisSect][line[0]]=line[1]
- else:
- retVal['nosect'][line[0]]=line[1]
- return retVal
- def lineParser(line):
- # parses an individual line
- if line.split(',')[0] == 'int':
- line = line.split(',')[1] # remove the type declaration
- return [line.split('=')[0],int(line.split('=')[1])]
- elif line.split(',')[0] == 'float':
- line = line.split(',')[1] # remove the type declaration
- return [line.split('=')[0],float(line.split('=')[1])]
- elif line.split(',')[0] == 'str':
- line = line.split(',')[1] # remove the type declaration
- return [line.split('=')[0],line.split('=')[1]]
- else:
- return [line.split('=')[0],line.split('=')[1]]
Advertisement
Add Comment
Please, Sign In to add comment