py: Generate pixel formats list
Generate a list of pixel formats under 'libcamera.formats'. The 'formats' is a "dummy" container class, the only purpose of which is to contain the read-only pixel format properties. Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
parent
c3515cf6e5
commit
9ecf311375
4 changed files with 96 additions and 0 deletions
56
src/py/libcamera/gen-py-formats.py
Executable file
56
src/py/libcamera/gen-py-formats.py
Executable file
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Generate Python format definitions from YAML
|
||||
|
||||
import argparse
|
||||
import string
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
|
||||
def generate(formats):
|
||||
fmts = []
|
||||
|
||||
for format in formats:
|
||||
name, format = format.popitem()
|
||||
fmts.append(f'\t\t.def_readonly_static("{name}", &libcamera::formats::{name})')
|
||||
|
||||
return {'formats': '\n'.join(fmts)}
|
||||
|
||||
|
||||
def fill_template(template, data):
|
||||
with open(template, encoding='utf-8') as f:
|
||||
template = f.read()
|
||||
|
||||
template = string.Template(template)
|
||||
return template.substitute(data)
|
||||
|
||||
|
||||
def main(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-o', dest='output', metavar='file', type=str,
|
||||
help='Output file name. Defaults to standard output if not specified.')
|
||||
parser.add_argument('input', type=str,
|
||||
help='Input file name.')
|
||||
parser.add_argument('template', type=str,
|
||||
help='Template file name.')
|
||||
args = parser.parse_args(argv[1:])
|
||||
|
||||
with open(args.input, encoding='utf-8') as f:
|
||||
formats = yaml.safe_load(f)['formats']
|
||||
|
||||
data = generate(formats)
|
||||
data = fill_template(args.template, data)
|
||||
|
||||
if args.output:
|
||||
with open(args.output, 'w', encoding='utf-8') as f:
|
||||
f.write(data)
|
||||
else:
|
||||
sys.stdout.write(data)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
|
@ -30,6 +30,18 @@ pycamera_sources += custom_target('py_gen_controls',
|
|||
output : ['py_control_enums_generated.cpp'],
|
||||
command : [gen_py_control_enums, '-o', '@OUTPUT@', '@INPUT@'])
|
||||
|
||||
gen_py_formats_input_files = files([
|
||||
'../../libcamera/formats.yaml',
|
||||
'py_formats_generated.cpp.in',
|
||||
])
|
||||
|
||||
gen_py_formats = files('gen-py-formats.py')
|
||||
|
||||
pycamera_sources += custom_target('py_gen_formats',
|
||||
input : gen_py_formats_input_files,
|
||||
output : ['py_formats_generated.cpp'],
|
||||
command : [gen_py_formats, '-o', '@OUTPUT@', '@INPUT@'])
|
||||
|
||||
pycamera_deps = [
|
||||
libcamera_public,
|
||||
py3_dep,
|
||||
|
|
25
src/py/libcamera/py_formats_generated.cpp.in
Normal file
25
src/py/libcamera/py_formats_generated.cpp.in
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
/*
|
||||
* Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
|
||||
*
|
||||
* Python bindings - Auto-generated formats
|
||||
*
|
||||
* This file is auto-generated. Do not edit.
|
||||
*/
|
||||
|
||||
#include <libcamera/formats.h>
|
||||
|
||||
#include <pybind11/smart_holder.h>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class PyFormats
|
||||
{
|
||||
};
|
||||
|
||||
void init_py_formats_generated(py::module& m)
|
||||
{
|
||||
py::class_<PyFormats>(m, "formats")
|
||||
${formats}
|
||||
;
|
||||
}
|
|
@ -132,6 +132,7 @@ static void handleRequestCompleted(Request *req)
|
|||
|
||||
void init_py_enums(py::module &m);
|
||||
void init_py_control_enums_generated(py::module &m);
|
||||
void init_py_formats_generated(py::module &m);
|
||||
void init_py_geometry(py::module &m);
|
||||
|
||||
PYBIND11_MODULE(_libcamera, m)
|
||||
|
@ -171,6 +172,8 @@ PYBIND11_MODULE(_libcamera, m)
|
|||
auto pyColorSpaceRange = py::enum_<ColorSpace::Range>(pyColorSpace, "Range");
|
||||
auto pyPixelFormat = py::class_<PixelFormat>(m, "PixelFormat");
|
||||
|
||||
init_py_formats_generated(m);
|
||||
|
||||
/* Global functions */
|
||||
m.def("log_set_level", &logSetLevel);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue