OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 (handle, path) = mkstemp(prefix='mail_', suffix='.eml', dir='.') | 117 (handle, path) = mkstemp(prefix='mail_', suffix='.eml', dir='.') |
118 os.close(handle) | 118 os.close(handle) |
119 f = codecs.open(path, 'wb', encoding='utf-8') | 119 f = codecs.open(path, 'wb', encoding='utf-8') |
120 print >>f, mail | 120 print >>f, mail |
121 f.close() | 121 f.close() |
122 else: | 122 else: |
123 subprocess.Popen([config.get('DEFAULT', 'mailer'), '-t'], stdin=subproce
ss.PIPE).communicate(mail.encode('utf-8')) | 123 subprocess.Popen([config.get('DEFAULT', 'mailer'), '-t'], stdin=subproce
ss.PIPE).communicate(mail.encode('utf-8')) |
124 | 124 |
125 | 125 |
126 def encode_email_address(email): | 126 def encode_email_address(email): |
127 ''' | 127 """ |
128 Validates and encodes an email address. | 128 Validates and encodes an email address. |
129 | 129 |
130 The validation implemented here is very rudamentery and not meant | 130 The validation implemented here is very rudamentery and not meant |
131 to be complete, as full email validation can get extremly complicated | 131 to be complete, as full email validation can get extremly complicated |
132 and is rarely needed. This function is primarily making sure that the | 132 and is rarely needed. This function is primarily making sure that the |
133 email address contains no whitespaces and only valid ASCII characters. | 133 email address contains no whitespaces and only valid ASCII characters. |
134 ''' | 134 """ |
135 match = re.search(r'^([^@\s]+)@([^@\s]+)$', email) | 135 match = re.search(r'^([^@\s]+)@([^@\s]+)$', email) |
136 if not match: | 136 if not match: |
137 raise ValueError | 137 raise ValueError |
138 | 138 |
139 try: | 139 try: |
140 return email.encode('ascii') | 140 return email.encode('ascii') |
141 except UnicodeEncodeError: | 141 except UnicodeEncodeError: |
142 return '%s@%s' % (match.group(1).encode('ascii'), | 142 return '%s@%s' % (match.group(1).encode('ascii'), |
143 match.group(2).encode('idna')) | 143 match.group(2).encode('idna')) |
144 | 144 |
145 _template_cache = {} | 145 _template_cache = {} |
146 | 146 |
147 | 147 |
148 def get_template(template, autoescape=True, template_path=siteScriptsPath): | 148 def get_template(template, autoescape=True, template_path=siteScriptsPath): |
149 """Parses and returns a Jinja2 template""" | 149 """Parses and returns a Jinja2 template""" |
150 key = (template_path, template, autoescape) | 150 key = (template_path, template, autoescape) |
151 if not key in _template_cache: | 151 if not key in _template_cache: |
152 if autoescape: | 152 if autoescape: |
153 env = get_template_environment(template_path) | 153 env = get_template_environment(template_path) |
154 else: | 154 else: |
155 env = get_unescaped_template_environment(template_path) | 155 env = get_unescaped_template_environment(template_path) |
156 _template_cache[key] = env.get_template(template) | 156 _template_cache[key] = env.get_template(template) |
157 return _template_cache[key] | 157 return _template_cache[key] |
158 | 158 |
159 | 159 |
160 @cached(float("inf")) | 160 @cached(float('inf')) |
161 def get_template_environment(template_path): | 161 def get_template_environment(template_path): |
162 """ | 162 """ |
163 Returns a Jinja2 template environment with autoescaping enabled. | 163 Returns a Jinja2 template environment with autoescaping enabled. |
164 """ | 164 """ |
165 from sitescripts.templateFilters import filters | 165 from sitescripts.templateFilters import filters |
166 import jinja2 | 166 import jinja2 |
167 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path), | 167 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path), |
168 autoescape=True) | 168 autoescape=True) |
169 env.filters.update(filters) | 169 env.filters.update(filters) |
170 return env | 170 return env |
171 | 171 |
172 | 172 |
173 @cached(float("inf")) | 173 @cached(float('inf')) |
174 def get_unescaped_template_environment(template_path): | 174 def get_unescaped_template_environment(template_path): |
175 """ | 175 """ |
176 Returns a Jinja2 template environment without autoescaping. Don't use this
to | 176 Returns a Jinja2 template environment without autoescaping. Don't use this
to |
177 generate HTML files! | 177 generate HTML files! |
178 """ | 178 """ |
179 from sitescripts.templateFilters import filters | 179 from sitescripts.templateFilters import filters |
180 import jinja2 | 180 import jinja2 |
181 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) | 181 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) |
182 env.filters.update(filters) | 182 env.filters.update(filters) |
183 return env | 183 return env |
184 | 184 |
185 | 185 |
186 def get_custom_template_environment(additional_filters, loader=None): | 186 def get_custom_template_environment(additional_filters, loader=None): |
187 """ | 187 """ |
188 Returns a custom Jinja2 template environment with additional filters. | 188 Returns a custom Jinja2 template environment with additional filters. |
189 """ | 189 """ |
190 from sitescripts.templateFilters import filters | 190 from sitescripts.templateFilters import filters |
191 import jinja2 | 191 import jinja2 |
192 if not loader: | 192 if not loader: |
193 loader = jinja2.FileSystemLoader(siteScriptsPath) | 193 loader = jinja2.FileSystemLoader(siteScriptsPath) |
194 env = jinja2.Environment(loader=loader, autoescape=True) | 194 env = jinja2.Environment(loader=loader, autoescape=True) |
195 env.filters.update(filters) | 195 env.filters.update(filters) |
196 env.filters.update(additional_filters) | 196 env.filters.update(additional_filters) |
197 return env | 197 return env |
OLD | NEW |