File size: 1,336 Bytes
34d0dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import aiofiles
import asyncio


class JSONStorage:
    def __init__(self):
        # The Lock ensures that multiple async tasks don't
        # write to the same file at the exact same time.
        self._lock = asyncio.Lock()

    async def save_jsonl(self, new_data, path="data.jsonl"):
        """

        Asynchronously appends a single dictionary as a JSON line.

        Strictly UTF-8 for global character support.

        """
        async with self._lock:
            try:
                async with aiofiles.open(path, mode="a", encoding="utf-8") as f:
                    # ensure_ascii=False keeps your UTF-8 characters readable
                    line = json.dumps(new_data, ensure_ascii=False)
                    await f.write(line + "\n")
            except Exception as e:
                print(f"❌ Storage Error: {e}")

    async def load_all(self, path="data.jsonl"):
        """

        Reads the JSONL file and returns a list of dictionaries.

        """
        data = []
        try:
            async with aiofiles.open(path, mode="r", encoding="utf-8") as f:
                async for line in f:
                    if line.strip():
                        data.append(json.loads(line))
        except FileNotFoundError:
            pass
        return data