Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/sources.py

Issue 5217703262420992: Issue 2181 - [cms] Test server should accept default page for subdirectories as well (Closed)
Patch Set: Created March 19, 2015, 10:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cms/bin/test_server.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import sys, os, subprocess, zipfile, json, urlparse, codecs 18 import sys, os, subprocess, zipfile, json, urlparse, codecs
19 from StringIO import StringIO 19 from StringIO import StringIO
20 from ConfigParser import SafeConfigParser 20 from ConfigParser import SafeConfigParser
21 21
22 class Source: 22 class Source:
23 def resolve_link(self, url, locale): 23 def resolve_link(self, url, locale):
24 parsed = urlparse.urlparse(url) 24 parsed = urlparse.urlparse(url)
25 page = parsed.path 25 page = parsed.path
26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): 26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
27 # Not a page link 27 # Not a page link
28 return None, None 28 return None, None
29 29
30 if parsed.path == "" and url != "": 30 if page == "" and url != "":
31 # Page-relative link 31 # Page-relative link
32 return None, None 32 return None, None
33 33
34 def has_locale(locale, page):
35 if config.has_option("locale_overrides", page):
Sebastian Noack 2015/03/20 09:47:41 I'd prefer to call config.get() without prior chec
Wladimir Palant 2015/03/20 15:42:36 Done.
36 page = config.get("locale_overrides", page)
37 return self.has_locale(locale, page)
38
34 config = self.read_config() 39 config = self.read_config()
35 default_locale = config.get("general", "defaultlocale") 40 default_locale = config.get("general", "defaultlocale")
36 default_page = config.get("general", "defaultpage") 41 default_page = config.get("general", "defaultpage")
42 alternative_page = "/".join([page, default_page]).lstrip("/")
37 43
38 checked_page = page 44 if self.has_localizable_file(default_locale, page):
39 if config.has_option("locale_overrides", page): 45 if not self.has_localizable_file(locale, page):
40 checked_page = config.get("locale_overrides", page)
Wladimir Palant 2015/03/19 22:33:53 Sorry about the marginally related changes but thi
41
42 if self.has_localizable_file(default_locale, checked_page):
43 if not self.has_localizable_file(locale, checked_page):
44 locale = default_locale 46 locale = default_locale
45 elif self.has_page(checked_page): 47 elif self.has_page(page):
46 if not self.has_locale(locale, checked_page): 48 if not has_locale(locale, page):
49 locale = default_locale
50 elif self.has_page(alternative_page):
51 if not has_locale(locale, alternative_page):
47 locale = default_locale 52 locale = default_locale
48 else: 53 else:
49 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page 54 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
50 55
51 if page == default_page: 56 parts = page.split("/")
52 page = "" 57 if parts[-1] == default_page:
58 page = "/".join(parts[:-1])
53 59
54 path = "/%s/%s" % (locale, page) 60 path = "/%s/%s" % (locale, page)
55 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) 61 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
56 62
57 def read_config(self): 63 def read_config(self):
58 configdata = self.read_file("settings.ini") 64 configdata = self.read_file("settings.ini")
59 config = SafeConfigParser() 65 config = SafeConfigParser()
60 config.readfp(StringIO(configdata)) 66 config.readfp(StringIO(configdata))
61 return config 67 return config
62 68
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 return 265 return
260 266
261 for filename in files: 267 for filename in files:
262 path = os.path.join(dir, filename) 268 path = os.path.join(dir, filename)
263 if os.path.isfile(path): 269 if os.path.isfile(path):
264 result.append(relpath + filename) 270 result.append(relpath + filename)
265 elif os.path.isdir(path): 271 elif os.path.isdir(path):
266 do_list(path, relpath + filename + "/") 272 do_list(path, relpath + filename + "/")
267 do_list(self.get_path(subdir), "") 273 do_list(self.get_path(subdir), "")
268 return result 274 return result
OLDNEW
« no previous file with comments | « cms/bin/test_server.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld