summaryrefslogtreecommitdiffstats
path: root/bindings/utils.py
blob: 2f6408f22a31fa28b58f4abd1591dc938519df02 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Lasso - A free implementation of the Liberty Alliance specifications.
# 
# Copyright (C) 2004-2007 Entr'ouvert
# http://lasso.entrouvert.org
#
# Authors: See AUTHORS file in top-level directory.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import re
import string

def format_as_camelcase(var):
    '''Format an identifier name into CamelCase'''
    if '_' in var:
        return format_underscore_as_camelcase(var)
    if var[0] in string.uppercase:
        var = var[0].lower() + var[1:]
    var = re.sub(r'([a-z])(ID)([A-Z]|$)', r'\1Id\3', var) # replace standing ID by Id
    return var

def format_as_underscored(var):
    '''Format an identifier name into underscored_name'''
    def rep(s):
        return s.group(0)[0] + '_' + s.group(1).lower()
    var = re.sub(r'[a-z0-9]([A-Z])', rep, var).lower()
    var = var.replace('id_wsf2_', 'idwsf2_')
    var = var.replace('_saslresponse', '_sasl_response')
    return var

def format_underscore_as_camelcase(var):
    '''Format an underscored identifier name into CamelCase'''
    def rep(s):
        return s.group(1)[0].upper() + s.group(1)[1:]
    var = re.sub(r'_([A-Za-z0-9]+)', rep, var)
    var = re.sub(r'([a-z])(ID)([A-Z]|$)', r'\1Id\3', var) # replace standing ID by Id
    return var



def last(x):
    return x[len(x)-1]

def common_prefix(x,y):
    max = min(len(x),len(y))
    last = 0
    for i in range(max):
        if x[i] != y[i]:
            return min(i,last+1)
        if x[i] == '_':
            last = i
    return max

def pgroup(group,prev):
    level, l = group
    i = 0
    for x in l:
        if i == 0:
            prefix = prev
        else:
            prefix = level
        if isinstance(x,tuple):
            pgroup(x,prefix)
        else:
            print prefix * ' ' + x[prefix:]
        i = i + 1

def group(list):
    list.sort()
    pile = [(0,[])]
    prev = ""
    for x in list:
        l, g = last(pile)
        u = common_prefix(x,prev)
        # Find the good level of insertion
        while u < l:
            pile.pop()
            l, g = last(pile)
        # Insert here
        if u == l:
            g.append(x)
        elif u > l:
            t = (u, [g.pop(),x])
            g.append(t)
            pile.append(t)
        prev = x
    return pile[0]