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
26 # pylint: disable=too-few-public-methods
28 '''ANSI Terminal colors'''
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
)
41 sys
.stderr
.write('{prefix}: {msg}\n'.format(prefix
=real_prefix
, msg
=msg
))
44 '''Print an error, and terminate'''
45 print_color(msg
, color
=Color
.RED
, prefix
='ERROR')
48 def print_warning(msg
, fatal
=False):
49 '''Print a warning, and optionally terminate'''
56 print_color(msg
, color
, prefix
)
62 print_color(msg
, color
=Color
.GREEN
, prefix
='INFO')
78 def dots_to_hyphens(s
):
79 return s
.replace('.', '-')
81 def camel_case_to_uscore(s
):
84 prev_was_lower
= False
87 # Keep initial underscores in camel case
88 if initial
and c
== '_':
96 prev_was_lower
= False
102 insert_uscore
= False
106 if s
and s
.find('_') > 0:
110 def lookup_annotation(annotations
, key
):
112 for a
in annotations
:
117 def lookup_docs(annotations
):
118 s
= lookup_annotation(annotations
, 'org.gtk.GDBus.DocString')
124 def lookup_since(annotations
):
125 s
= lookup_annotation(annotations
, 'org.gtk.GDBus.Since')
131 def lookup_brief_docs(annotations
):
132 s
= lookup_annotation(annotations
, 'org.gtk.GDBus.DocString.Short')
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':
148 return (distutils
.version
.LooseVersion(v
), key
[1])