Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / gdbus-2.0 / codegen / utils.py
blob95ba107d372785ecbde25f42c0dc226fbe6a4b73
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.1 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, see <http://www.gnu.org/licenses/>.
20 # Author: David Zeuthen <davidz@redhat.com>
22 import distutils.version
23 import os
24 import sys
26 # pylint: disable=too-few-public-methods
27 class Color:
28 '''ANSI Terminal colors'''
29 GREEN = '\033[1;32m'
30 BLUE = '\033[1;34m'
31 YELLOW = '\033[1;33m'
32 RED = '\033[1;31m'
33 END = '\033[0m'
35 def print_color(msg, color=Color.END, prefix='MESSAGE'):
36 '''Print a string with a color prefix'''
37 if os.isatty(sys.stderr.fileno()):
38 real_prefix = '{start}{prefix}{end}'.format(start=color, prefix=prefix, end=Color.END)
39 else:
40 real_prefix = prefix
41 sys.stderr.write('{prefix}: {msg}\n'.format(prefix=real_prefix, msg=msg))
43 def print_error(msg):
44 '''Print an error, and terminate'''
45 print_color(msg, color=Color.RED, prefix='ERROR')
46 sys.exit(1)
48 def print_warning(msg, fatal=False):
49 '''Print a warning, and optionally terminate'''
50 if fatal:
51 color = Color.RED
52 prefix = 'ERROR'
53 else:
54 color = Color.YELLOW
55 prefix = 'WARNING'
56 print_color(msg, color, prefix)
57 if fatal:
58 sys.exit(1)
60 def print_info(msg):
61 '''Print a message'''
62 print_color(msg, color=Color.GREEN, prefix='INFO')
64 def strip_dots(s):
65 ret = ''
66 force_upper = False
67 for c in s:
68 if c == '.':
69 force_upper = True
70 else:
71 if force_upper:
72 ret += c.upper()
73 force_upper = False
74 else:
75 ret += c
76 return ret
78 def dots_to_hyphens(s):
79 return s.replace('.', '-')
81 def camel_case_to_uscore(s):
82 ret = ''
83 insert_uscore = False
84 prev_was_lower = False
85 initial = True;
86 for c in s:
87 # Keep initial underscores in camel case
88 if initial and c == '_':
89 ret += '_'
90 continue;
91 initial = False
93 if c.isupper():
94 if prev_was_lower:
95 insert_uscore = True
96 prev_was_lower = False
97 else:
98 prev_was_lower = True
99 if insert_uscore:
100 ret += '_'
101 ret += c.lower()
102 insert_uscore = False
103 return ret
105 def is_ugly_case(s):
106 if s and s.find('_') > 0:
107 return True
108 return False
110 def lookup_annotation(annotations, key):
111 if annotations:
112 for a in annotations:
113 if a.key == key:
114 return a.value
115 return None
117 def lookup_docs(annotations):
118 s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString')
119 if s is None:
120 return ''
121 else:
122 return s
124 def lookup_since(annotations):
125 s = lookup_annotation(annotations, 'org.gtk.GDBus.Since')
126 if s is None:
127 return ''
128 else:
129 return s
131 def lookup_brief_docs(annotations):
132 s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString.Short')
133 if s is None:
134 return ''
135 else:
136 return s
138 def version_cmp_key(key):
139 # If the 'since' version is 'UNRELEASED', compare higher than anything else
140 # If it is empty put a 0 in its place as this will
141 # allow LooseVersion to work and will always compare lower.
142 if key[0] == 'UNRELEASED':
143 v = '9999'
144 elif key[0]:
145 v = str(key[0])
146 else:
147 v = '0'
148 return (distutils.version.LooseVersion(v), key[1])