old_contents: # -*- coding: utf-8 -*-
  
  import logging
  import hipchat
  
  
  class HipChat(object):
  
      def __init__(self, admin_token, default_room):
          self.api = hipchat.HipChat(token=admin_token)
          self.default_room = default_room
          self.room_ids_cache = {}
  
      def find_room_id(self, name):
          room_id = self.room_ids_cache.get(name)
          if not room_id:
              try:
                  room_id = self.api.find_room(name)['room_id']
              except TypeError:
                  print('WARNING: hipchat room "{0}" not found!'.format(name))
                  return None
              self.room_ids_cache[name] = room_id
          return room_id
  
      def send_message(self, message, sender='log', color='yellow', room=None, notify=0):
          if not room:
              room = self.default_room
          room_id = self.find_room_id(room)
          if room_id is None:
              return
          self.api.message_room(
              room_id=room_id,
              message_from=sender,
              color=color,
              message=message,
              notify=notify,
          )
  
  
  class HipChatHandler(logging.Handler):
  
      def __init__(self, admin_token, room, sender=''):
          logging.Handler.__init__(self)
          self.sender = sender
          self.api = HipChat(admin_token, room)
  
      def emit(self, record):
          if hasattr(record, "color"):
              color = record.color
          else:
              color = self.__color_for_level(record.levelno)
          if hasattr(record, "sender"):
              sender = record.sender
          else:
              sender = '-'.join([n for n in [self.sender, record.levelname] if n])
          if hasattr(record, "notify"):
              notify = record.notify
          else:
              notify = self.__notify_for_level(record.levelno)
          self.api.send_message(
              self.format(record),
              sender=sender,
              color=color,
              notify=notify,
          )
  
      def __color_for_level(self, levelno):
          if levelno > logging.WARNING:
              return 'red'
          if levelno == logging.WARNING:
              return 'yellow'
          if levelno == logging.INFO:
              return 'green'
          if levelno == logging.DEBUG:
              return 'gray'
          return 'purple'
  
      def __notify_for_level(self, levelno):
          if levelno > logging.WARNING:
              return 1
          if levelno == logging.WARNING:
              return 0
          if levelno == logging.INFO:
              return 0
          if levelno == logging.DEBUG:
              return 0
          return 0
new_contents: # -*- coding: utf-8 -*-
  """ Refactors hiplogging (https://github.com/invernizzi/hiplogging) to use
  HipChat's API v2 via HypChat (https://github.com/RidersDiscountCom/HypChat)
  """
  import logging
  from hypchat import HypChat
  
  
  class HipChatHandler(logging.Handler):
  
      def __init__(self, access_token, room_name, endpoint='https://api.hipchat.com'):
          logging.Handler.__init__(self)
          self.room = HypChat(access_token, endpoint).get_room(room_name)
  
      def emit(self, record):
          if hasattr(record, "color"):
              color = record.color
          else:
              color = self.__color_for_level(record.levelno)
          if hasattr(record, "notify"):
              notify = bool(record.notify)
          else:
              notify = self.__notify_for_level(record.levelno)
          print 'calling emit'
          self.room.notification(
              message=self.format(record),
              color=color,
              notify=notify
          )
  
      def __color_for_level(self, levelno):
          if levelno > logging.WARNING:
              return 'red'
          if levelno == logging.WARNING:
              return 'yellow'
          if levelno == logging.INFO:
              return 'green'
          if levelno == logging.DEBUG:
              return 'gray'
          return 'purple'
  
      def __notify_for_level(self, levelno):
          if levelno > logging.WARNING:
              return True
          if levelno == logging.WARNING:
              return False
          if levelno == logging.INFO:
              return False
          if levelno == logging.DEBUG:
              return False
          return False
  
  
  
subject: Update to rely on HypChat to make API v2 compliant