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# pylint: disable=too-few-public-methods 27class 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' 34 35def 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)) 42 43def print_error(msg): 44 '''Print an error, and terminate''' 45 print_color(msg, color=Color.RED, prefix='ERROR') 46 sys.exit(1) 47 48def 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) 59 60def print_info(msg): 61 '''Print a message''' 62 print_color(msg, color=Color.GREEN, prefix='INFO') 63 64def 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 77 78def dots_to_hyphens(s): 79 return s.replace('.', '-') 80 81def 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 92 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 104 105def is_ugly_case(s): 106 if s and s.find('_') > 0: 107 return True 108 return False 109 110def lookup_annotation(annotations, key): 111 if annotations: 112 for a in annotations: 113 if a.key == key: 114 return a.value 115 return None 116 117def lookup_docs(annotations): 118 s = lookup_annotation(annotations, 'org.gtk.GDBus.DocString') 119 if s is None: 120 return '' 121 else: 122 return s 123 124def lookup_since(annotations): 125 s = lookup_annotation(annotations, 'org.gtk.GDBus.Since') 126 if s is None: 127 return '' 128 else: 129 return s 130 131def 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 137 138def 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]) 149