[gobject-introspection/ebassi/property-annotation: 8/17] Add annotations for property setters and getters




commit 7107b299d924e8247368d137285d3f5e52a3f09d
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Thu Jun 17 14:14:09 2021 +0100

    Add annotations for property setters and getters
    
    We need new annotations to allow library developers to associate a
    setter and a getter functions to a property definition.

 giscanner/annotationparser.py | 52 +++++++++++++++++++++++++++++++++++++++----
 giscanner/ast.py              |  2 ++
 giscanner/girparser.py        |  4 +++-
 giscanner/girwriter.py        |  4 ++++
 giscanner/maintransformer.py  | 10 +++++++--
 5 files changed, 65 insertions(+), 7 deletions(-)
---
diff --git a/giscanner/annotationparser.py b/giscanner/annotationparser.py
index 95ee1f97..27863437 100644
--- a/giscanner/annotationparser.py
+++ b/giscanner/annotationparser.py
@@ -198,6 +198,7 @@ ANN_ELEMENT_TYPE = 'element-type'
 ANN_FOREIGN = 'foreign'
 ANN_GET_PROPERTY = 'get-property'
 ANN_GET_VALUE_FUNC = 'get-value-func'
+ANN_GETTER = 'getter'
 ANN_IN = 'in'
 ANN_INOUT = 'inout'
 ANN_METHOD = 'method'
@@ -210,6 +211,7 @@ ANN_RENAME_TO = 'rename-to'
 ANN_SCOPE = 'scope'
 ANN_SET_PROPERTY = 'set-property'
 ANN_SET_VALUE_FUNC = 'set-value-func'
+ANN_SETTER = 'setter'
 ANN_SKIP = 'skip'
 ANN_TRANSFER = 'transfer'
 ANN_TYPE = 'type'
@@ -230,6 +232,7 @@ GI_ANNS = [ANN_ALLOW_NONE,
            ANN_FOREIGN,
            ANN_GET_PROPERTY,
            ANN_GET_VALUE_FUNC,
+           ANN_GETTER,
            ANN_IN,
            ANN_INOUT,
            ANN_METHOD,
@@ -239,6 +242,7 @@ GI_ANNS = [ANN_ALLOW_NONE,
            ANN_SCOPE,
            ANN_SET_PROPERTY,
            ANN_SET_VALUE_FUNC,
+           ANN_SETTER,
            ANN_SKIP,
            ANN_TRANSFER,
            ANN_TYPE,
@@ -840,6 +844,18 @@ class GtkDocAnnotatable(object):
 
         self._validate_annotation(position, ann_name, options, exact_n_options=1)
 
+    def _do_validate_getter(self, position, ann_name, options):
+        '''
+        Validate the ``(getter)`` annotation.
+
+        :param position: :class:`giscanner.message.Position` of the line in the source file
+                         containing the annotation to be validated
+        :param ann_name: name of the annotation holding the options to validate
+        :param options: annotation options to validate
+        '''
+
+        self._validate_annotation(position, ann_name, options, exact_n_options=1)
+
     def _do_validate_in(self, position, ann_name, options):
         '''
         Validate the ``(in)`` annotation.
@@ -987,6 +1003,18 @@ class GtkDocAnnotatable(object):
 
         self._validate_annotation(position, ann_name, options, exact_n_options=1)
 
+    def _do_validate_setter(self, position, ann_name, options):
+        '''
+        Validate the ``(setter)`` annotation.
+
+        :param position: :class:`giscanner.message.Position` of the line in the source file
+                         containing the annotation to be validated
+        :param ann_name: name of the annotation holding the options to validate
+        :param options: annotation options to validate
+        '''
+
+        self._validate_annotation(position, ann_name, options, exact_n_options=1)
+
     def _do_validate_skip(self, position, ann_name, options):
         '''
         Validate the ``(skip)`` annotation.
@@ -1120,10 +1148,26 @@ class GtkDocCommentBlock(GtkDocAnnotatable):
                  'name', 'params', 'description', 'tags')
 
     #: Valid annotation names for the GTK-Doc comment block identifier part.
-    valid_annotations = (ANN_ATTRIBUTES, ANN_CONSTRUCTOR, ANN_FOREIGN, ANN_GET_PROPERTY,
-                         ANN_GET_VALUE_FUNC, ANN_METHOD, ANN_REF_FUNC, ANN_RENAME_TO,
-                         ANN_SET_PROPERTY, ANN_SET_VALUE_FUNC, ANN_SKIP, ANN_TRANSFER,
-                         ANN_TYPE, ANN_UNREF_FUNC, ANN_VALUE, ANN_VFUNC)
+    valid_annotations = (
+        ANN_ATTRIBUTES,
+        ANN_CONSTRUCTOR,
+        ANN_FOREIGN,
+        ANN_GET_PROPERTY,
+        ANN_GET_VALUE_FUNC,
+        ANN_GETTER,
+        ANN_METHOD,
+        ANN_REF_FUNC,
+        ANN_RENAME_TO,
+        ANN_SET_PROPERTY,
+        ANN_SET_VALUE_FUNC,
+        ANN_SETTER,
+        ANN_SKIP,
+        ANN_TRANSFER,
+        ANN_TYPE,
+        ANN_UNREF_FUNC,
+        ANN_VALUE,
+        ANN_VFUNC,
+    )
 
     def __init__(self, name, position=None):
         GtkDocAnnotatable.__init__(self, position)
diff --git a/giscanner/ast.py b/giscanner/ast.py
index 7cb6f622..fa81c220 100644
--- a/giscanner/ast.py
+++ b/giscanner/ast.py
@@ -1296,6 +1296,8 @@ class Property(Node):
             self.transfer = PARAM_TRANSFER_NONE
         else:
             self.transfer = transfer
+        self.setter = None
+        self.getter = None
         self.parent = None  # A Class or Interface
 
 
diff --git a/giscanner/girparser.py b/giscanner/girparser.py
index 0fe85ed4..e10fde73 100644
--- a/giscanner/girparser.py
+++ b/giscanner/girparser.py
@@ -603,8 +603,10 @@ class GIRParser(object):
                             node.attrib.get('construct') == '1',
                             node.attrib.get('construct-only') == '1',
                             node.attrib.get('transfer-ownership'))
-        self._parse_generic_attribs(node, prop)
+        prop.setter = node.attrib.get('setter')
+        prop.getter = node.attrib.get('getter')
         prop.parent = parent
+        self._parse_generic_attribs(node, prop)
         return prop
 
     def _parse_member(self, node):
diff --git a/giscanner/girwriter.py b/giscanner/girwriter.py
index f8e61985..a7a60df1 100644
--- a/giscanner/girwriter.py
+++ b/giscanner/girwriter.py
@@ -554,6 +554,10 @@ class GIRWriter(XMLWriter):
             attrs.append(('construct-only', '1'))
         if prop.transfer:
             attrs.append(('transfer-ownership', prop.transfer))
+        if prop.setter:
+            attrs.append(('setter', prop.setter))
+        if prop.getter:
+            attrs.append(('getter', prop.getter))
         with self.tagcontext('property', attrs):
             self._write_generic(prop)
             self._write_type(prop.type)
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index 23073b4e..33f58485 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -24,9 +24,9 @@ from . import message
 from .annotationparser import (TAG_DEPRECATED, TAG_SINCE, TAG_STABILITY, TAG_RETURNS)
 from .annotationparser import (ANN_ALLOW_NONE, ANN_ARRAY, ANN_ATTRIBUTES, ANN_CLOSURE,
                                ANN_CONSTRUCTOR, ANN_DESTROY, ANN_ELEMENT_TYPE, ANN_FOREIGN,
-                               ANN_GET_PROPERTY, ANN_GET_VALUE_FUNC, ANN_IN, ANN_INOUT,
+                               ANN_GET_PROPERTY, ANN_GET_VALUE_FUNC, ANN_GETTER, ANN_IN, ANN_INOUT,
                                ANN_METHOD, ANN_OUT, ANN_REF_FUNC, ANN_RENAME_TO, ANN_SCOPE,
-                               ANN_SET_PROPERTY, ANN_SET_VALUE_FUNC, ANN_SKIP, ANN_TRANSFER,
+                               ANN_SET_PROPERTY, ANN_SET_VALUE_FUNC, ANN_SETTER, ANN_SKIP, ANN_TRANSFER,
                                ANN_TYPE, ANN_UNREF_FUNC, ANN_VALUE, ANN_VFUNC, ANN_NULLABLE,
                                ANN_OPTIONAL, ANN_NOT)
 from .annotationparser import (OPT_ARRAY_FIXED_SIZE, OPT_ARRAY_LENGTH, OPT_ARRAY_ZERO_TERMINATED,
@@ -923,6 +923,12 @@ class MainTransformer(object):
         type_annotation = block.annotations.get(ANN_TYPE)
         if type_annotation:
             prop.type = self._resolve_toplevel(type_annotation[0], prop.type, prop, parent)
+        setter = block.annotations.get(ANN_SETTER)
+        if setter:
+            prop.setter = setter[0]
+        getter = block.annotations.get(ANN_GETTER)
+        if getter:
+            prop.getter = getter[0]
 
     def _apply_annotations_signal(self, parent, signal):
         names = []


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]