RaConverter

import os
import platform

from raconverter.constants import *


class Misc:
    """
    Internal class.

    The class defines methods that are (or may be) shared among the modules.
    """

    @staticmethod
    def center_window_on_screen(top_level):
        """
        Center the window on the screen.

        :param top_level: The top level window
        """
        top_level.withdraw()
        top_level.update_idletasks()
        x = (top_level.winfo_screenwidth() - top_level.winfo_reqwidth()) / 2
        y = (top_level.winfo_screenheight() - top_level.winfo_reqheight()) / 2
        top_level.geometry(OFFSET % (x, y))
        top_level.deiconify()

    @staticmethod
    def is_non_empty_file(file_name):
        """
        Check whether the file exists.
        The path is automatically set to the current working directory.

        :param file_name: The physical file name with file extension
        :return: Empty string | The file name with extension
        """
        file = ''

        # If file name exists
        if os.path.isfile(file_name) and os.path.getsize(file_name) > 0:
            file = file_name

        return file

    @staticmethod
    def validate_encoding(file_name):
        """
        Check whether the file is valid UTF-8.
        Move file to a new subdirectory if a decoding error occurs.
        The path is automatically set to the current working directory.

        :param file_name: The physical file name with file extension
        :return: True | False
        """
        # Get platform name
        platform_name = platform.system()
        # Set directory separator
        directory_separator = UNIX_SEPARATOR
        if platform_name == WINDOWS_OS:
            directory_separator = WINDOWS_SEPARATOR

        # Set subdirectory
        new_folder = DECODING_ERROR_DIRECTORY
        # Set destination
        destination = new_folder + directory_separator + file_name

        try:
            with open(file_name, encoding=UTF8, errors=STRICT) as f:
                f.read()
        except UnicodeDecodeError:
            # Create subdirectory
            if not os.path.exists(new_folder):
                os.makedirs(new_folder)
            # Remove existing file in destination
            if os.path.isfile(destination):
                os.remove(destination)
            # Move file to subdirectory
            os.rename(file_name, destination)
            return False
        else:
            pass

        return True

    @staticmethod
    def remove_bom(file_name):
        """
        Remove byte order mark in a UTF-8 encoded file.
        The path is automatically set to the current working directory.

        :param file_name: The physical file name with file extension
        """
        # Read file as string
        with open(file_name, mode=READ_MODE, encoding=UTF8) as f:
            data = f.read()

        # Convert string to bytes (encode)
        encoded_bytes = data.encode(UTF8)
        # Convert bytes to string (decode) - without byte order mark
        decoded_bytes = encoded_bytes.decode(UTF8_NO_BOM)

        # Write file
        with open(file_name, mode=WRITE_MODE, encoding=UTF8) as f:
            f.write(decoded_bytes)

    @staticmethod
    def list_comprehension(file_name):
        """
        Add the content of a UTF-8 encoded file to a list.
        The path is automatically set to the current working directory.

        :param file_name: The physical name of the file without extension
        :return: A list with the file content (each line is an item in the list)
        """
        file_content = [line.strip() for line in open(file_name, READ_MODE, encoding=UTF8)]

        return file_content

Leave a Reply

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


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