permissions: Use proper logging (#2089)

Refactored so we can use the proper logging infrastructure instead of `print`.
This commit is contained in:
Adrian Sampson 2016-06-28 10:29:26 -07:00
parent 3a700eb2a4
commit e33c19a3e9

View file

@ -48,54 +48,55 @@ class Permissions(BeetsPlugin):
u'dir': '755' u'dir': '755'
}) })
self.register_listener('item_imported', permissions) self.register_listener('item_imported', self.fix)
self.register_listener('album_imported', permissions) self.register_listener('album_imported', self.fix)
def fix(self, lib, item=None, album=None):
"""Fix the permissions for an imported Item or Album.
"""
# Getting the config.
file_perm = config['permissions']['file'].get()
dir_perm = config['permissions']['dir'].get()
def permissions(lib, item=None, album=None): # Converts permissions to oct.
"""Running the permission fixer. file_perm = convert_perm(file_perm)
""" dir_perm = convert_perm(dir_perm)
# Getting the config.
file_perm = config['permissions']['file'].get()
dir_perm = config['permissions']['dir'].get()
# Converts permissions to oct. # Create chmod_queue.
file_perm = convert_perm(file_perm) file_chmod_queue = []
dir_perm = convert_perm(dir_perm) if item:
file_chmod_queue.append(item.path)
elif album:
for album_item in album.items():
file_chmod_queue.append(album_item.path)
# Create chmod_queue. # A set of directories to change permissions for.
file_chmod_queue = [] dir_chmod_queue = set()
if item:
file_chmod_queue.append(item.path)
elif album:
for album_item in album.items():
file_chmod_queue.append(album_item.path)
# A set of directories to change permissions for. for path in file_chmod_queue:
dir_chmod_queue = set() # Changing permissions on the destination file.
os.chmod(util.bytestring_path(path), file_perm)
for path in file_chmod_queue: # Checks if the destination path has the permissions configured.
# Changing permissions on the destination file. if not check_permissions(util.bytestring_path(path), file_perm):
os.chmod(util.bytestring_path(path), file_perm) self._log.warn(
u'There was a problem setting permissions on file {}',
path,
)
# Checks if the destination path has the permissions configured. # Adding directories to the directory chmod queue.
if not check_permissions(util.bytestring_path(path), file_perm): dir_chmod_queue.update(
message = u'There was a problem setting permission on {}'.format( dirs_in_library(lib.directory,
path) path))
print(message)
# Adding directories to the directory chmod queue. # Change permissions for the directories.
dir_chmod_queue.update( for path in dir_chmod_queue:
dirs_in_library(lib.directory, # Chaning permissions on the destination directory.
path)) os.chmod(util.bytestring_path(path), dir_perm)
# Change permissions for the directories. # Checks if the destination path has the permissions configured.
for path in dir_chmod_queue: if not check_permissions(util.bytestring_path(path), dir_perm):
# Chaning permissions on the destination directory. self._log.warn(
os.chmod(util.bytestring_path(path), dir_perm) u'There was a problem setting permissions on directory {}',
path,
# Checks if the destination path has the permissions configured. )
if not check_permissions(util.bytestring_path(path), dir_perm):
message = u'There was a problem setting permission on {}'.format(
path)
print(message)