Skip to content

Instantly share code, notes, and snippets.

@syshack
Created January 17, 2012 03:21
Show Gist options
  • Save syshack/1624381 to your computer and use it in GitHub Desktop.
Save syshack/1624381 to your computer and use it in GitHub Desktop.
config file parse
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
########################################################################
class cnfparse:
"""config file parse
Usage:cnfparse [path_to_cnf-file]"""
#----------------------------------------------------------------------
def __init__(self,cnf_file):
self.items ={}
for line in file(cnf_file):
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue
key,value = line.split('=',1)
self.items[key.strip()] = value.strip()
def getcnf(self):
return self.items
if __name__ == "__main__":
print "Self Testing"
#Generate the test file
if len(sys.argv) == 1:
os.system("echo Nickname=syshack >test.cnf")
os.system("echo #[email protected] >>test.cnf")
os.system("echo "">>test.cnf")
os.system("echo time.today=2012.1.17 >>test.cnf")
pf = cnfparse("test.cnf")
else:
pf = cnfparse(sys.argv[1])
print pf.getcnf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment