This repository was archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys_crate.py
60 lines (54 loc) · 2.15 KB
/
sys_crate.py
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
# grust-gen - Rust binding generator for GObject introspection
#
# Copyright (C) 2015 Mikhail Zabaluev <[email protected]>
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
from ..giscanner import ast
from ..giscanner import message
from ..mapping import RawMapper, MappingError
class SysCrateWriter(object):
"""Generator for -sys crates."""
def __init__(self,
transformer,
template,
options,
gir_filename=None):
self._mapper = RawMapper(transformer, options)
self._template = template
self._options = options
if gir_filename:
self._message_positions = set(
(message.Position(filename=gir_filename),))
else:
self._message_positions = set()
transformer.namespace.walk(
lambda node, chain: self._prepare_walk(node, chain))
def write(self, output):
result = self._template.render_unicode(
mapper=self._mapper,
message_positions=self._message_positions)
output.write(result)
def _prepare_walk(self, node, chain):
try:
self._mapper.resolve_types_for_node(node)
except MappingError as e:
message.error_node(node, e,
positions=self._message_positions,
context=node)
return False
return True
def get_mapper(self):
return self._mapper