fs.permissions

Abstract permissions container.

class fs.permissions.Permissions(names=None, mode=None, user=None, group=None, other=None, sticky=None, setuid=None, setguid=None)

An abstraction for file system permissions.

Permissions objects store information regarding the permissions on a resource. It supports Linux permissions, but is generic enough to manage permission information from almost any filesystem.

Parameters:
  • names (list, optional) – A list of permissions.
  • mode (int, optional) – A mode integer.
  • user (str, optional) – A triplet of user permissions, e.g. "rwx" or "r--"
  • group (str, optional) – A triplet of group permissions, e.g. "rwx" or "r--"
  • other (str, optional) – A triplet of other permissions, e.g. "rwx" or "r--"
  • sticky (bool, optional) – A boolean for the sticky bit.
  • setuid (bool, optional) – A boolean for the setuid bit.
  • setguid (bool, optional) – A boolean for the setguid bit.

Example

>>> from fs.permissions import Permissions
>>> p = Permissions(user='rwx', group='rw-', other='r--')
>>> print(p)
rwxrw-r--
>>> p.mode
500
>>> oct(p.mode)
'0764'
add(*permissions)

Add permission(s).

Parameters:*permissions (str) – Permission name(s), such as 'u_w' or 'u_x'.
as_str()

Get a Linux-style string representation of permissions.

check(*permissions)

Check if one or more permissions are enabled.

Parameters:*permissions (str) – Permission name(s), such as 'u_w' or 'u_x'.
Returns:True if all given permissions are set.
Return type:bool
copy()

Make a copy of this permissions object.

classmethod create(init=None)

Create a permissions object from an initial value.

Parameters:init (int or list, optional) – May be None to use 0o777 permissions, a mode integer, or a list of permission names.
Returns:mode integer that may be used for instance by os.makedir.
Return type:int

Example

>>> Permissions.create(None)
Permissions(user='rwx', group='rwx', other='rwx')
>>> Permissions.create(0o700)
Permissions(user='rwx', group='', other='')
>>> Permissions.create(['u_r', 'u_w', 'u_x'])
Permissions(user='rwx', group='', other='')
dump()

Get a list suitable for serialization.

g_r

Boolean for ‘g_r’ permission.

g_w

Boolean for ‘g_w’ permission.

g_x

Boolean for ‘g_x’ permission.

classmethod get_mode(init)

Convert an initial value to a mode integer.

classmethod load(permissions)

Load a serialized permissions object.

mode

int – mode integer.

o_r

Boolean for ‘o_r’ permission.

o_w

Boolean for ‘o_w’ permission.

o_x

Boolean for ‘o_x’ permission.

classmethod parse(ls)

Parse permissions in Linux notation.

remove(*permissions)

Remove permission(s).

Parameters:*permissions (str) – Permission name(s), such as 'u_w' or 'u_x'.s
setguid

Boolean for ‘setguid’ permission.

setuid

Boolean for ‘setuid’ permission.

sticky

Boolean for ‘sticky’ permission.

u_r

Boolean for ‘u_r’ permission.

u_w

Boolean for ‘u_w’ permission.

u_x

Boolean for ‘u_x’ permission.

fs.permissions.make_mode(init)

Make a mode integer from an initial value.