2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Embeds standalone JavaScript snippets in C++ code.
8 The script requires the devtools/front_end/toolbox/OverridesUI.js file from
9 WebKit that lists the preset network conditions to be passed in as the only
10 argument. The list of network conditions will be written to a C-style string to
11 be parsed with JSONReader.
22 UNLIMITED_THROUGHPUT
= ('WebInspector.OverridesSupport'
23 '.NetworkThroughputUnlimitedValue')
26 def quotizeKeys(s
, keys
):
27 """Returns the string s with each instance of each key wrapped in quotes.
30 s: a string containing keys that need to be wrapped in quotes.
31 keys: an iterable of keys to be wrapped in quotes in the string.
34 s
= re
.sub('%s: ' % key
, '"%s": ' % key
, s
)
37 def evaluateMultiplications(s
):
38 """Returns the string s with each bare multiplication evaluated.
40 Since the source is JavaScript, which includes bare arithmetic, and the
41 output must be JSON format, we must evaluate all expressions.
44 s: a string containing bare multiplications that need to be evaluated.
46 def evaluateBinaryMultiplication(match
):
47 return str(float(match
.group(1)) * float(match
.group(2)))
49 return re
.sub('([0-9\.]+) \* ([0-9\.]+)', evaluateBinaryMultiplication
, s
)
53 parser
= optparse
.OptionParser()
55 '', '--directory', type='string', default
='.',
56 help='Path to directory where the cc/h files should be created')
57 options
, args
= parser
.parse_args()
62 with
open(file_name
, 'r') as f
:
65 if 'WebInspector.OverridesUI._networkConditionsPresets = [' in line
:
68 if line
.strip() == '];':
71 line
= line
.replace(UNLIMITED_THROUGHPUT
, "-1")
72 networks
+= line
.strip()
74 output_dir
= 'chrome/test/chromedriver/chrome'
76 networks
= quotizeKeys(networks
, ['id', 'title', 'throughput', 'latency'])
77 networks
= evaluateMultiplications(networks
)
78 cpp_source
.WriteSource('network_list',
80 options
.directory
, {'kNetworks': networks
})
82 clang_format
= ['clang-format', '-i']
83 subprocess
.Popen(clang_format
+ ['%s/network_list.cc' % output_dir
])
84 subprocess
.Popen(clang_format
+ ['%s/network_list.h' % output_dir
])
87 if __name__
== '__main__':