Merge lp:~vila/bzr-bookmarks/712935-2.3-compat into lp:~luks/bzr-bookmarks/trunk

Proposed by Vincent Ladeuil
Status: Merged
Merge reported by: Vincent Ladeuil
Merged at revision: not available
Proposed branch: lp:~vila/bzr-bookmarks/712935-2.3-compat
Merge into: lp:~luks/bzr-bookmarks/trunk
Diff against target: 189 lines (+47/-46)
1 file modified
__init__.py (+47/-46)
To merge this branch: bzr merge lp:~vila/bzr-bookmarks/712935-2.3-compat
Reviewer Review Type Date Requested Status
Lukáš Lalinský Pending
Review via email: mp+48589@code.launchpad.net

Description of the change

This fixes the compatibility with bzr-2.3 without trying to stay compatible with previous bzr versions.

Note that bzr config framework should (RSN) support this kind of feature out of the box (by using the trick pioneered here of prefixing config options instead of requiring a dedicated section).

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

@lukas: I'd be happy to discuss further maintenance of the plugin with you.

19. By Vincent Ladeuil

Add a version (2.3.0) to get better feedback in bug reports and make it clear we intend to be compatible with bzr >= 2.3.0.

Revision history for this message
Vincent Ladeuil (vila) wrote :

Merged in trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '__init__.py'
2--- __init__.py 2008-12-09 21:31:59 +0000
3+++ __init__.py 2011-02-04 11:24:10 +0000
4@@ -36,85 +36,84 @@
5 $ bzr help bookmarks
6 """
7
8+import bzrlib
9+from bzrlib import api
10+
11+bzr_plugin_version = (2, 3, 0, 'dev', 0)
12+# bzr-2.4 should include enough config enhancements to provide the same
13+# features but it doesn't hurt to try supporting it either.
14+api.require_any_api(bzrlib, [(2, 3, 0), (2, 4, 0)])
15+
16+from bzrlib import (
17+ config,
18+ decorators,
19+ errors,
20+ )
21 from bzrlib.urlutils import unescape_for_display
22-from bzrlib.config import GlobalConfig, ensure_config_dir_exists
23 from bzrlib.commands import Command, register_command
24-from bzrlib.errors import InvalidURL, BzrCommandError, NotBranchError
25 from bzrlib.option import Option
26 from bzrlib.branch import Branch
27 from bzrlib.transport import get_transport, register_transport
28
29
30-class GlobalBookmarkProvider(object):
31+class GlobalBookmarkProvider(config.GlobalConfig):
32
33+ @decorators.needs_write_lock
34 def set_bookmark(self, name, location):
35- config = GlobalConfig()
36- parser = config._get_parser()
37- if "BOOKMARKS" not in parser:
38- parser["BOOKMARKS"] = {}
39- parser["BOOKMARKS"][name] = location
40- parser.write(file(config._get_filename(), 'wb'))
41+ self._set_option(name, location, 'BOOKMARKS')
42
43 def unset_bookmark(self, name):
44- config = GlobalConfig()
45- parser = config._get_parser()
46- del parser["BOOKMARKS"][name]
47- parser.write(file(config._get_filename(), 'wb'))
48+ self.remove_user_option(name, 'BOOKMARKS')
49
50 def resolve_bookmark(self, name):
51- config = GlobalConfig()
52 try:
53- return config._get_parser().get_value("BOOKMARKS", name)
54+ return self._get_parser().get_value('BOOKMARKS', name)
55 except KeyError:
56 return None
57
58 def get_bookmarks(self):
59 bookmarks = {}
60- config = GlobalConfig()
61- filename = config._get_filename()
62- for name, value in config._get_parser().get("BOOKMARKS", {}).items():
63+ filename = self.file_name
64+ for name, value in self._get_parser().get("BOOKMARKS", {}).items():
65 bookmarks[name] = filename, value
66 return bookmarks
67
68
69-class LocationBookmarkProvider(object):
70+class LocationBookmarkProvider(config.BranchConfig):
71
72 def __init__(self, base='.'):
73 try:
74- self.branch = Branch.open_containing(base)[0]
75- except NotBranchError:
76- self.branch = None
77+ b = Branch.open_containing(base)[0]
78+ except errors.NotBranchError:
79+ b = None
80+ super(LocationBookmarkProvider, self).__init__(b)
81
82 def set_bookmark(self, name, location):
83 if self.branch is None:
84- return NotBranchError
85- config = self.branch.get_config()
86- return config.set_user_option("bookmark_%s" % name, location)
87+ raise errors.NotBranchError
88+ return self.set_user_option("bookmark_%s" % name, location)
89
90 def unset_bookmark(self, name):
91 if self.branch is None:
92- return NotBranchError
93- config = self.branch.get_config()
94- # FIXME: missing API to delete an option
95- return config.set_user_option("bookmark_%s" % name, '')
96+ raise errors.NotBranchError
97+ self.remove_user_option('bookmark_%s' % (name,))
98
99 def resolve_bookmark(self, name):
100 if self.branch is None:
101 return None
102- config = self.branch.get_config()
103- return config._get_user_option("bookmark_%s" % name)
104+ return self._get_user_option("bookmark_%s" % name)
105
106 def get_bookmarks(self):
107 bookmarks = {}
108 if self.branch is None:
109 return bookmarks
110- config = self.branch.get_config()
111- for source_class in config.option_sources:
112+ for source_class in self.option_sources:
113 source = source_class()
114 try:
115- filename = source._get_filename()
116+ filename = source.file_name
117 except AttributeError:
118- filename = source._config._transport.base + source._config._filename
119+ filename = (source._config._transport.base
120+ + source._config._filename)
121 filename = unescape_for_display(filename, 'utf-8')
122 for section_name, extra_path in source._get_matching_sections():
123 parser = source._get_parser()
124@@ -136,7 +135,7 @@
125 elif base_url.startswith('bm:'):
126 name = base_url[3:]
127 else:
128- raise InvalidURL(path=base_url)
129+ raise errors.InvalidURL(path=base_url)
130 if '/' in name:
131 bookmark, name = name.split('/', 1)
132 else:
133@@ -151,7 +150,7 @@
134 real_url = '/'.join([real_url, name])
135 break
136 else:
137- raise InvalidURL(path=base_url)
138+ raise errors.InvalidURL(path=base_url)
139 return real_url
140
141
142@@ -169,7 +168,7 @@
143
144 class cmd_bookmark(Command):
145 """Add, remove or modify a bookmark."""
146-
147+
148 _see_also = ["bookmarks"]
149
150 takes_args = ['name', 'location?']
151@@ -183,18 +182,20 @@
152 provider = LocationBookmarkProvider()
153 else:
154 provider = GlobalBookmarkProvider()
155- ensure_config_dir_exists()
156+ # FIXME: ensure_config_dir_exists is probably not needed but it's hard
157+ # to be sure without proper unit tests -- vila 20110204
158+ config.ensure_config_dir_exists()
159 if delete:
160 if provider.resolve_bookmark(name) is None:
161- raise BzrCommandError(
162+ raise errors.BzrCommandError(
163 'bookmark "%s" does not exist' % (name,))
164 provider.unset_bookmark(name)
165 else:
166 if '/' in name:
167- raise BzrCommandError(
168+ raise errors.BzrCommandError(
169 '"%s" contains a "/" character, bookmarks should not contain"/" characters"' % name)
170 if not location:
171- raise BzrCommandError(
172+ raise errors.BzrCommandError(
173 'no location provided for bookmark "%s"' % (name,))
174 provider.set_bookmark(name, location)
175
176@@ -209,10 +210,10 @@
177 bookmarks = {}
178 for provider_class in providers:
179 provider = provider_class()
180- for name, (source, url) in provider.get_bookmarks().items():
181- bookmarks.setdefault(source, {})[name] = url
182- for source, items in sorted(bookmarks.items()):
183- self.outf.write('%s:\n' % (source,))
184+ for name, (file_name, url) in provider.get_bookmarks().items():
185+ bookmarks.setdefault(file_name, {})[name] = url
186+ for file_name, items in sorted(bookmarks.items()):
187+ self.outf.write('%s:\n' % (file_name,))
188 for name, url in sorted(items.items()):
189 self.outf.write(' %-20s %s\n' % (name, url))
190

Subscribers

People subscribed via source and target branches