File size: 6,848 Bytes
fb503bc
 
 
 
 
 
d3ba43b
fb503bc
d3ba43b
 
 
fb503bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
File I/O utilities for JSON, JSONL, Pickle, Text, and CSV files.
Provides safe read/write with error handling.
"""

import csv
import json
import logging
from pathlib import Path
import pickle
from typing import Any, Dict, Iterator, List, Optional, Union

logger = logging.getLogger(__name__)


# ---------- Text (TXT) ----------
def read_text(file_path: Union[str, Path], encoding: str = "utf-8") -> str:
    """Read entire content of a text file."""
    path = Path(file_path)
    try:
        with open(path, "r", encoding=encoding) as f:
            return f.read()
    except Exception as e:
        logger.error(f"Failed to read text file {path}: {e}")
        raise


def write_text(file_path: Union[str, Path], content: str, encoding: str = "utf-8") -> None:
    """Write string content to a text file."""
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    try:
        with open(path, "w", encoding=encoding) as f:
            f.write(content)
    except Exception as e:
        logger.error(f"Failed to write text file {path}: {e}")
        raise


# ---------- JSON ----------
def read_json(file_path: Union[str, Path]) -> Any:
    """Load JSON from file."""
    path = Path(file_path)
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception as e:
        logger.error(f"Failed to read JSON file {path}: {e}")
        raise


def write_json(file_path: Union[str, Path], data: Any, indent: int = 2) -> None:
    """Write data to JSON file."""
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    try:
        with open(path, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=indent, ensure_ascii=False)
    except Exception as e:
        logger.error(f"Failed to write JSON file {path}: {e}")
        raise


# ---------- JSONL (JSON Lines) ----------
def read_jsonl(file_path: Union[str, Path]) -> List[Dict]:
    """Read JSONL file (one JSON object per line) into a list of dicts."""
    path = Path(file_path)
    data = []
    try:
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    data.append(json.loads(line))
        return data
    except Exception as e:
        logger.error(f"Failed to read JSONL file {path}: {e}")
        raise


def write_jsonl(file_path: Union[str, Path], data: List[Dict]) -> None:
    """Write list of dicts to JSONL file."""
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    try:
        with open(path, "w", encoding="utf-8") as f:
            for item in data:
                f.write(json.dumps(item, ensure_ascii=False) + "\n")
    except Exception as e:
        logger.error(f"Failed to write JSONL file {path}: {e}")
        raise


def stream_jsonl(file_path: Union[str, Path]) -> Iterator[Dict]:
    """
    Stream JSONL file line by line, yielding one dict at a time.
    Useful for large files that don't fit in memory.
    """
    path = Path(file_path)
    try:
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    yield json.loads(line)
    except Exception as e:
        logger.error(f"Failed to stream JSONL file {path}: {e}")
        raise


# ---------- Pickle ----------
def read_pickle(file_path: Union[str, Path]) -> Any:
    """Load Python object from pickle file."""
    path = Path(file_path)
    try:
        with open(path, "rb") as f:
            return pickle.load(f)
    except Exception as e:
        logger.error(f"Failed to read pickle file {path}: {e}")
        raise


def write_pickle(file_path: Union[str, Path], data: Any, protocol: int = pickle.HIGHEST_PROTOCOL) -> None:
    """Write Python object to pickle file."""
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    try:
        with open(path, "wb") as f:
            pickle.dump(data, f, protocol=protocol)
    except Exception as e:
        logger.error(f"Failed to write pickle file {path}: {e}")
        raise


# ---------- CSV ----------
def read_csv(
    file_path: Union[str, Path],
    delimiter: str = ",",
    has_header: bool = True,
    encoding: str = "utf-8"
) -> Union[List[Dict], List[List[str]]]:
    """
    Read CSV file.
    If has_header=True, returns list of dictionaries (fieldnames as keys).
    Else, returns list of rows (each row is list of strings).
    """
    path = Path(file_path)
    try:
        with open(path, "r", encoding=encoding) as f:
            reader = csv.reader(f, delimiter=delimiter)
            if has_header:
                header = next(reader)
                data = []
                for row in reader:
                    data.append({header[i]: row[i] for i in range(len(header))})
                return data
            else:
                return list(reader)
    except Exception as e:
        logger.error(f"Failed to read CSV file {path}: {e}")
        raise


def write_csv(
    file_path: Union[str, Path],
    data: Union[List[Dict], List[List[str]]],
    delimiter: str = ",",
    fieldnames: Optional[List[str]] = None,
    encoding: str = "utf-8"
) -> None:
    """
    Write CSV file.
    If data is list of dicts, fieldnames can be provided or inferred (keys of first dict).
    If data is list of lists, writes as rows without header.
    """
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    try:
        with open(path, "w", encoding=encoding, newline="") as f:
            if data and isinstance(data[0], dict):
                # Write dict as CSV with header
                if fieldnames is None:
                    fieldnames = list(data[0].keys())
                writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=delimiter)
                writer.writeheader()
                writer.writerows(data)
            else:
                # Write list of lists
                writer = csv.writer(f, delimiter=delimiter)
                writer.writerows(data)
    except Exception as e:
        logger.error(f"Failed to write CSV file {path}: {e}")
        raise


# ---------- Generic safe read/write ----------
def safe_read_json(file_path: Union[str, Path], default: Any = None) -> Any:
    """Try to read JSON, return default if file not found or error."""
    try:
        return read_json(file_path)
    except FileNotFoundError:
        return default
    except Exception:
        return default


def safe_write_json(file_path: Union[str, Path], data: Any, indent: int = 2) -> bool:
    """Try to write JSON, return True if success else False."""
    try:
        write_json(file_path, data, indent)
        return True
    except Exception:
        return False