File: gobject_property.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (129 lines) | stat: -rw-r--r-- 4,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# gobject_property.py
#
# Copyright 2022 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later


from .binding import Binding
from .common import *
from .contexts import ValueTypeCtx
from .gtk_menu import menu
from .values import ArrayValue, ExprValue, ObjectValue, Value


class Property(AstNode):
    grammar = Statement(
        UseIdent("name"),
        ":",
        AnyOf(Binding, ExprValue, menu, ObjectValue, Value, ArrayValue),
    )

    @property
    def name(self) -> str:
        return self.tokens["name"]

    @property
    def value(self) -> T.Union[Binding, ExprValue, ObjectValue, Value, ArrayValue]:
        return self.children[0]

    @property
    def gir_class(self):
        return self.parent.parent.gir_class

    @property
    def gir_property(self) -> T.Optional[gir.Property]:
        if self.gir_class is not None and not isinstance(self.gir_class, ExternType):
            return self.gir_class.properties.get(self.tokens["name"])
        else:
            return None

    @property
    def document_symbol(self) -> DocumentSymbol:
        if isinstance(self.value, ObjectValue) or self.value is None:
            detail = None
        else:
            detail = self.value.range.text

        return DocumentSymbol(
            self.name,
            SymbolKind.Property,
            self.range,
            self.group.tokens["name"].range,
            detail,
        )

    @validate()
    def binding_valid(self):
        if (
            isinstance(self.value, Binding)
            and self.gir_property is not None
            and self.gir_property.construct_only
        ):
            raise CompileError(
                f"{self.gir_property.full_name} can't be bound because it is construct-only",
                hints=["construct-only properties may only be set to a static value"],
            )

    @context(ValueTypeCtx)
    def value_type(self) -> ValueTypeCtx:
        if self.gir_property is not None:
            type = self.gir_property.type
        else:
            type = None

        return ValueTypeCtx(type)

    @validate("name")
    def property_exists(self):
        if self.gir_class is None or self.gir_class.incomplete:
            # Objects that we have no gir data on should not be validated
            # This happens for classes defined by the app itself
            return

        if self.gir_property is None:
            raise CompileError(
                f"Class {self.gir_class.full_name} does not have a property called {self.tokens['name']}",
                did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()),
            )

    @validate("name")
    def property_writable(self):
        if self.gir_property is not None and not self.gir_property.writable:
            raise CompileError(f"{self.gir_property.full_name} is not writable")

    @validate("name")
    def unique_in_parent(self):
        self.validate_unique_in_parent(
            f"Duplicate property '{self.tokens['name']}'",
            check=lambda child: child.tokens["name"] == self.tokens["name"],
        )

    @validate("name")
    def deprecated(self) -> None:
        if self.gir_property is not None and self.gir_property.deprecated:
            hints = []
            if self.gir_property.deprecated_doc:
                hints.append(self.gir_property.deprecated_doc)
            raise DeprecatedWarning(
                f"{self.gir_property.signature} is deprecated",
                hints=hints,
            )

    @docs("name")
    def property_docs(self):
        if self.gir_property is not None:
            return self.gir_property.doc