1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# found in the LICENSE file.
4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)'''
6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Provides a Manifest Feature abstraction, similar to but more strict than the
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Feature schema (see feature_utility.py).
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Each Manifest Feature has a 'level' in addition to the keys defined in a
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Feature. 'level' can be 'required', 'only_one', 'recommended', or 'optional',
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)indicating how an app or extension should define a manifest property. If 'level'
12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)is missing, 'optional' is assumed.
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)'''
14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def ConvertDottedKeysToNested(features):
16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  '''Some Manifest Features are subordinate to others, such as app.background to
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  app. Subordinate Features can be moved inside the parent Feature under the key
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  'children'.
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Modifies |features|, a Manifest Features dictionary, by moving subordinate
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Features with names of the form 'parent.child' into the 'parent' Feature.
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Child features are renamed to the 'child' section of their previous name.
23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Applied recursively so that children can be nested arbitrarily.
25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  '''
26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def add_child(features, parent, child_name, value):
27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    value['name'] = child_name
28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if not 'children' in features[parent]:
29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      features[parent]['children'] = {}
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    features[parent]['children'][child_name] = value
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def insert_children(features):
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    for name in features.keys():
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if '.' in name:
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        value = features.pop(name)
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        parent, child_name = name.split('.', 1)
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        add_child(features, parent, child_name, value)
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    for value in features.values():
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if 'children' in value:
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        insert_children(value['children'])
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  insert_children(features)
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return features
45