2
2
import ast
3
3
import json
4
4
import os
5
- from typing import Any , Dict , Generator , List , Tuple
5
+ from typing import Any , Generator
6
6
7
7
8
8
from ocp_resources .resource import Resource # noqa
9
9
10
+ # Cache for resources definitions to avoid reloading the large JSON file
11
+ _RESOURCES_DEFINITIONS_CACHE : dict [str , Any ] = {}
12
+
10
13
11
14
def _get_api_value_by_type (api_value : str , api_type : str ) -> str :
12
15
try :
@@ -23,9 +26,8 @@ def _get_api_value_by_type(api_value: str, api_type: str) -> str:
23
26
return api_value
24
27
25
28
26
- def _get_api_group_and_version (bodies : List [Any ]) -> Tuple [str , str ]:
29
+ def _get_api_group_and_version (bodies : list [Any ]) -> tuple [str , str ]:
27
30
for targets in bodies :
28
- targets : ast .AnnAssign | ast .Attribute
29
31
if isinstance (targets , ast .AnnAssign ):
30
32
api_type = targets .target .id
31
33
else :
@@ -39,11 +41,11 @@ def _get_api_group_and_version(bodies: List[Any]) -> Tuple[str, str]:
39
41
40
42
def validate_resource (
41
43
cls : ast .ClassDef ,
42
- resource_list : List [ Dict [str , Any ]],
44
+ resource_list : list [ dict [str , Any ]],
43
45
api_value : str ,
44
46
api_type : str ,
45
- ) -> List [str ]:
46
- errors : List [str ] = []
47
+ ) -> list [str ]:
48
+ errors : list [str ] = []
47
49
resource_str : str = "Resource"
48
50
namespaced_resource_str : str = "NamespacedResource"
49
51
_base_class_error : str = f"Resource { cls .name } must have { resource_str } or { namespaced_resource_str } base class"
@@ -66,7 +68,7 @@ def validate_resource(
66
68
67
69
namespaced_based : bool = base_class_from [0 ].id == namespaced_resource_str
68
70
api_value_name = _get_api_value_by_type (api_value = api_value , api_type = api_type )
69
- matched_resource : Dict [str , Any ] = {}
71
+ matched_resource : dict [str , Any ] = {}
70
72
71
73
for resource_dict in resource_list :
72
74
_x_kind = resource_dict ["x-kubernetes-group-version-kind" ]
@@ -113,8 +115,8 @@ def resource_file() -> Generator[str, None, None]:
113
115
yield os .path .join (root , _file )
114
116
115
117
116
- def parse_resource_file_for_errors (data ) -> List [str ]:
117
- errors : List [str ] = []
118
+ def parse_resource_file_for_errors (data ) -> list [str ]:
119
+ errors : list [str ] = []
118
120
_resources_definitions = resources_definitions ()
119
121
120
122
if data .startswith (
@@ -147,6 +149,9 @@ def parse_resource_file_for_errors(data) -> List[str]:
147
149
return errors
148
150
149
151
150
- def resources_definitions () -> Dict [str , Any ]:
151
- with open ("class_generator/schema/__resources-mappings.json" ) as fd :
152
- return json .load (fd )
152
+ def resources_definitions () -> dict [str , Any ]:
153
+ global _RESOURCES_DEFINITIONS_CACHE
154
+ if not _RESOURCES_DEFINITIONS_CACHE :
155
+ with open ("class_generator/schema/__resources-mappings.json" ) as fd :
156
+ _RESOURCES_DEFINITIONS_CACHE = json .load (fd )
157
+ return _RESOURCES_DEFINITIONS_CACHE
0 commit comments