did even more refactoring with the suggestions of sampsyo. added documentation page

This commit is contained in:
Marvin Steadfast 2014-11-18 13:45:55 +01:00
parent df1136756a
commit 5f03185bdd
3 changed files with 66 additions and 40 deletions

View file

@ -1,40 +0,0 @@
'''
Fixes file permissions after the file gets written on import.
Put something like the following in your config.yaml to configure:
fix_permissions:
file: '644'
IMPORTANT: Needs to be a string.
'''
import os
from beets import config
from beets.plugins import BeetsPlugin
def check_permissions(path, permission):
''' checks the permissions on the written path '''
return oct(os.stat(path).st_mode & 0o777) == oct(permission)
class FixPermissions(BeetsPlugin):
''' our plugin class '''
pass
@FixPermissions.listen('after_write')
def fix_permissions(path):
''' running the permission fixer '''
# getting the config
file_perm = int(config['fix_permissions'].get()['file'], 8)
# doing the permission magic
os.chmod(path, file_perm)
# check permissions
if not check_permissions(path, file_perm):
message = 'There was a problem fixing permission on {}'.format(path)
print(message)

48
beetsplug/permissions.py Normal file
View file

@ -0,0 +1,48 @@
"""Fixes file permissions after the file gets written on import. Put something
like the following in your config.yaml to configure:
permissions:
file: 644
"""
import os
from beets import config, util
from beets.plugins import BeetsPlugin
def check_permissions(path, permission):
"""Checks the permissions of a path.
"""
return oct(os.stat(path).st_mode & 0o777) == oct(permission)
class Permissions(BeetsPlugin):
def __init__(self):
super(Permissions, self).__init__()
# Adding defaults.
self.config.add({
u'file': 644
})
@Permissions.listen('after_write')
def permissions(path):
"""Running the permission fixer.
"""
# Getting the config
file_perm = config['permissions']['file'].get()
# If the config is a int it will first convert it to a string and back
# to an oct int. Else it just converts it to oct.
if isinstance(file_perm, int):
file_perm = int(str(file_perm), 8)
else:
file_perm = int(file_perm, 8)
# Changing permissions on the path.
os.chmod(util.bytestring_path(path), file_perm)
# Checks if the path has the permissions configured.
if not check_permissions(util.bytestring_path(path), file_perm):
message = 'There was a problem setting permission on {}'.format(path)
print(message)

View file

@ -0,0 +1,18 @@
Permissions Plugin
==================
The ``permissions`` plugin allows you to set file permissions after they got written.
To use the ``permissions`` plugin, enable it in your configuration (see
:ref:`using-plugins`).
Configuration
-------------
To configure the plugin, make an ``permissions:`` section in your configuration
file. You need to use **octal modes** to configure permissions.
Here's an example::
permissions:
file: 644