import posixpath 
from parallels.core.connections.source_server import SingleServerPanelSourceServer
from parallels.core.runners.entities import ExecutionOptions


class ConfixxSourceServer(SingleServerPanelSourceServer):
    def __init__(self, node_id, node_settings, global_context):
        super(SingleServerPanelSourceServer, self).__init__(
            node_id, node_settings, global_context.migrator_server
        )
        self._global_context = global_context

    def get_unix_vhost_dirs(self, runner, domain_name):
        """Return list of directories with domain hosting content."""
        return [self._global_context.confixx_configuration.get_domain_vhost_dir(domain_name)]

    def get_unix_domain_mail_dirs(self, runner, domain_name):
        """Return list of directories with system domain mail content.
        
        In Confixx, mail directories/mailboxes are named this way:
            <client id>p<mail user number>
        , for example:
            web123p44
            
        :type runner: parallels.core.runners.base.BaseRunner
        """
        mail_dirs = runner.execute_command(
            u'ls -d {mailbox_base_path}p*', dict(
                mailbox_base_path=posixpath.join(
                    self._global_context.confixx_configuration.get_mailbox_root(),
                    self._global_context.confixx_configuration.get_client_id(domain_name)
                )
            ),
            ExecutionOptions(ignore_exit_code=True)
        ).stdout
        return mail_dirs.split('\n')
