Skip to content
Browse files

Revert "Make ReadConfigLine an iterator"

This reverts commit 19b718e which breaks
the virtual employer mechanism.
  • Loading branch information...
1 parent 47ffed3 commit 954b559f131a1a801c135463eefbd12d5303eaa2 Jonathan Corbet committed Jul 11, 2011
Showing with 41 additions and 72 deletions.
  1. +41 −72 ConfigFile.py
View
113 ConfigFile.py
@@ -13,42 +13,18 @@
import sys, re, datetime, os.path
import database
-class ReadConfigLine:
- """
- ReadConfigLine provides a iterator to extract line
- from an config file without comments.
-
- Typical use case:
-
- fd = open(filename, 'r')
- for line in ReadConfigLine(fd):
- parse_line(line)
- fd.close(fd)
- """
-
- def __init__(self, fd):
- self.fd = fd
- self.buffer = None
- self.patch = []
-
- def __iter__(self):
- return self
-
- def next(self):
- line = self.fd.readline()
- while line:
- line = line.split('#')[0] # Get rid of any comments
- line = line.strip() # and extra white space
- if len(line) == 0: # we got rid of everything
- line = self.fd.readline()
- else:
- break
-
- if not line:
- raise StopIteration
-
- return line
-
+#
+# Read a line and strip out junk.
+#
+def ReadConfigLine (file):
+ line = file.readline ()
+ if not line:
+ return None
+ line = line.split('#')[0] # Get rid of any comments
+ line = line.strip () # and extra white space
+ if len (line) == 0: # we got rid of everything
+ return ReadConfigLine (file)
+ return line
#
# Give up and die.
@@ -62,19 +38,19 @@ def croak (message):
#
def ReadEmailAliases (name):
try:
- fd = open (name, 'r')
+ file = open (name, 'r')
except IOError:
croak ('Unable to open email alias file %s' % (name))
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
m = re.match ('^("[^"]+"|\S+)\s+(.+)$', line)
if not m or len (m.groups ()) != 2:
croak ('Funky email alias line "%s"' % (line))
if m and m.group (2).find ('@') <= 0:
croak ('Non-addresses in email alias "%s"' % (line))
database.AddEmailAlias (m.group (1).replace ('"', ''), m.group (2))
-
- fd.close ()
+ line = ReadConfigLine (file)
+ file.close ()
#
# The Email/Employer map
@@ -83,20 +59,20 @@ def ReadEmailAliases (name):
def ReadEmailEmployers (name):
try:
- fd = open (name, 'r')
+ file = open (name, 'r')
except IOError:
croak ('Unable to open email/employer file %s' % (name))
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
m = EMMpat.match (line)
if not m:
croak ('Funky email/employer line "%s"' % (line))
email = m.group (1)
company = m.group (2).strip ()
enddate = ParseDate (m.group (4))
database.AddEmailEmployerMapping (email, company, enddate)
-
- fd.close ()
+ line = ReadConfigLine (file)
+ file.close ()
def ParseDate (cdate):
if not cdate:
@@ -107,22 +83,22 @@ def ParseDate (cdate):
def ReadGroupMap (fname, employer):
try:
- fd = open (fname, 'r')
+ file = open (fname, 'r')
except IOError:
croak ('Unable to open group map file %s' % (fname))
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
database.AddEmailEmployerMapping (line, employer)
-
- fd.close ()
+ line = ReadConfigLine (file)
+ file.close ()
#
# Read in a virtual employer description.
#
-def ReadVirtual (fd, name):
+def ReadVirtual (file, name):
ve = database.VirtualEmployer (name)
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
sl = line.split (None, 1)
first = sl[0]
if first == 'end':
@@ -140,6 +116,7 @@ def ReadVirtual (fd, name):
if not (0 < percent <= 100):
croak ('Bad split value "%s" for virtual empl %s' % (first, name))
ve.addsplit (' '.join (sl[1:]), percent/100.0)
+ line = ReadConfigLine (file)
#
# We should never get here
#
@@ -150,20 +127,21 @@ def ReadVirtual (fd, name):
#
def ReadFileType (filename):
try:
- fd = open (filename, 'r')
+ file = open (filename, 'r')
except IOError:
croak ('Unable to open file type mapping file %s' % (filename))
patterns = {}
order = []
regex_order = re.compile ('^order\s+(.*)$')
regex_file_type = re.compile ('^filetype\s+(\S+)\s+(.+)$')
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
o = regex_order.match (line)
if o:
# Consider only the first definition in the config file
elements = o.group(1).replace (' ', '')
order = order or elements.split(',')
+ line = ReadConfigLine (file)
continue
m = regex_file_type.match (line)
@@ -177,7 +155,8 @@ def ReadFileType (filename):
patterns[m.group (1)].append (re.compile (m.group (2), re.IGNORECASE))
- fd.close ()
+ line = ReadConfigLine (file)
+ file.close ()
return patterns, order
#
@@ -186,11 +165,11 @@ def ReadFileType (filename):
def ConfigFile (name, confdir):
try:
- fd = open (name, 'r')
+ file = open (name, 'r')
except IOError:
croak ('Unable to open config file %s' % (name))
-
- for line in ReadConfigLine (fd):
+ line = ReadConfigLine (file)
+ while line:
sline = line.split (None, 2)
if len (sline) < 2:
croak ('Funky config line: "%s"' % (line))
@@ -209,15 +188,5 @@ def ConfigFile (name, confdir):
database.FileTypes = database.FileType (patterns, order)
else:
croak ('Unrecognized config line: "%s"' % (line))
-
-
-if __name__ == '__main__':
- '''Test the iterato for reading configuration files'''
- try:
- fd = open(sys.argv[1])
- except:
- croak('Usage: %s <config-file>' % sys.argv[0])
-
- for line in ReadConfigLine(fd):
- print line
+ line = ReadConfigLine (file)

0 comments on commit 954b559

Please sign in to comment.
Something went wrong with that request. Please try again.