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

Delta Between Two Patch Sets: cms/sources.py

Issue 5217703262420992: Issue 2181 - [cms] Test server should accept default page for subdirectories as well (Closed)
Left Patch Set: Created March 19, 2015, 10:31 p.m.
Right Patch Set: Ask for forgiveness Created March 20, 2015, 3:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « cms/bin/test_server.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 # 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 codecs
19 import ConfigParser
20 import json
21 import os
19 from StringIO import StringIO 22 from StringIO import StringIO
20 from ConfigParser import SafeConfigParser 23 import subprocess
Sebastian Noack 2015/03/20 15:50:13 json, subprocess and zipfile seem to be unused. So
Wladimir Palant 2015/03/20 16:04:21 I actually verified that all of them are used. Did
Sebastian Noack 2015/03/20 16:11:36 Oh yes, I did.
24 import sys
25 import urlparse
26 import zipfile
21 27
22 class Source: 28 class Source:
23 def resolve_link(self, url, locale): 29 def resolve_link(self, url, locale):
24 parsed = urlparse.urlparse(url) 30 parsed = urlparse.urlparse(url)
25 page = parsed.path 31 page = parsed.path
26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): 32 if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
27 # Not a page link 33 # Not a page link
28 return None, None 34 return None, None
29 35
30 if page == "" and url != "": 36 if page == "" and url != "":
31 # Page-relative link 37 # Page-relative link
32 return None, None 38 return None, None
33 39
34 def has_locale(locale, page): 40 def has_locale(locale, page):
35 if config.has_option("locale_overrides", page): 41 try:
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) 42 page = config.get("locale_overrides", page)
43 except ConfigParser.Error:
44 pass
37 return self.has_locale(locale, page) 45 return self.has_locale(locale, page)
38 46
39 config = self.read_config() 47 config = self.read_config()
40 default_locale = config.get("general", "defaultlocale") 48 default_locale = config.get("general", "defaultlocale")
41 default_page = config.get("general", "defaultpage") 49 default_page = config.get("general", "defaultpage")
42 alternative_page = "/".join([page, default_page]).lstrip("/") 50 alternative_page = "/".join([page, default_page]).lstrip("/")
43 51
44 if self.has_localizable_file(default_locale, page): 52 if self.has_localizable_file(default_locale, page):
45 if not self.has_localizable_file(locale, page): 53 if not self.has_localizable_file(locale, page):
46 locale = default_locale 54 locale = default_locale
47 elif self.has_page(page): 55 elif self.has_page(page):
48 if not has_locale(locale, page): 56 if not has_locale(locale, page):
49 locale = default_locale 57 locale = default_locale
50 elif self.has_page(alternative_page): 58 elif self.has_page(alternative_page):
51 if not has_locale(locale, alternative_page): 59 if not has_locale(locale, alternative_page):
52 locale = default_locale 60 locale = default_locale
53 else: 61 else:
54 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page 62 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
55 63
56 parts = page.split("/") 64 parts = page.split("/")
57 if parts[-1] == default_page: 65 if parts[-1] == default_page:
58 page = "/".join(parts[:-1]) 66 page = "/".join(parts[:-1])
59 67
60 path = "/%s/%s" % (locale, page) 68 path = "/%s/%s" % (locale, page)
61 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) 69 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
62 70
63 def read_config(self): 71 def read_config(self):
64 configdata = self.read_file("settings.ini") 72 configdata = self.read_file("settings.ini")
65 config = SafeConfigParser() 73 config = ConfigParser.SafeConfigParser()
66 config.readfp(StringIO(configdata)) 74 config.readfp(StringIO(configdata))
67 return config 75 return config
68 76
69 # 77 #
70 # Page helpers 78 # Page helpers
71 # 79 #
72 80
73 @staticmethod 81 @staticmethod
74 def page_filename(page, format): 82 def page_filename(page, format):
75 return "pages/%s.%s" % (page, format) 83 return "pages/%s.%s" % (page, format)
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 return 273 return
266 274
267 for filename in files: 275 for filename in files:
268 path = os.path.join(dir, filename) 276 path = os.path.join(dir, filename)
269 if os.path.isfile(path): 277 if os.path.isfile(path):
270 result.append(relpath + filename) 278 result.append(relpath + filename)
271 elif os.path.isdir(path): 279 elif os.path.isdir(path):
272 do_list(path, relpath + filename + "/") 280 do_list(path, relpath + filename + "/")
273 do_list(self.get_path(subdir), "") 281 do_list(self.get_path(subdir), "")
274 return result 282 return result
LEFTRIGHT

Powered by Google App Engine
This is Rietveld