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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module for reading and parsing event-log-tags files."""
import re
import sys
class Tag(object):
__slots__ = ["tagnum", "tagname", "description", "filename", "linenum"]
def __init__(self, tagnum, tagname, description, filename, linenum):
self.tagnum = tagnum
self.tagname = tagname
self.description = description
self.filename = filename
self.linenum = linenum
class TagFile(object):
"""Read an input event-log-tags file."""
def AddError(self, msg, linenum=None):
if linenum is None:
linenum = self.linenum
self.errors.append((self.filename, linenum, msg))
def AddWarning(self, msg, linenum=None):
if linenum is None:
linenum = self.linenum
self.warnings.append((self.filename, linenum, msg))
def __init__(self, filename, file_object=None):
"""'filename' is the name of the file (included in any error
messages). If 'file_object' is None, 'filename' will be opened
for reading."""
self.errors = []
self.warnings = []
self.tags = []
self.options = {}
self.filename = filename
self.linenum = 0
if file_object is None:
try:
file_object = open(filename, "rb")
except (IOError, OSError), e:
self.AddError(str(e))
return
try:
for self.linenum, line in enumerate(file_object):
self.linenum += 1
line = line.strip()
if not line or line[0] == '#': continue
parts = re.split(r"\s+", line, 2)
if len(parts) < 2:
self.AddError("failed to parse \"%s\"" % (line,))
continue
if parts[0] == "option":
self.options[parts[1]] = parts[2:]
continue
if parts[0] == "?":
tag = None
else:
try:
tag = int(parts[0])
except ValueError:
self.AddError("\"%s\" isn't an integer tag or '?'" % (parts[0],))
continue
tagname = parts[1]
if len(parts) == 3:
description = parts[2]
else:
description = None
if description:
# EventLog.java checks that the description field is
# surrounded by parens, so we should too (to avoid a runtime
# crash from badly-formatted descriptions).
if not re.match(r"\(.*\)\s*$", description):
self.AddError("tag \"%s\" has unparseable description" % (tagname,))
continue
self.tags.append(Tag(tag, tagname, description,
self.filename, self.linenum))
except (IOError, OSError), e:
self.AddError(str(e))
def BooleanFromString(s):
"""Interpret 's' as a boolean and return its value. Raise
ValueError if it's not something we can interpret as true or
false."""
s = s.lower()
if s in ("true", "t", "1", "on", "yes", "y"):
return True
if s in ("false", "f", "0", "off", "no", "n"):
return False
raise ValueError("'%s' not a valid boolean" % (s,))
def WriteOutput(output_file, data):
"""Write 'data' to the given output filename (which may be None to
indicate stdout). Emit an error message and die on any failure.
'data' may be a string or a StringIO object."""
if not isinstance(data, str):
data = data.getvalue()
try:
if output_file is None:
out = sys.stdout
output_file = "<stdout>"
else:
out = open(output_file, "wb")
out.write(data)
out.close()
except (IOError, OSError), e:
print >> sys.stderr, "failed to write %s: %s" % (output_file, e)
sys.exit(1)
|