Skip to content
12 changes: 11 additions & 1 deletion mnamer/setting_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mnamer.exceptions import MnamerException
from mnamer.language import Language
from mnamer.setting_spec import SettingSpec
from mnamer.types import MediaType, ProviderType, SettingType
from mnamer.types import MediaType, ProviderType, SettingType, RelocateType
from mnamer.utils import crawl_out, json_loads, normalize_containers

__all__ = ["SettingStore"]
Expand Down Expand Up @@ -218,6 +218,16 @@ class SettingStore:
help="--episode-format: set episode renaming format specification",
)(),
)
relocation_mode: Optional[Union[RelocateType, str]] = dataclasses.field(
default=RelocateType.DEFAULT.value,
metadata=SettingSpec(
dest="relocation_mode",
choices=[ix.value for ix in RelocateType],
flags=["--link"],
Comment thread
WaywardWizard marked this conversation as resolved.
Outdated
group=SettingType.PARAMETER,
help=f"--link={'|'.join([ix.value for ix in RelocateType])}: when given, {'|'.join([ix.value for ix in RelocateType])} link or copy instead of move files",
)(),
)

# directive attributes -----------------------------------------------------

Expand Down
18 changes: 14 additions & 4 deletions mnamer/target.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import date
from os import path
from os import path, symlink, link
from pathlib import Path, PurePath
from shutil import move
from shutil import move, copy2
from typing import Dict, List, Optional, Set, Union

from guessit import guessit
Expand All @@ -11,7 +11,7 @@
from mnamer.metadata import Metadata, MetadataEpisode, MetadataMovie
from mnamer.providers import Provider
from mnamer.setting_store import SettingStore
from mnamer.types import MediaType, ProviderType
from mnamer.types import MediaType, ProviderType, RelocateType
from mnamer.utils import (
crawl_in,
filename_replace,
Expand All @@ -37,6 +37,13 @@ class Target:
_parsed_metadata: Metadata
source: PurePath

_relocation_mode = {
RelocateType.DEFAULT.value: move,
RelocateType.HARDLINK.value: link,
RelocateType.SYMBOLICLINK.value: symlink,
RelocateType.COPY.value: copy2,
}

def __init__(self, file_path: Path, settings: SettingStore = None):
self._settings = settings or SettingStore()
self._has_moved: False
Expand Down Expand Up @@ -244,6 +251,9 @@ def relocate(self) -> None:
destination_path = Path(self.destination).resolve()
destination_path.parent.mkdir(parents=True, exist_ok=True)
try:
move(self.source, destination_path)
relocate_function = self._relocation_mode[
self._settings.relocation_mode
]
relocate_function(self.source, destination_path)
except OSError: # pragma: no cover
raise MnamerException
7 changes: 7 additions & 0 deletions mnamer/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class ProviderType(Enum):
OMDB = "omdb"


class RelocateType(Enum):
DEFAULT = "move"
HARDLINK = "hard"
SYMBOLICLINK = "sym"
COPY = "copy"


class SettingType(Enum):
DIRECTIVE = "directive"
PARAMETER = "parameter"
Expand Down