Data File Module

import os
import tkinter
import tkinter.filedialog

from raconverter.constants import *


class DataFile:
    """
    Get and evaluate file attributes against supported data file types.
    """

    @staticmethod
    def get_full_file_name():
        """
        Get the full file name including path, file name, and file extension.

        :return: The full file name including path, file name, and file extension
        """
        # Select a file (prompt open file dialog box)
        full_file_name = tkinter.filedialog.askopenfilename()

        return full_file_name

    @staticmethod
    def get_directory_path(full_file_name):
        """
        Get the directory path from the full file name.

        :param full_file_name: The full file name including path, file name, and file extension
        :return: The path to the file directory
        """
        directory_path = os.path.split(os.path.abspath(full_file_name))[0]

        return directory_path

    @staticmethod
    def get_file_name(full_file_name):
        """
        Get the file name from the full file name.

        :param full_file_name: The full file name including path, file name, and file extension
        :return: The file name without extension
        """
        file_name_with_extension = os.path.split(os.path.abspath(full_file_name))[1]
        file_name = os.path.splitext(file_name_with_extension)[0]

        return file_name

    @staticmethod
    def get_file_extension(full_file_name):
        """
        Get the file extension from the full file name.

        :param full_file_name: The full file name including path, file name, and file extension
        :return: The file extension
        """
        file_name_with_extension = os.path.split(os.path.abspath(full_file_name))[1]
        file_extension = os.path.splitext(file_name_with_extension)[1]

        return file_extension

    @staticmethod
    def validate_file_extension(file_extension):
        """
        Validate the file extension.
        The file extension is valid if it represents a data file from a supported statistical program.

        :param file_extension: The file extension
        :return: True | False
        """
        valid = True

        string_list = VALID_EXTENSIONS
        valid_extensions = string_list.split()

        # If invalid file extension
        if file_extension not in valid_extensions:
            valid = False

        return valid

    @staticmethod
    def get_catalog_file(file_name):
        """
        Check whether a catalog file with a given name exists.
        The path is automatically set to the current working directory.

        :param file_name: The physical name of the catalog file without extension
        :return: True | False
        """
        valid = True

        # Get the name of the expected catalog file
        catalog_file = file_name + SAS_CATALOG

        # If the catalog file is missing
        if not os.path.isfile(catalog_file):
            valid = False

        return valid

    @staticmethod
    def detect_any_catalog_file():
        """
        # Check whether the current working directory contains any catalog file.

        :return: True | False
        """
        valid = False

        for file in os.listdir('.'):
            if file.endswith(SAS_CATALOG):
                valid = True
                # Exit for loop
                break

        return valid

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.