Coverage for trimesh/schemas.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-24 04:40 +0000

1""" 

2schemas.py 

3------------- 

4 

5Tools for dealing with schemas, particularly JSONschema 

6""" 

7 

8import json 

9 

10from .util import decode_text 

11 

12 

13def resolve(item, resolver): 

14 """ 

15 Given a JSON Schema containing `$ref` keys recursively 

16 evaluate to find and replace referenced files with their 

17 actual values using trimesh.resolvers.Resolver objects. 

18 

19 Parameters 

20 --------------- 

21 item : any 

22 JSON schema including `$ref` to other files 

23 resolver : trimesh.visual.resolver.Resolver 

24 Resolver to fetch referenced assets 

25 

26 Returns 

27 ---------- 

28 result : any 

29 JSONSchema with references replaced 

30 """ 

31 if isinstance(item, list): 

32 # run the resolver on every list item 

33 return [resolve(i, resolver) for i in item] 

34 elif isinstance(item, dict): 

35 if "$ref" in item: 

36 # if we have a reference to a file pop the key 

37 # and update the dict with the reference in-place 

38 raw = decode_text(resolver.get(item.pop("$ref"))) 

39 item.update(json.loads(raw)) 

40 # run the resolver on the dict again 

41 resolve(item, resolver) 

42 else: 

43 # make sure all keys are evaluated 

44 for i in item.values(): 

45 resolve(i, resolver) 

46 return item