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

Delta Between Two Patch Sets: sitescripts/web/sources.py

Issue 17817001: Simple CMS as Anwiki replacement (Closed)
Left Patch Set: Completed functionality Created Oct. 24, 2013, 9:32 p.m.
Right Patch Set: Fixed MIME type Created Nov. 4, 2013, 4:11 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 | « sitescripts/web/converters.py ('k') | sitescripts/web/utils.py » ('j') | 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-2013 Eyeo GmbH 4 # Copyright (C) 2006-2013 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,
(...skipping 30 matching lines...) Expand all
41 elif self.has_locale(default_locale, checked_page): 41 elif self.has_locale(default_locale, checked_page):
42 if not self.has_locale(locale, checked_page): 42 if not self.has_locale(locale, checked_page):
43 locale = default_locale 43 locale = default_locale
44 else: 44 else:
45 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page 45 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
46 46
47 if page == default_page: 47 if page == default_page:
48 page = "" 48 page = ""
49 49
50 path = "/%s/%s" % (locale, page) 50 path = "/%s/%s" % (locale, page)
51 return locale, urlparse.urlunparse(list(parsed[0:2]) + [path] + list(parsed[ 3:])) 51 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
Sebastian Noack 2013/10/29 11:04:17 If you just wrap path in to a tuple instead of a l
52 52
53 def read_config(self): 53 def read_config(self):
54 configdata = self.read_file("settings.ini") 54 configdata = self.read_file("settings.ini")
55 config = SafeConfigParser() 55 config = SafeConfigParser()
56 config.readfp(StringIO(configdata)) 56 config.readfp(StringIO(configdata))
57 return config 57 return config
58 58
59 # 59 #
60 # Page helpers 60 # Page helpers
61 # 61 #
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 def has_static(self, filename): 111 def has_static(self, filename):
112 return self.has_file(self.static_filename(filename)) 112 return self.has_file(self.static_filename(filename))
113 113
114 def read_static(self, filename): 114 def read_static(self, filename):
115 return self.read_file(self.static_filename(filename), binary=True) 115 return self.read_file(self.static_filename(filename), binary=True)
116 116
117 # 117 #
118 # Locale helpers 118 # Locale helpers
119 # 119 #
120 120
121 @staticmethod 121 @classmethod
122 def locale_filename(locale, page): 122 def locale_filename(cls, locale, page):
123 return "locales/%s/%s.json" % (locale, page) 123 return cls.localizable_file_filename(locale, page + ".json")
Sebastian Noack 2013/10/29 11:04:17 Why don't you call localizable_file_filename() and
124 124
125 def list_locales(self): 125 def list_locales(self):
126 result = set() 126 result = set()
127 for filename in self.list_files("locales"): 127 for filename in self.list_files("locales"):
128 if "/" in filename: 128 if "/" in filename:
129 locale, path = filename.split("/", 1) 129 locale, path = filename.split("/", 1)
130 result.add(locale) 130 result.add(locale)
131 return result 131 return result
132 132
133 def has_locale(self, locale, page): 133 def has_locale(self, locale, page):
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 def has_include(self, include, format): 173 def has_include(self, include, format):
174 return self.has_file(self.include_filename(include, format)) 174 return self.has_file(self.include_filename(include, format))
175 175
176 def read_include(self, include, format): 176 def read_include(self, include, format):
177 return self.read_file(self.include_filename(include, format)) 177 return self.read_file(self.include_filename(include, format))
178 178
179 class MercurialSource(Source): 179 class MercurialSource(Source):
180 def __init__(self, repo): 180 def __init__(self, repo):
181 command = ["hg", "-R", repo, "archive", "-r", "default", 181 command = ["hg", "-R", repo, "archive", "-r", "default",
182 "-t", "uzip", "-p", ".", "-"] 182 "-t", "uzip", "-p", ".", "-"]
183 data, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communicate() 183 data = subprocess.check_output(command)
Sebastian Noack 2013/10/29 11:04:17 Have a look at subprocess.check_output(), which is
184 self._archive = zipfile.ZipFile(StringIO(data), mode="r") 184 self._archive = zipfile.ZipFile(StringIO(data), mode="r")
185 185
186 command = ["hg", "-R", repo, "id", "-n", "-r", "default"] 186 command = ["hg", "-R", repo, "id", "-n", "-r", "default"]
187 self.version, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communic ate() 187 self.version = subprocess.check_output(command).strip()
188 self.version = self.version.strip()
189 188
190 def __enter__(self): 189 def __enter__(self):
191 return self 190 return self
192 191
193 def __exit__(self, type, value, traceback): 192 def __exit__(self, type, value, traceback):
194 self.close() 193 self.close()
195 return False 194 return False
196 195
197 def close(self): 196 def close(self):
198 self._archive.close() 197 self._archive.close()
199 198
200 def has_file(self, filename): 199 def has_file(self, filename):
201 try: 200 try:
202 self._archive.getinfo("./%s" % filename) 201 self._archive.getinfo("./%s" % filename)
203 except KeyError: 202 except KeyError:
204 return False 203 return False
205 return True 204 return True
206 205
207 def read_file(self, filename, binary=False): 206 def read_file(self, filename, binary=False):
208 result = self._archive.read("./%s" % filename) 207 result = self._archive.read("./%s" % filename)
209 if not binary: 208 if not binary:
210 result = result.decode("utf-8") 209 result = result.decode("utf-8")
211 return result 210 return result
212 211
213 def list_files(self, subdir): 212 def list_files(self, subdir):
214 prefix = "./" + subdir + "/" 213 prefix = "./%s/" % subdir
Sebastian Noack 2013/10/29 11:04:17 You should use format strings when concatenating m
215 for filename in self._archive.namelist(): 214 for filename in self._archive.namelist():
216 if filename.startswith(prefix): 215 if filename.startswith(prefix):
217 yield filename[len(prefix):] 216 yield filename[len(prefix):]
218 217
219 class FileSource(Source): 218 class FileSource(Source):
220 def __init__(self, dir): 219 def __init__(self, dir):
221 self._dir = dir 220 self._dir = dir
222 221
223 def __enter__(self): 222 def __enter__(self):
224 return self 223 return self
(...skipping 20 matching lines...) Expand all
245 def do_list(dir, relpath): 244 def do_list(dir, relpath):
246 files = os.listdir(dir) 245 files = os.listdir(dir)
247 for filename in files: 246 for filename in files:
248 path = os.path.join(dir, filename) 247 path = os.path.join(dir, filename)
249 if os.path.isfile(path): 248 if os.path.isfile(path):
250 result.append(relpath + filename) 249 result.append(relpath + filename)
251 elif os.path.isdir(path): 250 elif os.path.isdir(path):
252 do_list(path, relpath + filename + "/") 251 do_list(path, relpath + filename + "/")
253 do_list(self.get_path(subdir), "") 252 do_list(self.get_path(subdir), "")
254 return result 253 return result
LEFTRIGHT

Powered by Google App Engine
This is Rietveld