From f8233551a19ce4ead9f809d39cff49a94126d141 Mon Sep 17 00:00:00 2001 From: Frederic Peters Date: Tue, 29 Apr 2008 12:03:40 +0000 Subject: [project @ fpeters@0d.be-20071031114522-jkrmvbpphcm0rms8] moved identifier name formatting functions to their own module, so they are not duplicated everywhere. Original author: Frederic Peters Date: 2007-10-31 12:45:22.367000+01:00 --- bindings/utils.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 bindings/utils.py (limited to 'bindings/utils.py') diff --git a/bindings/utils.py b/bindings/utils.py new file mode 100644 index 00000000..3e95a7ac --- /dev/null +++ b/bindings/utils.py @@ -0,0 +1,49 @@ +# 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:] + 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 + -- cgit