# Path: federal_spending/usaspending/utils/log.py
# def set_up_logger(importer_name, log_path, email_subject, email_recipients=settings.LOGGING_EMAIL['recipients']):
#     # create logger
#     log = logging.getLogger(importer_name)
#     log.setLevel(logging.DEBUG)
#     formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# 
#     # create console handler and set level to debug
#     ch = logging.FileHandler(os.path.join(log_path, importer_name + '.log'))
#     ch.setLevel(logging.DEBUG)
#     ch.setFormatter(formatter)
#     log.addHandler(ch)
# 
#     # create email handler and set level to warn
#     if settings.LOGGING_EMAIL:
#         eh = logging.handlers.SMTPHandler(
#             (settings.LOGGING_EMAIL['host'], settings.LOGGING_EMAIL['port']), # host
#             settings.LOGGING_EMAIL['username'], # from address
#             email_recipients,
#             email_subject,
#             (settings.LOGGING_EMAIL['username'], settings.LOGGING_EMAIL['password']) # credentials tuple
#         )
#         eh.setLevel(logging.WARN)
#         eh.setFormatter(formatter)
#         eh.setFormatter(EncodingFormatter('%(message)s', encoding='iso8859-1'))
#         log.addHandler(eh)
# 
#     return log


import os
import os.path
import fnmatch
import time
import datetime
from federal_spending.usaspending.utils.log import set_up_logger
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from django.conf import settings

class BaseImporter(BaseCommand):

    IN_DIR       = None # '/home/datacommons/data/auto/nimsp/raw/IN'
    DONE_DIR     = None # '/home/datacommons/data/auto/nimsp/raw/DONE'
    REJECTED_DIR = None # '/home/datacommons/data/auto/nimsp/raw/REJECTED'
    OUT_DIR      = None # '/home/datacommons/data/auto/nimsp/denormalized/IN'
    FILE_PATTERN = None # bash-style, ala '*.sql'

    email_subject = 'Unhappy Loading App'
    email_recipients = settings.LOGGING_EMAIL['recipients']

    # initializing this here so that tests which don't call handle() don't fail
    dry_run = None

    option_list = BaseCommand.option_list + (
        make_option('--dry-run', '-d',
            action='store_true',
            dest='dry_run',
            default=False,
            help='Do a test run of the command, only printing what would be done.'
        ),
    )


    def __init__(self):
        super(BaseImporter, self).__init__()
        self.class_name = self.__class__.__name__
        self.module_name = self.__module__.split('.')[-1]
        self.log_path = settings.LOGGING_DIRECTORY

# Path: federal_spending/usaspending/utils/log.py
# def set_up_logger(importer_name, log_path, email_subject, email_recipients=settings.LOGGING_EMAIL['recipients']):
#     # create logger
#     log = logging.getLogger(importer_name)
#     log.setLevel(logging.DEBUG)
#     formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# 
#     # create console handler and set level to debug
#     ch = logging.FileHandler(os.path.join(log_path, importer_name + '.log'))
#     ch.setLevel(logging.DEBUG)
#     ch.setFormatter(formatter)
#     log.addHandler(ch)
# 
#     # create email handler and set level to warn
#     if settings.LOGGING_EMAIL:
#         eh = logging.handlers.SMTPHandler(
#             (settings.LOGGING_EMAIL['host'], settings.LOGGING_EMAIL['port']), # host
#             settings.LOGGING_EMAIL['username'], # from address
#             email_recipients,
#             email_subject,
#             (settings.LOGGING_EMAIL['username'], settings.LOGGING_EMAIL['password']) # credentials tuple
#         )
#         eh.setLevel(logging.WARN)
#         eh.setFormatter(formatter)
#         eh.setFormatter(EncodingFormatter('%(message)s', encoding='iso8859-1'))
#         log.addHandler(eh)
# 
#     return log


# Path: federal_spending/usaspending/management/base/importer.py
import os
import os.path
import fnmatch
import time
import datetime
from federal_spending.usaspending.utils.log import set_up_logger
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from django.conf import settings


class BaseImporter(BaseCommand):

    IN_DIR       = None # '/home/datacommons/data/auto/nimsp/raw/IN'
    DONE_DIR     = None # '/home/datacommons/data/auto/nimsp/raw/DONE'
    REJECTED_DIR = None # '/home/datacommons/data/auto/nimsp/raw/REJECTED'
    OUT_DIR      = None # '/home/datacommons/data/auto/nimsp/denormalized/IN'
    FILE_PATTERN = None # bash-style, ala '*.sql'

    email_subject = 'Unhappy Loading App'
    email_recipients = settings.LOGGING_EMAIL['recipients']

    # initializing this here so that tests which don't call handle() don't fail
    dry_run = None

    option_list = BaseCommand.option_list + (
        make_option('--dry-run', '-d',
            action='store_true',
            dest='dry_run',
            default=False,
            help='Do a test run of the command, only printing what would be done.'
        ),
    )


    def __init__(self):
        super(BaseImporter, self).__init__()
        self.class_name = self.__class__.__name__
        self.module_name = self.__module__.split('.')[-1]
        self.log_path = settings.LOGGING_DIRECTORY

        self.log = set_up_logger(self.module_name, self.log_path, self.email_subject, email_recipients=self.email_recipients)