Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / whats_new_data_source.py
blob5ca6fd5e7f81756f6245e1b5e9e652f966f01cbb
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 from itertools import groupby
5 from operator import itemgetter
7 from data_source import DataSource
8 from extensions_paths import JSON_TEMPLATES
9 from extensions_paths import PUBLIC_TEMPLATES
10 from future import Gettable, Future
13 class WhatsNewDataSource(DataSource):
14 ''' This class creates a list of "what is new" by chrome version.
15 '''
17 def __init__(self, server_instance, _):
18 self._parse_cache = server_instance.compiled_fs_factory.ForJson(
19 server_instance.host_file_system_provider.GetTrunk())
20 self._object_store = server_instance.object_store_creator.Create(
21 WhatsNewDataSource)
22 self._api_models = server_instance.api_models
23 self._availability_finder = server_instance.availability_finder
24 self._api_categorizer = server_instance.api_categorizer
26 def _GenerateChangesListWithVersion(self, platform, whats_new_json):
27 return [{
28 'id': change_id,
29 'type': change['type'],
30 'description': change['description'],
31 'version': change['version']
32 } for change_id, change in whats_new_json.iteritems()]
34 def _GetApiVersion(self, platform, api_name):
35 version = None
36 category = self._api_categorizer.GetCategory(platform, api_name)
37 if category == 'chrome':
38 channel_info = self._availability_finder.GetApiAvailability(api_name)
39 channel = channel_info.channel
40 if channel == 'stable':
41 version = channel_info.version
42 return version
44 def _GenerateApiListWithVersion(self, platform):
45 data = []
46 for api_name, api_model in self._api_models.IterModels():
47 version = self._GetApiVersion(platform, api_name)
48 if version:
49 api = {
50 'name': api_name,
51 'description': api_model.description,
52 'version' : version,
53 'type': 'apis',
55 data.append(api)
56 data.sort(key=itemgetter('version'))
57 return data
59 def _GenerateWhatsNewDict(self):
60 whats_new_json_future = self._parse_cache.GetFromFile(
61 '%s/whats_new.json' % JSON_TEMPLATES)
62 def _MakeDictByPlatform(platform):
63 whats_new_json = whats_new_json_future.Get()
64 platform_list = []
65 apis = self._GenerateApiListWithVersion(platform)
66 apis.extend(self._GenerateChangesListWithVersion(platform,
67 whats_new_json))
68 apis.sort(key=itemgetter('version'), reverse=True)
69 for version, group in groupby(apis, key=itemgetter('version')):
70 whats_new_by_version = {
71 'version': version,
73 for item in group:
74 item_type = item['type']
75 if item_type not in whats_new_by_version:
76 whats_new_by_version[item_type] = []
77 whats_new_by_version[item_type].append(item)
78 platform_list.append(whats_new_by_version)
79 return platform_list
81 def resolve():
82 return {
83 'apps': _MakeDictByPlatform('apps'),
84 'extensions': _MakeDictByPlatform('extensions')
86 return Future(delegate=Gettable(resolve))
88 def _GetCachedWhatsNewData(self):
89 data = self._object_store.Get('whats_new_data').Get()
90 if data is None:
91 data = self._GenerateWhatsNewDict().Get()
92 self._object_store.Set('whats_new_data', data)
93 return data
95 def get(self, key):
96 return self._GetCachedWhatsNewData().get(key)
98 def Cron(self):
99 return self._GenerateWhatsNewDict()