synology_link_manipultor/media.py

37 lines
1.1 KiB
Python

import pathlib
import typing
import os
class Media:
def __init__(self, path: typing.Union[str, pathlib.Path]):
self.path = pathlib.Path(path)
def exists(self) -> bool:
return self.path.exists()
def is_symlink(self) -> bool:
return self.path.is_symlink()
def number_of_links(self) -> int:
return self.path.lstat().st_nlink
def inode(self) -> int:
return self.path.lstat().st_ino
def relative_to_parents(self, target: pathlib.Path) -> str:
path_parent = self.path.resolve().parent
target_parent = target.resolve().parent
return os.path.relpath(path=target_parent, start=path_parent)
def symlink_to(self, target: pathlib.Path):
self.path.symlink_to(target=os.path.join(self.relative_to_parents(target=target), target.name),
target_is_directory=False)
def convert_symlink_to_hdlink(self):
if self.is_symlink() is True:
src = self.path.resolve()
dst = self.path
os.remove(dst)
os.link(src=src, dst=dst)