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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/test_site/pages/sitemap.tmpl » ('j') | tests/test_site/pages/sitemap.tmpl » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/test_site/globals/get_pages_metadata.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/tests/test_site/globals/get_pages_metadata.py
@@ -0,0 +1,43 @@
+from jinja2 import contextfunction
+import re
+
+
+@contextfunction
+def get_pages_metadata(context, filters=None):
+ if not isinstance(filters, dict) and filters:
+ raise TypeError('Filters are not a dictionary')
+
+ return_data = []
+ for page_name, _format in context['source'].list_pages():
+ 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.
+ data, filename = context['source'].read_file(path)
+ page_data = parse_page_metadata(data, filename)
+
+ if filter_metadata(filters, page_data, page_name) is True:
+ return_data.append(page_data)
+
+ return return_data
+
+
+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.
+ 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.
+ lines = data.splitlines(True)
+ for i, line in enumerate(lines):
+ if not re.search(r'^\s*[\w\-]+\s*=', line):
+ break
+ name, value = line.split('=', 1)
+ value = tuple(value.strip().split(','))
+ page_metadata[name.strip()] = value
+ return page_metadata
+
+
+def filter_metadata(filters, metadata, page):
+ if filters is None:
+ return True
+ for filter_name, filter_value in filters.items():
+ if filter_name not in metadata:
+ return False
+ for option in filter_value.split(','):
+ if option not in metadata[filter_name]:
+ return False
+ return True
« 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