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 # Internal VFS path 32 self.path = bstring(path, self.enc) 33 # Full VFS path 34 self.full_path = bstring(full_path, self.enc) 35 # External VFS path 36 self.ext_path = bstring(ext_path, self.enc) 37 self.parent = parent 38 self.driver = driver 39 self.kwargs = kwargs 40 self.fileobj = None 41 42 # File name 43 self.name = os.path.basename(self.path) 44 45 # File type 46 self.ftype = None 47 48 # Access time 49 self.atime = None 50 51 # Modified time 52 self.mtime = None 53 54 # Changed time 55 self.ctime = None 56 57 # Size in bytes 58 self.size = None 59 60 # Owner UID 61 self.uid = None 62 63 # Group 64 self.gid = None 65 66 # Mode 67 self.mode = None 68 69 # Inode 70 self.inode = None 71 72 # Visual file type 73 self.vtype = None 74 75 # Visual file representation 76 self.visual = None 77 78 # File info 79 self.info = None 80 81 # Any type-specific data 82 self.data = None 83 84 # List of significant attributes 85 self.attributes = () 86 87 # Run local constructor 88 self._prepare() 89 90 self.__ni_msg = _(u"Feature not implemented")
91 92 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 93
94 - def is_file(self):
95 """ 96 Return True if instance is representing regular file 97 """ 98 99 return isinstance(self.ftype, types.VFSTypeFile)
100 101 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 102
103 - def is_dir(self):
104 """ 105 Return True if instance is representing directory 106 """ 107 108 return isinstance(self.ftype, types.VFSTypeDir)
109 110 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 111
112 - def is_dir_empty(self):
113 """ 114 Return True if instance is representing directory and it is empty 115 """ 116 117 if not self.is_dir(): 118 return False 119 120 _, _, objs = self.walk() 121 122 return len(objs) == 0
123 124 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 125 132 133 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 134
135 - def is_char(self):
136 """ 137 Return True if instance is representing soft char device 138 """ 139 140 return isinstance(self.ftype, types.VFSTypeChar)
141 142 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 143
144 - def is_block(self):
145 """ 146 Return True if instance is representing block device 147 """ 148 149 return isinstance(self.ftype, types.VFSTypeBlock)
150 151 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 152
153 - def is_fifo(self):
154 """ 155 Return True if instance is representing FIFO 156 """ 157 158 return isinstance(self.ftype, types.VFSTypeFifo)
159 160 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 161
162 - def is_socket(self):
163 """ 164 Return True if instance is representing socket 165 """ 166 167 return isinstance(self.ftype, types.VFSTypeSocket)
168 169 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 170
171 - def copy(self, path, existcb=None, errorcb=None, 172 save_attrs=True, follow_links=False, cancel=None):
173 """ 174 Copy file to specified location 175 176 @param path: Local path to copy file to 177 @param existcb: Callback function to be called if there exists 178 an object in target directory with the same name. 179 Callback function receives VFSObject instance as an 180 argument and must return one of: 181 'override' - to override this very object 182 'override all' - to override any future collisions 183 'skip' - to skip the object 184 'skip all' - to skip all future collisions 185 'abort' - to abort the process. 186 If no existscb provided 'abort' is used as default 187 @param errorcb: Callback function to be called in case an error occured 188 during copying. Function receives VFSObject instance 189 and error string as arguments and must return one of: 190 'skip' - to continue the process 191 'skip all' - to skip all future errors 192 'abort' - to abort the process. 193 If no errorcb provided 'abort' is used as default 194 @param save_attrs: Whether to save object attributes 195 @param follow_links: Whether to follow symlinks 196 @param cancel: a threading.Event instance, if it is found set - abort 197 """ 198 199 raise NotImplementedError(self.__ni_msg)
200 201 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 202
203 - def move(self, path, existcb=None, errorcb=None, save_attrs=True, 204 follow_links=False, cancel=None):
205 """ 206 Move object 207 Arguments are the same as for copy() 208 """ 209 210 raise NotImplementedError(self.__ni_msg)
211 212 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 213
214 - def mkdir(self, newdir):
215 """ 216 Create new dir inside object (only valid for directory object types) 217 """ 218 219 raise NotImplementedError(self.__ni_msg)
220 221 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 222
223 - def remove(self, recursive=True):
224 """ 225 [Recursively] remove object 226 """ 227 228 raise NotImplementedError(self.__ni_msg)
229 230 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 231
232 - def walk(self):
233 """ 234 Directory tree walker 235 """ 236 237 raise NotImplementedError(self.__ni_msg)
238 239 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 240
241 - def open(self, mode='r'):
242 """ 243 Open self object in provided mode 244 """ 245 246 raise NotImplementedError(self.__ni_msg)
247 248 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 249
250 - def close(self):
251 """ 252 Close self object 253 """ 254 255 raise NotImplementedError(self.__ni_msg)
256 257 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 258
259 - def read(self, bytes=None):
260 """ 261 Read bytes from self object 262 """ 263 264 raise NotImplementedError(self.__ni_msg)
265 266 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 267
268 - def tell(self):
269 """ 270 Tell file position 271 """ 272 273 raise NotImplementedError(self.__ni_msg)
274 275 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 276
277 - def seek(self, offset, whence=None):
278 """ 279 Perform seek() on object 280 """ 281 282 raise NotImplementedError(self.__ni_msg)
283 284 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 285
286 - def __repr__(self):
287 return self.__str__()
288 289 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 290
291 - def __unicode__(self):
292 return ustring(self.__str__)
293 294 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 295
296 - def _prepare(self):
297 raise NotImplementedError(self.__ni_msg)
298 299 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 300
301 - def __del__(self):
302 if self.fileobj: 303 try: 304 self.close() 305 except Exception: 306 pass
307