Return None instead of crashing on undefined integer config items
[stgit] / stgit / config.py
CommitLineData
41a6d859
CM
1"""Handles the Stacked GIT configuration files
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
c73e63b7 21import os, re
170f576b 22from stgit import basedir
87c93eab 23from stgit.exception import *
f0de3f92 24from stgit.run import *
41a6d859 25
87c93eab 26class GitConfigException(StgException):
c73e63b7
YD
27 pass
28
29class GitConfig:
30 __defaults={
31 'stgit.autoresolved': 'no',
32 'stgit.smtpserver': 'localhost:25',
33 'stgit.smtpdelay': '5',
1576d681
CM
34 'stgit.pullcmd': 'git pull',
35 'stgit.fetchcmd': 'git fetch',
3b3c26fa 36 'stgit.pull-policy': 'pull',
c73e63b7
YD
37 'stgit.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \
38 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
39 'stgit.autoimerge': 'no',
40 'stgit.keeporig': 'yes',
41 'stgit.keepoptimized': 'no',
42 'stgit.extensions': '.ancestor .current .patched',
43 'stgit.shortnr': '5'
44 }
45
9a4bf454
YD
46 __cache={}
47
c73e63b7 48 def get(self, name):
9a4bf454
YD
49 if self.__cache.has_key(name):
50 return self.__cache[name]
f0de3f92 51 try:
dbb0cf2c 52 value = Run('git', 'config', '--get', name).output_one_line()
f0de3f92
KH
53 except RunException:
54 value = self.__defaults.get(name, None)
9a4bf454
YD
55 self.__cache[name] = value
56 return value
c73e63b7
YD
57
58 def getall(self, name):
9a4bf454
YD
59 if self.__cache.has_key(name):
60 return self.__cache[name]
dbb0cf2c 61 values = Run('git', 'config', '--get-all', name
f0de3f92 62 ).returns([0, 1]).output_lines()
9a4bf454 63 self.__cache[name] = values
c73e63b7
YD
64 return values
65
66 def getint(self, name):
67 value = self.get(name)
e928a3ec
KH
68 if value == None:
69 return None
70 elif value.isdigit():
c73e63b7
YD
71 return int(value)
72 else:
73 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
74
cb5be4c3 75 def rename_section(self, from_name, to_name):
f0de3f92
KH
76 """Rename a section in the config file. Silently do nothing if
77 the section doesn't exist."""
dbb0cf2c 78 Run('git', 'config', '--rename-section', from_name, to_name
f0de3f92 79 ).returns([0, 1]).run()
8591add9 80 self.__cache.clear()
cb5be4c3 81
9a6bcbe2
KH
82 def remove_section(self, name):
83 """Remove a section in the config file. Silently do nothing if
84 the section doesn't exist."""
dbb0cf2c 85 Run('git', 'config', '--remove-section', name
9a6bcbe2
KH
86 ).returns([0, 1]).discard_stderr().discard_output()
87 self.__cache.clear()
88
c73e63b7 89 def set(self, name, value):
dbb0cf2c 90 Run('git', 'config', name, value).run()
8591add9 91 self.__cache[name] = value
c73e63b7 92
0aee23c2 93 def unset(self, name):
dbb0cf2c 94 Run('git', 'config', '--unset', name)
8591add9 95 self.__cache[name] = None
0aee23c2 96
c73e63b7
YD
97 def sections_matching(self, regexp):
98 """Takes a regexp with a single group, matches it against all
99 config variables, and returns a list whose members are the
100 group contents, for all variable names matching the regexp.
101 """
102 result = []
dbb0cf2c 103 for line in Run('git', 'config', '--get-regexp', '"^%s$"' % regexp
f0de3f92 104 ).returns([0, 1]).output_lines():
c73e63b7
YD
105 m = re.match('^%s ' % regexp, line)
106 if m:
107 result.append(m.group(1))
c73e63b7
YD
108 return result
109
110config=GitConfig()
abcc2620 111
eee7283e
CM
112def config_setup():
113 global config
114
eee7283e 115 # Set the PAGER environment to the config value (if any)
c73e63b7
YD
116 pager = config.get('stgit.pager')
117 if pager:
118 os.environ['PAGER'] = pager
119 # FIXME: handle EDITOR the same way ?
eee7283e
CM
120
121class ConfigOption:
122 """Delayed cached reading of a configuration option.
123 """
124 def __init__(self, section, option):
125 self.__section = section
126 self.__option = option
127 self.__value = None
128
129 def __str__(self):
130 if not self.__value:
c73e63b7 131 self.__value = config.get(self.__section + '.' + self.__option)
eee7283e 132 return self.__value
d7fade4b
CM
133
134
135# cached extensions
136__extensions = None
137
138def file_extensions():
139 """Returns a dictionary with the conflict file extensions
140 """
141 global __extensions
142
143 if not __extensions:
c73e63b7 144 cfg_ext = config.get('stgit.extensions').split()
d7fade4b
CM
145 if len(cfg_ext) != 3:
146 raise CmdException, '"extensions" configuration error'
147
148 __extensions = { 'ancestor': cfg_ext[0],
149 'current': cfg_ext[1],
150 'patched': cfg_ext[2] }
151
152 return __extensions