• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22import distutils.version
23import os
24import sys
25
26
27# pylint: disable=too-few-public-methods
28class Color:
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
38def 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
49def print_error(msg):
50    """Print an error, and terminate"""
51    print_color(msg, color=Color.RED, prefix="ERROR")
52    sys.exit(1)
53
54
55def 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
68def print_info(msg):
69    """Print a message"""
70    print_color(msg, color=Color.GREEN, prefix="INFO")
71
72
73def 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
88def dots_to_hyphens(s):
89    return s.replace(".", "-")
90
91
92def 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
117def is_ugly_case(s):
118    if s and s.find("_") > 0:
119        return True
120    return False
121
122
123def lookup_annotation(annotations, key):
124    if annotations:
125        for a in annotations:
126            if a.key == key:
127                return a.value
128    return None
129
130
131def lookup_docs(annotations):
132    s = lookup_annotation(annotations, "org.gtk.GDBus.DocString")
133    if s is None:
134        return ""
135    else:
136        return s
137
138
139def lookup_since(annotations):
140    s = lookup_annotation(annotations, "org.gtk.GDBus.Since")
141    if s is None:
142        return ""
143    else:
144        return s
145
146
147def lookup_brief_docs(annotations):
148    s = lookup_annotation(annotations, "org.gtk.GDBus.DocString.Short")
149    if s is None:
150        return ""
151    else:
152        return s
153
154
155def version_cmp_key(key):
156    # If the 'since' version is 'UNRELEASED', compare higher than anything else
157    # If it is empty put a 0 in its place as this will
158    # allow LooseVersion to work and will always compare lower.
159    if key[0] == "UNRELEASED":
160        v = "9999"
161    elif key[0]:
162        v = str(key[0])
163    else:
164        v = "0"
165    return (distutils.version.LooseVersion(v), key[1])
166