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