Skip to content

file_handler

FileHandler

Source code in libs\cafex_core\src\cafex_core\handlers\file_handler.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class FileHandler:

    @staticmethod
    def create_json_file(directory, filename, data=None, indent=2):
        """Creates a new JSON file at the specified directory with the given
        filename.

        Args:
            directory (str): The directory where the file will be created.
            filename (str): The name of the file to be created.
            data (list or dict): The data to be written to the file. Can be a list or a dictionary.
            indent (int): The number of spaces to use for indentation. Default is 2.
        """
        filepath = os.path.join(directory, filename)
        if data is None:
            data = {}
        with open(filepath, "w") as file:
            json.dump(data, file, indent=indent)

    @staticmethod
    def delete_file(filepath):
        """Deletes the file at the specified filepath.

        Args:
            filepath (str): The path of the file to be deleted.
        """
        if os.path.exists(filepath):
            os.remove(filepath)
        else:
            print("File does not exist.")

    @staticmethod
    def add_data_to_json_file(filepath, data):
        """Adds data to an existing JSON file.

        Args:
            filepath (str): The path of the JSON file.
            data (dict): The data to be added to the JSON file.
        """
        with open(filepath, "r+") as file:
            json_data = json.load(file)
            json_data.update(data)
            file.seek(0)
            json.dump(json_data, file, indent=4)

    @staticmethod
    def read_data_from_json_file(filepath):
        """Reads data from a JSON file and returns it.

        Args:
            filepath (str): The path of the JSON file.

        Returns:
            dict or list: The data read from the JSON file.
        """

        try:
            with open(filepath, "r") as file:
                return json.load(file)
        except FileNotFoundError:
            raise FileNotFoundError(f"JSON file not found: {filepath}")
        except json.JSONDecodeError:
            raise ValueError(f"Invalid JSON in file: {filepath}")

add_data_to_json_file(filepath, data) staticmethod

Adds data to an existing JSON file.

Parameters:

Name Type Description Default
filepath str

The path of the JSON file.

required
data dict

The data to be added to the JSON file.

required
Source code in libs\cafex_core\src\cafex_core\handlers\file_handler.py
36
37
38
39
40
41
42
43
44
45
46
47
48
@staticmethod
def add_data_to_json_file(filepath, data):
    """Adds data to an existing JSON file.

    Args:
        filepath (str): The path of the JSON file.
        data (dict): The data to be added to the JSON file.
    """
    with open(filepath, "r+") as file:
        json_data = json.load(file)
        json_data.update(data)
        file.seek(0)
        json.dump(json_data, file, indent=4)

create_json_file(directory, filename, data=None, indent=2) staticmethod

Creates a new JSON file at the specified directory with the given filename.

Parameters:

Name Type Description Default
directory str

The directory where the file will be created.

required
filename str

The name of the file to be created.

required
data list or dict

The data to be written to the file. Can be a list or a dictionary.

None
indent int

The number of spaces to use for indentation. Default is 2.

2
Source code in libs\cafex_core\src\cafex_core\handlers\file_handler.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@staticmethod
def create_json_file(directory, filename, data=None, indent=2):
    """Creates a new JSON file at the specified directory with the given
    filename.

    Args:
        directory (str): The directory where the file will be created.
        filename (str): The name of the file to be created.
        data (list or dict): The data to be written to the file. Can be a list or a dictionary.
        indent (int): The number of spaces to use for indentation. Default is 2.
    """
    filepath = os.path.join(directory, filename)
    if data is None:
        data = {}
    with open(filepath, "w") as file:
        json.dump(data, file, indent=indent)

delete_file(filepath) staticmethod

Deletes the file at the specified filepath.

Parameters:

Name Type Description Default
filepath str

The path of the file to be deleted.

required
Source code in libs\cafex_core\src\cafex_core\handlers\file_handler.py
24
25
26
27
28
29
30
31
32
33
34
@staticmethod
def delete_file(filepath):
    """Deletes the file at the specified filepath.

    Args:
        filepath (str): The path of the file to be deleted.
    """
    if os.path.exists(filepath):
        os.remove(filepath)
    else:
        print("File does not exist.")

read_data_from_json_file(filepath) staticmethod

Reads data from a JSON file and returns it.

Parameters:

Name Type Description Default
filepath str

The path of the JSON file.

required

Returns:

Type Description

dict or list: The data read from the JSON file.

Source code in libs\cafex_core\src\cafex_core\handlers\file_handler.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@staticmethod
def read_data_from_json_file(filepath):
    """Reads data from a JSON file and returns it.

    Args:
        filepath (str): The path of the JSON file.

    Returns:
        dict or list: The data read from the JSON file.
    """

    try:
        with open(filepath, "r") as file:
            return json.load(file)
    except FileNotFoundError:
        raise FileNotFoundError(f"JSON file not found: {filepath}")
    except json.JSONDecodeError:
        raise ValueError(f"Invalid JSON in file: {filepath}")