File size: 3,463 Bytes
216c0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import uuid
import shutil
import tempfile

# Create a dedicated temporary directory
def ensure_temp_dir():
    """
    Ensures that a temporary directory exists in the current directory
    
    Returns:
        Path to the temporary directory
    """
    tmp_dir = os.path.join(os.getcwd(), "tmp")
    if not os.path.exists(tmp_dir):
        os.makedirs(tmp_dir)
    return tmp_dir

def get_random_filename(prefix="", suffix=""):
    """
    Generate a random filename with an optional prefix and suffix
    
    Args:
        prefix: Optional prefix for the filename
        suffix: Optional suffix for the filename (e.g. file extension)
        
    Returns:
        A unique random filename
    """
    random_id = str(uuid.uuid4())
    return f"{prefix}{random_id}{suffix}"

def create_temp_file(prefix="", suffix="", content=None):
    """
    Create a temporary file with random name in the tmp directory
    
    Args:
        prefix: Optional prefix for the filename
        suffix: Optional suffix for the filename
        content: Optional content to write to the file
        
    Returns:
        Path to the created temporary file
    """
    tmp_dir = ensure_temp_dir()
    filename = get_random_filename(prefix, suffix)
    filepath = os.path.join(tmp_dir, filename)
    
    if content is not None:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(content)
    
    return filepath

def create_temp_dir(prefix=""):
    """
    Create a temporary directory with a random name in the tmp directory
    
    Args:
        prefix: Optional prefix for the directory name
        
    Returns:
        Path to the created temporary directory
    """
    tmp_dir = ensure_temp_dir()
    dirname = get_random_filename(prefix=prefix)
    dirpath = os.path.join(tmp_dir, dirname)
    os.makedirs(dirpath)
    return dirpath

def cleanup_temp_file(filepath):
    """
    Delete a temporary file
    
    Args:
        filepath: Path to the temporary file
    """
    if os.path.exists(filepath):
        os.remove(filepath)

def cleanup_temp_dir(dirpath):
    """
    Delete a temporary directory and all its contents
    
    Args:
        dirpath: Path to the temporary directory
    """
    if os.path.exists(dirpath):
        shutil.rmtree(dirpath)

def create_fallback_svg(output_path, width=800, height=600, error_message="Failed to generate chart"):
    """
    Create a simple SVG file with an error message when all other methods fail
    
    Args:
        output_path: Path where to save the SVG file
        width: Width of the SVG
        height: Height of the SVG
        error_message: Error message to display in the SVG
        
    Returns:
        Path to the generated SVG file
    """
    svg_content = f"""<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">
    <rect width="100%" height="100%" fill="#f8f9fa" />
    <text x="50%" y="40%" font-family="Arial" font-size="20px" text-anchor="middle" font-weight="bold">
        Error Generating
    </text>
    <text x="50%" y="50%" font-family="Arial" font-size="16px" text-anchor="middle">
        {error_message}
    </text>
    <text x="50%" y="60%" font-family="Arial" font-size="14px" text-anchor="middle" fill="#555">
        Please check the console for detailed error messages.
    </text>
</svg>"""
    
    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(svg_content)
    
    return output_path