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

Source Code for Module libxyz.vfs.util

  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  import os 
 19  import pwd 
 20  import grp 
 21   
 22  from libxyz.vfs import types as vfstypes 
 23   
 24  _types = ( 
 25            (stat.S_ISDIR,  vfstypes.VFSTypeDir), 
 26            (stat.S_ISCHR,  vfstypes.VFSTypeChar), 
 27            (stat.S_ISBLK,  vfstypes.VFSTypeBlock), 
 28            (stat.S_ISREG,  vfstypes.VFSTypeFile), 
 29            (stat.S_ISFIFO, vfstypes.VFSTypeFifo), 
 30            (stat.S_ISLNK,  vfstypes.VFSTypeLink), 
 31            (stat.S_ISSOCK, vfstypes.VFSTypeSocket), 
 32            ) 
 33   
34 -def get_file_type(st_mode):
35 """ 36 Find out file type 37 @param st_mode: Raw st_mode obtained from os.stat() 38 """ 39 40 global _types 41 42 for _test, _type in _types: 43 if _test(st_mode): 44 return _type() 45 46 return vfstypes.VFSTypeUnknown()
47 48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 49
50 -def format_size(size):
51 """ 52 Format file-object size 53 """ 54 55 _s = long(size) 56 57 _data = ( 58 (1024 * 1024 * 1024, u"G", lambda x, y: u"%.2f" % (float(x) / y)), 59 (1024 * 1024, u"M", lambda x, y: unicode(x / y)), 60 (1024, u"K", lambda x, y: unicode(x / y)), 61 ) 62 63 for _size, _suffix, _func in _data: 64 if _s >= _size: 65 return u"%s%s" % (_func(_s, _size), _suffix) 66 67 return size
68 69 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 70
71 -def split_path(path):
72 return [x for x in path.split(os.sep) if x]
73 74 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 75
76 -def get_user(uid=None):
77 """ 78 Get user name by UID 79 80 @param uid: User ID 81 @return: username or None 82 """ 83 84 if uid is None: 85 uid = os.getuid() 86 87 try: 88 name = pwd.getpwuid(uid).pw_name 89 except (KeyError, TypeError): 90 name = None 91 92 return name
93 94 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 95
96 -def get_group(gid=None):
97 """ 98 Get group name by GID 99 100 @param gid: Group ID 101 @return: group name or None 102 """ 103 104 if gid is None: 105 gid = os.getuid() 106 107 try: 108 name = grp.getgrgid(gid).gr_name 109 except (KeyError, TypeError): 110 name = None 111 112 return name
113