Chromium Code Reviews| Index: tools/metrics/histograms/update_use_counter_css.py |
| diff --git a/tools/metrics/histograms/update_use_counter_css.py b/tools/metrics/histograms/update_use_counter_css.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d4e5beaed251fdc2f57802893b6cc3e1a8b3cb99 |
| --- /dev/null |
| +++ b/tools/metrics/histograms/update_use_counter_css.py |
| @@ -0,0 +1,66 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2014 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. |
| + |
| +"""Scans the Chromium source of UseCounter, formats the Feature enum for |
| +histograms.xml and merges it. This script can also generate a python code |
| +snippet to put in uma.py of Chromium Dashboard. Make sure that you review the |
| +output for correctness. |
| +""" |
| + |
| +import optparse |
| +import os |
| +import re |
| +import sys |
| + |
| +sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
| +from update_histogram_enum import ReadHistogramValues |
| +from update_histogram_enum import UpdateHistogramFromDict |
| +from update_use_counter_feature_enum import print_enum_for_dashboard |
| + |
| + |
| +USE_COUNTER_CPP_PATH = \ |
| + '../../../third_party/WebKit/Source/core/frame/UseCounter.cpp' |
| + |
| + |
| +def enum_to_css_property(enum_name): |
| + """Converts a camel cased enum name to the lower case CSS property.""" |
| + return re.sub(r'([a-zA-Z])([A-Z])', r'\1-\2', enum_name).lower() |
|
Ilya Sherman
2014/05/13 03:11:33
Hmm, why does the first captured group have upperc
Mike Lawther (Google)
2014/05/20 07:45:03
Good question! This was to deal with the property
Ilya Sherman
2014/05/20 14:13:17
Hmm, how odd. Could you please add a comment expl
Mike Lawther (Google)
2014/05/21 07:30:41
Done.
|
| + |
| + |
| +def read_css_properties(filename): |
|
Ilya Sherman
2014/05/13 03:11:33
nit: I think function names are supposed to be wri
Mike Lawther (Google)
2014/05/20 07:45:03
Oh OK. I'd seen others in lower case (eg in update
|
| + # Read the file as a list of lines |
| + with open(filename) as f: |
| + content = f.readlines() |
| + |
| + # Looking for a line like "case CSSPropertyGrid: return 453;" |
| + bucket_finder = re.compile(r'.*CSSProperty(.*):\s*return\s*([0-9]+).*') |
|
Ilya Sherman
2014/05/13 03:11:33
nit: Could you write this as a multi-line regex, s
Ilya Sherman
2014/05/13 03:11:33
nit: Do you really need the leading and trailing .
Ilya Sherman
2014/05/13 03:11:33
Optional nit: I'd name this something like PROPERT
Mike Lawther (Google)
2014/05/20 07:45:03
Did you mean break the regex string itself out and
Mike Lawther (Google)
2014/05/20 07:45:03
As in use re.MULTILINE so I can use ^ and $? Now t
Mike Lawther (Google)
2014/05/20 07:45:03
No. Removed. I had them there because I was using
Ilya Sherman
2014/05/20 14:13:17
I meant the compiled regex.
Ilya Sherman
2014/05/20 14:13:17
I meant using verbose mode: https://docs.python.or
Mike Lawther (Google)
2014/05/21 07:30:41
Done. I called it 'ENUM_REGEX', as that's what it'
Mike Lawther (Google)
2014/05/21 07:30:41
Gotcha. Done.
|
| + |
| + bucket_dict = {} |
|
Ilya Sherman
2014/05/13 03:11:33
Optional nit: I'd name this "properties"
Mike Lawther (Google)
2014/05/20 07:45:03
Agreed. I also changed some of the other names in
Ilya Sherman
2014/05/20 14:13:17
LG, thanks :)
|
| + for line in content: |
| + bucket_match = bucket_finder.match(line) |
| + if bucket_match: |
| + enum_name = bucket_match.group(1) |
|
Ilya Sherman
2014/05/13 03:11:33
Optional nit: I'd name this "property"
Mike Lawther (Google)
2014/05/20 07:45:03
I've left this as 'enum_name', because later down
Ilya Sherman
2014/05/20 14:13:17
Ok.
|
| + bucket = int(bucket_match.group(2)) |
| + bucket_dict[bucket] = enum_to_css_property(enum_name) |
| + |
| + return bucket_dict |
| + |
| + |
| +if __name__ == '__main__': |
| + parser = optparse.OptionParser() |
| + parser.add_option('--for-dashboard', action='store_true', dest='dashboard', |
| + default=False, |
| + help='Print enum definition formatted for use in uma.py of ' |
| + 'Chromium dashboard developed at ' |
| + 'https://github.com/GoogleChrome/chromium-dashboard') |
| + options, args = parser.parse_args() |
| + |
| + if options.dashboard: |
| + enum_dict = read_css_properties(USE_COUNTER_CPP_PATH) |
| + print_enum_for_dashboard(enum_dict) |
| + else: |
| + UpdateHistogramFromDict( |
| + 'MappedCSSProperties', read_css_properties(USE_COUNTER_CPP_PATH), |
| + USE_COUNTER_CPP_PATH) |