Package libxyz :: Package core :: Module userdata

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.userdata

     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 os
    18  import os.path
    19
    20  import libxyz.const
    21
    22  from libxyz.exceptions import XYZRuntimeError
    23
    24 -class UserData(object):
    25      """
    26      Common interface for access data-files in user directory
    27      """
    28
    29 -    def __init__(self):
    30          self.user_dir = os.path.join(os.path.expanduser("~"),
    31                                       libxyz.const.USER_DIR)
    32
    33      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    34
    35 -    def openfile(self, filename, mode, subdir=None):
    36          """
    37          Open data file and return open file-object
    38          For instance:
    39          > openfile("keycodes", "rb", "data")
    40          Will open file ~/.xyzcmd/data/keycodes for binary reading etc.
    41
    42          @param filename: File name
    43          @param mode: Open mode
    44          @param subdir: Optional subdirectory
    45          @return open file-object or XYZRuntimeError raised on error
    46          """
    47
    48          try:
    49              return open(self.makepath(filename, subdir), mode)
    50          except IOError, e:
    51              raise XYZRuntimeError(e)
    52
    53      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    54
    55 -    def delfile(self, filename, subdir=None):
    56          """
    57          Delete file in user-directory
    58          """
    59
    60          _path = self.makepath(filename, subdir)
    61
    62          if os.path.isfile(_path):
    63              try:
    64                  os.unlink(_path)
    65              except OSError, e:
    66                  raise XYZRuntimeError(e)
    67
    68      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    69
    70 -    def makepath(self, filename, subdir=None):
    71          """
    72          Make absolute path to file
    73          """
    74
    75          _path = [self.user_dir]
    76
    77          if subdir is not None:
    78              _path.append(subdir)
    79
    80              _prelpath = os.path.join(*_path)
    81
    82              # Create subdirectory if it doesn't exist
    83              if not os.access(_prelpath, os.F_OK):
    84                  try:
    85                      os.mkdir(_prelpath)
    86                  except OSError, e:
    87                      raise XYZRuntimeError(e)
    88
    89          _path.append(filename)
    90
    91          return os.path.join(*_path)
    92