Start working on recycling RevisionBuildEditor.
[jelmer/subvertpy.git] / config.py
1 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 """Stores per-repository settings."""
17
18 from bzrlib import osutils
19 from bzrlib.config import IniBasedConfig, config_dir, ensure_config_dir_exists
20
21 import os
22
23 from scheme import BranchingScheme
24
25 # Settings are stored by UUID. 
26 # Data stored includes default branching scheme and locations the repository 
27 # was seen at.
28
29 def subversion_config_filename():
30     """Return per-user configuration ini file filename."""
31     return osutils.pathjoin(config_dir(), 'subversion.conf')
32
33 class SvnRepositoryConfig(IniBasedConfig):
34     """Per-repository settings."""
35
36     def __init__(self, uuid):
37         name_generator = subversion_config_filename
38         super(SvnRepositoryConfig, self).__init__(name_generator)
39         self.uuid = uuid
40         if not self.uuid in self._get_parser():
41             self._get_parser()[self.uuid] = {}
42
43     def set_branching_scheme(self, scheme):
44         """Change the branching scheme.
45
46         :param scheme: New branching scheme.
47         """
48         self.set_user_option('branching-scheme', str(scheme))
49
50     def get_branching_scheme(self):
51         """Get the branching scheme.
52
53         :return: BranchingScheme instance.
54         """
55         try:
56             return BranchingScheme.find_scheme(self._get_parser()[self.uuid]['branching-scheme'])
57         except KeyError:
58             return None
59
60     def get_locations(self):
61         """Find the locations this repository has been seen at.
62
63         :return: Set with URLs.
64         """
65         try:
66             return set(self._get_parser()[self.uuid]['locations'].split(";"))
67         except KeyError:
68             return set()
69
70     def add_location(self, location):
71         """Add a location for this repository.
72
73         :param location: URL of location to add.
74         """
75         locations = self.get_locations()
76         locations.add(location.rstrip("/"))
77         self.set_user_option('locations', ";".join(list(locations)))
78
79     def set_user_option(self, name, value):
80         """Change a user option.
81
82         :param name: Name of the option.
83         :param value: Value of the option.
84         """
85         conf_dir = os.path.dirname(self._get_filename())
86         ensure_config_dir_exists(conf_dir)
87         self._get_parser()[self.uuid][name] = value
88         f = open(self._get_filename(), 'wb')
89         self._get_parser().write(f)
90         f.close()