Skip to content

Commit 117b401

Browse files
committed
Merging 'master' into 'wrap'
2 parents 838e74d + 3a5e715 commit 117b401

File tree

8 files changed

+68
-88
lines changed

8 files changed

+68
-88
lines changed

wrap/gtwrap/interface_parser/classes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Author: Duy Nguyen Ta, Fan Jiang, Matthew Sklar, Varun Agrawal, and Frank Dellaert
1111
"""
1212

13-
from typing import Iterable, List, Union
13+
from typing import Any, Iterable, List, Union
1414

1515
from pyparsing import Literal, Optional, ZeroOrMore # type: ignore
1616

@@ -48,12 +48,12 @@ class Hello {
4848
args_list, t.is_const))
4949

5050
def __init__(self,
51-
template: str,
51+
template: Union[Template, Any],
5252
name: str,
5353
return_type: ReturnType,
5454
args: ArgumentList,
5555
is_const: str,
56-
parent: Union[str, "Class"] = ''):
56+
parent: Union["Class", Any] = ''):
5757
self.template = template
5858
self.name = name
5959
self.return_type = return_type
@@ -98,7 +98,7 @@ def __init__(self,
9898
name: str,
9999
return_type: ReturnType,
100100
args: ArgumentList,
101-
parent: Union[str, "Class"] = ''):
101+
parent: Union["Class", Any] = ''):
102102
self.name = name
103103
self.return_type = return_type
104104
self.args = args
@@ -129,7 +129,7 @@ class Constructor:
129129
def __init__(self,
130130
name: str,
131131
args: ArgumentList,
132-
parent: Union["Class", str] = ''):
132+
parent: Union["Class", Any] = ''):
133133
self.name = name
134134
self.args = args
135135

@@ -167,7 +167,7 @@ def __init__(self,
167167
return_type: ReturnType,
168168
args: ArgumentList,
169169
is_const: str,
170-
parent: Union[str, "Class"] = ''):
170+
parent: Union["Class", Any] = ''):
171171
self.name = name
172172
self.operator = operator
173173
self.return_type = return_type
@@ -284,7 +284,7 @@ def __init__(
284284
properties: List[Variable],
285285
operators: List[Operator],
286286
enums: List[Enum],
287-
parent: str = '',
287+
parent: Any = '',
288288
):
289289
self.template = template
290290
self.is_virtual = is_virtual

wrap/gtwrap/interface_parser/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def __init__(self,
169169
return_type: ReturnType,
170170
args_list: ArgumentList,
171171
template: Template,
172-
parent: str = ''):
172+
parent: Any = ''):
173173
self.name = name
174174
self.return_type = return_type
175175
self.args = args_list

wrap/gtwrap/interface_parser/type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# pylint: disable=unnecessary-lambda, expression-not-assigned
1414

15-
from typing import Iterable, List, Union
15+
from typing import List, Sequence, Union
1616

1717
from pyparsing import (Forward, Optional, Or, ParseResults, # type: ignore
1818
delimitedList)
@@ -49,12 +49,12 @@ class Typename:
4949

5050
def __init__(self,
5151
t: ParseResults,
52-
instantiations: Iterable[ParseResults] = ()):
52+
instantiations: Sequence[ParseResults] = ()):
5353
self.name = t[-1] # the name is the last element in this list
5454
self.namespaces = t[:-1]
5555

5656
if instantiations:
57-
if isinstance(instantiations, Iterable):
57+
if isinstance(instantiations, Sequence):
5858
self.instantiations = instantiations # type: ignore
5959
else:
6060
self.instantiations = instantiations.asList()
Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
"""Mixins for reducing the amount of boilerplate in the main wrapper class."""
22

3+
from typing import Any, Tuple, Union
4+
35
import gtwrap.interface_parser as parser
46
import gtwrap.template_instantiator as instantiator
57

68

79
class CheckMixin:
810
"""Mixin to provide various checks."""
911
# Data types that are primitive types
10-
not_ptr_type = ['int', 'double', 'bool', 'char', 'unsigned char', 'size_t']
12+
not_ptr_type: Tuple = ('int', 'double', 'bool', 'char', 'unsigned char',
13+
'size_t')
1114
# Ignore the namespace for these datatypes
12-
ignore_namespace = ['Matrix', 'Vector', 'Point2', 'Point3']
15+
ignore_namespace: Tuple = ('Matrix', 'Vector', 'Point2', 'Point3')
1316
# Methods that should be ignored
14-
ignore_methods = ['pickle']
17+
ignore_methods: Tuple = ('pickle', )
1518
# Methods that should not be wrapped directly
16-
whitelist = ['serializable', 'serialize']
19+
whitelist: Tuple = ('serializable', 'serialize')
1720
# Datatypes that do not need to be checked in methods
1821
not_check_type: list = []
1922

@@ -23,7 +26,7 @@ def _has_serialization(self, cls):
2326
return True
2427
return False
2528

26-
def is_shared_ptr(self, arg_type):
29+
def is_shared_ptr(self, arg_type: parser.Type):
2730
"""
2831
Determine if the `interface_parser.Type` should be treated as a
2932
shared pointer in the wrapper.
@@ -33,7 +36,7 @@ def is_shared_ptr(self, arg_type):
3336
and arg_type.typename.name not in self.ignore_namespace
3437
and arg_type.typename.name != 'string')
3538

36-
def is_ptr(self, arg_type):
39+
def is_ptr(self, arg_type: parser.Type):
3740
"""
3841
Determine if the `interface_parser.Type` should be treated as a
3942
raw pointer in the wrapper.
@@ -43,7 +46,7 @@ def is_ptr(self, arg_type):
4346
and arg_type.typename.name not in self.ignore_namespace
4447
and arg_type.typename.name != 'string')
4548

46-
def is_ref(self, arg_type):
49+
def is_ref(self, arg_type: parser.Type):
4750
"""
4851
Determine if the `interface_parser.Type` should be treated as a
4952
reference in the wrapper.
@@ -55,7 +58,14 @@ def is_ref(self, arg_type):
5558

5659
class FormatMixin:
5760
"""Mixin to provide formatting utilities."""
58-
def _clean_class_name(self, instantiated_class):
61+
62+
ignore_namespace: tuple
63+
data_type: Any
64+
data_type_param: Any
65+
_return_count: Any
66+
67+
def _clean_class_name(self,
68+
instantiated_class: instantiator.InstantiatedClass):
5969
"""Reformatted the C++ class name to fit Matlab defined naming
6070
standards
6171
"""
@@ -65,23 +75,23 @@ def _clean_class_name(self, instantiated_class):
6575
return instantiated_class.name
6676

6777
def _format_type_name(self,
68-
type_name,
69-
separator='::',
70-
include_namespace=True,
71-
constructor=False,
72-
method=False):
78+
type_name: parser.Typename,
79+
separator: str = '::',
80+
include_namespace: bool = True,
81+
is_constructor: bool = False,
82+
is_method: bool = False):
7383
"""
7484
Args:
7585
type_name: an interface_parser.Typename to reformat
7686
separator: the statement to add between namespaces and typename
7787
include_namespace: whether to include namespaces when reformatting
78-
constructor: if the typename will be in a constructor
79-
method: if the typename will be in a method
88+
is_constructor: if the typename will be in a constructor
89+
is_method: if the typename will be in a method
8090
8191
Raises:
8292
constructor and method cannot both be true
8393
"""
84-
if constructor and method:
94+
if is_constructor and is_method:
8595
raise ValueError(
8696
'Constructor and method parameters cannot both be True')
8797

@@ -93,9 +103,9 @@ def _format_type_name(self,
93103
if name not in self.ignore_namespace and namespace != '':
94104
formatted_type_name += namespace + separator
95105

96-
if constructor:
106+
if is_constructor:
97107
formatted_type_name += self.data_type.get(name) or name
98-
elif method:
108+
elif is_method:
99109
formatted_type_name += self.data_type_param.get(name) or name
100110
else:
101111
formatted_type_name += name
@@ -106,8 +116,8 @@ def _format_type_name(self,
106116
template = '{}'.format(
107117
self._format_type_name(type_name.instantiations[idx],
108118
include_namespace=include_namespace,
109-
constructor=constructor,
110-
method=method))
119+
is_constructor=is_constructor,
120+
is_method=is_method))
111121
templates.append(template)
112122

113123
if len(templates) > 0: # If there are no templates
@@ -119,15 +129,15 @@ def _format_type_name(self,
119129
self._format_type_name(type_name.instantiations[idx],
120130
separator=separator,
121131
include_namespace=False,
122-
constructor=constructor,
123-
method=method))
132+
is_constructor=is_constructor,
133+
is_method=is_method))
124134

125135
return formatted_type_name
126136

127137
def _format_return_type(self,
128-
return_type,
129-
include_namespace=False,
130-
separator="::"):
138+
return_type: parser.function.ReturnType,
139+
include_namespace: bool = False,
140+
separator: str = "::"):
131141
"""Format return_type.
132142
133143
Args:
@@ -154,18 +164,15 @@ def _format_return_type(self,
154164

155165
return return_wrap
156166

157-
def _format_class_name(self, instantiated_class, separator=''):
167+
def _format_class_name(self,
168+
instantiated_class: instantiator.InstantiatedClass,
169+
separator: str = ''):
158170
"""Format a template_instantiator.InstantiatedClass name."""
159171
if instantiated_class.parent == '':
160172
parent_full_ns = ['']
161173
else:
162174
parent_full_ns = instantiated_class.parent.full_namespaces()
163-
# class_name = instantiated_class.parent.name
164-
#
165-
# if class_name != '':
166-
# class_name += separator
167-
#
168-
# class_name += instantiated_class.name
175+
169176
parentname = "".join([separator + x
170177
for x in parent_full_ns]) + separator
171178

@@ -175,10 +182,12 @@ def _format_class_name(self, instantiated_class, separator=''):
175182

176183
return class_name
177184

178-
def _format_static_method(self, static_method, separator=''):
179-
"""Example:
180-
181-
gtsamPoint3.staticFunction
185+
def _format_static_method(self,
186+
static_method: parser.StaticMethod,
187+
separator: str = ''):
188+
"""
189+
Example:
190+
gtsam.Point3.staticFunction()
182191
"""
183192
method = ''
184193

@@ -188,35 +197,17 @@ def _format_static_method(self, static_method, separator=''):
188197

189198
return method[2 * len(separator):]
190199

191-
def _format_instance_method(self, instance_method, separator=''):
192-
"""Example:
193-
194-
gtsamPoint3.staticFunction
195-
"""
196-
method = ''
197-
198-
if isinstance(instance_method, instantiator.InstantiatedMethod):
199-
method_list = [
200-
separator + x
201-
for x in instance_method.parent.parent.full_namespaces()
202-
]
203-
method += "".join(method_list) + separator
204-
205-
method += instance_method.parent.name + separator
206-
method += instance_method.original.name
207-
method += "<" + instance_method.instantiations.to_cpp() + ">"
208-
209-
return method[2 * len(separator):]
210-
211-
def _format_global_method(self, static_method, separator=''):
200+
def _format_global_function(self,
201+
function: Union[parser.GlobalFunction, Any],
202+
separator: str = ''):
212203
"""Example:
213204
214205
gtsamPoint3.staticFunction
215206
"""
216207
method = ''
217208

218-
if isinstance(static_method, parser.GlobalFunction):
219-
method += "".join([separator + x for x in static_method.parent.full_namespaces()]) + \
209+
if isinstance(function, parser.GlobalFunction):
210+
method += "".join([separator + x for x in function.parent.full_namespaces()]) + \
220211
separator
221212

222213
return method[2 * len(separator):]

wrap/gtwrap/matlab_wrapper/wrapper.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from functools import partial, reduce
1212
from typing import Dict, Iterable, List, Union
1313

14-
from loguru import logger
15-
1614
import gtwrap.interface_parser as parser
1715
import gtwrap.template_instantiator as instantiator
1816
from gtwrap.matlab_wrapper.mixins import CheckMixin, FormatMixin
@@ -200,7 +198,7 @@ def _wrap_variable_arguments(self, args, wrap_datatypes=True):
200198
check_type = self._format_type_name(
201199
arg.ctype.typename,
202200
separator='.',
203-
constructor=not wrap_datatypes)
201+
is_constructor=not wrap_datatypes)
204202

205203
var_arg_wrap += " && isa(varargin{{{num}}},'{data_type}')".format(
206204
num=i, data_type=check_type)
@@ -1090,11 +1088,10 @@ def wrap_collector_function_return(self, method):
10901088
if method.instantiations:
10911089
# method_name += '<{}>'.format(
10921090
# self._format_type_name(method.instantiations))
1093-
# method_name = self._format_instance_method(method, '::')
10941091
method = method.to_cpp()
10951092

10961093
elif isinstance(method, parser.GlobalFunction):
1097-
method_name = self._format_global_method(method, '::')
1094+
method_name = self._format_global_function(method, '::')
10981095
method_name += method.name
10991096

11001097
else:

wrap/gtwrap/template_instantiator.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import itertools
66
from copy import deepcopy
7-
from typing import Iterable, List
7+
from typing import Any, Iterable, List, Sequence
88

99
import gtwrap.interface_parser as parser
1010

@@ -214,17 +214,17 @@ class A {
214214
}
215215
"""
216216
def __init__(self,
217-
original,
217+
original: parser.Method,
218218
instantiations: Iterable[parser.Typename] = ()):
219219
self.original = original
220220
self.instantiations = instantiations
221-
self.template = ''
221+
self.template: Any = ''
222222
self.is_const = original.is_const
223223
self.parent = original.parent
224224

225225
# Check for typenames if templated.
226226
# This way, we can gracefully handle both templated and non-templated methods.
227-
typenames = self.original.template.typenames if self.original.template else []
227+
typenames: Sequence = self.original.template.typenames if self.original.template else []
228228
self.name = instantiate_name(original.name, self.instantiations)
229229
self.return_type = instantiate_return_type(
230230
original.return_type,
@@ -348,13 +348,12 @@ def __repr__(self):
348348
return "{virtual}Class {cpp_class} : {parent_class}\n"\
349349
"{ctors}\n{static_methods}\n{methods}\n{operators}".format(
350350
virtual="virtual " if self.is_virtual else '',
351-
name=self.name,
352351
cpp_class=self.to_cpp(),
353352
parent_class=self.parent,
354353
ctors="\n".join([repr(ctor) for ctor in self.ctors]),
355-
methods="\n".join([repr(m) for m in self.methods]),
356354
static_methods="\n".join([repr(m)
357355
for m in self.static_methods]),
356+
methods="\n".join([repr(m) for m in self.methods]),
358357
operators="\n".join([repr(op) for op in self.operators])
359358
)
360359

0 commit comments

Comments
 (0)