summaryrefslogtreecommitdiff
path: root/systrace/catapult/common/py_utils/py_utils/refactor/module.py
blob: d6eae00cdb42a6ea5a10ed1563ecbd2f84b5cd2a (plain)
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
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from py_utils.refactor import annotated_symbol


class Module(object):

  def __init__(self, file_path):
    self._file_path = file_path

    with open(self._file_path, 'r') as f:
      self._snippet = annotated_symbol.Annotate(f)

  @property
  def file_path(self):
    return self._file_path

  @property
  def modified(self):
    return self._snippet.modified

  def FindAll(self, snippet_type):
    return self._snippet.FindAll(snippet_type)

  def FindChildren(self, snippet_type):
    return self._snippet.FindChildren(snippet_type)

  def Write(self):
    """Write modifications to the file."""
    if not self.modified:
      return

    # Stringify before opening the file for writing.
    # If we fail, we won't truncate the file.
    string = str(self._snippet)
    with open(self._file_path, 'w') as f:
      f.write(string)