File: Synopsis/QualifiedName.py
 1#
 2# Copyright (C) 2008 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8class QualifiedName(tuple):
 9
10    sep = ''
11
12    def __getitem__(self, i):
13        """If i is a slice, make sure a QualifiedName is returned."""
14
15        result = super(QualifiedName, self).__getitem__(i)
16        if type(i) is slice:
17            # Wrap the result
18            return type(self)(result)
19        else:
20            return result
21
22    def __getslice__(self, begin, end):
23        """This method exists because python < 3.0 still uses __getslice__
24        for builtin types. (See http://bugs.python.org/issue2041)"""
25
26        return self.__getitem__(slice(begin, end, None))
27
28    def __add__(self, other):
29        """Overload self + other to preserve the type."""
30
31        return type(self)(tuple(self) + other)
32
33    def prune(self, other):
34        """Return a copy of other with any prefix it shares with self removed.
35   
36        e.g. ('A', 'B', 'C', 'D').prune(('A', 'B', 'D')) -> ('C', 'D')"""
37
38        target = list(other)
39        i = 0
40        while (len(target) > 1 and i < len(self) and target[0] == self[i]):
41            del target[0]
42            i += 1
43        return type(other)(target)
44
45class QualifiedCxxName(QualifiedName):
46
47    sep = '::'
48
49    def __str__(self):
50        return '::'.join(self)
51
52class QualifiedPythonName(QualifiedName):
53
54    sep = '.'
55
56    def __str__(self):
57        return '.'.join(self)
58
59