Syntax File Module

import codecs
import platform

import raconverter
import raconverter.data_file
from raconverter.constants import *


class SyntaxFile:
    """
    Create a syntax file in a supported statistical program language.
    """

    def __init__(self):

        # Instantiate class
        self.data_file = raconverter.data_file.DataFile()

    def create_syntax(self, directory_path, file_name, file_extension):
        """
        Create syntax output.

        :param directory_path: The path to the directory with the data file
        :param file_name: The physical name of the data file without extension
        :param file_extension: The data file extension
        :return: A list with the syntax content
        """
        syntax_content = []

        # Return platform name
        platform_name = platform.system()
        # Set default directory separator (Unix)
        directory_separator = UNIX_SEPARATOR
        # Change default directory separator to Windows (if needed)
        if platform_name == WINDOWS_OS:
            directory_separator = WINDOWS_SEPARATOR

        # If Stata data file
        if file_extension == STATA_DATA:
            syntax_content.append(STATA_SYNTAX_CONTENT)
        # If SAS data file
        elif file_extension == SAS_DATA:
            include_catalog = self.data_file.get_catalog_file(file_name)
            syntax_content = self.create_sas_syntax(include_catalog)
        # If SPSS data file
        elif file_extension == SPSS_DATA:
            syntax_content.append(SPSS_SYNTAX_CONTENT)

        # If syntax has been added
        if syntax_content:
            # Insert directory separator, path to file, data file name, dollar sign, and number sign
            syntax_content = [line.replace('{0}', directory_separator) for line in syntax_content]
            syntax_content = [line.replace('{1}', directory_path) for line in syntax_content]
            syntax_content = [line.replace('{2}', file_name) for line in syntax_content]
            syntax_content = [line.replace('{3}', DOLLAR_SIGN) for line in syntax_content]

        return syntax_content

    @staticmethod
    def create_sas_syntax(include_catalog=1):
        """
        Create SAS syntax with or without catalog file dependency.

        :param include_catalog: True (1) | False (0)
        :return: A list with the syntax content
        """
        syntax_content = []

        if include_catalog == 0:
            syntax_content.append(SAS_SYNTAX_CONTENT_WITHOUT_CATALOG)
        elif include_catalog == 1:
            syntax_content.append(SAS_SYNTAX_CONTENT_WITH_CATALOG)

        return syntax_content

    def write_syntax_file(self, file_name, file_extension, syntax_content):
        """
        Write a UTF-8 encoded syntax file.
        The path is automatically set to the current working directory.

        :param file_name: The physical name of the data file without extension
        :param file_extension: The data file extension
        :param syntax_content: A list with the syntax content
        """
        list_content = []

        # Add line breaks
        for item in syntax_content:
            list_content.extend('%s\n' % item)
        # Convert list to string
        string_content = ''.join(list_content)
        # Convert string to bytes (encode)
        byte_syntax = string_content.encode(UTF8)

        # Get file name
        syntax_extension = self.get_syntax_extension(file_extension)
        file = ''.join([file_name, syntax_extension])

        # Write syntax file
        with open(file, WRITE_MODE, encoding=UTF8) as f:
            # Add byte order mark to SPSS syntax
            if syntax_extension == SPSS_SYNTAX:
                bom_utf8 = codecs.BOM_UTF8
                f.write(bom_utf8.decode(UTF8))

            # Convert bytes to string (decode)
            f.write(byte_syntax.decode(UTF8))

    @staticmethod
    def get_data_file_application(file_extension):
        """
        Get the application name based on the data file extension.

        :param file_extension: The data file extension
        :return: The application name
        """
        application_name = ''

        if file_extension == STATA_DATA:
            application_name = STATA_PROGRAM
        elif file_extension == SAS_DATA:
            application_name = SAS_PROGRAM
        elif file_extension == SPSS_DATA:
            application_name = SPSS_PROGRAM

        return application_name

    @staticmethod
    def get_syntax_extension(file_extension):
        """
        Get the syntax extension based on the data file extension.

        :param file_extension: The data file extension
        :return: The syntax extension
        """
        syntax_extension = ''

        if file_extension == STATA_DATA:
            syntax_extension = STATA_SYNTAX
        elif file_extension == SAS_DATA:
            syntax_extension = SAS_SYNTAX
        elif file_extension == SPSS_DATA:
            syntax_extension = SPSS_SYNTAX

        return syntax_extension

Leave a Reply

Your email address will not be published.