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

Unified Diff: cms/sources.py

Issue 5217703262420992: Issue 2181 - [cms] Test server should accept default page for subdirectories as well (Closed)
Patch Set: Ask for forgiveness Created March 20, 2015, 3:41 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cms/bin/test_server.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/sources.py
===================================================================
--- a/cms/sources.py
+++ b/cms/sources.py
@@ -10,58 +10,72 @@
# Adblock Plus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
-import sys, os, subprocess, zipfile, json, urlparse, codecs
+import codecs
+import ConfigParser
+import json
+import os
from StringIO import StringIO
-from ConfigParser import SafeConfigParser
+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.
+import sys
+import urlparse
+import zipfile
class Source:
def resolve_link(self, url, locale):
parsed = urlparse.urlparse(url)
page = parsed.path
if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
# Not a page link
return None, None
- if parsed.path == "" and url != "":
+ if page == "" and url != "":
# Page-relative link
return None, None
+ def has_locale(locale, page):
+ try:
+ page = config.get("locale_overrides", page)
+ except ConfigParser.Error:
+ pass
+ return self.has_locale(locale, page)
+
config = self.read_config()
default_locale = config.get("general", "defaultlocale")
default_page = config.get("general", "defaultpage")
+ alternative_page = "/".join([page, default_page]).lstrip("/")
- checked_page = page
- if config.has_option("locale_overrides", page):
- checked_page = config.get("locale_overrides", page)
-
- if self.has_localizable_file(default_locale, checked_page):
- if not self.has_localizable_file(locale, checked_page):
+ if self.has_localizable_file(default_locale, page):
+ if not self.has_localizable_file(locale, page):
locale = default_locale
- elif self.has_page(checked_page):
- if not self.has_locale(locale, checked_page):
+ elif self.has_page(page):
+ if not has_locale(locale, page):
+ locale = default_locale
+ elif self.has_page(alternative_page):
+ if not has_locale(locale, alternative_page):
locale = default_locale
else:
print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
- if page == default_page:
- page = ""
+ parts = page.split("/")
+ if parts[-1] == default_page:
+ page = "/".join(parts[:-1])
path = "/%s/%s" % (locale, page)
return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
def read_config(self):
configdata = self.read_file("settings.ini")
- config = SafeConfigParser()
+ config = ConfigParser.SafeConfigParser()
config.readfp(StringIO(configdata))
return config
#
# Page helpers
#
@staticmethod
« 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