scripts/export-all.py (30 lines of code) (raw):

#!/usr/bin/env python # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. """Export all messages as a message provider JSM. This script will create the file `outgoing/InflightAssetsMessageProvider`, which contains all messages in this repository. This file is intended to be landed in mozilla-central at browser/components/newtab/test/ so we can be sure that they validate with the JSON schemas in mozilla-central. (See https://bugzilla.mozilla.org/show_bug.cgi?id=1775849 for the initial import.) Usage: make export """ import itertools import json from pathlib import Path FORMATS = [ "cfr", "cfr-heartbeat", "moments", "whats-new-panel", "messaging-experiments", ] JSM_CONTENTS = """\ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // This file is generated by: // https://github.com/mozilla/messaging-system-inflight-assets/tree/master/scripts/export-all.py export const InflightAssetsMessageProvider = {{ getMessages() {{ return {messages_json}; }} }}; """ def main(): all_messages = [] for format in FORMATS: path = Path("outgoing", f"{format}.json") with path.open() as f: messages = json.load(f) if messages is not None: all_messages.extend(messages) # Indent all but the first line by 4 spaces. json_lines = json.dumps(all_messages, indent=2).split("\n") messages_json = "\n".join( itertools.chain( [json_lines[0]], (f" {line}" for line in json_lines[1:]), ) ) with Path("outgoing", "InflightAssetsMessageProvider.sys.mjs").open("wb") as f: f.write(JSM_CONTENTS.format(messages_json=messages_json).encode("utf-8")) if __name__ == "__main__": main()