2 # Stuff for dealing with configuration files.
5 # This code is part of the LWN git data miner.
7 # Copyright 2007-8 LWN.net
8 # Copyright 2007-8 Jonathan Corbet <corbet@lwn.net>
10 # This file may be distributed under the terms of the GNU General
11 # Public License, version 2.
13 import sys
, re
, datetime
17 # Read a line and strip out junk.
19 def ReadConfigLine (file):
20 line
= file.readline ()
23 line
= line
.split('#')[0] # Get rid of any comments
24 line
= line
.strip () # and extra white space
25 if len (line
) == 0: # we got rid of everything
26 return ReadConfigLine (file)
33 sys
.stderr
.write (message
+ '\n')
37 # Read a list of email aliases.
39 def ReadEmailAliases (name
):
41 file = open (name
, 'r')
43 croak ('Unable to open email alias file %s' % (name
))
44 line
= ReadConfigLine (file)
48 croak ('Funky email alias line "%s"' % (line
))
49 if sline
[0].index ('@') <= 0 or sline
[1].index ('@') <= 0:
50 croak ('Non-addresses in email alias "%s"' % (line
))
51 database
.AddEmailAlias (sline
[0], sline
[1])
52 line
= ReadConfigLine (file)
56 # The Email/Employer map
58 EMMpat
= re
.compile (r
'^([^\s]+)\s+([^<]+)\s*(<\s*(\d+-\d+-\d+)\s*)?$')
60 def ReadEmailEmployers (name
):
62 file = open (name
, 'r')
64 croak ('Unable to open email/employer file %s' % (name
))
65 line
= ReadConfigLine (file)
67 m
= EMMpat
.match (line
)
69 croak ('Funky email/employer line "%s"' % (line
))
71 company
= m
.group (2).strip ()
72 enddate
= ParseDate (m
.group (4))
73 database
.AddEmailEmployerMapping (email
, company
, enddate
)
74 line
= ReadConfigLine (file)
77 def ParseDate (cdate
):
80 sdate
= cdate
.split ('-')
81 return datetime
.date (int (sdate
[0]), int (sdate
[1]), int (sdate
[2]))
84 def ReadGroupMap (fname
, employer
):
86 file = open (fname
, 'r')
88 croak ('Unable to open group map file %s' % (fname
))
89 line
= ReadConfigLine (file)
91 database
.AddEmailEmployerMapping (line
, employer
)
92 line
= ReadConfigLine (file)
96 # Read an overall config file.
98 def ConfigFile (name
):
100 file = open (name
, 'r')
102 croak ('Unable to open config file %s' % (name
))
103 line
= ReadConfigLine (file)
105 sline
= line
.split ()
107 croak ('Funky config line: "%s"' % (line
))
108 if sline
[0] == 'EmailAliases':
109 ReadEmailAliases (sline
[1])
110 elif sline
[0] == 'EmailMap':
111 ReadEmailEmployers (sline
[1])
112 elif sline
[0] == 'GroupMap':
114 croak ('Funky group map line "%s"' % (line
))
115 ReadGroupMap (sline
[1], sline
[2])
117 croak ('Unrecognized config line: "%s"' % (line
))
118 line
= ReadConfigLine (file)