| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import abc | 5 import abc |
| 6 import re | 6 import re |
| 7 | 7 |
| 8 from collections import namedtuple | 8 from collections import namedtuple |
| 9 | 9 |
| 10 from . import types | 10 from . import types |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 cls = self.__class__ | 52 cls = self.__class__ |
| 53 return self._TOSTRING_MAP.get(cls.__name__, cls.default_tostring_fn) | 53 return self._TOSTRING_MAP.get(cls.__name__, cls.default_tostring_fn) |
| 54 | 54 |
| 55 @classmethod | 55 @classmethod |
| 56 def set_tostring_fn(cls, new_tostring_fn): | 56 def set_tostring_fn(cls, new_tostring_fn): |
| 57 assert cls.__name__ not in cls._TOSTRING_MAP, ( | 57 assert cls.__name__ not in cls._TOSTRING_MAP, ( |
| 58 'tostring_fn already installed for %s' % cls) | 58 'tostring_fn already installed for %s' % cls) |
| 59 cls._TOSTRING_MAP[cls.__name__] = new_tostring_fn | 59 cls._TOSTRING_MAP[cls.__name__] = new_tostring_fn |
| 60 | 60 |
| 61 def default_tostring_fn(self): | 61 def default_tostring_fn(self): |
| 62 raise NotImplementedError | 62 raise NotImplementedError() |
| 63 | 63 |
| 64 def __str__(self): | 64 def __str__(self): |
| 65 return self.tostring_fn(self) # pylint: disable=not-callable | 65 return self.tostring_fn(self) # pylint: disable=not-callable |
| 66 | 66 |
| 67 | 67 |
| 68 class BasePath(object): | 68 class BasePath(object): |
| 69 __metaclass__ = abc.ABCMeta | 69 __metaclass__ = abc.ABCMeta |
| 70 | 70 |
| 71 | 71 |
| 72 class NamedBasePath(BasePath, namedtuple('NamedBasePath', 'name')): | 72 class NamedBasePath(BasePath, namedtuple('NamedBasePath', 'name')): |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 """True if |child| is in a subdirectory of this path.""" | 153 """True if |child| is in a subdirectory of this path.""" |
| 154 # Assumes base paths are not nested. | 154 # Assumes base paths are not nested. |
| 155 # TODO(vadimsh): We should not rely on this assumption. | 155 # TODO(vadimsh): We should not rely on this assumption. |
| 156 if self.base != child.base: | 156 if self.base != child.base: |
| 157 return False | 157 return False |
| 158 # A path is not a parent to itself. | 158 # A path is not a parent to itself. |
| 159 if len(self.pieces) >= len(child.pieces): | 159 if len(self.pieces) >= len(child.pieces): |
| 160 return False | 160 return False |
| 161 return child.pieces[:len(self.pieces)] == self.pieces | 161 return child.pieces[:len(self.pieces)] == self.pieces |
| 162 | 162 |
| 163 def default_tostring_fn(self): | |
| 164 suffix = '' | |
| 165 if self.platform_ext: | |
| 166 suffix = ', platform_ext=%r' % (self.platform_ext,) | |
| 167 pieces = '' | |
| 168 if self.pieces: | |
| 169 pieces = ', ' + (', '.join(map(repr, self.pieces))) | |
| 170 return 'Path(\'%s\'%s%s)' % (self.base, pieces, suffix) | |
| 171 | |
| 172 def __repr__(self): | 163 def __repr__(self): |
| 173 s = "Path(%r" % self.base | 164 s = "Path(%r" % self.base |
| 174 if self.pieces: | 165 if self.pieces: |
| 175 s += ", %s" % ",".join(repr(x) for x in self.pieces) | 166 s += ", %s" % ",".join(repr(x) for x in self.pieces) |
| 176 | 167 |
| 177 return s + ")" | 168 return s + ")" |
| OLD | NEW |