From 4ee3b18533653fec2e1c4c8ec466e72429850ce3 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sun, 24 Mar 2019 01:30:33 +0300 Subject: [PATCH] better tools --- tools/common.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ tools/make-html-h.py | 12 +++++----- tools/make-jpeg-h.py | 19 ++++++++-------- tools/prepend.h | 23 ------------------- 4 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 tools/common.py delete mode 100644 tools/prepend.h diff --git a/tools/common.py b/tools/common.py new file mode 100644 index 0000000..971609e --- /dev/null +++ b/tools/common.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +#============================================================================# +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#============================================================================# + + +import textwrap + + +# ===== +def get_prepend() -> str: + return textwrap.dedent(""" + /***************************************************************************** + # # + # uStreamer - Lightweight and fast MJPG-HTTP streamer. # + # # + # Copyright (C) 2018 Maxim Devaev # + # # + # This program is free software: you can redistribute it and/or modify # + # it under the terms of the GNU General Public License as published by # + # the Free Software Foundation, either version 3 of the License, or # + # (at your option) any later version. # + # # + # This program is distributed in the hope that it will be useful, # + # but WITHOUT ANY WARRANTY; without even the implied warranty of # + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # + # GNU General Public License for more details. # + # # + # You should have received a copy of the GNU General Public License # + # along with this program. If not, see . # + # # + *****************************************************************************/ + + + #pragma once + """).strip() + "\n" diff --git a/tools/make-html-h.py b/tools/make-html-h.py index 8263565..9bb4210 100755 --- a/tools/make-html-h.py +++ b/tools/make-html-h.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S python3 -B #============================================================================# # # # uStreamer - Lightweight and fast MJPG-HTTP streamer. # @@ -22,12 +22,13 @@ import sys -import os import textwrap +import common + # ===== -def main(): +def main() -> None: assert len(sys.argv) == 4, "%s " % (sys.argv[0]) html_path = sys.argv[1] header_path = sys.argv[2] @@ -36,9 +37,6 @@ def main(): with open(html_path, "r") as html_file: text = html_file.read() - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "prepend.h")) as prepend_file: - prepend = prepend_file.read() - text = text.strip() text = text.replace("\"", "\\\"") text = text.replace("%VERSION%", "\" VERSION \"") @@ -48,7 +46,7 @@ def main(): for line in text.split("\n") ) text = "const char HTML_%s_PAGE[] = \" \\\n%s\n\";\n" % (name, text) - text = prepend + "\n#include \"../../config.h\"\n\n\n" + text + text = common.get_prepend() + "\n#include \"../../config.h\"\n\n\n" + text with open(header_path, "w") as header_file: header_file.write(text) diff --git a/tools/make-jpeg-h.py b/tools/make-jpeg-h.py index e6faf25..efed1b2 100755 --- a/tools/make-jpeg-h.py +++ b/tools/make-jpeg-h.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env -S python3 -B #============================================================================# # # # uStreamer - Lightweight and fast MJPG-HTTP streamer. # @@ -22,13 +22,17 @@ import sys -import os import io import struct +from typing import Tuple +from typing import List + +import common + # ===== -def _get_jpeg_size(data): +def _get_jpeg_size(data: bytes) -> Tuple[int, int]: # https://sheep.horse/2013/9/finding_the_dimensions_of_a_jpeg_file_in_python.html stream = io.BytesIO(data) @@ -55,7 +59,7 @@ def _get_jpeg_size(data): # ===== -def main(): +def main() -> None: assert len(sys.argv) == 4, "%s " % (sys.argv[0]) jpeg_path = sys.argv[1] header_path = sys.argv[2] @@ -66,10 +70,7 @@ def main(): (width, height) = _get_jpeg_size(jpeg_data) - with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "prepend.h")) as prepend_file: - prepend = prepend_file.read() - - rows = [[]] + rows: List[List[str]] = [[]] for ch in jpeg_data: if len(rows[-1]) > 20: rows.append([]) @@ -79,7 +80,7 @@ def main(): text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (name, text) text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (name, height) + text text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (name, width) + text - text = prepend + "\n\n" + text + text = common.get_prepend() + "\n\n" + text with open(header_path, "w") as header_file: header_file.write(text) diff --git a/tools/prepend.h b/tools/prepend.h deleted file mode 100644 index 4788ffb..0000000 --- a/tools/prepend.h +++ /dev/null @@ -1,23 +0,0 @@ -/***************************************************************************** -# # -# uStreamer - Lightweight and fast MJPG-HTTP streamer. # -# # -# Copyright (C) 2018 Maxim Devaev # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -*****************************************************************************/ - - -#pragma once