-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEudoraLog.py
92 lines (73 loc) · 2.14 KB
/
EudoraLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
# Verbosity.
# Determines if subroutines {log,warn,err}_msg send output to stdout, too:
#
# verbose = -1 # ultra quiet: not even the mailbox's message total
# verbose = 0 # really quiet
# verbose = 1 # errors only
# verbose = 2 # warnings and errors only
# verbose = 3 # logging, warnings and errors
#
verbose = 2
log = None
msg_no = 0
line_no = 0
LOG_SFX = '.E2U_LOG'
ERR_SFX = '.E2U_ERR'
WARN_SFX = '.E2U_WARN'
def fatal(msg):
if msg and len( msg ) > 0:
print >> sys.stderr, msg
return 1
# SW
class Log:
"""A log dedicated to a specific Eudora2Mbox mail box that we
are converting. Records messages in it (depending on
verbosity, also prints on stdout), summarizes messages
recorded, and closes file."""
total_msgs = 0
exit_code = 0
def __init__(self, mbx):
self.mbxname = mbx
self.log_msgs = 0
self.warn_msgs = 0
self.error_msgs = 0
def record(self, filename, msg, verbosity):
global P, verbose
msg += os.linesep
out = self.mbxname + ' (msg #' + `msg_no` + ', line #' \
+ `line_no` + '):' + os.linesep + msg
if verbose >= verbosity:
print out
try:
OUT = open( filename, 'a' )
OUT.write( out + os.linesep )
OUT.flush()
except IOError, ( errno, strerror ):
return fatal( P + ': cannot open "'
+ filename + '"' + ": " + strerror )
finally:
if OUT:
OUT.close()
Log.total_msgs += 1
def _summary(self, n_msgs, logtype):
if n_msgs == 0: return 'no ' + logtype + ' messages'
if n_msgs == 1: return '1 ' + logtype + ' message'
if n_msgs >= 1: return str(n_msgs) + ' ' + logtype + ' messages'
def summary(self):
return self._summary(self.log_msgs, 'log') + os.linesep + \
self._summary(self.warn_msgs, 'warning') + os.linesep + \
self._summary(self.error_msgs, 'error') + os.linesep
def log(self, msg):
self.record(self.mbxname + LOG_SFX, msg, 3)
self.log_msgs += 1
def warn(self, msg):
self.record(self.mbxname + WARN_SFX, msg, 2)
self.warn_msgs += 1
Log.exit_code = 1
def error(self, msg):
self.record(self.mbxname + ERR_SFX, msg, 1)
self.error_msgs += 1
Log.exit_code = 1
def mbx_name(self):
return self.mbxname