Source

thg / thgutil / settings.py

# settings.py - TortoiseHg dialog settings library
#
# Copyright 2007 TK Soh <teekaysoh@gmail.com>
# Copyright 2009 Steve Borho <steve@borho.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2, incorporated herein by reference.

import os
import cPickle
from mercurial import util

class SimpleMRUList(object):
    def __init__(self, size=10, reflist=[], compact=True):
        self._size = size
        self._list = reflist
        if compact:
            self.compact()

    def __iter__(self):
        for elem in self._list:
            yield elem

    def add(self, val):
        if val in self._list:
            self._list.remove(val)
        self._list.insert(0, val)
        self.flush()

    def get_size(self):
        return self._size

    def set_size(self, size):
        self._size = size
        self.flush()

    def flush(self):
        while len(self._list) > self._size:
            del self._list[-1]

    def compact(self):
        ''' remove duplicate in list '''
        newlist = []
        for v in self._list:
            if v not in newlist:
                newlist.append(v)
        self._list[:] = newlist


class Settings(object):
    def __init__(self, appname, path=None):
        self._appname = appname
        self._data = {}
        self._path = path and path or self._get_path(appname)
        self._audit()
        self.read()

    def get_value(self, key, default=None, create=False):
        if key in self._data:
            return self._data[key]
        elif create:
            self._data[key] = default
        return default

    def set_value(self, key, value):
        self._data[key] = value

    def mrul(self, key, size=10):
        ''' wrapper method to create a most-recently-used (MRU) list '''
        ls = self.get_value(key, [], True)
        ml = SimpleMRUList(size=size, reflist=ls)
        return ml

    def get_keys(self):
        return self._data.keys()

    def get_appname(self):
        return self._appname

    def read(self):
        self._data.clear()
        if os.path.exists(self._path):
            try:
                f = file(self._path, 'rb')
                self._data = cPickle.loads(f.read())
                f.close()
            except Exception:
                pass

    def write(self):
        self._write(self._path, self._data)

    def _write(self, appname, data):
        s = cPickle.dumps(data)
        f = util.atomictempfile(appname, 'wb', None)
        f.write(s)
        try:
            f.rename()
        except WindowsError:
            pass # silently ignore these errors

    def _get_path(self, appname):
        if os.name == 'nt':
            try:
                import pywintypes
                from win32com.shell import shell, shellcon
                appdir = shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_APPDATA)
            except (ImportError, pywintypes.com_error):
                appdir = os.environ['APPDATA']
            return os.path.join(appdir, 'TortoiseHg', appname)
        else:
            home = os.path.expanduser('~')
            return os.path.join(home, '.tortoisehg', appname)

    def _audit(self):
        if os.path.exists(os.path.dirname(self._path)):
            return
        os.makedirs(os.path.dirname(self._path))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.