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: Created Oct. 23, 2013, 1:52 p.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
19 from StringIO import StringIO
20 from ConfigParser import SafeConfigParser
21
22 default_locale = "en"
23
24 class Source:
25 def resolve_link(self, url, locale):
26 parsed = urlparse.urlparse(url)
27 page = parsed.path
28 if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
29 # Not a page link
30 return None, None
31
32 if self.has_localizable_file(default_locale, page):
33 if not self.has_localizable_file(locale, page):
34 locale = default_locale
35 elif self.has_locale(default_locale, page):
36 if not self.has_locale(locale, page):
37 locale = default_locale
38 else:
39 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page
40
41 if page == "index":
42 page = ""
43
44 path = "/%s/%s" % (locale, page)
45 return locale, urlparse.urlunparse(list(parsed[0:2]) + [path] + list(parsed[ 3:]))
46
47 def read_config(self):
48 configdata = self.read_file("settings.ini").decode("utf-8")
49 config = SafeConfigParser()
50 config.readfp(StringIO(configdata))
51 return config
52
53 #
54 # Page helpers
55 #
56
57 @staticmethod
58 def page_filename(page, format):
59 return "pages/%s.%s" % (page, format)
60
61 def list_pages(self):
62 for filename in self.list_files("pages"):
63 root, ext = os.path.splitext(filename)
64 format = ext[1:]
65 yield root, format
66
67 def has_page(self, page, format):
68 return self.has_file(self.page_filename(page, format))
69
70 def read_page(self, page, format):
71 return self.read_file(self.page_filename(page, format))
72
73 #
74 # Localizable files helpers
75 #
76
77 @staticmethod
78 def localizable_file_filename(locale, filename):
79 return "locales/%s/%s" % (locale, filename)
80
81 def list_localizable_files(self):
82 return filter(
83 lambda f: os.path.splitext(f)[1] != ".json",
84 self.list_files("locales/%s" % default_locale)
85 )
86
87 def has_localizable_file(self, locale, filename):
88 return self.has_file(self.localizable_file_filename(locale, filename))
89
90 def read_localizable_file(self, locale, filename):
91 return self.read_file(self.localizable_file_filename(locale, filename))
92
93 #
94 # Static file helpers
95 #
96
97 @staticmethod
98 def static_filename(filename):
99 return "static/%s" % filename
100
101 def list_static(self):
102 return self.list_files("static")
103
104 def has_static(self, filename):
105 return self.has_file(self.static_filename(filename))
106
107 def read_static(self, filename):
108 return self.read_file(self.static_filename(filename))
109
110 #
111 # Locale helpers
112 #
113
114 @staticmethod
115 def locale_filename(locale, page):
116 return "locales/%s/%s.json" % (locale, page)
117
118 def list_locales(self):
119 result = set()
120 for filename in self.list_files("locales"):
121 if "/" in filename:
122 locale, path = filename.split("/", 1)
123 result.add(locale)
124 return result
125
126 def has_locale(self, locale, page):
127 return self.has_file(self.locale_filename(locale, page))
128
129 def read_locale(self, locale, page):
130 if locale == default_locale:
131 result = {}
132 else:
133 result = self.read_locale(default_locale, page)
134
135 if self.has_locale(locale, page):
136 filedata = self.read_file(self.locale_filename(locale, page))
137 localedata = json.loads(filedata.decode("utf-8"))
138 for key, value in localedata.iteritems():
139 result[key] = value["message"]
140
141 return result
142
143 #
144 # Template helpers
145 #
146
147 @staticmethod
148 def template_filename(template):
149 return "templates/%s.tmpl" % template
150
151 def read_template(self, template):
152 return self.read_file(self.template_filename(template))
153
154 class MercurialSource(Source):
155 def __init__(self, repo):
156 command = ["hg", "-R", repo, "archive", "-t", "uzip", "-p", ".", "-"]
157 data, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()
158 self._archive = zipfile.ZipFile(StringIO(data), mode="r")
159
160 def __enter__(self):
161 return self
162
163 def __exit__(self, type, value, traceback):
164 self.close()
165 return False
166
167 def close(self):
168 self._archive.close()
169
170 def has_file(self, filename):
171 try:
172 self._archive.getinfo("./%s" % filename)
173 except KeyError:
174 return False
175 return True
176
177 def read_file(self, filename):
178 return self._archive.read("./%s" % filename)
179
180 def list_files(self, subdir):
181 prefix = "./" + subdir + "/"
182 for filename in self._archive.namelist():
183 if filename.startswith(prefix):
184 yield filename[len(prefix):]
185
186 class FileSource(Source):
187 def __init__(self, dir):
188 self._dir = dir
189
190 def __enter__(self):
191 return self
192
193 def __exit__(self, type, value, traceback):
194 return False
195
196 def close(self):
197 pass
198
199 def get_path(self, filename):
200 return os.path.join(self._dir, *filename.split("/"))
201
202 def has_file(self, filename):
203 return os.path.isfile(self.get_path(filename))
204
205 def read_file(self, filename):
206 with open(self.get_path(filename), "rb") as handle:
207 return handle.read()
208
209 def list_files(self, subdir):
210 result = []
211 def do_list(dir, relpath):
212 files = os.listdir(dir)
213 for filename in files:
214 path = os.path.join(dir, filename)
215 if os.path.isfile(path):
216 result.append(relpath + filename)
217 elif os.path.isdir(path):
218 do_list(path, relpath + filename + "/")
219 do_list(self.get_path(subdir), "")
220 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