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):
27 """ 28 @param st_mode: Raw st_mode obtained from os.stat() 29 """ 30 31 self.raw = st_mode 32 self.string = self._make_string_mode()
33 34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 35
36 - def __str__(self):
37 return self.string
38 39 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 40
41 - def __repr__(self):
42 return self.__str__()
43 44 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 45
46 - def _make_string_mode(self):
47 """ 48 Make string mode representaion 49 """ 50 51 _str_mode = [util.get_file_type(self.raw).str_type] 52 53 # usr bits 54 _str_mode.extend(self._usr_bits()) 55 56 # group bits 57 _str_mode.extend(self._grp_bits()) 58 59 # other bits 60 _str_mode.extend(self._oth_bits()) 61 62 return u"".join(_str_mode)
63 64 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 65
66 - def _usr_bits(self):
67 _raw = self.raw 68 _str_mode = [] 69 70 if _raw & stat.S_IRUSR: 71 _bit = u"r" 72 else: 73 _bit = u"-" 74 _str_mode.append(_bit) 75 76 if _raw & stat.S_IWUSR: 77 _bit = u"w" 78 else: 79 _bit = u"-" 80 _str_mode.append(_bit) 81 82 _o_mode = _raw & (stat.S_IXUSR | stat.S_ISUID) 83 84 if _o_mode == 0: 85 _bit = u"-" 86 elif _o_mode == stat.S_IXUSR: 87 _bit = u"x" 88 elif _o_mode == stat.S_ISUID: 89 _bit = u"S" 90 elif _o_mode == stat.S_IXUSR | stat.S_ISUID: 91 _bit = u"s" 92 _str_mode.append(_bit) 93 94 return _str_mode
95 96 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 97
98 - def _grp_bits(self):
99 _raw = self.raw 100 _str_mode = [] 101 102 if _raw & stat.S_IRGRP: 103 _bit = u"r" 104 else: 105 _bit = u"-" 106 _str_mode.append(_bit) 107 108 if _raw & stat.S_IWGRP: 109 _bit = u"w" 110 else: 111 _bit = u"-" 112 _str_mode.append(_bit) 113 114 _o_mode = _raw & (stat.S_IXGRP | stat.S_ISGID) 115 116 if _o_mode == 0: 117 _bit = u"-" 118 elif _o_mode == stat.S_IXGRP: 119 _bit = u"x" 120 elif _o_mode == stat.S_ISGID: 121 _bit = u"S" 122 elif _o_mode == stat.S_IXGRP | stat.S_ISGID: 123 _bit = u"s" 124 _str_mode.append(_bit) 125 126 return _str_mode
127 128 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 129
130 - def _oth_bits(self):
131 _raw = self.raw 132 _str_mode = [] 133 134 if _raw & stat.S_IROTH: 135 _bit = u"r" 136 else: 137 _bit = u"-" 138 _str_mode.append(_bit) 139 140 if _raw & stat.S_IWOTH: 141 _bit = u"w" 142 else: 143 _bit = u"-" 144 _str_mode.append(_bit) 145 146 _o_mode = _raw & (stat.S_IXOTH | stat.S_ISVTX) 147 148 if _o_mode == 0: 149 _bit = u"-" 150 elif _o_mode == stat.S_IXOTH: 151 _bit = u"x" 152 elif _o_mode == stat.S_ISVTX: 153 _bit = u"T" 154 elif _o_mode == stat.S_IXOTH | stat.S_ISVTX: 155 _bit = u"t" 156 _str_mode.append(_bit) 157 158 return _str_mode
159