[gobject] set all properties before constructed()
[glib.git] / gio / gdbus-2.0 / codegen / utils.py
blob239b64e126e95a38696b76be1c35e2af8c403145
1 # -*- Mode: Python -*-
3 # GDBus - GLib D-Bus Library
5 # Copyright (C) 2008-2011 Red Hat, Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General
18 # Public License along with this library; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307, USA.
22 # Author: David Zeuthen <davidz@redhat.com>
24 import distutils.version
26 def strip_dots(s):
27 ret = ''
28 force_upper = False
29 for c in s:
30 if c == '.':
31 force_upper = True
32 else:
33 if force_upper:
34 ret += c.upper()
35 force_upper = False
36 else:
37 ret += c
38 return ret
40 def dots_to_hyphens(s):
41 return s.replace('.', '-')
43 def camel_case_to_uscore(s):
44 ret = ''
45 insert_uscore = False
46 prev_was_lower = False
47 initial = True;
48 for c in s:
49 # Keep initial underscores in camel case
50 if initial and c == '_':
51 ret += '_'
52 continue;
53 initial = False
55 if c.isupper():
56 if prev_was_lower:
57 insert_uscore = True
58 prev_was_lower = False
59 else:
60 prev_was_lower = True
61 if insert_uscore:
62 ret += '_'
63 ret += c.lower()
64 insert_uscore = False
65 return ret
67 def is_ugly_case(s):
68 if s and s.find('_') > 0:
69 return True
70 return False
72 def lookup_annotation(annotations, key):
73 if annotations:
74 for a in annotations:
75 if a.key == key:
76 return a.value
77 return None
79 def lookup_docs(annotations):
80 s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString')
81 if s == None:
82 return ''
83 else:
84 return s
86 def lookup_since(annotations):
87 s = lookup_annotation(annotations, 'org.gtk.GDBus.Since')
88 if s == None:
89 return ''
90 else:
91 return s
93 def lookup_brief_docs(annotations):
94 s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString.Short')
95 if s == None:
96 return ''
97 else:
98 return s
100 def version_cmp_key(key):
101 # If the 'since' version is empty put a 0 in its place as this will
102 # allow LooseVersion to work and will always compare lower.
103 v = key[0] if key[0] else '0'
104 return (distutils.version.LooseVersion(v), key[1])