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(
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we must allow items of type "RelocateType". But i see that was done elsewhere. I think this is redundant because we only pass instances of str and never of RelocateType.

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 instead of move files",
)(),
)

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

Expand Down
15 changes: 12 additions & 3 deletions mnamer/target.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import date
from os import path
from os import path,symlink,link
from pathlib import Path, PurePath
from shutil import move
from typing import Dict, List, Optional, Set, Union
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,12 @@ class Target:
_parsed_metadata: Metadata
source: PurePath

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

def __init__(self, file_path: Path, settings: SettingStore = None):
self._settings = settings or SettingStore()
self._has_moved: False
Expand Down Expand Up @@ -244,6 +250,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
6 changes: 6 additions & 0 deletions mnamer/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class ProviderType(Enum):
OMDB = "omdb"


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


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