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

Side by Side Diff: sitescripts/web/sources.py

Issue 17817001: Simple CMS as Anwiki replacement (Closed)
Patch Set: Various improvements Created Oct. 24, 2013, 11:42 a.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 | « sitescripts/web/converters.py ('k') | sitescripts/web/utils.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2013 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import sys, os, subprocess, zipfile, json, urlparse, codecs
19 from StringIO import StringIO
20 from ConfigParser import SafeConfigParser
21
22 class Source:
23 def resolve_link(self, url, locale):
24 parsed = urlparse.urlparse(url)
25 page = parsed.path
26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
27 # Not a page link
28 return None, None
29
30 config = self.read_config()
31 default_locale = config.get("general", "defaultlocale")
32 default_page = config.get("general", "defaultpage")
33
34 if self.has_localizable_file(default_locale, page):
35 if not self.has_localizable_file(locale, page):
36 locale = default_locale
37 elif self.has_locale(default_locale, page):
38 if not self.has_locale(locale, page):
39 locale = default_locale
40 else:
41 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
42
43 if page == default_page:
44 page = ""
45
46 path = "/%s/%s" % (locale, page)
47 return locale, urlparse.urlunparse(list(parsed[0:2]) + [path] + list(parsed[ 3:]))
48
49 def read_config(self):
50 configdata = self.read_file("settings.ini")
51 config = SafeConfigParser()
52 config.readfp(StringIO(configdata))
53 return config
54
55 #
56 # Page helpers
57 #
58
59 @staticmethod
60 def page_filename(page, format):
61 return "pages/%s.%s" % (page, format)
62
63 def list_pages(self):
64 for filename in self.list_files("pages"):
65 root, ext = os.path.splitext(filename)
66 format = ext[1:]
67 yield root, format
68
69 def has_page(self, page, format):
70 return self.has_file(self.page_filename(page, format))
71
72 def read_page(self, page, format):
73 return self.read_file(self.page_filename(page, format))
74
75 #
76 # Localizable files helpers
77 #
78
79 @staticmethod
80 def localizable_file_filename(locale, filename):
81 return "locales/%s/%s" % (locale, filename)
82
83 def list_localizable_files(self):
84 default_locale = self.read_config().get("general", "defaultlocale")
85 return filter(
86 lambda f: os.path.splitext(f)[1] != ".json",
87 self.list_files("locales/%s" % default_locale)
88 )
89
90 def has_localizable_file(self, locale, filename):
91 return self.has_file(self.localizable_file_filename(locale, filename))
92
93 def read_localizable_file(self, locale, filename):
94 return self.read_file(self.localizable_file_filename(locale, filename), bina ry=True)
95
96 #
97 # Static file helpers
98 #
99
100 @staticmethod
101 def static_filename(filename):
102 return "static/%s" % filename
103
104 def list_static(self):
105 return self.list_files("static")
106
107 def has_static(self, filename):
108 return self.has_file(self.static_filename(filename))
109
110 def read_static(self, filename):
111 return self.read_file(self.static_filename(filename), binary=True)
112
113 #
114 # Locale helpers
115 #
116
117 @staticmethod
118 def locale_filename(locale, page):
119 return "locales/%s/%s.json" % (locale, page)
120
121 def list_locales(self):
122 result = set()
123 for filename in self.list_files("locales"):
124 if "/" in filename:
125 locale, path = filename.split("/", 1)
126 result.add(locale)
127 return result
128
129 def has_locale(self, locale, page):
130 return self.has_file(self.locale_filename(locale, page))
131
132 def read_locale(self, locale, page):
133 default_locale = self.read_config().get("general", "defaultlocale")
134 if locale == default_locale:
135 result = {}
136 else:
137 result = self.read_locale(default_locale, page)
138
139 if self.has_locale(locale, page):
140 filedata = self.read_file(self.locale_filename(locale, page))
141 localedata = json.loads(filedata)
142 for key, value in localedata.iteritems():
143 result[key] = value["message"]
144
145 return result
146
147 #
148 # Template helpers
149 #
150
151 @staticmethod
152 def template_filename(template):
153 return "templates/%s.tmpl" % template
154
155 def read_template(self, template):
156 return self.read_file(self.template_filename(template))
157
158 class MercurialSource(Source):
159 def __init__(self, repo):
160 command = ["hg", "-R", repo, "archive", "-r", "default",
161 "-t", "uzip", "-p", ".", "-"]
162 data, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()
163 self._archive = zipfile.ZipFile(StringIO(data), mode="r")
164
165 command = ["hg", "-R", repo, "id", "-n", "-r", "default"]
166 self.version, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communic ate()
167 self.version = self.version.strip()
168
169 def __enter__(self):
170 return self
171
172 def __exit__(self, type, value, traceback):
173 self.close()
174 return False
175
176 def close(self):
177 self._archive.close()
178
179 def has_file(self, filename):
180 try:
181 self._archive.getinfo("./%s" % filename)
182 except KeyError:
183 return False
184 return True
185
186 def read_file(self, filename, binary=False):
187 result = self._archive.read("./%s" % filename)
188 if not binary:
189 result = result.decode("utf-8")
190 return result
191
192 def list_files(self, subdir):
193 prefix = "./" + subdir + "/"
194 for filename in self._archive.namelist():
195 if filename.startswith(prefix):
196 yield filename[len(prefix):]
197
198 class FileSource(Source):
199 def __init__(self, dir):
200 self._dir = dir
201
202 def __enter__(self):
203 return self
204
205 def __exit__(self, type, value, traceback):
206 return False
207
208 def close(self):
209 pass
210
211 def get_path(self, filename):
212 return os.path.join(self._dir, *filename.split("/"))
213
214 def has_file(self, filename):
215 return os.path.isfile(self.get_path(filename))
216
217 def read_file(self, filename, binary=False):
218 encoding = None if binary else "utf-8"
219 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle :
220 return handle.read()
221
222 def list_files(self, subdir):
223 result = []
224 def do_list(dir, relpath):
225 files = os.listdir(dir)
226 for filename in files:
227 path = os.path.join(dir, filename)
228 if os.path.isfile(path):
229 result.append(relpath + filename)
230 elif os.path.isdir(path):
231 do_list(path, relpath + filename + "/")
232 do_list(self.get_path(subdir), "")
233 return result
OLDNEW
« no previous file with comments | « sitescripts/web/converters.py ('k') | sitescripts/web/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld