1 # Copyright (C) 2009 The Android Open Source Project
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """A module for reading and parsing event-log-tags files."""
21 __slots__ = ["tagnum", "tagname", "description", "filename", "linenum"]
23 def __init__(self, tagnum, tagname, description, filename, linenum):
25 self.tagname = tagname
26 self.description = description
27 self.filename = filename
28 self.linenum = linenum
31 class TagFile(object):
32 """Read an input event-log-tags file."""
33 def AddError(self, msg, linenum=None):
35 linenum = self.linenum
36 self.errors.append((self.filename, linenum, msg))
38 def AddWarning(self, msg, linenum=None):
40 linenum = self.linenum
41 self.warnings.append((self.filename, linenum, msg))
43 def __init__(self, filename, file_object=None):
44 """'filename' is the name of the file (included in any error
45 messages). If 'file_object' is None, 'filename' will be opened
52 self.filename = filename
55 if file_object is None:
57 file_object = open(filename, "rb")
58 except (IOError, OSError), e:
63 for self.linenum, line in enumerate(file_object):
67 if not line or line[0] == '#': continue
68 parts = re.split(r"\s+", line, 2)
71 self.AddError("failed to parse \"%s\"" % (line,))
74 if parts[0] == "option":
75 self.options[parts[1]] = parts[2:]
84 self.AddError("\"%s\" isn't an integer tag or '?'" % (parts[0],))
89 description = parts[2]
94 # EventLog.java checks that the description field is
95 # surrounded by parens, so we should too (to avoid a runtime
96 # crash from badly-formatted descriptions).
97 if not re.match(r"\(.*\)\s*$", description):
98 self.AddError("tag \"%s\" has unparseable description" % (tagname,))
101 self.tags.append(Tag(tag, tagname, description,
102 self.filename, self.linenum))
103 except (IOError, OSError), e:
104 self.AddError(str(e))
107 def BooleanFromString(s):
108 """Interpret 's' as a boolean and return its value. Raise
109 ValueError if it's not something we can interpret as true or
112 if s in ("true", "t", "1", "on", "yes", "y"):
114 if s in ("false", "f", "0", "off", "no", "n"):
116 raise ValueError("'%s' not a valid boolean" % (s,))
119 def WriteOutput(output_file, data):
120 """Write 'data' to the given output filename (which may be None to
121 indicate stdout). Emit an error message and die on any failure.
122 'data' may be a string or a StringIO object."""
123 if not isinstance(data, str):
124 data = data.getvalue()
126 if output_file is None:
128 output_file = "<stdout>"
130 out = open(output_file, "wb")
133 except (IOError, OSError), e:
134 print >> sys.stderr, "failed to write %s: %s" % (output_file, e)