File: recentunits.py

package info (click to toggle)
convertall 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,448 kB
  • ctags: 521
  • sloc: python: 2,338; makefile: 7
file content (64 lines) | stat: -rw-r--r-- 2,045 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3

#****************************************************************************
# recentunits.py, provides a list of recently used units
#
# ConvertAll, a units conversion program
# Copyright (C) 2014, Douglas W. Bell
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version.  This program is distributed in the hope that it will be useful,
# but WITTHOUT ANY WARRANTY.  See the included LICENSE file for details.
#*****************************************************************************


class RecentUnits(list):
    """A list of recent unit combo names.
    """
    def __init__(self, options):
        list.__init__(self)
        self.options = options
        self.updateQuantity()
        self.loadList()

    def updateQuantity(self):
        """Update number of entries from options.
        """
        self.numEntries = self.options.intData('RecentUnits', 0, 99)
        del self[self.numEntries:]

    def loadList(self):
        """Load recent units from option file.
        """
        self[:] = []
        for num in range(self.numEntries):
            name = self.options.strData(self.optionTitle(num), True)
            if name:
                self.append(name)

    def writeList(self):
        """Write list of paths to options.
        """
        for num in range(self.numEntries):
            try:
                name = self[num]
            except IndexError:
                name = ''
            self.options.changeData(self.optionTitle(num), name, True)
        self.options.writeChanges()

    def addEntry(self, name):
        """Move name to start if found, otherwise add it.
        """
        try:
            self.remove(name)
        except ValueError:
            pass
        self.insert(0, name)
        del self[self.numEntries:]

    def optionTitle(self, num):
        """Return option key for the given nummber.
        """
        return 'RecentUnit{0}'.format(num + 1)