From patchwork Fri Aug 9 00:59:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20859 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 77332C32D5 for ; Fri, 9 Aug 2024 00:59:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D2CE1633C9; Fri, 9 Aug 2024 02:59:56 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="bTWLPzwM"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 234FD633CD for ; Fri, 9 Aug 2024 02:59:52 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2B5BC83F for ; Fri, 9 Aug 2024 02:58:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1723165138; bh=7fKKDaIFyyJ5HbNkseLGDiGUEMMyrsKzO/H3tnbmhcU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=bTWLPzwM4baMKs5sUh/dDJS+4Blz2YNfs+fFF/XnG5GjXPiKqUfretZxz6K6tPIbA YWU03IPvMCuRcI/+D4t1QRz824a657i0znjtK+JpQB3x67GFncAXrLE2eX8rFaRcZT ex6ZoRg7iOxoAsoaa5pgh8NNCCZRtk4FwU4dH7bE= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 09/10] py: gen-py-controls: Use Control class Date: Fri, 9 Aug 2024 03:59:13 +0300 Message-ID: <20240809005914.20662-10-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.2 In-Reply-To: <20240809005914.20662-1-laurent.pinchart@ideasonboard.com> References: <20240809005914.20662-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Replace manual extraction of data from YAML with the Control helper class. This centralizes YAML parsing and avoids manual mistakes. In order to import the controls module, add the utils/codegen/ directory to the PYTHONPATH through the Python build environment. Signed-off-by: Laurent Pinchart Reviewed-by: Tomi Valkeinen Reviewed-by: Paul Elder --- src/py/libcamera/gen-py-controls.py | 32 ++++++++++++++--------------- src/py/libcamera/meson.build | 6 ++++-- utils/codegen/meson.build | 1 + 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/py/libcamera/gen-py-controls.py b/src/py/libcamera/gen-py-controls.py index 8efbf95b9433..a18dc5337090 100755 --- a/src/py/libcamera/gen-py-controls.py +++ b/src/py/libcamera/gen-py-controls.py @@ -8,6 +8,8 @@ import string import sys import yaml +from controls import Control + def find_common_prefix(strings): prefix = strings[0] @@ -28,9 +30,7 @@ def generate_py(controls, mode): vendor_defs = [] vendors = [] for vendor, ctrl_list in controls.items(): - for ctrls in ctrl_list: - name, ctrl = ctrls.popitem() - + for ctrl in ctrl_list: if vendor not in vendors and vendor != 'libcamera': vendor_mode_str = f'{vendor.capitalize()}{mode.capitalize()}' vendors_class_def.append('class Py{}\n{{\n}};\n'.format(vendor_mode_str)) @@ -44,29 +44,28 @@ def generate_py(controls, mode): ns = 'libcamera::{}::'.format(mode) container = 'controls' - out += f'\t{container}.def_readonly_static("{name}", static_cast(&{ns}{name}));\n\n' + out += f'\t{container}.def_readonly_static("{ctrl.name}", static_cast(&{ns}{ctrl.name}));\n\n' - enum = ctrl.get('enum') - if not enum: + if not ctrl.is_enum: continue - cpp_enum = name + 'Enum' + cpp_enum = ctrl.name + 'Enum' out += '\tpy::enum_<{}{}>({}, \"{}\")\n'.format(ns, cpp_enum, container, cpp_enum) if mode == 'controls': # Adjustments for controls - if name == 'LensShadingMapMode': + if ctrl.name == 'LensShadingMapMode': prefix = 'LensShadingMapMode' else: - prefix = find_common_prefix([e['name'] for e in enum]) + prefix = find_common_prefix([e.name for e in ctrl.enum_values]) else: # Adjustments for properties - prefix = find_common_prefix([e['name'] for e in enum]) + prefix = find_common_prefix([e.name for e in ctrl.enum_values]) - for entry in enum: - cpp_enum = entry['name'] - py_enum = entry['name'][len(prefix):] + for entry in ctrl.enum_values: + cpp_enum = entry.name + py_enum = entry.name[len(prefix):] out += '\t\t.value(\"{}\", {}{})\n'.format(py_enum, ns, cpp_enum) @@ -103,9 +102,10 @@ def main(argv): controls = {} for input in args.input: - data = open(input, 'rb').read() - vendor = yaml.safe_load(data)['vendor'] - controls[vendor] = yaml.safe_load(data)['controls'] + data = yaml.safe_load(open(input, 'rb').read()) + vendor = data['vendor'] + ctrls = data['controls'] + controls[vendor] = [Control(*ctrl.popitem(), vendor) for ctrl in ctrls] data = generate_py(controls, args.mode) diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build index 2e67407598db..6ad2d7713e4d 100644 --- a/src/py/libcamera/meson.build +++ b/src/py/libcamera/meson.build @@ -35,7 +35,8 @@ pycamera_sources += custom_target('py_gen_controls', input : controls_files, output : ['py_controls_generated.cpp'], command : [gen_py_controls, '--mode', 'controls', '-o', '@OUTPUT@', - '-t', gen_py_controls_template, '@INPUT@']) + '-t', gen_py_controls_template, '@INPUT@'], + env : py_build_env) # Generate properties @@ -45,7 +46,8 @@ pycamera_sources += custom_target('py_gen_properties', input : properties_files, output : ['py_properties_generated.cpp'], command : [gen_py_controls, '--mode', 'properties', '-o', '@OUTPUT@', - '-t', gen_py_properties_template, '@INPUT@']) + '-t', gen_py_properties_template, '@INPUT@'], + env : py_build_env) # Generate formats diff --git a/utils/codegen/meson.build b/utils/codegen/meson.build index fb2196ee0d20..adf33bbab9e1 100644 --- a/utils/codegen/meson.build +++ b/utils/codegen/meson.build @@ -5,6 +5,7 @@ py_build_env = environment() # \todo Investigate usage of PYTHONPYCACHEPREFIX for Python >= 3.8 py_build_env.set('PYTHONDONTWRITEBYTECODE', '1') +py_build_env.prepend('PYTHONPATH', meson.current_source_dir()) py_modules += ['jinja2', 'yaml']