Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13)

Delta Between Two Patch Sets: MoinMoin/util/interwiki.py

Issue 10439044: Interwiki Module Fixed
Left Patch Set: all tests checked Created 11 years, 9 months ago
Right Patch Set: TODO Removed :| Created 11 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « MoinMoin/util/_tests/test_interwiki.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright: 2010 MoinMoin:ThomasWaldmann 1 # Copyright: 2010 MoinMoin:ThomasWaldmann
2 # Copyright: 2010 MoinMoin:MicheleOrru 2 # Copyright: 2010 MoinMoin:MicheleOrru
3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details. 3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
4 4
5 """ 5 """
6 MoinMoin - interwiki support code 6 MoinMoin - interwiki support code
7 """ 7 """
8 8
9 from __future__ import absolute_import, division 9 from __future__ import absolute_import, division
10 10
11 from werkzeug import url_quote 11 from werkzeug import url_quote
12 12
13 from flask import current_app as app 13 from flask import current_app as app
14 from flask import url_for 14 from flask import url_for
15 15
16 import os.path 16 import os.path
17 from collections import namedtuple 17 from collections import namedtuple
18 18
19 from MoinMoin.constants.keys import CURRENT, FIELDS, NAME_EXACT 19 from MoinMoin.constants.keys import CURRENT, FIELDS, NAME_EXACT, NAMESPACE
20 from MoinMoin.constants.contenttypes import CHARSET 20 from MoinMoin.constants.contenttypes import CHARSET
21 21
22 from MoinMoin import log 22 from MoinMoin import log
23 logging = log.getLogger(__name__) 23 logging = log.getLogger(__name__)
24 24
25 25
26 def is_local_wiki(wiki_name): 26 def is_local_wiki(wiki_name):
27 """ 27 """
28 check if <wiki_name> is THIS wiki 28 check if <wiki_name> is THIS wiki
29 """ 29 """
30 return wiki_name in [u'', u'Self', app.cfg.interwikiname, ] 30 return wiki_name in [u'', u'Self', app.cfg.interwikiname, ]
31 31
32 32
33 def is_known_wiki(wiki_name): 33 def is_known_wiki(wiki_name):
34 """ 34 """
35 check if <wiki_name> is a known wiki name 35 check if <wiki_name> is a known wiki name
36 36
37 Note: interwiki_map should have entries for the special wikinames 37 Note: interwiki_map should have entries for the special wikinames
38 denoting THIS wiki, so we do not need to check these names separately. 38 denoting THIS wiki, so we do not need to check these names separately.
39 """ 39 """
40 return wiki_name in app.cfg.interwiki_map 40 return wiki_name in app.cfg.interwiki_map
41 41
42 42
43 def get_fqname(item_name, field, namespace): 43 def get_fqname(item_name, field, namespace):
waldi 2013/06/29 14:30:10 You may want to convert this to a member of Compos
44 """ 44 """
45 Compute composite name from item_name, field, namespace 45 Compute composite name from item_name, field, namespace
46 composite name == [NAMESPACE/][@FIELD/]NAME 46 composite name == [NAMESPACE/][@FIELD/]NAME
47 """ 47 """
48 if field == NAME_EXACT: 48 if field == NAME_EXACT:
49 field = u'' 49 field = u''
50 if field: 50 if field:
waldi 2013/06/29 14:30:10 "if field and field != NAME_EXACT:" may be faster
51 item_name = u'@{0}/{1}'.format(field, item_name) 51 item_name = u'@{0}/{1}'.format(field, item_name)
52 if namespace: 52 if namespace:
53 item_name = u'{0}/{1}'.format(namespace, item_name) 53 item_name = u'{0}/{1}'.format(namespace, item_name)
54 return item_name 54 return item_name
55 55
56 56
57 def url_for_item(item_name, wiki_name=u'', field=u'', namespace=u'', rev=CURRENT , endpoint=u'frontend.show_item', _external=False): 57 def url_for_item(item_name, wiki_name=u'', field=u'', namespace=u'', rev=CURRENT , endpoint=u'frontend.show_item', _external=False):
58 """ 58 """
59 Compute URL for some local or remote/interwiki item. 59 Compute URL for some local or remote/interwiki item.
60 60
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 length = len(namespace) + 1 136 length = len(namespace) + 1
137 url = url[length:] 137 url = url[length:]
138 return namespace, url 138 return namespace, url
139 139
140 140
141 class CompositeName(namedtuple('CompositeName', 'namespace, field, value')): 141 class CompositeName(namedtuple('CompositeName', 'namespace, field, value')):
142 """ 142 """
143 namedtuple to hold the compositename 143 namedtuple to hold the compositename
144 """ 144 """
145 @property 145 @property
146 def split(self): 146 def split(self):
waldi 2013/06/29 14:30:10 Is this used? This looks like backward compatibili
147 return {NAMESPACE: self.namespace, u'field': self.field, u'item_name': s elf.value} 147 return {NAMESPACE: self.namespace, u'field': self.field, u'item_name': s elf.value}
148 148
149 @property 149 @property
150 def fullname(self): 150 def fullname(self):
151 return get_fqname(self.value, self.field, self.namespace) 151 return get_fqname(self.value, self.field, self.namespace)
152 152
153 def __unicode__(self): 153 def __unicode__(self):
154 return self.fullname 154 return self.fullname
155 155
156 156
157 def split_fqname(url): 157 def split_fqname(url):
waldi 2013/06/29 14:30:10 You may want to convert this to a (static) member
158 """ 158 """
159 Split a fully qualified url into namespace, field and pagename 159 Split a fully qualified url into namespace, field and pagename
160 url -> [NAMESPACE/][@FIELD/]NAME 160 url -> [NAMESPACE/][@FIELD/]NAME
161 param: url: the url to split 161 param: url: the url to split
162 returns: a namedtuple CompositeName(namespace, field, itemname) 162 returns: a namedtuple CompositeName(namespace, field, itemname)
163 Example: 163 Example:
164 url: u'ns1/ns2/@itemid/Page' return u'ns1/ns2', u'itemid', u'Page' 164 url: u'ns1/ns2/@itemid/Page' return u'ns1/ns2', u'itemid', u'Page'
165 url: u'@revid/OtherPage' return u'', u'revid', u'OtherPage' 165 url: u'@revid/OtherPage' return u'', u'revid', u'OtherPage'
166 url: u'ns1/Page' return u'ns1', u'', u'Page' 166 url: u'ns1/Page' return u'ns1', u'', u'Page'
167 url: u'ns1/ns2/@notfield' return u'ns1/ns2', u'', u'@notfield' 167 url: u'ns1/ns2/@notfield' return u'ns1/ns2', u'', u'@notfield'
(...skipping 22 matching lines...) Expand all
190 'ns1/ns2/AnyPage' -> "Self", "ns1/ns2", "", "AnyPage" if ns1/ns2 namespace e xists OR 190 'ns1/ns2/AnyPage' -> "Self", "ns1/ns2", "", "AnyPage" if ns1/ns2 namespace e xists OR
191 "Self", "ns1", "", "ns2/AnyPage" if ns1 namespace exist s OR 191 "Self", "ns1", "", "ns2/AnyPage" if ns1 namespace exist s OR
192 "Self", "", "", "ns1/ns2/AnyPage" else. 192 "Self", "", "", "ns1/ns2/AnyPage" else.
193 'MoinMoin/ns/@Somefield/AnyPage' -> "MoinMoin", "ns", "", "@Somefield/AnyPag e" if ns namespace exists and field Somefield does not OR 193 'MoinMoin/ns/@Somefield/AnyPage' -> "MoinMoin", "ns", "", "@Somefield/AnyPag e" if ns namespace exists and field Somefield does not OR
194 "MoinMoin", "ns", "Somefield", "AnyPage" if ns namespace and field Somefield exist OR 194 "MoinMoin", "ns", "Somefield", "AnyPage" if ns namespace and field Somefield exist OR
195 "MoinMoin", "", "", "ns/@Somefield/AnyPage" else. 195 "MoinMoin", "", "", "ns/@Somefield/AnyPage" else.
196 :param wikiurl: the url to split 196 :param wikiurl: the url to split
197 :rtype: tuple 197 :rtype: tuple
198 :returns: (wikiname, namespace, field, pagename) 198 :returns: (wikiname, namespace, field, pagename)
199 """ 199 """
200 # TODO Now using slash('/') as delimiter in interwiki markup instead of ':', change the moin 1.9 importer accordingly.
201 if not isinstance(wikiurl, unicode): 200 if not isinstance(wikiurl, unicode):
202 wikiurl = wikiurl.decode('utf-8') 201 wikiurl = wikiurl.decode('utf-8')
203 # Base case: no colon in wikiurl 202 # Base case: no colon in wikiurl
204 if not '/' in wikiurl: 203 if not '/' in wikiurl:
205 return u'Self', u'', NAME_EXACT, wikiurl 204 return u'Self', u'', NAME_EXACT, wikiurl
206 wikiname = field = namespace = u'' 205 wikiname = field = namespace = u''
207 if not wikiurl.startswith('/'): 206 if not wikiurl.startswith('/'):
208 interwiki_mapping = set() 207 interwiki_mapping = set()
209 for interwiki_name in app.cfg.interwiki_map: 208 for interwiki_name in app.cfg.interwiki_map:
210 interwiki_mapping.add(interwiki_name.split('/')[0]) 209 interwiki_mapping.add(interwiki_name.split('/')[0])
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 @staticmethod 318 @staticmethod
320 def from_file(filename): 319 def from_file(filename):
321 """ 320 """
322 Load and parse a valid interwiki map file. 321 Load and parse a valid interwiki map file.
323 """ 322 """
324 filename = os.path.expanduser(filename) 323 filename = os.path.expanduser(filename)
325 with open(filename) as f: 324 with open(filename) as f:
326 parser = InterWikiMap(f.read()) 325 parser = InterWikiMap(f.read())
327 326
328 return parser 327 return parser
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b