From e97c39c58759d8ea325c03634f41b8c8418c9d23 Mon Sep 17 00:00:00 2001 From: Puneeth Chaganti Date: Mon, 20 Apr 2020 18:57:36 +0530 Subject: [PATCH] tools: Extract png generation code to separate module. --- tools/generate-integration-docs-screenshot | 2 +- .../generate_integration_bots_avatars.py | 22 +++++++++++++++++++ .../setup/generate_zulip_bots_static_files.py | 13 +---------- 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 tools/setup/generate_integration_bots_avatars.py diff --git a/tools/generate-integration-docs-screenshot b/tools/generate-integration-docs-screenshot index da19db1361..b2cd665aa9 100755 --- a/tools/generate-integration-docs-screenshot +++ b/tools/generate-integration-docs-screenshot @@ -35,7 +35,7 @@ from zerver.lib.upload import upload_avatar_image from zerver.lib.actions import do_change_avatar_fields from zerver.lib.integrations import WebhookIntegration, INTEGRATIONS, split_fixture_path from zerver.lib.webhooks.common import get_fixture_http_headers -from setup.generate_zulip_bots_static_files import create_png_from_svg +from setup.generate_integration_bots_avatars import create_png_from_svg from tools.lib.test_script import prepare_puppeteer_run def create_integration_bot(integration: WebhookIntegration) -> UserProfile: diff --git a/tools/setup/generate_integration_bots_avatars.py b/tools/setup/generate_integration_bots_avatars.py new file mode 100644 index 0000000000..c31a3903cd --- /dev/null +++ b/tools/setup/generate_integration_bots_avatars.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import os +import sys +import tempfile +from typing import Optional + +ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +if ZULIP_PATH not in sys.path: + sys.path.append(ZULIP_PATH) +from scripts.lib.setup_path import setup_path +setup_path() + +import cairosvg + +def create_png_from_svg(svg_path: str, destination_dir: Optional[str]=None) -> str: + png_name = os.path.splitext(os.path.basename(svg_path))[0] + '.png' + if destination_dir is None: + destination_dir = tempfile.gettempdir() + png_path = os.path.join(destination_dir, png_name) + cairosvg.svg2png(url=svg_path, write_to=png_path) + return png_path diff --git a/tools/setup/generate_zulip_bots_static_files.py b/tools/setup/generate_zulip_bots_static_files.py index 39b294c517..83c22fbd7b 100755 --- a/tools/setup/generate_zulip_bots_static_files.py +++ b/tools/setup/generate_zulip_bots_static_files.py @@ -4,8 +4,7 @@ import glob import os import sys import shutil -import tempfile -from typing import List, Optional +from typing import List ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) if ZULIP_PATH not in sys.path: @@ -44,15 +43,5 @@ def generate_zulip_bots_static_files() -> None: docs = glob.glob(doc_glob_pattern) copyfiles(docs) -def create_png_from_svg(svg_path: str, destination_dir: Optional[str]=None) -> str: - import cairosvg - - png_name = os.path.splitext(os.path.basename(svg_path))[0] + '.png' - if destination_dir is None: - destination_dir = tempfile.gettempdir() - png_path = os.path.join(destination_dir, png_name) - cairosvg.svg2png(url=svg_path, write_to=png_path) - return png_path - if __name__ == "__main__": generate_zulip_bots_static_files()