Look up all permutations of class names when scanning methods/ctors based
[gnome.gobject-introspection] / giscanner / utils.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19 #
20
21 import re
22 import os
23
24 # Copied from h2defs.py
25 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
26 _upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
27 _upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
28
29
30 def to_underscores(name):
31     """Converts a typename to the equivalent underscores name.
32     This is used to form the type conversion macros and enum/flag
33     name variables"""
34     name = _upperstr_pat1.sub(r'\1_\2', name)
35     name = _upperstr_pat2.sub(r'\1_\2', name)
36     name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
37     return name
38
39
40 def _gen_pascal_combinations(nameset):
41     firstname = [nameset[0].title(), nameset[0].upper()]
42     if len(nameset) == 1:
43         return firstname
44     else:
45         subset = _gen_pascal_combinations(nameset[1:])
46         results = []
47         for x in subset:
48             results.append(firstname[0] + x)
49             results.append(firstname[1] + x)
50         return results
51
52
53 def to_pascal_combinations(name):
54     return _gen_pascal_combinations(name.split('_'))
55
56
57 _libtool_pat = re.compile("dlname='([A-z0-9\.\-\+]+)'\n")
58
59
60 def extract_libtool(libname):
61     data = open(libname).read()
62     filename = _libtool_pat.search(data).groups()[0]
63     libname = os.path.join(os.path.dirname(libname),
64                            '.libs', filename)
65     return libname
66
67
68 def strip_common_prefix(first, second):
69     second = second.replace('_', '')
70     for i, c in enumerate(first.upper()):
71         if c != second[i]:
72             break
73     return second[i:]