Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 | 61 |
62 path = "/%s/%s" % (locale, page) | 62 path = "/%s/%s" % (locale, page) |
63 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) | 63 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) |
64 | 64 |
65 def read_config(self): | 65 def read_config(self): |
66 configdata = self.read_file("settings.ini")[0] | 66 configdata = self.read_file("settings.ini")[0] |
67 config = ConfigParser.SafeConfigParser() | 67 config = ConfigParser.SafeConfigParser() |
68 config.readfp(StringIO(configdata)) | 68 config.readfp(StringIO(configdata)) |
69 return config | 69 return config |
70 | 70 |
71 def exec_file(self, filename): | |
72 source, filename = self.read_file(filename) | |
73 code = compile(source, filename, "exec") | |
74 namespace = {} | |
75 exec code in namespace | |
76 return namespace | |
77 | |
71 # | 78 # |
72 # Page helpers | 79 # Page helpers |
73 # | 80 # |
74 | 81 |
75 @staticmethod | 82 @staticmethod |
76 def page_filename(page, format): | 83 def page_filename(page, format): |
77 return "pages/%s.%s" % (page, format) | 84 return "pages/%s.%s" % (page, format) |
78 | 85 |
79 def list_pages(self): | 86 def list_pages(self): |
80 for filename in self.list_files("pages"): | 87 for filename in self.list_files("pages"): |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 try: | 229 try: |
223 self._archive.getinfo("./%s" % filename) | 230 self._archive.getinfo("./%s" % filename) |
224 except KeyError: | 231 except KeyError: |
225 return False | 232 return False |
226 return True | 233 return True |
227 | 234 |
228 def read_file(self, filename, binary=False): | 235 def read_file(self, filename, binary=False): |
229 data = self._archive.read("./%s" % filename) | 236 data = self._archive.read("./%s" % filename) |
230 if not binary: | 237 if not binary: |
231 data = data.decode("utf-8") | 238 data = data.decode("utf-8") |
232 return (data, None) | 239 return (data, "%s!%s" % (self._name, filename)) |
Wladimir Palant
2015/09/16 17:35:53
How about returning "%s!%s" % (self._name, filenam
Sebastian Noack
2015/09/16 19:04:04
Not sure if I like the notation with the exclamati
Wladimir Palant
2015/09/16 21:08:59
For reference, this notation is inspired by the JA
| |
233 | |
234 def exec_file(self, filename): | |
235 code = self.read_file(filename)[0] | |
236 namespace = {} | |
237 exec code in namespace | |
238 return namespace | |
Wladimir Palant
2015/09/16 17:35:53
I looked into this and this change appears to be u
Sebastian Noack
2015/09/16 19:04:04
Nice catch. Done.
| |
239 | 240 |
240 def list_files(self, subdir): | 241 def list_files(self, subdir): |
241 prefix = "./%s/" % subdir | 242 prefix = "./%s/" % subdir |
242 for filename in self._archive.namelist(): | 243 for filename in self._archive.namelist(): |
243 if filename.startswith(prefix): | 244 if filename.startswith(prefix): |
244 yield filename[len(prefix):] | 245 yield filename[len(prefix):] |
245 | 246 |
246 if os.name == "posix": | 247 if os.name == "posix": |
247 def get_cache_dir(self): | 248 def get_cache_dir(self): |
248 return "/var/cache/" + self._name | 249 return "/var/cache/" + self._name |
(...skipping 20 matching lines...) Expand all Loading... | |
269 def read_file(self, filename, binary=False): | 270 def read_file(self, filename, binary=False): |
270 path = self.get_path(filename) | 271 path = self.get_path(filename) |
271 | 272 |
272 if binary: | 273 if binary: |
273 file = open(path, "rb") | 274 file = open(path, "rb") |
274 else: | 275 else: |
275 file = io.open(path, "r", encoding="utf-8") | 276 file = io.open(path, "r", encoding="utf-8") |
276 | 277 |
277 with file: | 278 with file: |
278 return (file.read(), path) | 279 return (file.read(), path) |
279 | |
280 def exec_file(self, filename): | |
281 namespace = {} | |
282 execfile(self.get_path(filename), namespace) | |
283 return namespace | |
284 | 280 |
285 def list_files(self, subdir): | 281 def list_files(self, subdir): |
286 result = [] | 282 result = [] |
287 def do_list(dir, relpath): | 283 def do_list(dir, relpath): |
288 try: | 284 try: |
289 files = os.listdir(dir) | 285 files = os.listdir(dir) |
290 except OSError: | 286 except OSError: |
291 return | 287 return |
292 | 288 |
293 for filename in files: | 289 for filename in files: |
294 path = os.path.join(dir, filename) | 290 path = os.path.join(dir, filename) |
295 if os.path.isfile(path): | 291 if os.path.isfile(path): |
296 result.append(relpath + filename) | 292 result.append(relpath + filename) |
297 elif os.path.isdir(path): | 293 elif os.path.isdir(path): |
298 do_list(path, relpath + filename + "/") | 294 do_list(path, relpath + filename + "/") |
299 do_list(self.get_path(subdir), "") | 295 do_list(self.get_path(subdir), "") |
300 return result | 296 return result |
301 | 297 |
302 def get_cache_dir(self): | 298 def get_cache_dir(self): |
303 return os.path.join(self._dir, "cache") | 299 return os.path.join(self._dir, "cache") |
LEFT | RIGHT |