From: Frank Brehm Date: Thu, 6 Dec 2018 17:08:51 +0000 (+0100) Subject: Removing lib/webhooks/common.py X-Git-Tag: 1.6.4^2~108 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=9355c6b97d5bf1f94fd9c0d69c073d1577d5af56;p=pixelpark%2Fpuppetmaster-webhooks.git Removing lib/webhooks/common.py --- diff --git a/lib/webhooks/common.py b/lib/webhooks/common.py deleted file mode 100644 index 2202e1c..0000000 --- a/lib/webhooks/common.py +++ /dev/null @@ -1,350 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@author: Frank Brehm -@contact: frank.brehm@pixelpark.com -@copyright: © 2017 by Frank Brehm, Berlin -@summary: The module for common used functions. -""" - -# Standard modules -import sys -import os -import logging -import re -import pprint -import platform -import locale -import collections - -# Third party modules - -# Own modules - -__version__ = '1.0.0' - -LOG = logging.getLogger(__name__) -RE_YES = re.compile(r'^\s*(?:y(?:es)?|true)\s*$', re.IGNORECASE) -RE_NO = re.compile(r'^\s*(?:no?|false|off)\s*$', re.IGNORECASE) - -RE_B2H_FINAL_ZEROES = re.compile(r'0+$') -RE_B2H_FINAL_SIGNS = re.compile(r'\D+$') - - -# ============================================================================= -def pp(value, indent=4, width=99, depth=None): - """ - Returns a pretty print string of the given value. - - @return: pretty print string - @rtype: str - """ - - pretty_printer = pprint.PrettyPrinter( - indent=indent, width=width, depth=depth) - return pretty_printer.pformat(value) - - -# ============================================================================= -def terminal_can_colors(debug=False): - """ - Method to detect, whether the current terminal (stdout and stderr) - is able to perform ANSI color sequences. - - @return: both stdout and stderr can perform ANSI color sequences - @rtype: bool - - """ - - cur_term = '' - if 'TERM' in os.environ: - cur_term = os.environ['TERM'].lower().strip() - - colored_term_list = ( - r'ansi', - r'linux.*', - r'screen.*', - r'[xeak]term.*', - r'gnome.*', - r'rxvt.*', - r'interix', - ) - term_pattern = r'^(?:' + r'|'.join(colored_term_list) + r')$' - re_term = re.compile(term_pattern) - - ansi_term = False - env_term_has_colors = False - - if cur_term: - if cur_term == 'ansi': - env_term_has_colors = True - ansi_term = True - elif re_term.search(cur_term): - env_term_has_colors = True - if debug: - sys.stderr.write( - "ansi_term: %r, env_term_has_colors: %r\n" % ( - ansi_term, env_term_has_colors)) - - has_colors = False - if env_term_has_colors: - has_colors = True - for handle in [sys.stdout, sys.stderr]: - if (hasattr(handle, "isatty") and handle.isatty()): - if debug: - sys.stderr.write("%s is a tty.\n" % (handle.name)) - if (platform.system() == 'Windows' and not ansi_term): - if debug: - sys.stderr.write("platform is Windows and not ansi_term.\n") - has_colors = False - else: - if debug: - sys.stderr.write("%s is not a tty.\n" % (handle.name)) - if ansi_term: - pass - else: - has_colors = False - - return has_colors - - -# ============================================================================= -def to_unicode(obj, encoding='utf-8'): - - if isinstance(obj, bytes): - obj = obj.decode(encoding) - - return obj - - -# ============================================================================= -def to_utf8(obj): - - return encode_or_bust(obj, 'utf-8') - - -# ============================================================================= -def encode_or_bust(obj, encoding='utf-8'): - - if isinstance(obj, str): - obj = obj.encode(encoding) - - return obj - - -# ============================================================================= -def to_bytes(obj, encoding='utf-8'): - "Wrapper for encode_or_bust()" - - return encode_or_bust(obj, encoding) - - -# ============================================================================= -def to_str(obj, encoding='utf-8'): - """ - Transformes the given string-like object into the str-type according - to the current Python version. - """ - - return to_unicode(obj, encoding) - - -# ============================================================================= -def to_bool(value): - """ - Converter from string to boolean values (e.g. from configurations) - """ - - if not value: - return False - - if isinstance(value, bool): - return value - - try: - v_int = int(value) - except ValueError: - pass - except TypeError: - pass - else: - if v_int == 0: - return False - else: - return True - - v_str = '' - if isinstance(value, str): - v_str = value - elif isinstance(value, bytes): - v_str = value.decode('utf-8') - else: - v_str = str(value) - - if RE_YES.search(v_str): - return True - - if RE_NO.search(v_str): - return False - - return bool(value) - - -# ============================================================================= -def is_sequence(arg): - - if not isinstance(arg, collections.Sequence): - return False - - if hasattr(arg, "strip"): - return False - - return True - -# ============================================================================= -def caller_search_path(): - """ - Builds a search path for executables from environment $PATH - including some standard paths. - - @return: all existing search paths - @rtype: list - """ - - path_list = [] - search_path = os.environ['PATH'] - if not search_path: - search_path = os.defpath - - search_path_list = [ - '/opt/pixelpark/bin', - '/opt/puppetlabs/puppet/bin', - '/www/bin', - '/opt/PPlocal/bin', - ] - - for d in search_path.split(os.pathsep): - search_path_list.append(d) - - default_path = [ - '/bin', - '/usr/bin', - '/usr/local/bin', - '/sbin', - '/usr/sbin', - '/usr/local/sbin', - '/usr/ucb', - '/usr/sfw/bin', - '/opt/csw/bin', - '/usr/openwin/bin', - '/usr/ccs/bin', - ] - - for d in default_path: - search_path_list.append(d) - - for d in search_path_list: - if not os.path.exists(d): - continue - if not os.path.isdir(d): - continue - d_abs = os.path.realpath(d) - if d_abs not in path_list: - path_list.append(d_abs) - - return path_list - - -# ============================================================================= -def bytes2human( - value, si_conform=False, precision=None, format_str='{value} {unit}'): - """ - Converts the given value in bytes into a human readable format. - The limit for electing the next higher prefix is at 1500. - - It raises a ValueError on invalid values. - - @param value: the value to convert - @type value: long - @param si_conform: use factor 1000 instead of 1024 for kB a.s.o., - if do so, than the units are for example MB instead MiB. - @type si_conform: bool - @param precision: how many digits after the decimal point have to stay - in the result - @type precision: int - @param format_str: a format string to format the result. - @type format_str: str - - @return: the value in a human readable format together with the unit - @rtype: str - - """ - - val = int(value) - - if not val: - return format_str.format(value=0, unit='Bytes') - - base = 1024 - prefixes = { - 1: 'KiB', - 2: 'MiB', - 3: 'GiB', - 4: 'TiB', - 5: 'PiB', - 6: 'EiB', - 7: 'ZiB', - 8: 'YiB', - } - if si_conform: - base = 1000 - prefixes = { - 1: 'kB', - 2: 'MB', - 3: 'GB', - 4: 'TB', - 5: 'PB', - 6: 'EB', - 7: 'ZB', - 8: 'YB', - } - - exponent = 0 - - float_val = float(val) - while float_val >= (2 * base) and exponent < 8: - float_val /= base - exponent += 1 - - unit = '' - if not exponent: - precision = None - unit = 'Bytes' - if val == 1: - unit = 'Byte' - value_str = locale.format_string('%d', val) - return format_str.format(value=value_str, unit=unit) - - unit = prefixes[exponent] - value_str = '' - if precision is None: - value_str = locale.format_string('%f', float_val) - value_str = RE_B2H_FINAL_ZEROES.sub('', value_str) - value_str = RE_B2H_FINAL_SIGNS.sub('', value_str) - else: - value_str = locale.format_string('%.*f', (precision, float_val)) - - if not exponent: - return value_str - - return format_str.format(value=value_str, unit=unit) - - -# ============================================================================= - -if __name__ == "__main__": - - pass - -# ============================================================================= -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list