1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from mozparsers
.shared_telemetry_utils
import ParserError
6 from perfecthash
import PerfectHash
12 from mozparsers
import parse_user_interactions
14 banner
= """/* This file is auto-generated, see gen_userinteraction_phf.py. */
18 #ifndef mozilla_TelemetryUserInteractionNameMap_h
19 #define mozilla_TelemetryUserInteractionNameMap_h
21 #include "mozilla/PerfectHash.h"
28 } // namespace mozilla
29 } // namespace Telemetry
30 #endif // mozilla_TelemetryUserInteractionNameMap_h
34 def main(output
, *filenames
):
36 Generate a Perfect Hash Table for the UserInteraction name -> UserInteraction ID lookup.
37 The table is immutable once generated and we can avoid any dynamic memory allocation.
44 user_interactions
= list(parse_user_interactions
.from_files(filenames
))
45 except ParserError
as ex
:
46 print("\nError processing UserInteractions:\n" + str(ex
) + "\n")
50 (bytearray(ui
.label
, "ascii"), idx
)
51 for (idx
, ui
) in enumerate(user_interactions
)
53 name_phf
= PerfectHash(user_interactions
, PHFSIZE
)
57 name
="UserInteractionIDByNameLookup",
58 entry_type
="uint32_t",
59 lower_entry
=lambda x
: str(x
[1]),
60 key_type
="const nsACString&",
61 key_bytes
="aKey.BeginReading()",
62 key_length
="aKey.Length()",
69 if __name__
== "__main__":
70 main(sys
.stdout
, *sys
.argv
[1:])