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

Side by Side Diff: tests/test_site/globals/get_pages_metadata.py

Issue 29370597: Issue 4687 - Add Context Function get_pages_metadata to Test Site (Closed)
Patch Set: Created Jan. 2, 2017, 9:30 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 | « no previous file | tests/test_site/pages/sitemap.tmpl » ('j') | tests/test_site/pages/sitemap.tmpl » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 from jinja2 import contextfunction
2 import re
3
4
5 @contextfunction
6 def get_pages_metadata(context, filters=None):
7 if not isinstance(filters, dict) and filters:
8 raise TypeError('Filters are not a dictionary')
9
10 return_data = []
11 for page_name, _format in context['source'].list_pages():
12 path = context['source'].page_filename(page_name, _format)
Vasily Kuznetsov 2017/01/11 10:52:44 You can replace this and the following line with:
Jon Sonesen 2017/01/11 17:37:04 Done.
13 data, filename = context['source'].read_file(path)
14 page_data = parse_page_metadata(data, filename)
15
16 if filter_metadata(filters, page_data, page_name) is True:
17 return_data.append(page_data)
18
19 return return_data
20
21
22 def parse_page_metadata(data, page):
Vasily Kuznetsov 2017/01/11 10:52:44 The return value of this function doesn't contain
Jon Sonesen 2017/01/11 17:37:04 Done.
23 page_metadata = {}
Vasily Kuznetsov 2017/01/10 19:34:25 Seems like one level of indentation was lost here
Jon Sonesen 2017/01/11 17:37:04 Done.
24 lines = data.splitlines(True)
25 for i, line in enumerate(lines):
26 if not re.search(r'^\s*[\w\-]+\s*=', line):
27 break
28 name, value = line.split('=', 1)
29 value = tuple(value.strip().split(','))
30 page_metadata[name.strip()] = value
31 return page_metadata
32
33
34 def filter_metadata(filters, metadata, page):
35 if filters is None:
36 return True
37 for filter_name, filter_value in filters.items():
38 if filter_name not in metadata:
39 return False
40 for option in filter_value.split(','):
41 if option not in metadata[filter_name]:
42 return False
43 return True
OLDNEW
« no previous file with comments | « no previous file | tests/test_site/pages/sitemap.tmpl » ('j') | tests/test_site/pages/sitemap.tmpl » ('J')

Powered by Google App Engine
This is Rietveld