aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_utils/py_utils/refactor/annotated_symbol/base_symbol.py
blob: 5e473bcf98d67cb78caa5464367510273160301b (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
40
# 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 __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from py_utils.refactor import snippet
from six.moves import range # pylint: disable=redefined-builtin


class AnnotatedSymbol(snippet.Symbol):
  def __init__(self, symbol_type, children):
    super(AnnotatedSymbol, self).__init__(symbol_type, children)
    self._modified = False

  @property
  def modified(self):
    if self._modified:
      return True
    return super(AnnotatedSymbol, self).modified

  def __setattr__(self, name, value):
    if (hasattr(self.__class__, name) and
        isinstance(getattr(self.__class__, name), property)):
      self._modified = True
    return super(AnnotatedSymbol, self).__setattr__(name, value)

  def Cut(self, child):
    for i in range(len(self._children)):
      if self._children[i] == child:
        self._modified = True
        del self._children[i]
        break
    else:
      raise ValueError('%s is not in %s.' % (child, self))

  def Paste(self, child):
    self._modified = True
    self._children.append(child)