Skip to content

Commit e403a1b

Browse files
committed
Add mechanism to ignore everything by default
1 parent 2c5f822 commit e403a1b

File tree

6 files changed

+148
-12
lines changed

6 files changed

+148
-12
lines changed

robotpy_build/autowrap/generator_data.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ class ClsReportData:
4040
functions: FnMissingData = dataclasses.field(default_factory=dict)
4141

4242

43-
_default_enum_data = EnumData()
44-
_default_fn_data = FunctionData()
45-
46-
4743
class GeneratorData:
4844
"""
4945
Used by the hooks to retrieve user-specified generation data, and
@@ -55,6 +51,13 @@ class GeneratorData:
5551
def __init__(self, data: AutowrapConfigYaml):
5652
self.data = data
5753

54+
default_ignore = self.data.defaults.ignore
55+
self._default_enum_data = EnumData(ignore=default_ignore)
56+
self._default_fn_data = FunctionData(ignore=default_ignore)
57+
self._default_method_data = FunctionData()
58+
self._default_class_data = ClassData(ignore=default_ignore)
59+
self._default_class_enum_data = EnumData()
60+
5861
# report data
5962
self.functions: FnMissingData = {}
6063
self.classes: Dict[str, ClsReportData] = {}
@@ -68,7 +71,7 @@ def get_class_data(self, name: str) -> ClassData:
6871
data = self.data.classes.get(name)
6972
missing = data is None
7073
if missing:
71-
data = ClassData()
74+
data = self._default_class_data
7275

7376
self.classes[name] = ClsReportData(missing=missing)
7477
return data
@@ -78,19 +81,19 @@ def get_cls_enum_data(
7881
) -> EnumData:
7982
if name is None:
8083
# TODO
81-
return _default_enum_data
84+
return self._default_class_enum_data
8285
data = cls_data.enums.get(name)
8386
if data is None:
8487
self.classes[cls_key].enums[name] = False
85-
data = _default_enum_data
88+
data = self._default_class_enum_data
8689

8790
return data
8891

8992
def get_enum_data(self, name: str) -> EnumData:
9093
data = self.data.enums.get(name)
9194
if data is None:
9295
self.enums[name] = False
93-
data = _default_enum_data
96+
data = self._default_enum_data
9497
return data
9598

9699
def get_function_data(
@@ -124,7 +127,10 @@ def get_function_data(
124127
# signature each time we defer it until we actually need to use it
125128

126129
if missing:
127-
data = _default_fn_data
130+
if cls_key:
131+
data = self._default_method_data
132+
else:
133+
data = self._default_fn_data
128134
report_data.deferred_signatures.append((fn, is_private))
129135
elif not data.overloads:
130136
report_data.deferred_signatures.append((fn, True))
@@ -187,22 +193,39 @@ def report_missing(self, name: str, reporter: "MissingReporter"):
187193
data yaml and print it out if there's missing data
188194
"""
189195

196+
ignore_default = self.data.defaults.ignore
197+
report_missing = True
198+
if ignore_default and not self.data.defaults.report_ignored_missing:
199+
report_missing = False
200+
190201
# note: sometimes we have strings from CppHeaderParser that aren't
191202
# strings, so we need to cast them to str so yaml doesn't complain
192203

193204
data = self._process_missing(
194-
self.attributes, self.functions, self.enums, "functions"
205+
self.attributes,
206+
self.functions,
207+
self.enums,
208+
"functions",
209+
ignore_default,
210+
report_missing,
195211
)
196212

197213
all_cls_data = {}
198214
for cls_key, cls_data in self.classes.items():
215+
if cls_data.missing and not report_missing:
216+
continue
217+
199218
result = self._process_missing(
200219
cls_data.attributes,
201220
cls_data.functions,
202221
cls_data.enums,
203222
"methods",
223+
False,
224+
True,
204225
)
205226
if result or cls_data.missing:
227+
if ignore_default and cls_data.missing:
228+
result["ignore"] = True
206229
all_cls_data[str(cls_key)] = result
207230
if all_cls_data:
208231
data["classes"] = all_cls_data
@@ -218,6 +241,8 @@ def _process_missing(
218241
fns: FnMissingData,
219242
enums: EnumMissingData,
220243
fn_key: str,
244+
ignore_default: bool,
245+
report_missing: bool,
221246
):
222247
data: Dict[str, Dict[str, Dict]] = {}
223248

@@ -230,11 +255,22 @@ def _process_missing(
230255

231256
# enums
232257
if enums:
233-
data["enums"] = {str(n): {} for n in enums.keys()}
258+
enums_report = {}
259+
for en, enum_present in enums.items():
260+
if not enum_present and not report_missing:
261+
continue
262+
enums_report[en] = {}
263+
if ignore_default:
264+
enums_report[en]["ignore"] = True
265+
if enums_report:
266+
data["enums"] = enums_report
234267

235268
# functions
236269
fn_report = {}
237270
for fn, fndata in fns.items():
271+
if fndata.missing and not report_missing:
272+
continue
273+
238274
fn = str(fn)
239275
overloads = fndata.overloads
240276
deferred_signatures = fndata.deferred_signatures
@@ -264,8 +300,12 @@ def _process_missing(
264300
for k, v in fn_report[fn]["overloads"].items():
265301
if "initializer_list" in k:
266302
v["ignore"] = True
303+
if ignore_default:
304+
v["ignore"] = True
267305
else:
268306
fn_report[fn] = d
307+
if ignore_default:
308+
d["ignore"] = True
269309
if fn_report:
270310
data[fn_key] = fn_report
271311

robotpy_build/config/autowrap_yml.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import enum
77
from typing import Dict, List, Tuple, Optional
88

9-
from pydantic import validator
9+
from pydantic import validator, Field
1010
from .util import Model, _generating_documentation
1111
import yaml
1212

@@ -463,12 +463,27 @@ class MyClass {};
463463
doc_append: Optional[str] = None
464464

465465

466+
class Defaults(Model):
467+
"""
468+
Defaults to apply to everything
469+
"""
470+
471+
#: Set this to True to ignore functions/enums/classes at namespace scope
472+
#: by default if they aren't present in the YAML
473+
ignore: bool = False
474+
475+
#: If False and default ignore is True, don't report missing items
476+
report_ignored_missing: bool = True
477+
478+
466479
class AutowrapConfigYaml(Model):
467480
"""
468481
Format of the file in [tool.robotpy-build.wrappers."PACKAGENAME"]
469482
generation_data
470483
"""
471484

485+
defaults: Defaults = Field(default_factory=Defaults)
486+
472487
strip_prefixes: List[str] = []
473488

474489
#: Adds ``#include <FILENAME>`` directives to the top of the autogenerated
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defaults:
2+
ignore: true
3+
# report_ignored_missing: false
4+
5+
enums:
6+
id_EnabledEnum:
7+
ignore: false
8+
functions:
9+
id_fnEnable:
10+
ignore: false
11+
classes:
12+
id_EnabledClass:
13+
ignore: false
14+
enums:
15+
InnerEnum:
16+
methods:
17+
fn:

tests/cpp/pyproject.toml.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ generate = [
5555
{ fields = "fields.h" },
5656
{ keepalive = "keepalive.h" },
5757
{ ignore = "ignore.h" },
58+
{ ignored_by_default = "ignored_by_default.h" },
5859
{ inline_code = "inline_code.h" },
5960
{ lifetime = "lifetime.h" },
6061
{ nested = "nested.h" },
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#pragma once
3+
4+
//
5+
// Ignore everything by default, but enable some things
6+
//
7+
8+
int id_fnIgnore(void) { return 1; }
9+
int id_fnEnable(void) { return 2; }
10+
11+
enum id_IgnoredEnum {
12+
Param1 = 1,
13+
Param2 = 2,
14+
};
15+
16+
enum id_EnabledEnum {
17+
Param3 = 3,
18+
Param4 = 4,
19+
};
20+
21+
struct id_IgnoreClass {
22+
enum InnerEnum {
23+
Param5 = 5,
24+
};
25+
};
26+
27+
struct id_EnabledClass {
28+
enum InnerEnum {
29+
Param6 = 6,
30+
};
31+
enum InnerEnumMissing {
32+
Param7 = 7,
33+
};
34+
35+
int fn() { return 3; }
36+
37+
// not in yaml but still works
38+
int fn_missing() { return 4; }
39+
};

tests/test_ft_ignore.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ def test_ignored_enums():
2929

3030
assert not hasattr(ft.EnumWithIgnored, "Ignored")
3131
assert ft.EnumWithIgnored.NotIgnored == 1
32+
33+
34+
#
35+
# ignored_by_default
36+
#
37+
38+
39+
def test_ignored_by_default_fn():
40+
assert not hasattr(ft._rpytest_ft, "id_fnIgnore")
41+
assert ft._rpytest_ft.id_fnEnable() == 2
42+
43+
44+
def test_ignored_by_default_enum():
45+
assert not hasattr(ft._rpytest_ft, "id_IgnoredEnum")
46+
assert ft._rpytest_ft.id_EnabledEnum.Param3 == 3
47+
48+
49+
def test_ignored_by_default_class():
50+
assert not hasattr(ft._rpytest_ft, "id_IgnoreClass")
51+
o = ft._rpytest_ft.id_EnabledClass()
52+
assert o.fn() == 3
53+
assert o.fn_missing() == 4
54+
assert ft._rpytest_ft.id_EnabledClass.InnerEnum.Param6 == 6
55+
assert ft._rpytest_ft.id_EnabledClass.InnerEnumMissing.Param7 == 7

0 commit comments

Comments
 (0)