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

Delta Between Two Patch Sets: cms/converters.py

Issue 29378736: NoIssue - Improves converters.py PEP8 compliance (Closed) Base URL: https://hg.adblockplus.org/cms
Left Patch Set: Created March 8, 2017, 12:15 p.m.
Right Patch Set: Created March 9, 2017, 2:03 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 | « no previous file | tox.ini » ('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 # 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 self._attrs, [''.join(s) for s in self._fixed_strings]) 69 self._attrs, [''.join(s) for s in self._fixed_strings])
70 finally: 70 finally:
71 self._string = None 71 self._string = None
72 self._attrs = None 72 self._attrs = None
73 self._pagename = None 73 self._pagename = None
74 self._inside_fixed = False 74 self._inside_fixed = False
75 self._fixed_strings = None 75 self._fixed_strings = None
76 76
77 def handle_starttag(self, tag, attrs): 77 def handle_starttag(self, tag, attrs):
78 if self._inside_fixed: 78 if self._inside_fixed:
79 raise Exception('Unexpected HTML tag "{}" inside a fixed string' 79 raise Exception("Unexpected HTML tag '{}' inside a fixed string"
Vasily Kuznetsov 2017/03/08 17:31:44 Do you think it's ok that we're changing the quote
Jon Sonesen 2017/03/08 17:51:57 Well, the otherway causes a style warning, however
Jon Sonesen 2017/03/08 17:55:25 whoops, I was wrong plz disregard XD
Vasily Kuznetsov 2017/03/09 12:33:27 Yeah, the logic is that if the string has single q
80 'on page {}'.format(tag, self._pagename)) 80 'on page {}'.format(tag, self._pagename))
81 if tag == 'fix': 81 if tag == 'fix':
82 self._inside_fixed = True 82 self._inside_fixed = True
83 self._fixed_strings.append([]) 83 self._fixed_strings.append([])
84 if tag in self._whitelist: 84 if tag in self._whitelist:
85 self._attrs.setdefault(tag, []).append(attrs) 85 self._attrs.setdefault(tag, []).append(attrs)
86 self._string.append('<{}>'.format(tag)) 86 self._string.append('<{}>'.format(tag))
87 else: 87 else:
88 raise Exception('Unexpected HTML tag "{}" inside a fixed string' 88 raise Exception("Unexpected HTML tag '{}' inside a fixed string"
89 'on page {}'.format(tag, self._pagename)) 89 'on page {}'.format(tag, self._pagename))
90 90
91 def handle_endtag(self, tag): 91 def handle_endtag(self, tag):
92 if tag == 'fix': 92 if tag == 'fix':
93 self._string.append('{{{}}}'.format(self._fixed_strings)) 93 self._string.append('{{{}}}'.format(self._fixed_strings))
94 self._inside_fixed = False 94 self._inside_fixed = False
95 else: 95 else:
96 self._string.append('</{}>'.format(tag)) 96 self._string.append('</{}>'.format(tag))
97 97
98 def _append_text(self, s): 98 def _append_text(self, s):
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 break 133 break
134 name, value = line.split('=', 1) 134 name, value = line.split('=', 1)
135 params[name.strip()] = value.strip() 135 params[name.strip()] = value.strip()
136 lines[i] = '\n' 136 lines[i] = '\n'
137 params[key] = (''.join(lines), filename) 137 params[key] = (''.join(lines), filename)
138 138
139 def localize_string( 139 def localize_string(
140 self, page, name, default, comment, localedata, escapes): 140 self, page, name, default, comment, localedata, escapes):
141 141
142 def escape(s): 142 def escape(s):
143 return re.sub(r'.', 143 return re.sub(r'.',
Vasily Kuznetsov 2017/03/08 17:31:44 This is also kind of hard to read but more importa
Jon Sonesen 2017/03/08 17:51:57 I agree here, but assumed it would require an issu
Vasily Kuznetsov 2017/03/09 12:33:27 Yeah, you're right.
144 lambda match: escapes.get(match.group(0), 144 lambda match: escapes.get(match.group(0),
145 match.group(0)), 145 match.group(0)),
146 s, flags=re.S) 146 s, flags=re.S)
147 147
148 def re_escape(s): 148 def re_escape(s):
149 return re.escape(escape(s)) 149 return re.escape(escape(s))
150 150
151 # Handle duplicated strings 151 # Handle duplicated strings
152 if default: 152 if default:
153 self._seen_defaults[(page, name)] = (default, comment) 153 self._seen_defaults[(page, name)] = (default, comment)
154 else: 154 else:
155 try: 155 try:
156 default, comment = self._seen_defaults[(page, name)] 156 default, comment = self._seen_defaults[(page, name)]
157 except KeyError: 157 except KeyError:
158 raise Exception('Text not yet defined for string {} on page' 158 raise Exception('Text not yet defined for string {} on page'
159 '{}'.format(name, page)) 159 '{}'.format(name, page))
160 160
161 # Extract tag attributes from default string 161 # Extract tag attributes from default string
162 default, saved_attributes, fixed_strings = (self._attribute_parser 162 default, saved_attributes, fixed_strings = (
Vasily Kuznetsov 2017/03/08 17:31:44 This looks kind of hard to read. How about this in
Jon Sonesen 2017/03/08 17:51:57 Yes that seems nicer, thank you!
163 .parse(default, 163 self._attribute_parser.parse(default, self._params['page']))
164 self._params['page']
165 ))
166 164
167 # Get translation 165 # Get translation
168 locale = self._params['locale'] 166 locale = self._params['locale']
169 if locale == self._params['defaultlocale']: 167 if locale == self._params['defaultlocale']:
170 result = default 168 result = default
171 elif name in localedata: 169 elif name in localedata:
172 result = localedata[name].strip() 170 result = localedata[name].strip()
173 else: 171 else:
174 result = default 172 result = default
175 self.missing_translations += 1 173 self.missing_translations += 1
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 lookup_string, 243 lookup_string,
246 text, 244 text,
247 flags=re.S 245 flags=re.S
248 ) 246 )
249 247
250 def process_links(self, text): 248 def process_links(self, text):
251 def process_link(match): 249 def process_link(match):
252 pre, attr, url, post = match.groups() 250 pre, attr, url, post = match.groups()
253 url = jinja2.Markup(url).unescape() 251 url = jinja2.Markup(url).unescape()
254 252
255 locale, new_url = self._params['source']\ 253 locale, new_url = (
Vasily Kuznetsov 2017/03/08 17:31:44 PEP8 discourages line continuation with backslashe
Jon Sonesen 2017/03/08 17:51:57 oh yeah, I though i replaced all the back slashes
256 .resolve_link(url, self._params['locale']) 254 self._params['source']
255 .resolve_link(url, self._params['locale']))
256
257 if new_url is not None: 257 if new_url is not None:
258 url = new_url 258 url = new_url
259 if attr == 'href': 259 if attr == 'href':
260 post += ' hreflang="{}"'\ 260 post += ' hreflang="{}"'\
Vasily Kuznetsov 2017/03/08 17:31:44 Maybe we can use continuation with parentheses ins
Jon Sonesen 2017/03/08 17:51:57 yesh
261 .format(jinja2.Markup.escape(locale)) 261 .format(jinja2.Markup.escape(locale))
262 262
263 return ''.join((pre, jinja2.Markup.escape(url), post)) 263 return ''.join((pre, jinja2.Markup.escape(url), post))
264 264
265 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', 265 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")',
266 process_link, text) 266 process_link, text)
267 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', 267 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")',
268 process_link, text) 268 process_link, text)
269 return text 269 return text
270 270
271 include_start_regex = '<' 271 include_start_regex = '<'
272 include_end_regex = '>' 272 include_end_regex = '>'
273 273
274 def resolve_includes(self, text): 274 def resolve_includes(self, text):
275 def resolve_include(match): 275 def resolve_include(match):
276 name = match.group(1) 276 name = match.group(1)
277 for format_, converter_class in converters.iteritems(): 277 for format_, converter_class in converters.iteritems():
278 if self._params['source'].has_include(name, format_): 278 if self._params['source'].has_include(name, format_):
279 self._params['includedata'] = self._params['source']\ 279 self._params['includedata'] = (
280 .read_include(name, format) 280 self._params['source'].read_include(name, format))
281
281 converter = converter_class(self._params, 282 converter = converter_class(self._params,
282 key='includedata') 283 key='includedata')
283 result = converter() 284 result = converter()
284 self.missing_translations += converter.missing_translations 285 self.missing_translations += converter.missing_translations
285 self.total_translations += converter.total_translations 286 self.total_translations += converter.total_translations
286 return result 287 return result
287 raise Exception('Failed to resolve include {}' 288 raise Exception('Failed to resolve include {}'
288 'on page {}'.format(name, self._params['page'])) 289 'on page {}'.format(name, self._params['page']))
289 290
290 return re.sub( 291 return re.sub(
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 stack.pop() 477 stack.pop()
477 stack[-1]['subitems'].append(item) 478 stack[-1]['subitems'].append(item)
478 stack.append(item) 479 stack.append(item)
479 return structured 480 return structured
480 481
481 converters = { 482 converters = {
482 'html': RawConverter, 483 'html': RawConverter,
483 'md': MarkdownConverter, 484 'md': MarkdownConverter,
484 'tmpl': TemplateConverter, 485 'tmpl': TemplateConverter,
485 } 486 }
LEFTRIGHT
« no previous file | tox.ini » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld