Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 import re | 1 import re |
2 from jinja2 import contextfunction | 2 from jinja2 import contextfunction |
3 | 3 |
4 | 4 |
5 @contextfunction | 5 @contextfunction |
6 def get_pages_metadata(context, filters=None): | 6 def get_pages_metadata(context, filters=None): |
7 if not isinstance(filters, dict) and filters: | 7 if not isinstance(filters, dict) and filters: |
8 raise TypeError('Filters are not a dictionary') | 8 raise TypeError('Filters are not a dictionary') |
9 | 9 |
10 return_data = [] | 10 return_data = [] |
11 for page_name, _format in context['source'].list_pages(): | 11 for page_name, _format in context['source'].list_pages(): |
12 data, filename = context['source'].read_page(page_name, _format) | 12 data, filename = context['source'].read_page(page_name, _format) |
13 page_data = parse_page_metadata(data, page_name) | 13 page_data = parse_page_metadata(data, page_name) |
14 | 14 |
15 if filter_metadata(filters, page_data) is True: | 15 if filter_metadata(filters, page_data) is True: |
16 return_data.append(page_data) | 16 return_data.append(page_data) |
17 | 17 |
18 return return_data | 18 return return_data |
19 | 19 |
20 | 20 |
21 def parse_page_metadata(data, page): | 21 def parse_page_metadata(data, page): |
22 page_metadata = {'page': page} | 22 page_metadata = {'page': page} |
23 lines = data.splitlines(True) | 23 lines = data.splitlines(True) |
24 for i, line in enumerate(lines): | 24 for i, line in enumerate(lines): |
25 if not re.search(r'^\s*[\w\-]+\s*=', line): | 25 if not re.search(r'^\s*[\w\-]+\s*=', line): |
26 break | 26 break |
27 name, value = line.split('=', 1) | 27 name, value = line.split('=', 1) |
28 value = tuple(value.strip().split(',')) | 28 value = value.strip() |
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
| |
29 if value.startswith('[') and value.endswith(']'): | |
30 value = [element.strip() for element in value[1:-1].split(',')] | |
29 page_metadata[name.strip()] = value | 31 page_metadata[name.strip()] = value |
30 return page_metadata | 32 return page_metadata |
31 | 33 |
32 | 34 |
33 def filter_metadata(filters, metadata): | 35 def filter_metadata(filters, metadata): |
34 if filters is None: | 36 if filters is None: |
35 return True | 37 return True |
36 for filter_name, filter_value in filters.items(): | 38 for filter_name, filter_value in filters.items(): |
37 if filter_name not in metadata: | 39 if filter_name not in metadata: |
38 return False | 40 return False |
39 for option in filter_value.split(','): | 41 if isinstance(metadata[filter_name], list): |
40 if option not in metadata[filter_name]: | 42 if isinstance(filter_value, basestring): |
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
| |
43 filter_value = [filter_value] | |
44 for option in filter_value: | |
45 if str(option) not in metadata[filter_name]: | |
46 return False | |
47 elif filter_value != metadata[filter_name]: | |
41 return False | 48 return False |
42 return True | 49 return True |
LEFT | RIGHT |