Package libxyz :: Package vfs :: Module mode
[hide private]
[frames] | no frames]

Source Code for Module libxyz.vfs.mode

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  import stat 
 18   
 19  from libxyz.vfs import util 
 20   
21 -class Mode(object):
22 """ 23 A stat st_mode field representaion 24 """ 25
26 - def __init__(self, st_mode, vfstype):
27 """ 28 @param st_mode: Raw st_mode obtained from os.stat() 29 @param vfstype: VFS file type 30 """ 31 32 self.raw = st_mode 33 self.vfstype = vfstype 34 self.string = self._make_string_mode()
35 36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 37
38 - def __str__(self):
39 return self.string
40 41 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 42
43 - def __repr__(self):
44 return self.__str__()
45 46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 47
48 - def _make_string_mode(self):
49 """ 50 Make string mode representaion 51 """ 52 53 _str_mode = [self.vfstype.str_type] 54 55 # usr bits 56 _str_mode.extend(self._usr_bits()) 57 58 # group bits 59 _str_mode.extend(self._grp_bits()) 60 61 # other bits 62 _str_mode.extend(self._oth_bits()) 63 64 return "".join(_str_mode)
65 66 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 67
68 - def _usr_bits(self):
69 _raw = self.raw 70 _str_mode = [] 71 72 if _raw & stat.S_IRUSR: 73 _bit = "r" 74 else: 75 _bit = "-" 76 _str_mode.append(_bit) 77 78 if _raw & stat.S_IWUSR: 79 _bit = "w" 80 else: 81 _bit = "-" 82 _str_mode.append(_bit) 83 84 _o_mode = _raw & (stat.S_IXUSR | stat.S_ISUID) 85 86 if _o_mode == 0: 87 _bit = "-" 88 elif _o_mode == stat.S_IXUSR: 89 _bit = "x" 90 elif _o_mode == stat.S_ISUID: 91 _bit = "S" 92 elif _o_mode == stat.S_IXUSR | stat.S_ISUID: 93 _bit = "s" 94 _str_mode.append(_bit) 95 96 return _str_mode
97 98 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 99
100 - def _grp_bits(self):
101 _raw = self.raw 102 _str_mode = [] 103 104 if _raw & stat.S_IRGRP: 105 _bit = "r" 106 else: 107 _bit = "-" 108 _str_mode.append(_bit) 109 110 if _raw & stat.S_IWGRP: 111 _bit = "w" 112 else: 113 _bit = "-" 114 _str_mode.append(_bit) 115 116 _o_mode = _raw & (stat.S_IXGRP | stat.S_ISGID) 117 118 if _o_mode == 0: 119 _bit = "-" 120 elif _o_mode == stat.S_IXGRP: 121 _bit = "x" 122 elif _o_mode == stat.S_ISGID: 123 _bit = "S" 124 elif _o_mode == stat.S_IXGRP | stat.S_ISGID: 125 _bit = "s" 126 _str_mode.append(_bit) 127 128 return _str_mode
129 130 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 131
132 - def _oth_bits(self):
133 _raw = self.raw 134 _str_mode = [] 135 136 if _raw & stat.S_IROTH: 137 _bit = "r" 138 else: 139 _bit = "-" 140 _str_mode.append(_bit) 141 142 if _raw & stat.S_IWOTH: 143 _bit = "w" 144 else: 145 _bit = "-" 146 _str_mode.append(_bit) 147 148 _o_mode = _raw & (stat.S_IXOTH | stat.S_ISVTX) 149 150 if _o_mode == 0: 151 _bit = "-" 152 elif _o_mode == stat.S_IXOTH: 153 _bit = "x" 154 elif _o_mode == stat.S_ISVTX: 155 _bit = "T" 156 elif _o_mode == stat.S_IXOTH | stat.S_ISVTX: 157 _bit = "t" 158 _str_mode.append(_bit) 159 160 return _str_mode
161