Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: chrome/common/extensions/docs/server2/sidenav_data_source.py

Issue 22824042: Docserver: SidenavDataSource refactor, transition to DataSourceRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import copy 5 import copy
6 import json
7 import logging 6 import logging
8 7
9 class SidenavDataSource(object): 8 from data_source import DataSource
10 """This class reads in and caches a JSON file representing the side navigation 9 from third_party.json_schema_compiler.json_parse import Parse
11 menu.
12 """
13 class Factory(object):
14 def __init__(self, compiled_fs_factory, json_path):
15 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict,
16 SidenavDataSource)
17 self._json_path = json_path
18 10
19 def Create(self, path): 11 _JSON_FILE_NAME = '%s/%s_sidenav.json'
Jeffrey Yasskin 2013/08/28 19:12:37 This is only used in one place, so it might be bet
jshumway 2013/08/29 00:13:09 Done.
20 """Create a SidenavDataSource, binding it to |path|. |path| is the url
21 of the page that is being rendered. It is used to determine which item
22 in the sidenav should be highlighted.
23 """
24 return SidenavDataSource(self._cache, self._json_path, path)
25 12
26 def _AddLevels(self, items, level):
27 """Levels represent how deeply this item is nested in the sidenav. We
28 start at 2 because the top <ul> is the only level 1 element.
29 """
30 for item in items:
31 item['level'] = level
32 if 'items' in item:
33 self._AddLevels(item['items'], level + 1)
34 13
35 def _CreateSidenavDict(self, json_path, json_str): 14 def _AddLevels(items, level):
36 items = json.loads(json_str) 15 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep
Jeffrey Yasskin 2013/08/28 19:12:37 Thanks for adding comments.
37 self._AddLevels(items, 2); 16 in |items| an item is. |level| sets the starting depth.
38 return items 17 '''
18 for item in items:
19 item['level'] = level
20 if 'items' in item:
21 _AddLevels(item['items'], level + 1)
39 22
40 def __init__(self, cache, json_path, path):
41 self._cache = cache
42 self._json_path = json_path
43 self._href = '/' + path
44 23
45 def _AddSelected(self, items): 24 def _AddSelected(items, path):
46 for item in items: 25 '''Add 'selected' and 'child_selected' properties to |items| so that the
47 if item.get('href', '') == self._href: 26 sidenav can be expanded to show which menu item has been selected.
Jeffrey Yasskin 2013/08/28 19:12:37 Mention what this function returns.
jshumway 2013/08/29 00:13:09 Done.
48 item['selected'] = True 27 '''
28 for item in items:
29 if item.get('href', '') == '/' + path:
30 item['selected'] = True
31 return True
32 if 'items' in item:
33 if _AddSelected(item['items'], path):
34 item['child_selected'] = True
49 return True 35 return True
50 if 'items' in item:
51 if self._AddSelected(item['items']):
52 item['child_selected'] = True
53 return True
54 return False
55 36
56 def _QualifyHrefs(self, items): 37 return False
57 for item in items:
58 if 'items' in item:
59 self._QualifyHrefs(item['items'])
60 38
61 href = item.get('href') 39
62 if href is not None and not href.startswith(('http://', 'https://')): 40 def _QualifyHrefs(items):
63 if not href.startswith('/'): 41 '''Force hrefs in |items| to either be absolute (http://...) or qualified
64 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) 42 (begins with a slash (/)). Other hrefs emit a warning and should be updated.
65 href = '/' + href 43 '''
66 item['href'] = href 44 for item in items:
45 if 'items' in item:
46 _QualifyHrefs(item['items'])
47
48 href = item.get('href')
49 if href is not None and not href.startswith(('http://', 'https://')):
50 if not href.startswith('/'):
51 logging.warn('Paths in sidenav must be qualified. %s is not.' % href)
52 href = '/' + href
53 item['href'] = href
54
55
56 class SidenavDataSource(DataSource):
57 '''Provides templates with access to JSON files used to create the side
58 navigation bar.
59 '''
60 def __init__(self, server_instance, request):
61 self._cache = server_instance.compiled_host_fs_factory.Create(
62 self._CreateSidenavDict, SidenavDataSource)
63 self._json_path = server_instance.sidenav_json_base_path
64 self._request = request
65
66 def _CreateSidenavDict(self, _, content):
Jeffrey Yasskin 2013/08/28 19:12:37 It doesn't look like you use self, so can you make
jshumway 2013/08/29 00:13:09 Done.
67 items = Parse(content)
68 # Start at level 2, the top <ul> element is level 1.
69 _AddLevels(items, level=2)
70 _QualifyHrefs(items)
71 return items
72
73 def Cron(self):
74 for platform in ['apps', 'extensions']:
75 self._cache.GetFromFile(_JSON_FILE_NAME % (self._json_path, platform))
67 76
68 def get(self, key): 77 def get(self, key):
69 sidenav = copy.deepcopy(self._cache.GetFromFile( 78 sidenav = copy.deepcopy(self._cache.GetFromFile(
70 '%s/%s_sidenav.json' % (self._json_path, key))) 79 '%s/%s_sidenav.json' % (self._json_path, key)))
71 self._AddSelected(sidenav) 80 _AddSelected(sidenav, self._request.path)
72 self._QualifyHrefs(sidenav)
73 return sidenav 81 return sidenav
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698