File size: 2,619 Bytes
b192407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Utils Module - Utility functions for the environment.

This module provides helper functions:
- Timeout handling for action execution
- File validation (CSV, JSON, XML, YAML)
- Directory management (create, delete)
- SHA256 hash calculation for file comparison

Reference: https://github.com/yiyihum/da-code/tree/main/da_agent/envs/utils.py
"""

import signal
import os
import hashlib
import shutil
import os
import pandas as pd
import json
import xml.etree.ElementTree as ET
import yaml


TIMEOUT_DURATION = 25

def is_file_valid(file_path):
    ext = os.path.splitext(file_path)[1].lower()
    try:
        if ext == '.csv':
            pd.read_csv(file_path)
        elif ext == '.json':
            with open(file_path, 'r') as f:
                json.load(f)
        elif ext == '.xml':
            ET.parse(file_path)
        elif ext == '.yaml' or ext == '.yml':
            with open(file_path, 'r') as f:
                yaml.safe_load(f)
        else:
            return True, None
        return True, None
    except Exception as e:
        return False, str(e)
        
import sys
import threading
class timeout:
    def __init__(self, seconds=TIMEOUT_DURATION, error_message="Timeout"):
        self.seconds = seconds
        self.error_message = error_message


    def handle_timeout(self, signum, frame):
        raise TimeoutError(self.error_message)


    def __enter__(self):
        # signal.signal(signal.SIGALRM, self.handle_timeout)
        # signal.alarm(self.seconds)
       
        if sys.platform != 'win32':
            signal.signal(signal.SIGALRM, self.handle_timeout)
            signal.alarm(self.seconds)
        else:
            self.timer = threading.Timer(self.seconds, self.handle_timeout)
            self.timer.start()


    def __exit__(self, type, value, traceback):
        # signal.alarm(0)
        if sys.platform != 'win32':
            signal.alarm(0)  # Cancel the alarm
        else:
            self.timer.cancel()  # Cancel the timer



def delete_files_in_folder(folder_path):
    if os.path.exists(folder_path):
        files = os.listdir(folder_path)
        for file in files:
            file_path = os.path.join(folder_path, file)
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        
def create_folder_if_not_exists(path):
    if not os.path.exists(path):
        os.makedirs(path)

def calculate_sha256(file_path):
    with open(file_path, 'rb') as f:
        file_data = f.read()
        return hashlib.sha256(file_data).hexdigest()