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
    20  from libxyz.vfs import types as vfstypes
    21
    22  _types = (
    23            (stat.S_ISDIR,  vfstypes.VFSTypeDir),
    24            (stat.S_ISCHR,  vfstypes.VFSTypeChar),
    25            (stat.S_ISBLK,  vfstypes.VFSTypeBlock),
    26            (stat.S_ISREG,  vfstypes.VFSTypeFile),
    27            (stat.S_ISFIFO, vfstypes.VFSTypeFifo),
    28            (stat.S_ISLNK,  vfstypes.VFSTypeLink),
    29            (stat.S_ISSOCK, vfstypes.VFSTypeSocket),
    30            )
    31
    32 -def get_file_type(st_mode):
    33      """
    34      Find out file type
    35      @param st_mode: Raw st_mode obtained from os.stat()
    36      """
    37
    38      global _types
    39
    40      for _test, _type in _types:
    41          if _test(st_mode):
    42              return _type()
    43
    44      return vfstypes.VFSTypeUnknown()
    45
    46  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    47
    48 -def format_size(size):
    49      """
    50      Format file-object size
    51      """
    52
    53      _s = long(size)
    54
    55      _data = (
    56               (1024 * 1024 * 1024, u"G", lambda x, y: u"%.2f" % (float(x) / y)),
    57               (1024 * 1024, u"M", lambda x, y: unicode(x / y)),
    58               (1024, u"K", lambda x, y: unicode(x / y)),
    59               )
    60
    61      for _size, _suffix, _func in _data:
    62          if _s >= _size:
    63              return u"%s%s" % (_func(_s, _size), _suffix)
    64
    65      return size
    66
    67  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    68
    69 -def split_path(path):
    70      return [x for x in path.split(os.sep) if x]
    71