forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_utils.py
executable file
·352 lines (291 loc) · 11 KB
/
generator_utils.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env vpython
# Copyright 2020 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.
"""Contains the parsers for .tsv and .xml files, annotations.tsv and
grouping.xml respectively. Also includes methods to parse the json object
returned by the Google Doc API's .get() method.
These parsers are used to populate the duplicated Google Doc template with
several placeholders, and, to populate the traffic annotations with their
relevant attributes, e.g. description, policy, etc.
"""
from __future__ import print_function
from collections import namedtuple
from collections import OrderedDict
import xml.etree.ElementTree
import enum
import json
import csv
import sys
import io
import re
TrafficAnnotation = namedtuple(
"TrafficAnnotation",
["unique_id", "description", "trigger", "data", "settings", "policy"])
class Placeholder(str, enum.Enum):
GROUP = "group"
SENDER = "sender"
ANNOTATION = "annotation"
ANNOTATION_BOLD = "annotation_bold"
PLACEHOLDER_STYLES = {
Placeholder.GROUP: {
"bold": False,
"font": "Roboto",
"fontSize": 20,
"namedStyleType": "HEADING_1"
},
Placeholder.SENDER: {
"bold": True,
"font": "Roboto",
"fontSize": 14,
"namedStyleType": "HEADING_2"
},
Placeholder.ANNOTATION: {
"bold": False,
"font": "Roboto",
"fontSize": 9
},
Placeholder.ANNOTATION_BOLD: {
"bold": True,
"font": "Roboto",
"fontSize": 9
}
}
def utf_8_encoder(input_file):
for line in input_file:
yield line.encode("utf-8")
def load_tsv_file(file_path, verbose):
""" Loads annotations TSV file.
Args:
file_path: str
Path to the TSV file.
verbose: bool
Whether to print messages about ignored rows.
Returns:
list of list Table of loaded annotations.
"""
rows = []
with io.open(file_path, mode="r", encoding="utf-8") as csvfile:
# CSV library does not support unicode, so encoding to utf-8 and back.
reader = csv.reader(utf_8_encoder(csvfile), delimiter="\t")
for row in reader:
row = [unicode(col, "utf-8") for col in row]
# If the last column of the file_row is empty, the row belongs to a
# platform different from the one that TSV file is generated on, hence it
# should be ignored.
if row[-1]:
rows.append(row)
elif verbose:
print("Ignored from other platforms: %s" % row[0])
return rows
def map_annotations(tsv_contents):
"""Creates a mapping between the unique_id of a given annotation and its
relevant attributes, e.g. description, trigger, data, etc.
Args:
tsv_contents: List[List]
Table of loaded annotations.
Returns:
unique_id_rel_attributes_map: <Dict[str, TrafficAnnotation]>
"""
unique_id_rel_attributes_map = {}
for annotation_row in tsv_contents:
unique_id = annotation_row[0].encode("utf-8")
description = annotation_row[3].encode("utf-8")
trigger = annotation_row[4].encode("utf-8")
data = annotation_row[5].encode("utf-8")
settings = annotation_row[9].encode("utf-8")
policy = annotation_row[10].encode("utf-8")
payload = [unique_id, description, trigger, data, settings, policy]
unique_id_rel_attributes_map[unique_id] = TrafficAnnotation._make(payload)
return unique_id_rel_attributes_map
class XMLParser:
"""Parses grouping.xml with the aim of generating the placeholders list"""
def __init__(self, file_path, annotations_mapping):
"""
Args:
file_path: str
The file path to the xml to parse. Ostensibly, grouping.xml located
within traffic_annotation/summary.
annotations_mapping: Dict[str, dict]
The mapping between a given annotation's unique_id and its relevant
attributes, e.g. description, policy, data, etc.
"""
self.parsed_xml = {}
self.annotations_mapping = annotations_mapping
self.parse_xml(file_path)
def parse_xml(self, file_path):
"""Parses the grouping.xml file and populates self.parsed_xml.
self.parsed_xml: <{Group1: {sender: [traffic_annotations]}, ...}>
"""
tree = xml.etree.ElementTree.parse(file_path)
root = tree.getroot()
for group in root.iter("group"):
assert group.tag == "group"
group_name = group.attrib["name"]
# Suppress if hidden="true" in the group block. Will not include any of
# the senders and annotations in the block.
if group.attrib.get("hidden", "") == "true":
continue
self.parsed_xml[group_name] = {}
for sender in group.iter("sender"):
sender_name = sender.attrib["name"]
# Suppress if hidden="true" (or hidden is even mentioned) in the given
# annotation, don't include in traffic_annotations.
traffic_annotations = sorted([
t_annotation.attrib["unique_id"]
for t_annotation in sender.iter("traffic_annotation")
if t_annotation.attrib.get("hidden", "") != "true"
])
self.parsed_xml[group_name][sender_name] = traffic_annotations
def _sort_parsed_xml(self):
"""Sort on the group and sender keys in alphabetical order, note that
annotations are already sorted."""
self.parsed_xml = {
k: OrderedDict(sorted(v.items()))
for k, v in self.parsed_xml.items()
}
self.parsed_xml = OrderedDict(
sorted(self.parsed_xml.items(), key=lambda t: t[0]))
def _add_group_placeholder(self, name):
return {"type": Placeholder.GROUP, "name": name}
def _add_sender_placeholder(self, name):
return {"type": Placeholder.SENDER, "name": name}
def _add_annotation_placeholder(self, unique_id):
"""
Args:
unique_id: str
The annotation's unique_id.
"""
traffic_annotation = self.annotations_mapping.get(unique_id, None)
is_complete = traffic_annotation and all(traffic_annotation)
if not is_complete:
print(
"Warning: {} row is empty in annotations.tsv but is in grouping.xml".
format(unique_id))
traffic_annotation = TrafficAnnotation(unique_id, "NA", "NA", "NA", "NA",
"NA")
return {
"type": Placeholder.ANNOTATION,
"traffic_annotation": traffic_annotation
}
def build_placeholders(self):
"""
Returns:
The placeholders <list> to be added in the order of their appearance.
The annotations are the TrafficAnnotation objects with the relevant
information.
"""
self._sort_parsed_xml()
placeholders = []
for group, senders in self.parsed_xml.items():
placeholders.append(self._add_group_placeholder(group))
for sender, annotations in senders.items():
placeholders.append(self._add_sender_placeholder(sender))
for annotation in annotations:
placeholders.append(self._add_annotation_placeholder(annotation))
return placeholders
def jprint(msg):
print(json.dumps(msg, indent=4), file=sys.stderr)
def extract_body(document=None, target="body", json_file_path="template.json"):
"""Google Doc API returns a .json object. Parse this doc object to obtain its
body.
The |template.json| object of the current state of
the document can be obtained by running the update_annotations_doc.py script
using the --debug flag.
"""
if document:
doc = document
else:
try:
with open(json_file_path) as json_file:
doc = json.load(json_file)
except IOError:
print("Couldn't find the .json file.")
if target == "all":
return doc
return doc[target]
def find_first_index(doc):
"""Finds the cursor index (location) that comes right after the Introduction
section. Namely, the endIndex of the paragraph block the |target_text| belongs
to.
Returns: int
The first cursor index (loc) of the template document, right after the
Introduction section.
"""
target_text = "The policy, if one exists, to control this type of network"
padding = 1 # We pad so as to overwrite cleanly.
body = extract_body(document=doc)
contents = body["content"]
for element in contents:
if "paragraph" in element:
end_index = element["endIndex"]
lines = element["paragraph"]["elements"]
for text_run in lines:
if target_text in text_run["textRun"]["content"]:
return end_index + padding
def find_last_index(doc):
"""
Returns: int
The last cursor index (loc) of the template document.
"""
body = extract_body(document=doc)
contents = body["content"]
last_index = contents[-1]["endIndex"]
return last_index - 1
def find_chrome_browser_version(doc):
"""Finds what the current chrome browser version is in the document.
We grab the current "Chrome Browser version MAJOR.MINOR.BUILD.PATCH" from the
document's header.
Returns: str
The chrome browser version string.
"""
# Only one header.
header = extract_body(document=doc, target="headers").values()[0]
header_elements = header["content"][0]["paragraph"]["elements"]
text = header_elements[0]["textRun"]["content"]
current_version = re.search(r"([\d.]+)", text).group()
return current_version
def find_bold_ranges(doc, debug=False):
"""Finds parts to bold given the targets of "trigger", "data", etc.
Returns:
The startIndex <int> and endIndex <int> tuple pairs as a list for all
occurrences of the targets. <List[Tuple[int, int]]>
"""
bold_ranges = []
targets = ["Trigger", "Data", "Settings", "Policy"]
content = extract_body(document=doc)["content"]
for i, element in enumerate(content):
element_type = list(element.keys())[-1]
if element_type != "table":
continue
# Recall that table is 1x2 in Docs, first cell contains unique_id, second
# cell has traffic annotation relevant attributes.
# Unique id column, messy parsing through. You can inspect the json output
# with jprint() to confirm/debug if broken.
unique_id_col = element["table"]["tableRows"][0]["tableCells"][0][
"content"][0]["paragraph"]["elements"][0]
if debug:
jprint(unique_id_col)
assert "textRun" in unique_id_col, "Not the correct unique_id cell"
start_index = unique_id_col["startIndex"]
end_index = unique_id_col["endIndex"]
bold_ranges.append((start_index, end_index))
start_index, end_index = None, None # Reset
# The info column, messy parsing through. You can inspect the json output
# with jprint() to confirm/debug if broken.
info_elements = element["table"]["tableRows"][0]["tableCells"][1]["content"]
for i, info_col in enumerate(info_elements):
info_col = info_elements[i]
start_index = info_col["startIndex"]
content = info_col["paragraph"]["elements"][0]["textRun"]["content"]
# To find the end_index, run through and find something in targets.
for target in targets:
if content.find("{}:".format(target)) != -1:
# Contains the string "|target|:"
end_index = start_index + len(target) + 1
bold_ranges.append((start_index, end_index))
break
if debug:
jprint(info_col)
print("#" * 30)
return bold_ranges