Exemple #1
0
    def set_output_dir_folder(self, output_folder):
        """Method to set the output directory

        :type output_folder: String
        :param output_folder: The folder to output to

        :rtype: None
        :returns: None
        """
        pdt.verify_directory(output_folder,
                             self.output_dir,
                             directory_create=True)
        self.output_dir = os.path.join(self.output_dir, output_folder)
Exemple #2
0
    def set_output_dir_folder(self, output_folder):
        """
        Method to send output to a folder in the output directory
        :param output_folder: The name of the folder
        :return:
            None

        """
        if isinstance(output_folder, str):
            pdt.verify_directory(output_folder,
                                 self.output_dir,
                                 directory_create=True)
            self.output_dir = os.path.join(self.output_dir, output_folder)

        else:
            error = 'A string is required as a argument for method set_output_dir_folder in class {}'.format(
                self)
            LOGGER.critical(error)
            raise TypeError(error)
    def __split_config(self):
        LOGGER.debug(
            'Starting method __split_config in class {class_obj}'.format(
                class_obj=type(self)))
        if self.hostname:
            pdt.verify_directory(self.hostname,
                                 self.output_dir,
                                 directory_create=True)
        else:
            self.hostname = 'NONE'
            pdt.verify_directory('NONE',
                                 self.output_dir,
                                 directory_create=True)

        if self.output_text_files:
            self.__output_text_files()

        else:
            self.__output_spreadsheet(self.hostname)
Exemple #4
0
    def __init__(self, base_dir, **kwargs):
        self.base_dir = base_dir
        self.logging_level = logging.WARNING
        self.data_dir = os.path.join(self.base_dir, 'Data')
        pdt.verify_directory('Data', self.base_dir, directory_create=True)
        try:
            self.yml_config_data = open(
                os.path.join(self.data_dir, 'config.yml'))

        except FileNotFoundError as e:
            self.__create_config_yml_file()
            self.yml_config_data = open(
                os.path.join(self.data_dir, 'config.yml'))

        self.dir_config = safe_load(self.yml_config_data).get('config')

        self.input_dir = self.__set_directory('input_directory', 'Input')

        self.output_dir = self.__set_directory('output_directory', 'Output')

        self.logging_dir = self.__set_directory('logging_directory', 'Logs')

        if kwargs.get('add_directories'):
            if isinstance(kwargs.get('add_directories'), dict):
                for dir_key, dir_value in kwargs.get(
                        'add_directories').items():
                    setattr(Directories, dir_key,
                            self.__set_directory(dir_key, dir_value))

            else:
                error = 'The add_directories kwarg takes a dict, ' \
                        'you entered a {}'.format(type(kwargs.get('add_directories')))
                LOGGER.critical(error)
                raise TypeError(error)

        # This sets the default logging level
        if self.dir_config.get('logging_level'):
            self.set_logging_level(self.dir_config.get('logging_level'))
Exemple #5
0
    def __set_directory(self, yml_key, default_dir):
        """
        Method to create a default directory, or use the yml config file
        :param yml_key: The key in the yml file to use
        :param default_dir: The default directory
        :return:
            The "Created" directory

        """
        if self.dir_config.get(yml_key):
            temp_dir = self.dir_config.get(yml_key)
            if not os.path.isdir(temp_dir):
                LOGGER.critical(
                    'Could not find directory stated in config.yml file {}'.
                    format(temp_dir))
                exit('Bad directory {}'.format(temp_dir))

        else:
            temp_dir = os.path.join(self.base_dir, default_dir)
            pdt.verify_directory(default_dir,
                                 self.base_dir,
                                 directory_create=True)

        return temp_dir
Exemple #6
0
LOGGER = logging.getLogger(__name__)
LOGGING_LEVEL = None

if __name__ == '__main__':
    if __status__ == 'prod':
        BASE_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))

    elif __status__ == 'dev':
        BASE_DIR = os.path.dirname(os.path.realpath(__file__))

    INPUT_DIR = os.path.join(BASE_DIR, 'Input')
    OUTPUT_DIR = os.path.join(BASE_DIR, 'Output')
    LOGGING_DIR = os.path.join(BASE_DIR, 'Logs')
    DATA_DIR = os.path.join(BASE_DIR, 'Data')
    LOGGING_LEVEL = logging.WARNING
    pdt.verify_directory('Input', BASE_DIR, directory_create=True)
    pdt.verify_directory('Output', BASE_DIR, directory_create=True)
    pdt.verify_directory('Logs', BASE_DIR, directory_create=True)
    pdt.verify_directory('Data', BASE_DIR, directory_create=True)
    existing_input_files = pdt.list_files_in_directory(INPUT_DIR)
    logging.basicConfig(
        format='%(asctime)s: %(name)s - %(levelname)s - %(message)s',
        filename=os.path.join(LOGGING_DIR, 'logs.txt'))
    logging.getLogger().setLevel(LOGGING_LEVEL)
    LOGGER.warning('Logging started!!')

    arg_parser = ArgumentParser(description='Simple Command Line Tools')
    arg_parser.add_argument(
        '-f',
        '--folder',
        help='Put output file in a folder, name of the folder')
Exemple #7
0
    def __init__(self, base_dir):  # pylint: disable=too-many-branches,too-many-statements
        self.base_dir = base_dir
        self.data_dir = os.path.join(self.base_dir, 'Data')
        pdt.verify_directory('Data', self.base_dir, directory_create=True)
        with open(os.path.join(self.data_dir, 'config.yml'), 'r') as file:
            self.yml_config_data = file.read()

        self.dir_config = safe_load(self.yml_config_data).get('config')
        self.templates_dir = list()
        self.yml_dir = list()

        # This sets the location of the yml input directory
        self.yml_dir.append(os.path.join(self.base_dir, 'yaml'))
        if self.dir_config.get('yml_directory'):
            if isinstance(self.dir_config.get('yml_directory'), list):
                self.yml_dir += self.dir_config.get('yml_directory')

            else:
                self.yml_dir.append(self.dir_config.get('yml_directory'))

            for directory in self.yml_dir:
                if not os.path.isdir(directory):
                    LOGGER.critical(
                        'Could not find yml directory stated in config file %s',
                        directory)
                    sys.exit('Bad yml directory {}'.format(directory))

        else:
            pdt.verify_directory('yaml', self.base_dir, directory_create=True)

        # This sets the default directory for templates, and adds extra directories for templates
        self.templates_dir.append(os.path.join(self.base_dir, 'templates'))
        if self.dir_config.get('templates_directory'):
            if isinstance(self.dir_config.get('templates_directory'), list):
                self.templates_dir += self.dir_config.get(
                    'templates_directory')

            else:
                self.templates_dir.append(
                    self.dir_config.get('templates_directory'))

            for directory in self.templates_dir:
                if not os.path.isdir(directory):
                    LOGGER.critical(
                        'Could not find templates in directory stated in config file %s',
                        directory)
                    sys.exit('Bad templates directory {}'.format(directory))

        else:
            pdt.verify_directory('templates',
                                 self.base_dir,
                                 directory_create=True)

        # This sets the location of the output directory
        if self.dir_config.get('output_directory'):
            self.output_dir = self.dir_config.get('output_directory')
            if not os.path.isdir(self.output_dir):
                LOGGER.critical(
                    'Could not find output directory stated in config file %s',
                    self.output_dir)
                sys.exit('Bad output directory {}'.format(self.output_dir))

        else:
            self.output_dir = os.path.join(self.base_dir, 'Output')
            pdt.verify_directory('Output',
                                 self.base_dir,
                                 directory_create=True)

        # This sets the logging directory
        if self.dir_config.get('logging_directory'):
            self.logging_dir = self.dir_config.get('logging_directory')
            if not os.path.isdir(self.logging_dir):
                LOGGER.critical(
                    'Could not find logging directory stated in config file %s',
                    self.logging_dir)
                sys.exit('Bad logging directory {}'.format(self.logging_dir))

        else:
            self.logging_dir = os.path.join(self.base_dir, 'Logs')
            pdt.verify_directory('Logs', self.base_dir, directory_create=True)

        # This sets the logging level
        if self.dir_config.get('logging_level'):
            self.set_logging_level(self.dir_config.get('logging_level'))

        else:
            self.logging_level = logging.WARNING
 def set_output_dir_folder(self, output_folder):
     pdt.verify_directory(output_folder,
                          self.output_dir,
                          directory_create=True)
     self.output_dir = os.path.join(self.output_dir, output_folder)