# Path: carrot/objects.py
# class VirtualHost(object):
#     """
#     A RabbitMQ virtual host
#     """
# 
#     def __init__(self,
#                  url: str = None,
#                  host: str = 'localhost',
#                  name: str = '%2f',
#                  port: int = 5672,
#                  username: str = 'guest',
#                  password: str = 'guest',
#                  secure: bool = False) -> None:
# 
#         self.secure = secure
#         if not url:
#             self.host = host
#             self.name = name
#             self.port = port
#             self.username = username
#             self.password = password
# 
#         else:
#             try:
#                 url = url.replace('amqp://', '')
#                 if '@' in url:
#                     # user:pass@host:port/vhost
#                     credentials, url = url.split('@')
#                     self.username, self.password = credentials.split(':')
# 
#                 else:
#                     # host:port/vhost
#                     self.username = username
#                     self.password = password
#                 import pika
# 
#                 try:
#                     url, self.name = url.split('/')
#                 except ValueError:
#                     url = url.split('/')[0]
#                     self.name = name
# 
#                 self.host, _port = url.split(':')
#                 self.port = int(_port)
# 
#             except Exception as err:
#                 raise Exception('Unable to parse the RabbitMQ server. Please check your configuration: %s' % err)
# 
#             if not self.name:
#                 self.name = '%2f'
# 
#     def __str__(self) -> str:
#         """
#         Returns the broker url
#         """
#         return 'amqp://%s:%s@%s:%s/%s' % (self.username, self.password, self.host, self.port, self.name)
# 
#     @property
#     def blocking_connection(self) -> pika.BlockingConnection:
#         """
#         Connect to the VHOST
#         """
#         credentials = pika.PlainCredentials(username=self.username, password=self.password)
#         if self.name == '%2f':
#             vhost = '/'
#         else:
#             vhost = self.name
# 
#         params = pika.ConnectionParameters(host=self.host, port=self.port, virtual_host=vhost,
#                                            credentials=credentials, connection_attempts=10, ssl=self.secure,
#                                            heartbeat=1200)
#         return pika.BlockingConnection(parameters=params)


import django, sys, os
import argparse
from django.conf import settings
from carrot.objects import VirtualHost
    from django.test.runner import DiscoverRunner


def runner(options):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(BASE_DIR)

    vhost = {
        'host': options.host,
        'port': options.port,
        'name': options.name,
        'username': options.username,
        'password': options.password,
        'secure': options.secure,
    }

# Path: carrot/objects.py
# class VirtualHost(object):
#     """
#     A RabbitMQ virtual host
#     """
# 
#     def __init__(self,
#                  url: str = None,
#                  host: str = 'localhost',
#                  name: str = '%2f',
#                  port: int = 5672,
#                  username: str = 'guest',
#                  password: str = 'guest',
#                  secure: bool = False) -> None:
# 
#         self.secure = secure
#         if not url:
#             self.host = host
#             self.name = name
#             self.port = port
#             self.username = username
#             self.password = password
# 
#         else:
#             try:
#                 url = url.replace('amqp://', '')
#                 if '@' in url:
#                     # user:pass@host:port/vhost
#                     credentials, url = url.split('@')
#                     self.username, self.password = credentials.split(':')
# 
#                 else:
#                     # host:port/vhost
#                     self.username = username
#                     self.password = password
#                 import pika
# 
#                 try:
#                     url, self.name = url.split('/')
#                 except ValueError:
#                     url = url.split('/')[0]
#                     self.name = name
# 
#                 self.host, _port = url.split(':')
#                 self.port = int(_port)
# 
#             except Exception as err:
#                 raise Exception('Unable to parse the RabbitMQ server. Please check your configuration: %s' % err)
# 
#             if not self.name:
#                 self.name = '%2f'
# 
#     def __str__(self) -> str:
#         """
#         Returns the broker url
#         """
#         return 'amqp://%s:%s@%s:%s/%s' % (self.username, self.password, self.host, self.port, self.name)
# 
#     @property
#     def blocking_connection(self) -> pika.BlockingConnection:
#         """
#         Connect to the VHOST
#         """
#         credentials = pika.PlainCredentials(username=self.username, password=self.password)
#         if self.name == '%2f':
#             vhost = '/'
#         else:
#             vhost = self.name
# 
#         params = pika.ConnectionParameters(host=self.host, port=self.port, virtual_host=vhost,
#                                            credentials=credentials, connection_attempts=10, ssl=self.secure,
#                                            heartbeat=1200)
#         return pika.BlockingConnection(parameters=params)


# Path: run_tests.py
import django, sys, os
import argparse
from django.conf import settings
from carrot.objects import VirtualHost
    from django.test.runner import DiscoverRunner



def runner(options):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(BASE_DIR)

    vhost = {
        'host': options.host,
        'port': options.port,
        'name': options.name,
        'username': options.username,
        'password': options.password,
        'secure': options.secure,
    }

    _vhost = VirtualHost(**vhost)