PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/util/settings.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 124 lines | 95 code | 20 blank | 9 comment | 18 complexity | d23edd45fc19683abdbb6e4f78e7de82 MD5 | raw file
  1# settings.py - TortoiseHg dialog settings library
  2#
  3# Copyright 2007 TK Soh <teekaysoh@gmail.com>
  4# Copyright 2009 Steve Borho <steve@borho.org>
  5#
  6# This software may be used and distributed according to the terms of the
  7# GNU General Public License version 2, incorporated herein by reference.
  8
  9import os
 10import cPickle
 11from mercurial import util
 12
 13class SimpleMRUList(object):
 14    def __init__(self, size=10, reflist=[], compact=True):
 15        self._size = size
 16        self._list = reflist
 17        if compact:
 18            self.compact()
 19
 20    def __iter__(self):
 21        for elem in self._list:
 22            yield elem
 23
 24    def add(self, val):
 25        if val in self._list:
 26            self._list.remove(val)
 27        self._list.insert(0, val)
 28        self.flush()
 29
 30    def get_size(self):
 31        return self._size
 32
 33    def set_size(self, size):
 34        self._size = size
 35        self.flush()
 36
 37    def flush(self):
 38        while len(self._list) > self._size:
 39            del self._list[-1]
 40
 41    def compact(self):
 42        ''' remove duplicate in list '''
 43        newlist = []
 44        for v in self._list:
 45            if v not in newlist:
 46                newlist.append(v)
 47        self._list[:] = newlist
 48
 49
 50class Settings(object):
 51    def __init__(self, appname, path=None):
 52        self._appname = appname
 53        self._data = {}
 54        self._path = path and path or self._get_path(appname)
 55        self._audit()
 56        self.read()
 57
 58    def get_value(self, key, default=None, create=False):
 59        if key in self._data:
 60            return self._data[key]
 61        elif create:
 62            self._data[key] = default
 63        return default
 64
 65    def set_value(self, key, value):
 66        self._data[key] = value
 67
 68    def mrul(self, key, size=10):
 69        ''' wrapper method to create a most-recently-used (MRU) list '''
 70        ls = self.get_value(key, [], True)
 71        ml = SimpleMRUList(size=size, reflist=ls)
 72        return ml
 73
 74    def get_keys(self):
 75        return self._data.keys()
 76
 77    def get_appname(self):
 78        return self._appname
 79
 80    def read(self):
 81        self._data.clear()
 82        if os.path.exists(self._path):
 83            try:
 84                f = file(self._path, 'rb')
 85                self._data = cPickle.loads(f.read())
 86                f.close()
 87            except Exception:
 88                pass
 89
 90    def write(self):
 91        self._write(self._path, self._data)
 92
 93    def _write(self, appname, data):
 94        s = cPickle.dumps(data)
 95        f = util.atomictempfile(appname, 'wb', None)
 96        f.write(s)
 97        try:
 98            f.rename()
 99        except OSError:
100            pass # silently ignore these errors
101
102    def _get_path(self, appname):
103        if os.name == 'nt':
104            try:
105                import pywintypes
106                try:
107                    from win32com.shell import shell, shellcon
108                    appdir = shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_APPDATA)
109                except pywintypes.com_error:
110                    appdir = os.environ['APPDATA']
111            except ImportError:
112                appdir = os.environ['APPDATA']
113            return os.path.join(appdir, 'TortoiseHg', appname)
114        else:
115            home = os.path.expanduser('~')
116            return os.path.join(home, '.tortoisehg', appname)
117
118    def _audit(self):
119        if os.path.exists(os.path.dirname(self._path)):
120            return
121        try:
122            os.makedirs(os.path.dirname(self._path))
123        except OSError:
124            pass # silently ignore these errors