HEX
Server: Apache
System: Linux host.creative4all.com 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Tue Nov 5 04:50:16 EST 2024 x86_64
User: agrimasfadeltral (1173)
PHP: 8.3.30
Disabled: exec,passthru,shell_exec,system,proc_open,parse_ini_file,show_source
Upload Files
File: //opt/cpmigrate/journal.py
"""Journal class for handling saving to journal."""

import json
import os
from functools import wraps
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
    from transfer import Transfer
    from user import User


class Journal:
    """Responsible for saving to journal."""

    def __init__(self, transfer: "Transfer"):
        self.xfer = transfer
        self.json_path = os.path.join(self.xfer.log_path, 'journal.json')

    def save(self):
        """Captures the state of the transfer, which recursively captures
        everything else in it as needed.
        """
        data = self.xfer.capture_state()

        with open(self.json_path, 'w', encoding="utf-8") as out:
            json.dump(data, out)


def save_state(func):
    """Wrapper for saving to journal before and after func execution."""

    @wraps(func)
    def wrapper(self: Union["User", "Transfer"], *args, **kwargs):
        self.journal.save()
        out = func(self, *args, **kwargs)
        self.journal.save()
        return out

    return wrapper