1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from py_utils.refactor import annotated_symbol
6
7
8class Module(object):
9
10  def __init__(self, file_path):
11    self._file_path = file_path
12
13    with open(self._file_path, 'r') as f:
14      self._snippet = annotated_symbol.Annotate(f)
15
16  @property
17  def file_path(self):
18    return self._file_path
19
20  @property
21  def modified(self):
22    return self._snippet.modified
23
24  def FindAll(self, snippet_type):
25    return self._snippet.FindAll(snippet_type)
26
27  def FindChildren(self, snippet_type):
28    return self._snippet.FindChildren(snippet_type)
29
30  def Write(self):
31    """Write modifications to the file."""
32    if not self.modified:
33      return
34
35    # Stringify before opening the file for writing.
36    # If we fail, we won't truncate the file.
37    string = str(self._snippet)
38    with open(self._file_path, 'w') as f:
39      f.write(string)
40