#
#		Python GUI - File references and types - Generic
#
#		Classes for dealing with file references and file types
#		in as platform-independent a manner as possible.
#
#		In this view of things, a file reference consists
#		of two parts:
#
#			 1) A directory reference, whose nature is
#					platform-dependent,
#
#			 2) A name.
#

import os
from Properties import Properties, overridable_property

class FileRef(Properties):
    """A FileRef is an object representing a file system
    object in a platform-independent way."""

    dir = overridable_property('dir', "DirRef representing the parent directory.")
    name = overridable_property('name', "Name of the file (last pathname component).")
    path = overridable_property('path', "Full pathname of the file.")

    _dir = None		# DirRef representing the parent directory
    _name = None	# Name, including type suffix if any

    #
    #		Properties
    #

    def get_dir(self):
        return self._dir

    def set_dir(self, x):
        if not isinstance(x, DirRef):
            x = DirRef(path = x)
        self._dir = x

    def get_name(self):
        "Return the name of the file."
        return self._name

    def set_name(self, x):
        "Set the name of the file."
        self._name = x
    
    def get_path(self):
        return os.path.join(self._dir.path, self._name)

    def set_path(self, x):
        d, n = os.path.split(x)
        self._dir = DirRef(path = d)
        self._name = n
    
    #def get_suffix(self):
    #	 "Return the type suffix of the name, if any."
    #	 return os.path.splitext(self._name)[1]

    #def set_suffix(self, x):
    #	 """Remove any existing suffix from the name and add the
    #	 given suffix."""
    #	 if self.suffix != x:
    #		 self._name += x

    #
    #		Methods
    #

    def open(self, mode):
        "Open the file with the given mode and return a file object."
        return open(self.path, mode)
    
    def __str__(self):
        return "GUI.FileRef(%r)" % self.path

#-------------------------------------------------------------------------

class DirRef(Properties):
    """A DirRef is an object representing a directory in the
    file system. Its representation is completely platform
    dependent."""

    _path = None

    path = overridable_property('path', "Full pathname of the directory.")

    def get_path(self):
        return self._path

    def set_path(self, x):
        self._path = x

    def __str__(self):
        return "GUI.DirRef(%r)" % self.path
