Advertisement
Guest User

sgharvey

a guest
Mar 22nd, 2008
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. from ini_regexen_dicts import *
  2. import re
  3.  
  4. class ConfigParser:
  5.    def __init__(self, filename="config.ini"):
  6.       f = file(filename)
  7.       lines = f.readlines()
  8.       f.close()
  9.      
  10.       # Remove the '\n's from the end of each line
  11.       lines = [line[0:line.__len__()-1] for line in lines]
  12.      
  13.       contents = []
  14.       content = {}
  15.       # Compile the regexen
  16.       patterns = {}
  17.       for pattern in pattern_strings:
  18.          patterns.update(pattern: re.compile(pattern_strings[pattern], re.VERBOSE))
  19.      
  20.       # Get the content in each line
  21.       for line in lines:
  22.          for pattern in patterns:
  23.             # Match each pattern to the current line
  24.             match = patterns[pattern].search(line)
  25.             if match:
  26.                content.update({pattern: match.groups()})
  27.             contents += (content.items())
  28.             content = {}
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement