1#!/usr/bin/env python
2# Copyright 2012 The Closure Linter Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS-IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17"""A simple, pickle-serializable class to represent a lint error."""
18
19__author__ = 'nnaze@google.com (Nathan Naze)'
20
21import gflags as flags
22
23from closure_linter import errors
24from closure_linter.common import erroroutput
25
26FLAGS = flags.FLAGS
27
28
29class ErrorRecord(object):
30  """Record-keeping struct that can be serialized back from a process.
31
32  Attributes:
33    path: Path to the file.
34    error_string: Error string for the user.
35    new_error: Whether this is a "new error" (see errors.NEW_ERRORS).
36  """
37
38  def __init__(self, path, error_string, new_error):
39    self.path = path
40    self.error_string = error_string
41    self.new_error = new_error
42
43
44def MakeErrorRecord(path, error):
45  """Make an error record with correctly formatted error string.
46
47  Errors are not able to be serialized (pickled) over processes because of
48  their pointers to the complex token/context graph.  We use an intermediary
49  serializable class to pass back just the relevant information.
50
51  Args:
52    path: Path of file the error was found in.
53    error: An error.Error instance.
54
55  Returns:
56    _ErrorRecord instance.
57  """
58  new_error = error.code in errors.NEW_ERRORS
59
60  if FLAGS.unix_mode:
61    error_string = erroroutput.GetUnixErrorOutput(
62        path, error, new_error=new_error)
63  else:
64    error_string = erroroutput.GetErrorOutput(error, new_error=new_error)
65
66  return ErrorRecord(path, error_string, new_error)
67