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

Side by Side Diff: cms/utils.py

Issue 29590620: Noissue - Refactor Converter.__init__ and __call__ (Closed)
Patch Set: Created Oct. 27, 2017, 5:54 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
OLDNEW
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-present eyeo GmbH 2 # Copyright (C) 2006-present 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
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from cms.converters import converters, TemplateConverter 16 import re
17
18 __all__ = [
19 'get_page_params',
20 'process_page',
21 'split_head_body',
22 'extract_page_metadata'
23 ]
24
25
26 def split_head_body(html):
Vasily Kuznetsov 2017/10/27 18:35:01 Maybe this function should be called `decapitate`.
mathias 2017/10/30 15:37:00 Funny, but no. The name is perfect, there's nothin
27 """Split HTML page into head and remaining content.
28
29 This is used to pass head and body of the page to the template as two
30 separate variables.
31
32 Parameters
33 ----------
34 html: str
35 Source HTML to split.
36
37 Returns
38 -------
39 (str, str)
40 Everything inside of <head> tags found in the page,
41 the rest of the page text with <head> tags removed.
42
43 """
44 head = []
45
46 def add_to_head(match):
47 head.append(match.group(1))
48 return ''
49
50 body = re.sub(r'<head>(.*?)</head>', add_to_head, html, flags=re.S)
51 return ''.join(head), body
52
53
54 def extract_page_metadata(source):
55 """Extract metadata variables from source text of the page.
56
57 Parameters
58 ----------
59 source: str
60 Source text of the page.
61
62 Returns
63 -------
64 (dict, str)
65 Metadata of the page, remaining source text without metadata.
66
67 """
68 metadata = {}
69 lines = source.splitlines(True)
70 for i, line in enumerate(lines):
71 if line.strip() in {'<!--', '-->'}:
72 lines[i] = ''
73 continue
74 if not re.search(r'^\s*[\w\-]+\s*=', line):
75 break
76 name, value = line.split('=', 1)
77 value = value.strip()
78 if value.startswith('[') and value.endswith(']'):
79 value = [element.strip() for element in value[1:-1].split(',')]
80 lines[i] = '\n'
81 metadata[name.strip()] = value
82 return metadata, ''.join(lines)
17 83
18 84
19 def get_page_params(source, locale, page, format=None, site_url_override=None, 85 def get_page_params(source, locale, page, format=None, site_url_override=None,
20 localized_string_callback=None): 86 localized_string_callback=None):
87 from cms.converters import converters
Vasily Kuznetsov 2017/10/27 18:35:02 It makes more sense to do contrived imports on thi
mathias 2017/10/30 15:37:00 Acknowledged.
88
21 # Guess page format if omitted, but default to Markdown for friendlier excep tions 89 # Guess page format if omitted, but default to Markdown for friendlier excep tions
22 if format is None: 90 if format is None:
23 for format in converters.iterkeys(): 91 for format in converters.iterkeys():
24 if source.has_page(page, format): 92 if source.has_page(page, format):
25 break 93 break
26 else: 94 else:
27 format = 'md' 95 format = 'md'
28 96
29 params = { 97 params = {
30 'source': source, 98 'source': source,
31 'template': 'default', 99 'template': 'default',
32 'locale': locale, 100 'locale': locale,
33 'page': page, 101 'page': page,
34 'pagedata': source.read_page(page, format),
35 'config': source.read_config(), 102 'config': source.read_config(),
36 'localized_string_callback': localized_string_callback, 103 'localized_string_callback': localized_string_callback,
37 } 104 }
38 105
39 localefile = page 106 localefile = page
40 if params['config'].has_option('locale_overrides', page): 107 if params['config'].has_option('locale_overrides', page):
41 localefile = params['config'].get('locale_overrides', page) 108 localefile = params['config'].get('locale_overrides', page)
42 params['localedata'] = source.read_locale(params['locale'], localefile) 109 params['localedata'] = source.read_locale(params['locale'], localefile)
43 110
44 if params['config'].has_option('general', 'siteurl'): 111 if params['config'].has_option('general', 'siteurl'):
45 if site_url_override: 112 if site_url_override:
46 params['site_url'] = site_url_override 113 params['site_url'] = site_url_override
47 else: 114 else:
48 params['site_url'] = params['config'].get('general', 'siteurl') 115 params['site_url'] = params['config'].get('general', 'siteurl')
49 116
50 try: 117 data, filename = source.read_page(page, format)
Vasily Kuznetsov 2017/10/27 18:35:01 This was done in the constructor of `Converter` be
mathias 2017/10/30 15:37:00 Acknowledged.
51 converter_class = converters[format] 118 metadata, body = extract_page_metadata(data)
52 except KeyError: 119 params['pagedata'] = body, filename
53 raise Exception('Page %s uses unknown format %s' % (page, format)) 120 params.update(metadata)
54 121
55 converter = converter_class(params)
56
57 # Note: The converter might change some parameters so we can only read in
58 # template data here.
59 params['templatedata'] = source.read_template(params['template']) 122 params['templatedata'] = source.read_template(params['template'])
60 123
61 defaultlocale = params['config'].get('general', 'defaultlocale') 124 defaultlocale = params['config'].get('general', 'defaultlocale')
62 params['defaultlocale'] = defaultlocale 125 params['defaultlocale'] = defaultlocale
63 126
64 locales = [ 127 locales = [
65 l 128 l
66 for l in source.list_locales() 129 for l in source.list_locales()
67 if source.has_locale(l, localefile) 130 if source.has_locale(l, localefile)
68 ] 131 ]
69 if defaultlocale not in locales: 132 if defaultlocale not in locales:
70 locales.append(defaultlocale) 133 locales.append(defaultlocale)
71 locales.sort() 134 locales.sort()
72 params['available_locales'] = locales 135 params['available_locales'] = locales
73 136
74 params['head'], params['body'] = converter() 137 try:
Vasily Kuznetsov 2017/10/27 18:35:02 Lines 137-143 should probably be in `converters.py
mathias 2017/10/30 15:37:00 Acknowledged.
138 converter_class = converters[format]
139 converter = converter_class(body, filename, params)
mathias 2017/10/30 15:37:00 This line should not be part of this try-block any
Vasily Kuznetsov 2017/11/07 17:08:29 Done.
140 except KeyError:
141 raise Exception('Page %s uses unknown format %s' % (page, format))
142
143 converted = converter()
144 params['head'], params['body'] = split_head_body(converted)
145
75 if converter.total_translations > 0: 146 if converter.total_translations > 0:
76 params['translation_ratio'] = ( 147 params['translation_ratio'] = (
77 1 - float(converter.missing_translations) / converter.total_translat ions 148 1 - float(converter.missing_translations) / converter.total_translat ions
78 ) 149 )
79 else: 150 else:
80 params['translation_ratio'] = 1 151 params['translation_ratio'] = 1
81 152
82 return params 153 return params
83 154
84 155
85 def process_page(source, locale, page, format=None, site_url_override=None, 156 def process_page(source, locale, page, format=None, site_url_override=None,
86 localized_string_callback=None): 157 localized_string_callback=None):
87 return TemplateConverter( 158 from cms.converters import TemplateConverter
88 get_page_params(source, locale, page, format, 159
89 site_url_override, localized_string_callback), 160 params = get_page_params(source, locale, page, format, site_url_override,
90 key='templatedata' 161 localized_string_callback)
91 )() 162 return TemplateConverter(*params['templatedata'], params=params)()
OLDNEW

Powered by Google App Engine
This is Rietveld