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
=== modified file '__init__.py'
--- __init__.py 2008-12-09 21:31:59 +0000
+++ __init__.py 2011-02-04 11:24:10 +0000
@@ -36,85 +36,84 @@
36 $ bzr help bookmarks36 $ bzr help bookmarks
37"""37"""
3838
39import bzrlib
40from bzrlib import api
41
42bzr_plugin_version = (2, 3, 0, 'dev', 0)
43# bzr-2.4 should include enough config enhancements to provide the same
44# features but it doesn't hurt to try supporting it either.
45api.require_any_api(bzrlib, [(2, 3, 0), (2, 4, 0)])
46
47from bzrlib import (
48 config,
49 decorators,
50 errors,
51 )
39from bzrlib.urlutils import unescape_for_display52from bzrlib.urlutils import unescape_for_display
40from bzrlib.config import GlobalConfig, ensure_config_dir_exists
41from bzrlib.commands import Command, register_command53from bzrlib.commands import Command, register_command
42from bzrlib.errors import InvalidURL, BzrCommandError, NotBranchError
43from bzrlib.option import Option54from bzrlib.option import Option
44from bzrlib.branch import Branch55from bzrlib.branch import Branch
45from bzrlib.transport import get_transport, register_transport56from bzrlib.transport import get_transport, register_transport
4657
4758
48class GlobalBookmarkProvider(object):59class GlobalBookmarkProvider(config.GlobalConfig):
4960
61 @decorators.needs_write_lock
50 def set_bookmark(self, name, location):62 def set_bookmark(self, name, location):
51 config = GlobalConfig()63 self._set_option(name, location, 'BOOKMARKS')
52 parser = config._get_parser()
53 if "BOOKMARKS" not in parser:
54 parser["BOOKMARKS"] = {}
55 parser["BOOKMARKS"][name] = location
56 parser.write(file(config._get_filename(), 'wb'))
5764
58 def unset_bookmark(self, name):65 def unset_bookmark(self, name):
59 config = GlobalConfig()66 self.remove_user_option(name, 'BOOKMARKS')
60 parser = config._get_parser()
61 del parser["BOOKMARKS"][name]
62 parser.write(file(config._get_filename(), 'wb'))
6367
64 def resolve_bookmark(self, name):68 def resolve_bookmark(self, name):
65 config = GlobalConfig()
66 try:69 try:
67 return config._get_parser().get_value("BOOKMARKS", name)70 return self._get_parser().get_value('BOOKMARKS', name)
68 except KeyError:71 except KeyError:
69 return None72 return None
7073
71 def get_bookmarks(self):74 def get_bookmarks(self):
72 bookmarks = {}75 bookmarks = {}
73 config = GlobalConfig()76 filename = self.file_name
74 filename = config._get_filename()77 for name, value in self._get_parser().get("BOOKMARKS", {}).items():
75 for name, value in config._get_parser().get("BOOKMARKS", {}).items():
76 bookmarks[name] = filename, value78 bookmarks[name] = filename, value
77 return bookmarks79 return bookmarks
7880
7981
80class LocationBookmarkProvider(object):82class LocationBookmarkProvider(config.BranchConfig):
8183
82 def __init__(self, base='.'):84 def __init__(self, base='.'):
83 try:85 try:
84 self.branch = Branch.open_containing(base)[0]86 b = Branch.open_containing(base)[0]
85 except NotBranchError:87 except errors.NotBranchError:
86 self.branch = None88 b = None
89 super(LocationBookmarkProvider, self).__init__(b)
8790
88 def set_bookmark(self, name, location):91 def set_bookmark(self, name, location):
89 if self.branch is None:92 if self.branch is None:
90 return NotBranchError93 raise errors.NotBranchError
91 config = self.branch.get_config()94 return self.set_user_option("bookmark_%s" % name, location)
92 return config.set_user_option("bookmark_%s" % name, location)
9395
94 def unset_bookmark(self, name):96 def unset_bookmark(self, name):
95 if self.branch is None:97 if self.branch is None:
96 return NotBranchError98 raise errors.NotBranchError
97 config = self.branch.get_config()99 self.remove_user_option('bookmark_%s' % (name,))
98 # FIXME: missing API to delete an option
99 return config.set_user_option("bookmark_%s" % name, '')
100100
101 def resolve_bookmark(self, name):101 def resolve_bookmark(self, name):
102 if self.branch is None:102 if self.branch is None:
103 return None103 return None
104 config = self.branch.get_config()104 return self._get_user_option("bookmark_%s" % name)
105 return config._get_user_option("bookmark_%s" % name)
106105
107 def get_bookmarks(self):106 def get_bookmarks(self):
108 bookmarks = {}107 bookmarks = {}
109 if self.branch is None:108 if self.branch is None:
110 return bookmarks109 return bookmarks
111 config = self.branch.get_config()110 for source_class in self.option_sources:
112 for source_class in config.option_sources:
113 source = source_class()111 source = source_class()
114 try:112 try:
115 filename = source._get_filename()113 filename = source.file_name
116 except AttributeError:114 except AttributeError:
117 filename = source._config._transport.base + source._config._filename115 filename = (source._config._transport.base
116 + source._config._filename)
118 filename = unescape_for_display(filename, 'utf-8')117 filename = unescape_for_display(filename, 'utf-8')
119 for section_name, extra_path in source._get_matching_sections():118 for section_name, extra_path in source._get_matching_sections():
120 parser = source._get_parser()119 parser = source._get_parser()
@@ -136,7 +135,7 @@
136 elif base_url.startswith('bm:'):135 elif base_url.startswith('bm:'):
137 name = base_url[3:]136 name = base_url[3:]
138 else:137 else:
139 raise InvalidURL(path=base_url)138 raise errors.InvalidURL(path=base_url)
140 if '/' in name:139 if '/' in name:
141 bookmark, name = name.split('/', 1)140 bookmark, name = name.split('/', 1)
142 else:141 else:
@@ -151,7 +150,7 @@
151 real_url = '/'.join([real_url, name])150 real_url = '/'.join([real_url, name])
152 break151 break
153 else:152 else:
154 raise InvalidURL(path=base_url)153 raise errors.InvalidURL(path=base_url)
155 return real_url154 return real_url
156155
157156
@@ -169,7 +168,7 @@
169168
170class cmd_bookmark(Command):169class cmd_bookmark(Command):
171 """Add, remove or modify a bookmark."""170 """Add, remove or modify a bookmark."""
172 171
173 _see_also = ["bookmarks"]172 _see_also = ["bookmarks"]
174173
175 takes_args = ['name', 'location?']174 takes_args = ['name', 'location?']
@@ -183,18 +182,20 @@
183 provider = LocationBookmarkProvider()182 provider = LocationBookmarkProvider()
184 else:183 else:
185 provider = GlobalBookmarkProvider()184 provider = GlobalBookmarkProvider()
186 ensure_config_dir_exists()185 # FIXME: ensure_config_dir_exists is probably not needed but it's hard
186 # to be sure without proper unit tests -- vila 20110204
187 config.ensure_config_dir_exists()
187 if delete:188 if delete:
188 if provider.resolve_bookmark(name) is None:189 if provider.resolve_bookmark(name) is None:
189 raise BzrCommandError(190 raise errors.BzrCommandError(
190 'bookmark "%s" does not exist' % (name,))191 'bookmark "%s" does not exist' % (name,))
191 provider.unset_bookmark(name)192 provider.unset_bookmark(name)
192 else:193 else:
193 if '/' in name:194 if '/' in name:
194 raise BzrCommandError(195 raise errors.BzrCommandError(
195 '"%s" contains a "/" character, bookmarks should not contain"/" characters"' % name)196 '"%s" contains a "/" character, bookmarks should not contain"/" characters"' % name)
196 if not location:197 if not location:
197 raise BzrCommandError(198 raise errors.BzrCommandError(
198 'no location provided for bookmark "%s"' % (name,))199 'no location provided for bookmark "%s"' % (name,))
199 provider.set_bookmark(name, location)200 provider.set_bookmark(name, location)
200201
@@ -209,10 +210,10 @@
209 bookmarks = {}210 bookmarks = {}
210 for provider_class in providers:211 for provider_class in providers:
211 provider = provider_class()212 provider = provider_class()
212 for name, (source, url) in provider.get_bookmarks().items():213 for name, (file_name, url) in provider.get_bookmarks().items():
213 bookmarks.setdefault(source, {})[name] = url214 bookmarks.setdefault(file_name, {})[name] = url
214 for source, items in sorted(bookmarks.items()):215 for file_name, items in sorted(bookmarks.items()):
215 self.outf.write('%s:\n' % (source,))216 self.outf.write('%s:\n' % (file_name,))
216 for name, url in sorted(items.items()):217 for name, url in sorted(items.items()):
217 self.outf.write(' %-20s %s\n' % (name, url))218 self.outf.write(' %-20s %s\n' % (name, url))
218219

Subscribers

People subscribed via source and target branches