# Path: hoplite/utils/server_logging.py
# def get_server_logger(name=""):
# def get_job_logger(name="", uuid=""):
# def _get_base_logger(name=""):
# def add_remote_function_logging_handlers(function_namespace, timestamp, filename=None):
# def get_log_filename(function_namespace, timestamp):
# def get_formatter_verbose():
# def get_formatter_limited():
# 
# Path: hoplite/client/status_updater.py
# class MockStatusUpdater(object):
#     """
#     For use while developing a job
#     """
#     def __init__(self, addr="0.0.0.0:5000", uuid="{0}", api_key=""):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Prints the status to stdout for debugging and sets the
#         member variable status for use in testing status updates
# 
#         :param status: status dictionary
#         """
#         self.status = status
#         print(self.status)
# 
# Path: hoplite/builtin_plugins/constants.py
# class DownloadFolderFromFtpJobConstants(object):
#     DEST_ROOT = "dest_root"
#     FTP_ROOT = "ftp_root"
#     SERVER_ADDRESS = "server_address"
#     SERVER_PORT = "server_port"
#     USERNAME = "username"
#     PASSWORD = "password"


from hoplite.utils import server_logging
from ftplib import FTP
from hoplite.client.status_updater import MockStatusUpdater
from hoplite.builtin_plugins.constants import DownloadFolderFromFtpJobConstants as KEYS
import ftplib
import os
import socket


def download_files(ftp_session, source, dest, status):
    # dest must already exist
    try:
        ftp_session.cwd(source)
    except ftplib.error_perm:
        # invalid entry (ensure input form: "/dir/folder/something/")
        msg = "Could not open the ftp directory: '{0}'".format(source)
        logger.error(msg)
        status.update({"error": msg})
        return
    if not os.path.isdir(dest):
        msg = "Local path does not exist: '{0}'".format(dest)
        logger.error(msg)
        status.update({"error": msg})
        return
    _get_files_in_dir(ftp_session, source, dest, status)


def run(config, status):
    """
    Job to recursively download a directory from an FTP server
    This will overwrite any files that are in the dest_root directory
    """
    ftp_addr = config.get(KEYS.SERVER_ADDRESS, "localhost")
    ftp_port = config.get(KEYS.SERVER_PORT, 21)
    ftp_root = config.get(KEYS.FTP_ROOT, "/")
    dest_root = config.get(KEYS.DEST_ROOT, "C:\\temp\\")
    user = config.get(KEYS.USERNAME, "")

# Path: hoplite/utils/server_logging.py
# def get_server_logger(name=""):
# def get_job_logger(name="", uuid=""):
# def _get_base_logger(name=""):
# def add_remote_function_logging_handlers(function_namespace, timestamp, filename=None):
# def get_log_filename(function_namespace, timestamp):
# def get_formatter_verbose():
# def get_formatter_limited():
# 
# Path: hoplite/client/status_updater.py
# class MockStatusUpdater(object):
#     """
#     For use while developing a job
#     """
#     def __init__(self, addr="0.0.0.0:5000", uuid="{0}", api_key=""):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Prints the status to stdout for debugging and sets the
#         member variable status for use in testing status updates
# 
#         :param status: status dictionary
#         """
#         self.status = status
#         print(self.status)
# 
# Path: hoplite/builtin_plugins/constants.py
# class DownloadFolderFromFtpJobConstants(object):
#     DEST_ROOT = "dest_root"
#     FTP_ROOT = "ftp_root"
#     SERVER_ADDRESS = "server_address"
#     SERVER_PORT = "server_port"
#     USERNAME = "username"
#     PASSWORD = "password"


# Path: hoplite/builtin_plugins/download_folder_from_ftp_job.py
from hoplite.utils import server_logging
from ftplib import FTP
from hoplite.client.status_updater import MockStatusUpdater
from hoplite.builtin_plugins.constants import DownloadFolderFromFtpJobConstants as KEYS
import ftplib
import os
import socket



def download_files(ftp_session, source, dest, status):
    # dest must already exist
    try:
        ftp_session.cwd(source)
    except ftplib.error_perm:
        # invalid entry (ensure input form: "/dir/folder/something/")
        msg = "Could not open the ftp directory: '{0}'".format(source)
        logger.error(msg)
        status.update({"error": msg})
        return
    if not os.path.isdir(dest):
        msg = "Local path does not exist: '{0}'".format(dest)
        logger.error(msg)
        status.update({"error": msg})
        return
    _get_files_in_dir(ftp_session, source, dest, status)


def run(config, status):
    """
    Job to recursively download a directory from an FTP server
    This will overwrite any files that are in the dest_root directory
    """
    ftp_addr = config.get(KEYS.SERVER_ADDRESS, "localhost")
    ftp_port = config.get(KEYS.SERVER_PORT, 21)
    ftp_root = config.get(KEYS.FTP_ROOT, "/")
    dest_root = config.get(KEYS.DEST_ROOT, "C:\\temp\\")
    user = config.get(KEYS.USERNAME, "")

    password = config.get(KEYS.PASSWORD, "")