github.com/deis/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/contrib/linode/linodeutils.py (about)

     1  from colorama import Fore, Style
     2  import colorama
     3  import collections
     4  import os
     5  
     6  
     7  def log_debug(message):
     8      print(Style.DIM + Fore.MAGENTA + message + Fore.RESET + Style.RESET_ALL)
     9  
    10  
    11  def log_info(message):
    12      print(Fore.CYAN + message + Fore.RESET)
    13  
    14  
    15  def log_warning(message):
    16      print(Fore.YELLOW + message + Fore.RESET)
    17  
    18  
    19  def log_success(message):
    20      print(Style.BRIGHT + Fore.GREEN + message + Fore.RESET + Style.RESET_ALL)
    21  
    22  def log_minor_success(message):
    23      print(Fore.GREEN + message + Fore.RESET + Style.RESET_ALL)
    24  
    25  def log_error(message):
    26      print(Style.BRIGHT + Fore.RED + message + Fore.RESET + Style.RESET_ALL)
    27  
    28      
    29  ''' See provision-cluster.py in method ProvisionCommand._report_created for 
    30      an example of use. Each row should be a string tuple. rows is a list of 
    31      tuples. '''
    32  def log_table(rows, header_msg, footer_msg):
    33      
    34      # set up the report constants
    35      divider = Style.BRIGHT + Fore.MAGENTA + ('=' * 109) + Fore.RESET + Style.RESET_ALL
    36      column_format = "  {:<20} {:<20} {:<20} {:<20} {:<12} {:>8}"
    37      formatted_header = column_format.format(*('HOSTNAME', 'PUBLIC IP', 'PRIVATE IP', 'GATEWAY', 'DC', 'PLAN'))
    38      
    39      # display the report
    40      print('')
    41      print(divider)
    42      print(divider)
    43      print('')
    44      print(Style.BRIGHT + Fore.LIGHTGREEN_EX + header_msg + Fore.RESET + Style.RESET_ALL)
    45      print('')
    46      print(Style.BRIGHT + Fore.CYAN + formatted_header + Fore.RESET + Style.RESET_ALL)
    47      for row in rows:
    48          print(Fore.CYAN + column_format.format(*row) + Fore.RESET)
    49      print('')
    50      print('')
    51      print(Fore.LIGHTYELLOW_EX + footer_msg + Fore.RESET)
    52      print(divider)
    53      print(divider)
    54      print('')
    55  
    56  
    57  def combine_dicts(orig_dict, new_dict):
    58      for key, val in new_dict.iteritems():
    59          if isinstance(val, collections.Mapping):
    60              tmp = combine_dicts(orig_dict.get(key, {}), val)
    61              orig_dict[key] = tmp
    62          elif isinstance(val, list):
    63              orig_dict[key] = (orig_dict.get(key, []) + val)
    64          else:
    65              orig_dict[key] = new_dict[key]
    66      return orig_dict
    67  
    68  
    69  def get_file(name, mode="r", abspath=False):
    70      current_dir = os.path.dirname(__file__)
    71  
    72      if abspath:
    73          return file(os.path.abspath(os.path.join(current_dir, name)), mode)
    74      else:
    75          return file(os.path.join(current_dir, name), mode)
    76  
    77      
    78  def init():
    79      colorama.init()