# Path: studio/util/util.py
# def sixdecode(sval):
#     if isinstance(sval, str):
#         return sval
#     if isinstance(sval, bytes):
#         return sval.decode('utf8')
#     raise TypeError("Unknown type of " + str(sval))
# 
# def retry(f,
#           no_retries=5, sleep_time=1,
#           exception_class=BaseException, logger=None):
#     for i in range(no_retries):
#         try:
#             return f()
#         except exception_class as exc:
#             check_for_kb_interrupt()
#             if i == no_retries - 1:
#                 raise exc
# 
#             if logger:
#                 logger.info(
#                     ('Exception {0} is caught, ' +
#                      'sleeping {1}s and retrying (attempt {2} of {3})')
#                         .format(exc, sleep_time, i, no_retries))
#             time.sleep(sleep_time)
#     return None
# 
# def parse_verbosity(verbosity=None):
#     if verbosity is None:
#         return parse_verbosity('info')
# 
#     if verbosity == 'True':
#         return parse_verbosity('info')
# 
#     logger_levels = {
#         'debug': 10,
#         'info': 20,
#         'warn': 30,
#         'error': 40,
#         'crit': 50
#     }
# 
#     if isinstance(verbosity, str) and \
#             verbosity in logger_levels.keys():
#         return logger_levels[verbosity]
#     return int(verbosity)
# 
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):


import os
import json
import time
from google.api_core.exceptions import RetryError
from studio.util.util import sixdecode, retry, parse_verbosity, check_for_kb_interrupt
from studio.util import logs
        from google.cloud import pubsub
    def dequeue(self, acknowledge=True, timeout=0):
        wait_step = 1
        for waited in range(0, timeout + wait_step, wait_step):
            response = self.subclient.api.pull(
                self.sub_name,
                return_immediately=True, max_messages=1)
            msgs = response.received_messages
            if any(msgs):
                break
            elif waited == timeout:
                return None
            else:
                self.logger.info(
                    ('No messages found, sleeping for {} ' +
                     ' (total sleep time {})').format(wait_step, waited))
                time.sleep(wait_step)

        retval = msgs[0]

        if acknowledge:
            self.acknowledge(retval.ack_id)
            self.logger.debug("Message {} received and acknowledged"
                              .format(retval.message.message_id))

            return sixdecode(retval.message.data)
        else:
            self.logger.debug(
                "Message {} received, ack_id {}" .format(
                    retval.message.message_id,
                    retval.ack_id))

# Path: studio/util/util.py
# def sixdecode(sval):
#     if isinstance(sval, str):
#         return sval
#     if isinstance(sval, bytes):
#         return sval.decode('utf8')
#     raise TypeError("Unknown type of " + str(sval))
# 
# def retry(f,
#           no_retries=5, sleep_time=1,
#           exception_class=BaseException, logger=None):
#     for i in range(no_retries):
#         try:
#             return f()
#         except exception_class as exc:
#             check_for_kb_interrupt()
#             if i == no_retries - 1:
#                 raise exc
# 
#             if logger:
#                 logger.info(
#                     ('Exception {0} is caught, ' +
#                      'sleeping {1}s and retrying (attempt {2} of {3})')
#                         .format(exc, sleep_time, i, no_retries))
#             time.sleep(sleep_time)
#     return None
# 
# def parse_verbosity(verbosity=None):
#     if verbosity is None:
#         return parse_verbosity('info')
# 
#     if verbosity == 'True':
#         return parse_verbosity('info')
# 
#     logger_levels = {
#         'debug': 10,
#         'info': 20,
#         'warn': 30,
#         'error': 40,
#         'crit': 50
#     }
# 
#     if isinstance(verbosity, str) and \
#             verbosity in logger_levels.keys():
#         return logger_levels[verbosity]
#     return int(verbosity)
# 
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):


# Path: studio/pubsub_queue.py
import os
import json
import time
from google.api_core.exceptions import RetryError
from studio.util.util import sixdecode, retry, parse_verbosity, check_for_kb_interrupt
from studio.util import logs
        from google.cloud import pubsub

    def dequeue(self, acknowledge=True, timeout=0):
        wait_step = 1
        for waited in range(0, timeout + wait_step, wait_step):
            response = self.subclient.api.pull(
                self.sub_name,
                return_immediately=True, max_messages=1)
            msgs = response.received_messages
            if any(msgs):
                break
            elif waited == timeout:
                return None
            else:
                self.logger.info(
                    ('No messages found, sleeping for {} ' +
                     ' (total sleep time {})').format(wait_step, waited))
                time.sleep(wait_step)

        retval = msgs[0]

        if acknowledge:
            self.acknowledge(retval.ack_id)
            self.logger.debug("Message {} received and acknowledged"
                              .format(retval.message.message_id))

            return sixdecode(retval.message.data)
        else:
            self.logger.debug(
                "Message {} received, ack_id {}" .format(
                    retval.message.message_id,
                    retval.ack_id))

            return (sixdecode(retval.message.data), retval.ack_id)