# 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
        with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
            credentials = json.loads(f.read())

        project_name = credentials['project_id']
        self.logger = logs.get_logger(self.__class__.__name__)
        if verbose is not None:
            self.logger.setLevel(parse_verbosity(verbose))

        self.pubclient = pubsub.PublisherClient()
        self.subclient = pubsub.SubscriberClient()

        self.project = project_name
        self.topic_name = self.pubclient.topic_path(project_name, queue_name)
        self.logger.info("Topic name = {}".format(self.topic_name))
        try:
            self.pubtopic = self.pubclient.get_topic(self.topic_name)
        except BaseException as e:
            check_for_kb_interrupt()
            self.pubtopic = self.pubclient.create_topic(self.topic_name)
            self.logger.info('topic {} created'.format(self.topic_name))

        sub_name = sub_name if sub_name else queue_name + "_sub"
        self.logger.info("Topic name = {}".format(queue_name))
        self.logger.info("Subscription name = {}".format(sub_name))

        self.sub_name = self.subclient.subscription_path(
            project_name, sub_name)
        try:
            self.subclient.get_subscription(self.sub_name)
        except BaseException as e:

# 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

        with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as f:
            credentials = json.loads(f.read())

        project_name = credentials['project_id']
        self.logger = logs.get_logger(self.__class__.__name__)
        if verbose is not None:
            self.logger.setLevel(parse_verbosity(verbose))

        self.pubclient = pubsub.PublisherClient()
        self.subclient = pubsub.SubscriberClient()

        self.project = project_name
        self.topic_name = self.pubclient.topic_path(project_name, queue_name)
        self.logger.info("Topic name = {}".format(self.topic_name))
        try:
            self.pubtopic = self.pubclient.get_topic(self.topic_name)
        except BaseException as e:
            check_for_kb_interrupt()
            self.pubtopic = self.pubclient.create_topic(self.topic_name)
            self.logger.info('topic {} created'.format(self.topic_name))

        sub_name = sub_name if sub_name else queue_name + "_sub"
        self.logger.info("Topic name = {}".format(queue_name))
        self.logger.info("Subscription name = {}".format(sub_name))

        self.sub_name = self.subclient.subscription_path(
            project_name, sub_name)
        try:
            self.subclient.get_subscription(self.sub_name)
        except BaseException as e:

            check_for_kb_interrupt()