Update of /cvsroot/freevo/freevo/src/util
In directory sc8-pr-cvs1:/tmp/cvs-serv26693

Added Files:
        vfs.py 
Log Message:
new vfs

--- NEW FILE: vfs.py ---
#if 0 /*
# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# util/vfs.py - virtual filesystem
# -----------------------------------------------------------------------
# $Id: vfs.py,v 1.1 2003/11/22 20:33:53 dischi Exp $
#
# Notes:
#
# This is a virtual filesystem for Freevo. It uses the structure in
# config.OVERLAY_DIR to store files that should be in the normal
# directory, but the user has no write access to it. It's meant to
# store fxd and image files (covers).
#
# Todo:        
#
# -----------------------------------------------------------------------
# $Log: vfs.py,v $
# Revision 1.1  2003/11/22 20:33:53  dischi
# new vfs
#
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */
#endif
            

import os
import config
import codecs
import traceback

def getoverlay(item):
    if not config.OVERLAY_DIR:
        return ''
    
    if isinstance(item, str):
        # it's a directory, return it
        directory = os.path.abspath(item)
        for media in config.REMOVABLE_MEDIA:
            if directory.startswith(media.mountdir):
                directory = directory[len(media.mountdir):]
                return os.path.join(config.OVERLAY_DIR, 'disc', media.id + directory)
        return os.path.join(config.OVERLAY_DIR, directory[1:])
        
    directory = item.dir
    
    if item.media:
        directory = directory[len(item.media.mountdir):]
        if len(directory) and directory[0] == '/':
            directory = directory[1:]
        return os.path.join(config.OVERLAY_DIR, 'disc', item.media.id, directory)
    else:
        if len(directory) and directory[0] == '/':
            directory = directory[1:]
        return os.path.join(config.OVERLAY_DIR, directory)


def abspath(name):
    """
    return the complete filename (including OVERLAY_DIR)
    """
    if os.path.exists(name):
        return os.path.abspath(name)
    overlay = getoverlay(name)
    if overlay and os.path.isfile(overlay):
        return overlay
    return ''


def isfile(name):
    """
    return if the given name is a file
    """
    return os.path.isfile(abspath(name))


def unlink(name):
    absname = abspath(name)
    if not absname:
        raise IOError, 'file %s not found' % name
    os.unlink(absname)


def stat(name):
    absname = abspath(name)
    if not absname:
        raise IOError, 'file %s not found' % name
    return os.stat(absname)

    
def open(name, mode='r'):
    """
    open the file
    """
    try:
        return file(name, mode)
    except:
        overlay = getoverlay(name)
        if not overlay:
            raise OSError
        try:
            if not os.path.isdir(os.path.dirname(overlay)):
                os.makedirs(os.path.dirname(overlay))
        except IOError:
            print 'error creating dir %s' % os.path.dirname(overlay)
            raise IOError
        try:
            return file(overlay, mode)
        except IOError:
            print 'error opening file %s' % overlay
            raise IOError


def codecs_open(name, mode, encoding):
    """
    use codecs.open to open the file
    """
    try:
        return codecs.open(name, mode, encoding=encoding)
    except:
        overlay = getoverlay(name)
        if not overlay:
            raise OSError
        try:
            if not os.path.isdir(os.path.dirname(overlay)):
                os.makedirs(os.path.dirname(overlay))
        except IOError:
            print 'error creating dir %s' % os.path.dirname(overlay)
            raise IOError
        try:
            return codecs.open(overlay, mode, encoding=encoding)
        except IOError, e:
            print 'error opening file %s' % overlay
            raise IOError, e
    

def listdir(directory, handle_exception=True):
    """
    get a directory listing (including OVERLAY_DIR)
    """
    try:
        files = ([ os.path.join(directory, fname)
                   for fname in os.listdir(directory) ])
        overlay = getoverlay(directory)
        if overlay and os.path.isdir(overlay):
            for f in ([ os.path.join(overlay, fname)
                        for fname in os.listdir(overlay) ]):
                if not os.path.isdir(f):
                    files.append(f)
                    
        return files
    except OSError:
        _debug_('Error in dir')
        if not handle_exception:
            raise OSError
        return []


def isoverlay(name):
    """
    return if the name is in the overlay dir
    """
    if not config.OVERLAY_DIR:
        return False
    return name.startswith(config.OVERLAY_DIR)


def normalize(name):
    """
    remove OVERLAY_DIR if it's in the path
    """
    if isoverlay(name):
        name = name[len(config.OVERLAY_DIR):]
        if name.startswith('disc'):
            name = name[5:]
            id = name[:name.find('/')]
            name = name[name.find('/')+1:]
            for media in config.REMOVABLE_MEDIA:
                if media.id == id:
                    name = os.path.join(media.mountdir, name)
        return name
    return name


# some other os functions (you don't need to use them)
basename = os.path.basename
join     = os.path.join
splitext = os.path.splitext
basename = os.path.basename
dirname  = os.path.dirname
exists   = os.path.exists
isdir    = os.path.isdir
islink   = os.path.islink

if not config.OVERLAY_DIR:
    print 'OVERLAY_DIR not set, virtual filesystem won\'t work'
else:
    print 'Virtual filesystem activated'
    




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to