@@ -549,6 +549,32 @@ def __init__(self, element: Element):
549549}
550550
551551INTERFACE_TEMPLATES : Dict [str , str ] = {
552+ "generic_method_flags" : (
553+ r"""
554+ {%- if method.dbus_input_signature %}
555+ input_signature="{{ method.dbus_input_signature }}",
556+ {%- endif %}
557+
558+ {%- if method.dbus_result_signature %}
559+ result_signature="{{ method.dbus_result_signature }}",
560+ {%- endif %}
561+
562+ {%- if method.flags_str %}
563+ flags={{ method.flags_str }},
564+ {%- endif %}
565+ """
566+ ),
567+ "generic_property_flags" : (
568+ r"""
569+ {%- if a_property.dbus_signature %}
570+ property_signature="{{ a_property.dbus_signature }}",
571+ {%- endif %}
572+
573+ {%- if a_property.flags_str %}
574+ flags={{ a_property.flags_str }},
575+ {%- endif %}
576+ """
577+ ),
552578 "generic_header" : r"""from __future__ import annotations
553579
554580from typing import Any, Dict, List, Tuple""" ,
@@ -598,18 +624,9 @@ def __init__(self, element: Element):
598624 "async_method" : (
599625 r"""
600626@dbus_method_async(
601-
602- {%- if method.dbus_input_signature %}
603- input_signature="{{ method.dbus_input_signature }}",
604- {%- endif %}
605-
606- {%- if method.dbus_result_signature %}
607- result_signature="{{ method.dbus_result_signature }}",
608- {%- endif %}
609-
610- {%- if method.flags_str %}
611- flags={{ method.flags_str }},
612- {%- endif %}
627+ {%- filter indent -%}
628+ {%- include 'generic_method_flags' -%}
629+ {%- endfilter %}
613630)
614631async def {{ method.python_name }}(
615632 self,
@@ -624,14 +641,9 @@ async def {{ method.python_name }}(
624641 "async_property" : (
625642 r"""
626643@dbus_property_async(
627-
628- {%- if a_property.dbus_signature %}
629- property_signature="{{ a_property.dbus_signature }}",
630- {%- endif %}
631-
632- {%- if a_property.flags_str %}
633- flags={{ a_property.flags_str }},
634- {%- endif %}
644+ {%- filter indent -%}
645+ {%- include 'generic_property_flags' -%}
646+ {%- endfilter %}
635647)
636648def {{ a_property.python_name }}(self) -> {{ a_property.typing }}:
637649 raise NotImplementedError"""
@@ -651,6 +663,72 @@ def {{ a_property.python_name }}(self) -> {{ a_property.typing }}:
651663def {{ signal.python_name }}(self) -> {{ signal.typing }}:
652664 raise NotImplementedError"""
653665 ),
666+ "blocking_imports_header" : r"""from sdbus import (
667+ DbusDeprecatedFlag,
668+ DbusInterfaceCommon,
669+ DbusNoReplyFlag,
670+ DbusPropertyConstFlag,
671+ DbusPropertyEmitsChangeFlag,
672+ DbusPropertyEmitsInvalidationFlag,
673+ DbusPropertyExplicitFlag,
674+ DbusUnprivilegedFlag,
675+ dbus_method,
676+ dbus_property,
677+ )""" ,
678+ "blocking_main" : (
679+ r"""{% if include_import_header -%}
680+ {% include 'generic_header' %}
681+
682+ {% include 'blocking_imports_header' %}
683+ {%- endif %}
684+ {% for interface in interfaces %}
685+
686+ {% include 'blocking_interface' %}
687+ {%- endfor %}
688+ """
689+ ),
690+ "blocking_interface" : (
691+ r"""class {{ interface.python_name }}(
692+ DbusInterfaceCommon,
693+ interface_name="{{ interface.interface_name }}",
694+ ):
695+ {%- filter indent -%}
696+ {% for method in interface.methods -%}
697+ {% include 'blocking_method' %}
698+ {% endfor -%}
699+ {% for a_property in interface.properties -%}
700+ {% include 'blocking_property' %}
701+ {% endfor -%}
702+ {%- endfilter -%}
703+ """
704+ ),
705+ "blocking_method" : (
706+ r"""
707+ @dbus_method(
708+ {%- filter indent -%}
709+ {%- include 'generic_method_flags' -%}
710+ {%- endfilter %}
711+ )
712+ def {{ method.python_name }}(
713+ self,
714+
715+ {%- for arg_name, arg_type in method.args_names_and_typing %}
716+ {{ arg_name }}: {{ arg_type }},
717+ {%- endfor %}
718+ ) -> {{ method.result_typing }}:
719+ raise NotImplementedError
720+ """
721+ ),
722+ "blocking_property" : (
723+ r"""
724+ @dbus_property(
725+ {%- filter indent -%}
726+ {%- include 'generic_property_flags' -%}
727+ {%- endfilter %}
728+ )
729+ def {{ a_property.python_name }}(self) -> {{ a_property.typing }}:
730+ raise NotImplementedError"""
731+ ),
654732}
655733
656734
@@ -690,15 +768,19 @@ def interfaces_from_str(xml_str: str) -> List[DbusInterfaceIntrospection]:
690768 return xml_to_interfaces_introspection (etree )
691769
692770
693- def generate_async_py_file (
694- interfaces : List [DbusInterfaceIntrospection ],
695- include_import_header : bool = True ) -> str :
771+ def generate_py_file (
772+ interfaces : List [DbusInterfaceIntrospection ],
773+ include_import_header : bool = True ,
774+ do_async : bool = True ,
775+ ) -> str :
696776
697777 from jinja2 import DictLoader
698778 from jinja2 import Environment as JinjaEnv
699779
780+ template_name = "async_main" if do_async else "blocking_main"
781+
700782 env = JinjaEnv (loader = DictLoader (INTERFACE_TEMPLATES ))
701- return env .get_template ("async_main" ).render (
783+ return env .get_template (template_name ).render (
702784 interfaces = interfaces ,
703785 include_import_header = include_import_header ,
704786 )
0 commit comments