glib (2.79.0)
1 # -*- Mode: Python -*-
2
3 # GDBus - GLib D-Bus Library
4 #
5 # Copyright (C) 2008-2011 Red Hat, Inc.
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 # Author: David Zeuthen <davidz@redhat.com>
21
22 import packaging.version
23 import os
24 import sys
25
26
27 # pylint: disable=too-few-public-methods
28 class ESC[4;38;5;81mColor:
29 """ANSI Terminal colors"""
30
31 GREEN = "\033[1;32m"
32 BLUE = "\033[1;34m"
33 YELLOW = "\033[1;33m"
34 RED = "\033[1;31m"
35 END = "\033[0m"
36
37
38 def print_color(msg, color=Color.END, prefix="MESSAGE"):
39 """Print a string with a color prefix"""
40 if os.isatty(sys.stderr.fileno()):
41 real_prefix = "{start}{prefix}{end}".format(
42 start=color, prefix=prefix, end=Color.END
43 )
44 else:
45 real_prefix = prefix
46 sys.stderr.write("{prefix}: {msg}\n".format(prefix=real_prefix, msg=msg))
47
48
49 def print_error(msg):
50 """Print an error, and terminate"""
51 print_color(msg, color=Color.RED, prefix="ERROR")
52 sys.exit(1)
53
54
55 def print_warning(msg, fatal=False):
56 """Print a warning, and optionally terminate"""
57 if fatal:
58 color = Color.RED
59 prefix = "ERROR"
60 else:
61 color = Color.YELLOW
62 prefix = "WARNING"
63 print_color(msg, color, prefix)
64 if fatal:
65 sys.exit(1)
66
67
68 def print_info(msg):
69 """Print a message"""
70 print_color(msg, color=Color.GREEN, prefix="INFO")
71
72
73 def strip_dots(s):
74 ret = ""
75 force_upper = False
76 for c in s:
77 if c == ".":
78 force_upper = True
79 else:
80 if force_upper:
81 ret += c.upper()
82 force_upper = False
83 else:
84 ret += c
85 return ret
86
87
88 def dots_to_hyphens(s):
89 return s.replace(".", "-")
90
91
92 def camel_case_to_uscore(s):
93 ret = ""
94 insert_uscore = False
95 prev_was_lower = False
96 initial = True
97 for c in s:
98 # Keep initial underscores in camel case
99 if initial and c == "_":
100 ret += "_"
101 continue
102 initial = False
103
104 if c.isupper():
105 if prev_was_lower:
106 insert_uscore = True
107 prev_was_lower = False
108 else:
109 prev_was_lower = True
110 if insert_uscore:
111 ret += "_"
112 ret += c.lower()
113 insert_uscore = False
114 return ret
115
116
117 def uscore_to_camel_case(s):
118 return "".join([s[0].upper() + s[1:].lower() if s else "_" for s in s.split("_")])
119
120
121 def is_ugly_case(s):
122 if s and s.find("_") > 0:
123 return True
124 return False
125
126
127 def lookup_annotation(annotations, key):
128 if annotations:
129 for a in annotations:
130 if a.key == key:
131 return a.value
132 return None
133
134
135 def lookup_docs(annotations):
136 s = lookup_annotation(annotations, "org.gtk.GDBus.DocString")
137 if s is None:
138 return ""
139 else:
140 return s
141
142
143 def lookup_since(annotations):
144 s = lookup_annotation(annotations, "org.gtk.GDBus.Since")
145 if s is None:
146 return ""
147 else:
148 return s
149
150
151 def lookup_brief_docs(annotations):
152 s = lookup_annotation(annotations, "org.gtk.GDBus.DocString.Short")
153 if s is None:
154 return ""
155 else:
156 return s
157
158
159 def version_cmp_key(key):
160 # If the 'since' version is 'UNRELEASED', compare higher than anything else
161 # If it is empty put a 0 in its place as this will
162 # allow LooseVersion to work and will always compare lower.
163 if key[0] == "UNRELEASED":
164 v = "9999"
165 elif key[0]:
166 v = str(key[0])
167 else:
168 v = "0"
169 return (packaging.version.Version(v), key[1])