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
1=== modified file 'editmoin'
2--- editmoin 2010-09-15 03:09:40 +0000
3+++ editmoin 2011-04-10 23:07:28 +0000
4@@ -31,6 +31,8 @@
5 import subprocess
6 import getpass
7 import Cookie
8+import urlparse
9+import urllib
10 try:
11 from hashlib import md5
12 except ImportError:
13@@ -258,6 +260,42 @@
14 def get_user(moinurl):
15 return get_token_with_url(USERSFILENAME, moinurl)
16
17+def extract_args(moinurl):
18+ """Given a URL with a query, strip those that conflict such
19+ as 'action' and extract those that are equivalent to command line
20+ arguments such as 'template'.
21+
22+ >>> extract_args('https://foo.org/Bar')
23+ ('https://foo.org/Bar', None)
24+
25+ >>> extract_args('https://foo.org/Baz?action=edit')
26+ ('https://foo.org/Baz', None)
27+
28+ >>> extract_args('https://foo.org/Dog?action=edit&template=Template')
29+ ('https://foo.org/Dog', 'Template')
30+ """
31+ try:
32+ parts = urlparse.urlparse(moinurl)
33+ params = urlparse.parse_qs(parts.query)
34+
35+ # Pull out the template name
36+ template = params.get('template', [None])[0]
37+
38+ # Delete all parameters that might interfere
39+ for blacklist in 'action template'.split():
40+ if blacklist in params:
41+ del params[blacklist]
42+
43+ # Turn the parameters back into a query string
44+ encoded = urllib.urlencode(params)
45+
46+ next = urlparse.urlunparse((parts.scheme, parts.netloc, parts.path, parts.params, encoded, parts.fragment))
47+
48+ return next, template
49+ except Exception, ex:
50+ # Something went wrong. Give up.
51+ return moinurl, None
52+
53 def translate_shortcut(moinurl):
54 if "://" in moinurl:
55 return moinurl
56@@ -328,7 +366,7 @@
57 if moinfile.question:
58 moinfile.answer = raw_input(moinfile.question + " ")
59 filename = moinfile.write_raw()
60- editor = os.environ.get("EDITOR", "vi")
61+ editor = os.environ.get("EDITOR", "sensible-editor")
62 digest = md5(open(filename).read()).digest()
63 subprocess.call("%s %s" % (editor, filename), shell=True)
64 if digest != md5(open(filename).read()).digest():
65@@ -415,6 +453,11 @@
66 raise Error("Couldn't obtain session cookie from server")
67
68 def edit(url, template=None, editfile_func=editfile):
69+ url, url_template = extract_args(url)
70+
71+ if url_template and not template:
72+ template = url_template
73+
74 url = translate_shortcut(url)
75 user = get_user(url)
76 if user:

Subscribers

People subscribed via source and target branches

to all changes: