summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/tools/template_writers/writers/doc_atomic_groups_writer.py
blob: 9f92347a6cbeebbed5564df79a968a171c624e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from writers import doc_writer


def GetWriter(config):
  '''Factory method for creating DocAtomicGroupsWriter objects.
  See the constructor of TemplateWriter for description of
  arguments.
  '''
  return DocAtomicGroupsWriter(['*'], config)


class DocAtomicGroupsWriter(doc_writer.DocWriter):
  '''Class for generating atomic policy group templates in HTML format.
  The intended use of the generated file is to upload it on
  https://www.chromium.org, therefore its format has some limitations:
  - No HTML and body tags.
  - Restricted set of element attributes: for example no 'class'.
  Because of the latter the output is styled using the 'style'
  attributes of HTML elements. This is supported by the dictionary
  self._STYLES[] and the method self._AddStyledElement(), they try
  to mimic the functionality of CSS classes. (But without inheritance.)

  This class is invoked by PolicyTemplateGenerator to create the HTML
  files.
  '''

  def _AddPolicyRow(self, parent, policy):
    '''Adds a row for the policy in the summary table.

    Args:
      parent: The DOM node of the summary table.
      policy: The data structure of the policy.
    '''
    tr = self._AddStyledElement(parent, 'tr', ['tr'])
    indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14)
    if policy['type'] != 'group':
      # Normal policies get two columns with name and caption.
      name_td = self._AddStyledElement(tr, 'td', ['td', 'td.left'],
                                       {'style': indent})
      policy_ref = './'
      if self.config.get('local', False):
        policy_ref = './chrome_policy_list.html'
      self.AddElement(name_td, 'a', {'href': policy_ref + '#' + policy['name']},
                      policy['name'])
      self._AddStyledElement(tr, 'td', ['td', 'td.right'], {},
                             policy['caption'])
    else:
      # Groups get one column with caption.
      name_td = self._AddStyledElement(tr, 'td', ['td', 'td.left'], {
          'style': indent,
          'colspan': '2'
      })
      self.AddElement(name_td, 'a', {'name': policy['name']}, policy['caption'])

  #
  # Implementation of abstract methods of TemplateWriter:
  #

  def WritePolicy(self, policy):
    self._AddPolicyRow(self._summary_tbody, policy)

  def BeginTemplate(self):
    self._BeginTemplate('group_intro', 'banner')

  def WriteTemplate(self, template):
    '''Writes the given template definition.

    Args:
      template: Template definition to write.

    Returns:
      Generated output for the passed template definition.
    '''
    self.messages = template['messages']
    self.Init()

    policies = self.PreprocessPolicies(
        template['policy_atomic_group_definitions'])

    self.BeginTemplate()
    for policy in policies:
      if policy['type'] != 'atomic_group':
        continue
      self.BeginPolicyGroup(policy)
      for child_policy in policy['policies']:
        # Nesting of groups is currently not supported.
        self.WritePolicy(child_policy)
      self.EndPolicyGroup()
    self.EndTemplate()

    return self.GetTemplateText()