mirror of
https://github.com/beetbox/beets.git
synced 2026-02-15 20:03:25 +01:00
Made the permissions plugin simpler. Got rid of some non-needed code and use the ancestors function instead of writing something new.
This commit is contained in:
parent
27f4732d3d
commit
dd0de2f04b
2 changed files with 17 additions and 45 deletions
|
|
@ -11,7 +11,7 @@ like the following in your config.yaml to configure:
|
|||
import os
|
||||
from beets import config, util
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.util import displayable_path
|
||||
from beets.util import ancestry
|
||||
|
||||
|
||||
def convert_perm(perm):
|
||||
|
|
@ -30,33 +30,12 @@ def check_permissions(path, permission):
|
|||
return oct(os.stat(path).st_mode & 0o777) == oct(permission)
|
||||
|
||||
|
||||
def get_music_directories(music_directory, imported_item):
|
||||
"""Creates a list of directories the imported item is in.
|
||||
def dirs_in_library(library, item):
|
||||
"""Creates a list of ancestor directories in the beets library path.
|
||||
"""
|
||||
# Checks for the directory in config and if it has a tilde in it.
|
||||
# If its that way it will be expanded to the full path.
|
||||
if '~' in music_directory:
|
||||
music_directory = os.path.expanduser(music_directory)
|
||||
|
||||
# Getting the absolute path of the directory.
|
||||
music_directory = os.path.abspath(music_directory)
|
||||
|
||||
# Creates a differential path list of the directory config path and
|
||||
# the path of the imported item.
|
||||
differential_path_list = os.path.split(
|
||||
displayable_path(imported_item).split(
|
||||
music_directory)[1])[0].split('/')[1:]
|
||||
|
||||
# Creating a list with full paths of all directories in the music library
|
||||
# we need to look at for chaning permissions.
|
||||
directory_list = []
|
||||
for path in differential_path_list:
|
||||
if len(directory_list) > 0:
|
||||
directory_list.append(os.path.join(directory_list[-1], path))
|
||||
else:
|
||||
directory_list.append(os.path.join(music_directory, path))
|
||||
|
||||
return directory_list
|
||||
return [ancestor
|
||||
for ancestor in ancestry(item)
|
||||
if library in ancestor][1:]
|
||||
|
||||
|
||||
class Permissions(BeetsPlugin):
|
||||
|
|
@ -91,8 +70,8 @@ def permissions(lib, item=None, album=None):
|
|||
for album_item in album.items():
|
||||
file_chmod_queue.append(album_item.path)
|
||||
|
||||
# A list of directories to set permissions for.
|
||||
dir_chmod_queue = []
|
||||
# A set of directories to change permissions for.
|
||||
dir_chmod_queue = set()
|
||||
|
||||
for path in file_chmod_queue:
|
||||
# Changing permissions on the destination file.
|
||||
|
|
@ -104,17 +83,10 @@ def permissions(lib, item=None, album=None):
|
|||
path)
|
||||
print(message)
|
||||
|
||||
# Adding directories to the chmod queue.
|
||||
dir_chmod_queue.append(
|
||||
get_music_directories(config['directory'].get(), path))
|
||||
|
||||
# Unpack sublists.
|
||||
dir_chmod_queue = [directory
|
||||
for dir_list in dir_chmod_queue
|
||||
for directory in dir_list]
|
||||
|
||||
# Get rid of duplicates.
|
||||
dir_chmod_queue = list(set(dir_chmod_queue))
|
||||
# Adding directories to the directory chmod queue.
|
||||
dir_chmod_queue.update(
|
||||
dirs_in_library(config['directory'].get(),
|
||||
path))
|
||||
|
||||
# Change permissions for the directories.
|
||||
for path in dir_chmod_queue:
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from test._common import unittest
|
|||
from test.helper import TestHelper
|
||||
from beetsplug.permissions import (check_permissions,
|
||||
convert_perm,
|
||||
get_music_directories)
|
||||
dirs_in_library)
|
||||
|
||||
|
||||
class PermissionsPluginTest(unittest.TestCase, TestHelper):
|
||||
|
|
@ -34,8 +34,8 @@ class PermissionsPluginTest(unittest.TestCase, TestHelper):
|
|||
dir_perm = self.config['permissions']['dir'].get()
|
||||
dir_perm = convert_perm(dir_perm)
|
||||
|
||||
music_dirs = get_music_directories(self.config['directory'].get(),
|
||||
item.path)
|
||||
music_dirs = dirs_in_library(self.config['directory'].get(),
|
||||
item.path)
|
||||
|
||||
self.assertTrue(check_permissions(item.path, file_perm))
|
||||
self.assertFalse(check_permissions(item.path, convert_perm(644)))
|
||||
|
|
@ -56,8 +56,8 @@ class PermissionsPluginTest(unittest.TestCase, TestHelper):
|
|||
dir_perm = self.config['permissions']['dir'].get()
|
||||
dir_perm = convert_perm(dir_perm)
|
||||
|
||||
music_dirs = get_music_directories(self.config['directory'].get(),
|
||||
item.path)
|
||||
music_dirs = dirs_in_library(self.config['directory'].get(),
|
||||
item.path)
|
||||
|
||||
self.assertTrue(check_permissions(item.path, file_perm))
|
||||
self.assertFalse(check_permissions(item.path, convert_perm(644)))
|
||||
|
|
|
|||
Loading…
Reference in a new issue