Merge lp:~michaelh1/editmoin/template-query into lp:editmoin

Proposed by Michael Hope
Status: Merged
Approved by: Gustavo Niemeyer
Approved revision: 38
Merge reported by: Gustavo Niemeyer
Merged at revision: not available
Proposed branch: lp:~michaelh1/editmoin/template-query
Merge into: lp:editmoin
Diff against target: 76 lines (+44/-1)
1 file modified
editmoin (+44/-1)
To merge this branch: bzr merge lp:~michaelh1/editmoin/template-query
Reviewer Review Type Date Requested Status
Gustavo Niemeyer Approve
Review via email: mp+57088@code.launchpad.net

Description of the change

Contains two patches:
revno: 38
  Use sensible-editor instead of vi to pick up the system level editor preference.

revno: 37
  Add support for URLs that include query parameters. Useful for pages
  such as the Linaro wiki meetings:
   https://wiki.linaro.org/WorkingGroups/ToolChain/Meetings/

  where the query includes the template and action such as:
   https://wiki.linaro.org/WorkingGroups/ToolChain/Meetings/2011-04-11?action=edit&template=WorkingGroups/ToolChain/MeetingTemplate

  This patch allows you right-click copy the URL and then go 'editmoin
  http://foo.bar/baz?template=Template' and have the correct template
  come through.

  Uses features that are in Python 2.6+

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Very nice, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'editmoin'
--- editmoin 2010-09-15 03:09:40 +0000
+++ editmoin 2011-04-10 23:07:28 +0000
@@ -31,6 +31,8 @@
31import subprocess31import subprocess
32import getpass32import getpass
33import Cookie33import Cookie
34import urlparse
35import urllib
34try:36try:
35 from hashlib import md537 from hashlib import md5
36except ImportError:38except ImportError:
@@ -258,6 +260,42 @@
258def get_user(moinurl):260def get_user(moinurl):
259 return get_token_with_url(USERSFILENAME, moinurl)261 return get_token_with_url(USERSFILENAME, moinurl)
260262
263def extract_args(moinurl):
264 """Given a URL with a query, strip those that conflict such
265 as 'action' and extract those that are equivalent to command line
266 arguments such as 'template'.
267
268 >>> extract_args('https://foo.org/Bar')
269 ('https://foo.org/Bar', None)
270
271 >>> extract_args('https://foo.org/Baz?action=edit')
272 ('https://foo.org/Baz', None)
273
274 >>> extract_args('https://foo.org/Dog?action=edit&template=Template')
275 ('https://foo.org/Dog', 'Template')
276 """
277 try:
278 parts = urlparse.urlparse(moinurl)
279 params = urlparse.parse_qs(parts.query)
280
281 # Pull out the template name
282 template = params.get('template', [None])[0]
283
284 # Delete all parameters that might interfere
285 for blacklist in 'action template'.split():
286 if blacklist in params:
287 del params[blacklist]
288
289 # Turn the parameters back into a query string
290 encoded = urllib.urlencode(params)
291
292 next = urlparse.urlunparse((parts.scheme, parts.netloc, parts.path, parts.params, encoded, parts.fragment))
293
294 return next, template
295 except Exception, ex:
296 # Something went wrong. Give up.
297 return moinurl, None
298
261def translate_shortcut(moinurl):299def translate_shortcut(moinurl):
262 if "://" in moinurl:300 if "://" in moinurl:
263 return moinurl301 return moinurl
@@ -328,7 +366,7 @@
328 if moinfile.question:366 if moinfile.question:
329 moinfile.answer = raw_input(moinfile.question + " ")367 moinfile.answer = raw_input(moinfile.question + " ")
330 filename = moinfile.write_raw()368 filename = moinfile.write_raw()
331 editor = os.environ.get("EDITOR", "vi")369 editor = os.environ.get("EDITOR", "sensible-editor")
332 digest = md5(open(filename).read()).digest()370 digest = md5(open(filename).read()).digest()
333 subprocess.call("%s %s" % (editor, filename), shell=True)371 subprocess.call("%s %s" % (editor, filename), shell=True)
334 if digest != md5(open(filename).read()).digest():372 if digest != md5(open(filename).read()).digest():
@@ -415,6 +453,11 @@
415 raise Error("Couldn't obtain session cookie from server")453 raise Error("Couldn't obtain session cookie from server")
416454
417def edit(url, template=None, editfile_func=editfile):455def edit(url, template=None, editfile_func=editfile):
456 url, url_template = extract_args(url)
457
458 if url_template and not template:
459 template = url_template
460
418 url = translate_shortcut(url)461 url = translate_shortcut(url)
419 user = get_user(url)462 user = get_user(url)
420 if user:463 if user:

Subscribers

People subscribed via source and target branches

to all changes: