~vcs-imports/shotfactory/trunk

« back to all changes in this revision

Viewing changes to shotfactory03/gui/linux/opera.py

  • Committer: johann
  • Date: 2007-05-27 12:44:46 UTC
  • Revision ID: vcs-imports@canonical.com-20070527124446-pl9q9fx1b4e73xap
Simple inifile module for Opera configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""
20
20
GUI-specific interface functions for X11.
21
 
 
22
 
>>> ini_find_section(TEST_LINES, 'User Prefs')
23
21
"""
24
22
 
25
 
__revision__ = '$Rev: 1315 $'
26
 
__date__ = '$Date: 2007-05-26 10:29:35 +0100 (Sat, 26 May 2007) $'
 
23
__revision__ = '$Rev: 1316 $'
 
24
__date__ = '$Date: 2007-05-27 13:44:46 +0100 (Sun, 27 May 2007) $'
27
25
__author__ = '$Author: johann $'
28
26
 
29
27
 
30
28
import os
31
29
import time
32
30
from shotfactory03.gui import linux as base
 
31
from shotfactory03.inifile import IniFile
33
32
 
34
33
 
35
34
class Gui(base.Gui):
45
44
        inifile = home + '/.opera/opera6.ini'
46
45
        if os.path.exists(inifile):
47
46
            print 'removing crash dialog from', inifile
48
 
            lines = file(inifile).readlines()
49
 
            ini_set(lines, 'User Prefs', 'Run', 0)
50
 
            ini_set(lines, 'User Prefs', 'Show New Opera Dialog', 1)
51
 
            open(inifile, 'w').write(''.join(lines))
52
 
 
53
 
def ini_set(lines, section, key, value):
54
 
    if lines[0][-2] == chr(13):
55
 
        crlf = lines[0][-2:]
56
 
    else:
57
 
        crlf = lines[0][-1]
58
 
    start, stop = self.ini_find_section(lines, section)
59
 
    if start is None:
60
 
        lines.append(crlf)
61
 
        lines.append('[' + section + ']')
62
 
        lines.append(key + '=' + str(value) + crlf)
63
 
    else:
64
 
        index = self.ini_find_key_line(lines, start, stop, key)
65
 
        if index:
66
 
            lines[index] = key + '=' + str(value) + crlf
67
 
        else:
68
 
            lines.insert(stop, key + '=' + str(value) + crlf)
69
 
 
70
 
 
71
 
def ini_find_section(lines, section):
72
 
    start = None
73
 
    for index, line in enumerate(lines):
74
 
        if line.startswith('[' + section + ']'):
75
 
            start = index
76
 
        if start and line.strip() == '':
77
 
            return start, index
78
 
    if start:
79
 
        return start, len(lines)
80
 
    return None, None
81
 
 
82
 
 
83
 
def ini_find_key_line(lines, start, stop, key):
84
 
    for index in range(start, stop):
85
 
        if lines[index].startswith(key + '='):
86
 
            return index
87
 
 
88
 
 
89
 
if __name__ == '__main__':
90
 
    import doctest
91
 
    TEST_LINES = [
92
 
        "[User Prefs]",
93
 
        ]
94
 
    doctest.testmod()
 
47
            ini = IniFile(inifile)
 
48
            ini.set(lines, 'User Prefs', 'Run', 0)
 
49
            ini.set(lines, 'User Prefs', 'Show New Opera Dialog', 1)
 
50
            ini.save()