sok.core.history
Rename history: what each rename did, and how to undo it.
A rename runs inside record(): the file operations report each move (move_file), each folder they create (make_folders) and each poster they save to the active Journal. At the end, the journal becomes an Operation of the HistoryStore, saved in history.json next to the settings.
Undoing puts the files back where they were, but never replaces a file: a file moved or deleted since, or an old place taken by another file, is skipped. A hard link or a copy is deleted when its source is still there. The .backup kept for a replaced file is put back, the posters the rename saved are deleted once all their files are back, and the folders it created are removed when they are empty.
Artworkdataclass
class Artwork(path: str, files: List[str], removed: bool = False)A poster or cover saved by a rename, and the files it belongs to.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
path | str | ||
files | List[str] | ||
removed | bool | False |
Attributes
| Name | Type |
|---|---|
files | List[str] |
path | str |
removed | bool |
HistoryStore
class HistoryStore(path: Path)The operations of the history, newest first, saved in a JSON file.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path |
Attributes
| Name |
|---|
path |
add
def add(operation: Operation) -> None:Add an operation, dropping the oldest beyond MAX_OPERATIONS.
Parameters
| Name | Type | Description |
|---|---|---|
operation | Operation |
clear
def clear() -> None:get
def get(operation_id: str) -> Optional[Operation]:Parameters
| Name | Type | Description |
|---|---|---|
operation_id | str |
operations
def operations() -> List[Operation]:The operations, newest first.
Sourceundo
def undo(operation_id: str, index: Optional[int] = None) -> UndoReport:Put the files of an operation back where they were.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
operation_id | str | The operation. | |
index | Optional[int] | None | One file of it (its position in moves), or None for all. The files that follow it, such as its subtitles, go back too. |
Returns
UndoReportThe files put back and the ones left in place, with why.
Journal
class Journal()What a rename does, reported by the file operations as it goes.
Sourcecreated_folders
def created_folders(folders: List[str]) -> None:Parameters
| Name | Type | Description |
|---|---|---|
folders | List[str] |
moved
def moved( source: str, target: str, backup: Optional[str], follows: Optional[str] = None, mode: str = MOVE,) -> None:Parameters
| Name | Type | Default | Description |
|---|---|---|---|
source | str | ||
target | str | ||
backup | Optional[str] | ||
follows | Optional[str] | None | |
mode | str | MOVE |
saved_artwork
def saved_artwork(path: str, files: List[str]) -> None:Parameters
| Name | Type | Description |
|---|---|---|
path | str | |
files | List[str] |
Movedataclass
class Move( source: str, target: str, backup: Optional[str] = None, undone: bool = False, follows: Optional[str] = None, mode: str = MOVE,)One file of a rename: where it was, where it went.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
source | str | ||
target | str | ||
backup | Optional[str] | None | |
undone | bool | False | |
follows | Optional[str] | None | |
mode | str | MOVE |
Attributes
| Name | Type |
|---|---|
backup | Optional[str] |
follows | Optional[str] |
mode | str |
source | str |
target | str |
undone | bool |
Operationdataclass
class Operation( id: str, date: str, kind: str, moves: List[Move] = list(), folders: List[str] = list(), artwork: List[Artwork] = list(),)One rename: all the files of one click on "Rename all".
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | str | ||
date | str | ||
kind | str | ||
moves | List[Move] | list() | |
folders | List[str] | list() | |
artwork | List[Artwork] | list() |
partly_undoneproperty
bool
True when some files, but not all, were put back.
undoneproperty
bool
True when every file was put back.
from_dictclassmethod
def from_dict(data: Dict[str, Any]) -> Operation:Parameters
| Name | Type | Description |
|---|---|---|
data | Dict[str, Any] |
UndoReportdataclass
class UndoReport(restored: List[Move] = list(), skipped: List[tuple] = list())What an undo did: the files put back and the files left in place.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
restored | List[Move] | list() | |
skipped | List[tuple] | list() |
Attributes
| Name | Type |
|---|---|
restored | List[Move] |
skipped | List[tuple] |
Functions
content_kind
def content_kind(media_item: Any) -> str:The kind of an operation: the ContentType value of its item.
Parameters
| Name | Type | Description |
|---|---|---|
media_item | Any |
current_journal
def current_journal() -> Optional[Journal]:The journal of the rename running in this thread, if any.
Sourceget_history
def get_history() -> HistoryStore:The history of the application, in history.json next to the settings.
Sourcerecord
def record( kind: str, store: Optional[HistoryStore] = None,) -> Iterator[Journal]:Record the rename run in the block as one operation of the history.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
kind | str | What was renamed, a ContentType value such as "movie". | |
store | Optional[HistoryStore] | None | The history to add it to (get_history() by default). |
Yields
JournalThe journal; its operation_id is set at the end when files moved.
Attributes
| Name | Value |
|---|---|
COPY | 'copy' |
FAILED | 'failed' |
FILE_MODES | (MOVE, LINK, COPY) |
LINK | 'link' |
MAX_OPERATIONS | 100 |
MISSING | 'missing' |
MOVE | 'move' |
OCCUPIED | 'occupied' |
SYSTEM_FILES | {'.DS_Store', 'Thumbs.db', 'desktop.ini'} |
logger | logging.getLogger(__name__) |