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

Source Code for Module libxyz.vfs.vfsobj

  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 os 
 18   
 19  from libxyz.core.utils import bstring, ustring 
 20  from libxyz.vfs import types 
 21   
22 -class VFSObject(object):
23 """ 24 Abstract interface for VFS objects 25 """ 26
27 - def __init__(self, xyz, path, full_path, ext_path, driver, parent, 28 enc=None, **kwargs):
29 self.xyz = xyz 30 self.enc = enc or xyzenc 31 self.path = bstring(path, self.enc) 32 self.full_path = bstring(full_path, self.enc) 33 self.ext_path = bstring(ext_path, self.enc) 34 self.parent = parent 35 self.driver = driver 36 self.kwargs = kwargs 37 38 # File name 39 self.name = os.path.basename(self.path) 40 41 # File type 42 self.ftype = None 43 44 # Access time 45 self.atime = None 46 47 # Modified time 48 self.mtime = None 49 50 # Changed time 51 self.ctime = None 52 53 # Size in bytes 54 self.size = None 55 56 # Owner UID 57 self.uid = None 58 59 # Group 60 self.gid = None 61 62 # Mode 63 self.mode = None 64 65 # Inode 66 self.inode = None 67 68 # Visual file type 69 self.vtype = None 70 71 # Visual file representation 72 self.visual = None 73 74 # File info 75 self.info = None 76 77 # Any type-specific data 78 self.data = None 79 80 # List of significant attributes 81 self.attributes = () 82 83 # Run local constructor 84 self._prepare() 85 86 self.__ni_msg = _(u"Feature not implemented")
87 88 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 89
90 - def is_file(self):
91 """ 92 Return True if instance is representing regular file 93 """ 94 95 return isinstance(self.ftype, types.VFSTypeFile)
96 97 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 98
99 - def is_dir(self):
100 """ 101 Return True if instance is representing directory 102 """ 103 104 return isinstance(self.ftype, types.VFSTypeDir)
105 106 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 107
108 - def is_dir_empty(self):
109 """ 110 Return True if instance is representing directory and it is empty 111 """ 112 113 if not self.is_dir(): 114 return False 115 116 _, _, dirs, files = self.walk() 117 118 return len(dirs) == 0 and len(files) == 0
119 120 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 121 128 129 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 130
131 - def is_char(self):
132 """ 133 Return True if instance is representing soft char device 134 """ 135 136 return isinstance(self.ftype, types.VFSTypeChar)
137 138 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 139
140 - def is_block(self):
141 """ 142 Return True if instance is representing block device 143 """ 144 145 return isinstance(self.ftype, types.VFSTypeBlock)
146 147 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 148
149 - def is_fifo(self):
150 """ 151 Return True if instance is representing FIFO 152 """ 153 154 return isinstance(self.ftype, types.VFSTypeFifo)
155 156 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 157
158 - def is_socket(self):
159 """ 160 Return True if instance is representing socket 161 """ 162 163 return isinstance(self.ftype, types.VFSTypeSocket)
164 165 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 166
167 - def copy(self, path, existcb=None, errorcb=None, 168 save_attrs=True, follow_links=False, cancel=None):
169 """ 170 Copy file to specified location 171 172 @param path: Local path to copy file to 173 @param existcb: Callback function to be called if there exists 174 an object in target directory with the same name. 175 Callback function receives VFSObject instance as an 176 argument and must return one of: 177 'override' - to override this very object 178 'override all' - to override any future collisions 179 'skip' - to skip the object 180 'skip all' - to skip all future collisions 181 'abort' - to abort the process. 182 If no existscb provided 'abort' is used as default 183 @param errorcb: Callback function to be called in case an error occured 184 during copying. Function receives VFSObject instance 185 and error string as arguments and must return one of: 186 'skip' - to continue the process 187 'skip all' - to skip all future errors 188 'abort' - to abort the process. 189 If no errorcb provided 'abort' is used as default 190 @param save_attrs: Whether to save object attributes 191 @param follow_links: Whether to follow symlinks 192 @param cancel: a threading.Event instance, if it is found set - abort 193 """ 194 195 raise NotImplementedError(self.__ni_msg)
196 197 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 198
199 - def move(self, path, existcb=None, errorcb=None, save_attrs=True, 200 follow_links=False, cancel=None):
201 """ 202 Move object 203 Arguments are the same as for copy() 204 """ 205 206 raise NotImplementedError(self.__ni_msg)
207 208 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 209
210 - def mkdir(self, newdir):
211 """ 212 Create new dir inside object (only valid for directory object types) 213 """ 214 215 raise NotImplementedError(self.__ni_msg)
216 217 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 218
219 - def remove(self, recursive=True):
220 """ 221 [Recursively] remove object 222 """ 223 224 raise NotImplementedError(self.__ni_msg)
225 226 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 227
228 - def walk(self):
229 """ 230 Directory tree generator 231 """ 232 233 raise NotImplementedError(self.__ni_msg)
234 235 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 236
237 - def __repr__(self):
238 return self.__str__()
239 240 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 241
242 - def __unicode__(self):
243 return ustring(self.__str__)
244 245 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 246
247 - def _prepare(self):
248 raise NotImplementedError(self.__ni_msg)
249