class GlobalExceptionHandlerController:
    def __init__(self):
        self.__logger = Logger().get_logger()

    def user_api_errors(self, error: UserApiException):
        self.__logger.error(f"Error during process: {error.message}")
        error_json = dumps(error.to_dict())

        return Response(error_json, status=error.status_code)

    def unknown_errors(self, error: Exception):
        self.__logger.error(f"Unknown exception: {str(error)}")

        return Response(str(error), status=500)
Exemplo n.º 2
0
class ReaderFromFile(DataProvider):
    """
    This class allow to provide data read from file.
    Args :
    :param dimension: int that represent dimention of the vector that will be in the data.
    :param file_name: Name of file containing the data.
    """
    def __init__(self, dimension, file_path):
        DataProvider.__init__(self, dimension)
        self.file_path = file_path
        self.logger = Logger('ReaderFromFile')

    #@overrides(DataProvider)
    def get_points(self):
        # We test if the file exist
        path_file = Path(self.file_path)
        self.logger.info('We get the points from' + str(path_file))

        if not path_file.is_file():
            self.logger.error('The file does not exist')
            raise Exception("The file does not exist" + str(path_file))

        data_file = pd.read_csv(str(path_file))
        line_counter = -1
        try:
            for i, row in enumerate(data_file.values):
                line_counter += 1
                coodinates = [
                    Decimal(int(elem)) for index, elem in enumerate(row)
                    if index != 0
                ]

                # We get a dimension problem.
                if len(coodinates) != self.dimension:
                    self.logger.error(
                        'The dimension is not correct, expected : ' +
                        str(len(coodinates)) + str(self.dimension))
                    raise Exception("The dimension of the point at line " +
                                    str(line_counter) + " is not correct")
                self.point_list.append(Point(coodinates))

        except Exception as e:
            self.logger.error("Probleme during reading line " +
                              str(line_counter))
            raise e

        return self.point_list
Exemplo n.º 3
0
class Arf():
    """
    This class is an implementation of Arf.
    Args :
    :param lambda_error: float representing how each coordinates of the points should be ceil or floor.
    """

    def __init__(self, path):
        self.path = path
        self.logger = Logger('Arf')
        self.compile()

    def compile(self):
        """
        Compile the C++ Code
        :return:
        """
        executable = os.path.join(self.path, MAKE_FILE)
        self.logger.info('Compile C++ code to : '+str(executable))
        print(executable)
        if os.path.isfile(executable):
            try:
                old_path = os.getcwd()
                # Change the current directory
                os.chdir(os.path.join(old_path, self.path))

                # clean the repo
                output = subprocess.check_output([MAKE_COMMAND, CLEAN], stderr=None)
                self.logger.debug('Make clean')
                # compile c++ code
            except subprocess.CalledProcessError as e:
                self.logger.error('Impossible to make clean')

            try :
                output = subprocess.check_output([MAKE_COMMAND], stderr=subprocess.STDOUT)
                self.logger.debug('Make')

                # reset the olde path.
                os.chdir(old_path)

            except subprocess.CalledProcessError as e:
                os.chdir(old_path)
                raise(e)

        else:
            raise Exception("Impossible to compile Arf code")


    def execute_program(self, argv):
        """
        Call the c++ program.
        :param argv:
        :return:
        """
        size = None
        false_positive = None
        executable = os.path.join(self.path, NAME_EXECUTABLE)

        print
        if os.path.isfile(executable):
            try:
                old_path = os.getcwd()

                # Change the current directory
                os.chdir(os.path.join(old_path, self.path))

                # clean the repo
                command = './' + " ".join([NAME_EXECUTABLE] + argv)
                self.logger.debug("size: "+ argv[4])
                output = subprocess.check_output(command, shell=True, stderr=subprocess.PIPE)
                outputs = str(output).split("*");
                size = outputs[1]
                false_positive = outputs[3]

                # Reset the directory
                os.chdir(old_path)

            except subprocess.CalledProcessError as e:
                self.logger.error(e.output)
                self.logger.error("Impossible to run Node program")

        else:
            raise Exception("Impossible to find Node program")

        return size, false_positive