# Path: lib/Wrappers/ArgumentParser.py
# class ArgumentParser:
#     DEFAULT_VERSION = 'last stable'
# 
#     def __init__(self, *args, argument_parser=None, version_provider=None, **kwargs):
#         self.parser = argument_parser or argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
#                                                                  *args,
#                                                                  **kwargs)
#         self._common_args()
# 
#     def add_argument(self, *args, **kwargs):
#         self.parser.add_argument(*args, **kwargs)
# 
#     def parse_args(self, args):
#         arguments = self.parser.parse_args(args)
#         if 'version' in arguments and arguments.version == self.DEFAULT_VERSION:
#             arguments.version = '1.0'
# 
#         return arguments
# 
#     def _common_args(self):
#         self.parser.add_argument('-S', '--settings', metavar='SETTINGS', type=str,
#                                  help='Path to the settings file.', default="settings.cfg")
# 
# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)
# 
# Path: lib/Settings.py
# class Settings:
#     def __init__(self,
#                  override_settings_path='settings.cfg'):
#         self.settingsFilePath = override_settings_path
#         self.parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
#         self.settings = None
#         self.getSettings()
# 
#     def getSettings(self):
#         self.parser.read('./settings.cfg')
#         self.settings = {section: dict(**self.parser[section]) for section in self.parser.sections()}
# 
#         return self.settings
# 
#     def getServiceIp(self, service):
#         return self.settings['general']['my_ip']
# 
# Path: lib/DomainManagement/Domain.py
# class Domain:
#     def __init__(self):
#         self.logger = Logger(__name__)
#         self.mail = MailDomain()
# 
#     def create_domain(self, domain):
#         self.mail.create_domain(domain)
# 
#     def domain_exists(self, domain):
#         return self.mail.domain_exists(domain)


from lib.Wrappers.ArgumentParser import ArgumentParser
from lib.Wrappers.Logger import Logger
from lib.Settings import Settings
from lib.DomainManagement.Domain import Domain
import re
import sys


class CreateDomain:

    def __init__(self):
        self.settings = Settings().getSettings()
        self.logger = Logger(__name__)
        self.domain = Domain()

    def execute(self, *args):
        args = self._parse_arguments(*args)
        args.domain[0] = args.domain[0].lower()
        if self.domain.domain_exists(args.domain[0]) is True:
            self.logger.error("Domain already exists")
            sys.exit(1)
        regex = "^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]{0,1}\.([a-zA-Z]{1,6}|[a-zA-Z0-9-]{1,30}\.[a-zA-Z]{2,})$"
        pattern = re.compile(regex)
        if not pattern.match(args.domain[0]):
            self.logger.error("The domain you provided is not valid!")

# Path: lib/Wrappers/ArgumentParser.py
# class ArgumentParser:
#     DEFAULT_VERSION = 'last stable'
# 
#     def __init__(self, *args, argument_parser=None, version_provider=None, **kwargs):
#         self.parser = argument_parser or argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
#                                                                  *args,
#                                                                  **kwargs)
#         self._common_args()
# 
#     def add_argument(self, *args, **kwargs):
#         self.parser.add_argument(*args, **kwargs)
# 
#     def parse_args(self, args):
#         arguments = self.parser.parse_args(args)
#         if 'version' in arguments and arguments.version == self.DEFAULT_VERSION:
#             arguments.version = '1.0'
# 
#         return arguments
# 
#     def _common_args(self):
#         self.parser.add_argument('-S', '--settings', metavar='SETTINGS', type=str,
#                                  help='Path to the settings file.', default="settings.cfg")
# 
# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)
# 
# Path: lib/Settings.py
# class Settings:
#     def __init__(self,
#                  override_settings_path='settings.cfg'):
#         self.settingsFilePath = override_settings_path
#         self.parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
#         self.settings = None
#         self.getSettings()
# 
#     def getSettings(self):
#         self.parser.read('./settings.cfg')
#         self.settings = {section: dict(**self.parser[section]) for section in self.parser.sections()}
# 
#         return self.settings
# 
#     def getServiceIp(self, service):
#         return self.settings['general']['my_ip']
# 
# Path: lib/DomainManagement/Domain.py
# class Domain:
#     def __init__(self):
#         self.logger = Logger(__name__)
#         self.mail = MailDomain()
# 
#     def create_domain(self, domain):
#         self.mail.create_domain(domain)
# 
#     def domain_exists(self, domain):
#         return self.mail.domain_exists(domain)


# Path: lib/Subcommands/CreateDomain.py
from lib.Wrappers.ArgumentParser import ArgumentParser
from lib.Wrappers.Logger import Logger
from lib.Settings import Settings
from lib.DomainManagement.Domain import Domain
import re
import sys



class CreateDomain:

    def __init__(self):
        self.settings = Settings().getSettings()
        self.logger = Logger(__name__)
        self.domain = Domain()

    def execute(self, *args):
        args = self._parse_arguments(*args)
        args.domain[0] = args.domain[0].lower()
        if self.domain.domain_exists(args.domain[0]) is True:
            self.logger.error("Domain already exists")
            sys.exit(1)
        regex = "^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]{0,1}\.([a-zA-Z]{1,6}|[a-zA-Z0-9-]{1,30}\.[a-zA-Z]{2,})$"
        pattern = re.compile(regex)
        if not pattern.match(args.domain[0]):
            self.logger.error("The domain you provided is not valid!")

            sys.exit(1)