# Path: libxyz/exceptions.py
# class XYZRuntimeError(XYZError):
#     pass


import os
import os.path
import libxyz.const
from libxyz.exceptions import XYZRuntimeError



class UserData(object):
    """
    Common interface for access data-files in user directory
    """

    def __init__(self):
        self.user_dir = os.path.join(os.path.expanduser("~"),
                                     libxyz.const.USER_DIR)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def openfile(self, filename, mode, subdir=None):
        """
        Open data file and return open file-object
        For instance:
        > openfile("keycodes", "rb", "data")
        Will open file ~/.xyzcmd/data/keycodes for binary reading etc.

        @param filename: File name
        @param mode: Open mode
        @param subdir: Optional subdirectory
        @return open file-object or XYZRuntimeError raised on error
        """

        try:
            return open(self.makepath(filename, subdir), mode)
        except IOError, e:

# Path: libxyz/exceptions.py
# class XYZRuntimeError(XYZError):
#     pass


# Path: libxyz/core/userdata.py
import os
import os.path
import libxyz.const
from libxyz.exceptions import XYZRuntimeError




class UserData(object):
    """
    Common interface for access data-files in user directory
    """

    def __init__(self):
        self.user_dir = os.path.join(os.path.expanduser("~"),
                                     libxyz.const.USER_DIR)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def openfile(self, filename, mode, subdir=None):
        """
        Open data file and return open file-object
        For instance:
        > openfile("keycodes", "rb", "data")
        Will open file ~/.xyzcmd/data/keycodes for binary reading etc.

        @param filename: File name
        @param mode: Open mode
        @param subdir: Optional subdirectory
        @return open file-object or XYZRuntimeError raised on error
        """

        try:
            return open(self.makepath(filename, subdir), mode)
        except IOError, e:

            raise XYZRuntimeError(e)