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: remove per line page assignment fix argument and data access errors Created Jan. 19, 2017, 9:12 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 | « tests/expected_output/sitemap ('k') | tests/test_site/pages/sitemap.tmpl » ('j') | no next file with comments »
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,42 @@
+import re
+from jinja2 import contextfunction
+
+
+@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():
+ data, filename = context['source'].read_page(page_name, _format)
+ page_data = parse_page_metadata(data, page_name)
+
+ if filter_metadata(filters, page_data) is True:
+ return_data.append(page_data)
+
+ return return_data
+
+
+def parse_page_metadata(data, page):
+ page_metadata = {'page': page}
+ 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(','))
juliandoucette 2017/02/27 21:27:31 1. It's annoying to have single values as arrays
Vasily Kuznetsov 2017/02/28 11:24:09 Yeah, makes sense. I suppose then not every metada
Jon Sonesen 2017/02/28 11:59:18 could we not just check that the value is not more
+ page_metadata[name.strip()] = value
+ return page_metadata
+
+
+def filter_metadata(filters, metadata):
+ 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]:
Vasily Kuznetsov 2017/01/20 11:53:38 Here if a list of options was given for the same f
juliandoucette 2017/01/23 16:59:31 Good question. A list of required fields is a requ
juliandoucette 2017/01/24 01:05:47 On that topic, I might suggest accepting a functio
juliandoucette 2017/01/24 01:10:29 - Replace "field" with "value" in my response abov
Vasily Kuznetsov 2017/01/24 09:24:46 Ok, I think we're not quite on the same page here
juliandoucette 2017/01/24 19:32:55 I think I understood your question correctly. Hel
Vasily Kuznetsov 2017/01/25 11:18:19 Ok, in this case I misunderstood your previous ans
+ return False
+ return True
« no previous file with comments | « tests/expected_output/sitemap ('k') | tests/test_site/pages/sitemap.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld