From 5f03185bdd53252674a0437f97e2d0f34bbefeac Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Tue, 18 Nov 2014 13:45:55 +0100 Subject: [PATCH] did even more refactoring with the suggestions of sampsyo. added documentation page --- beetsplug/fix_permissions.py | 40 ------------------------------ beetsplug/permissions.py | 48 ++++++++++++++++++++++++++++++++++++ docs/plugins/permissions.rst | 18 ++++++++++++++ 3 files changed, 66 insertions(+), 40 deletions(-) delete mode 100644 beetsplug/fix_permissions.py create mode 100644 beetsplug/permissions.py create mode 100644 docs/plugins/permissions.rst diff --git a/beetsplug/fix_permissions.py b/beetsplug/fix_permissions.py deleted file mode 100644 index 6a4d773b0..000000000 --- a/beetsplug/fix_permissions.py +++ /dev/null @@ -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) diff --git a/beetsplug/permissions.py b/beetsplug/permissions.py new file mode 100644 index 000000000..227390826 --- /dev/null +++ b/beetsplug/permissions.py @@ -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) diff --git a/docs/plugins/permissions.rst b/docs/plugins/permissions.rst new file mode 100644 index 000000000..250597c3b --- /dev/null +++ b/docs/plugins/permissions.rst @@ -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