Refactor path functions from utils to utils.paths

This commit is contained in:
Labrys of Knossos 2019-01-05 17:12:43 -05:00
parent 094fe555b8
commit a6d2c6e96f
2 changed files with 51 additions and 38 deletions

View file

@ -21,10 +21,21 @@ from core import extractor, logger
from core.utils.download_info import get_download_info, update_download_info_status from core.utils.download_info import get_download_info, update_download_info_status
from core.utils.network import test_connection, wake_on_lan, wake_up from core.utils.network import test_connection, wake_on_lan, wake_up
from core.utils.parsers import ( from core.utils.parsers import (
parse_args, parse_deluge, parse_other, parse_qbittorrent, parse_rtorrent, parse_transmission, parse_args,
parse_utorrent, parse_vuze, parse_deluge,
parse_other,
parse_qbittorrent,
parse_rtorrent,
parse_transmission,
parse_utorrent,
parse_vuze,
)
from core.utils.paths import (
get_dir_size, make_dir,
remote_dir,
remove_empty_folders,
remove_read_only,
) )
from core.utils.paths import get_dir_size, make_dir, remote_dir
from core.utils.processes import RunningProcess from core.utils.processes import RunningProcess
from core.utils.torrents import create_torrent_class, pause_torrent, remove_torrent, resume_torrent from core.utils.torrents import create_torrent_class, pause_torrent, remove_torrent, resume_torrent
@ -283,41 +294,6 @@ def flatten(output_destination):
remove_empty_folders(output_destination) # Cleanup empty directories remove_empty_folders(output_destination) # Cleanup empty directories
def remove_empty_folders(path, remove_root=True):
"""Function to remove empty folders"""
if not os.path.isdir(path):
return
# remove empty subfolders
logger.debug('Checking for empty folders in:{0}'.format(path))
files = os.listdir(text_type(path))
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
remove_empty_folders(fullpath)
# if folder empty, delete it
files = os.listdir(text_type(path))
if len(files) == 0 and remove_root:
logger.debug('Removing empty folder:{}'.format(path))
os.rmdir(path)
def remove_read_only(filename):
if os.path.isfile(filename):
# check first the read-only attribute
file_attribute = os.stat(filename)[0]
if not file_attribute & stat.S_IWRITE:
# File is read-only, so make it writeable
logger.debug('Read only mode on file {name}. Attempting to make it writeable'.format
(name=filename))
try:
os.chmod(filename, stat.S_IWRITE)
except Exception:
logger.warning('Cannot change permissions of {file}'.format(file=filename), logger.WARNING)
def char_replace(name): def char_replace(name):
# Special character hex range: # Special character hex range:
# CP850: 0x80-0xA5 (fortunately not used in ISO-8859-15) # CP850: 0x80-0xA5 (fortunately not used in ISO-8859-15)

View file

@ -2,10 +2,12 @@
from functools import partial from functools import partial
import os import os
import re import re
import stat
from six import text_type from six import text_type
import core import core
from core import logger
def make_dir(path): def make_dir(path):
@ -41,3 +43,38 @@ def get_dir_size(input_path):
(os.path.getsize(f) if os.path.isfile(f) else get_dir_size(f)) (os.path.getsize(f) if os.path.isfile(f) else get_dir_size(f))
for f in map(prepend, os.listdir(text_type(input_path))) for f in map(prepend, os.listdir(text_type(input_path)))
]) ])
def remove_empty_folders(path, remove_root=True):
"""Function to remove empty folders"""
if not os.path.isdir(path):
return
# remove empty subfolders
logger.debug('Checking for empty folders in:{0}'.format(path))
files = os.listdir(text_type(path))
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
remove_empty_folders(fullpath)
# if folder empty, delete it
files = os.listdir(text_type(path))
if len(files) == 0 and remove_root:
logger.debug('Removing empty folder:{}'.format(path))
os.rmdir(path)
def remove_read_only(filename):
if os.path.isfile(filename):
# check first the read-only attribute
file_attribute = os.stat(filename)[0]
if not file_attribute & stat.S_IWRITE:
# File is read-only, so make it writeable
logger.debug('Read only mode on file {name}. Attempting to make it writeable'.format
(name=filename))
try:
os.chmod(filename, stat.S_IWRITE)
except Exception:
logger.warning('Cannot change permissions of {file}'.format(file=filename), logger.WARNING)