2
2
AUTOSAR XML Workspace
3
3
"""
4
4
import posixpath
5
+ import os
5
6
from typing import Any
6
7
import autosar .base as ar_base
7
8
import autosar .xml .element as ar_element
8
9
import autosar .xml .enumeration as ar_enum
9
10
import autosar .xml .template as ar_template
10
11
import autosar .xml .document as ar_document
11
12
from autosar .xml .writer import Writer
13
+ try :
14
+ import tomllib
15
+ except ModuleNotFoundError :
16
+ import tomli as tomllib
17
+
18
+
19
+ class DocumentConfig :
20
+ """
21
+ Internal class used during document creation
22
+ """
23
+
24
+ def __init__ (self , file_path : str , package_refs : list [str ] | str | None = None ) -> None :
25
+ self .document = ar_document .Document ()
26
+ self .file_path = file_path
27
+ self .package_refs : list [str ] = []
28
+ if isinstance (package_refs , str ):
29
+ self .package_refs .append (package_refs )
30
+ else :
31
+ for package_ref in package_refs :
32
+ assert isinstance (package_ref , str )
33
+ self .package_refs .append (package_ref )
12
34
13
35
14
36
class Namespace :
@@ -35,11 +57,14 @@ class Workspace:
35
57
Workspace
36
58
"""
37
59
38
- def __init__ (self , config_file_path : str | None = None ) -> None :
60
+ def __init__ (self , config_file_path : str | None = None , document_root : str | None = None ) -> None :
39
61
self .namespaces : dict [str , Namespace ] = {}
40
62
self .packages : list [ar_element .Package ] = []
41
63
self ._package_map : dict [str , ar_element .Package ] = {}
42
- self .documents : list [tuple (ar_document .Document , str )] = []
64
+ self .documents : list [DocumentConfig ] = []
65
+ self .document_root = document_root
66
+ if config_file_path is not None :
67
+ self .load_config (config_file_path )
43
68
44
69
def create_namespace (self , name : str , package_map : dict [str , str ], base_ref : str | None = None ) -> None :
45
70
"""
@@ -138,35 +163,68 @@ def apply(self, template: Any, **kwargs) -> Any:
138
163
else :
139
164
raise NotImplementedError (f"Unknown template type: { str (type (template ))} " )
140
165
141
- def create_document (self , file_path : str , packages : str | list [str ]) -> ar_document . Document :
166
+ def create_document (self , file_path : str , packages : str | list [str ] | None = None ) -> None :
142
167
"""
143
168
Creates a new document object and appends one or more packages to it.
144
169
Use the write_documents method to write documents to file system
145
170
"""
146
- document = ar_document .Document ()
147
- if isinstance (packages , str ):
148
- package = self .find (packages )
149
- if package is None :
150
- raise ValueError (f"Invalid package reference: '{ packages } '" )
151
- document .append (package )
152
- else :
153
- for package_ref in packages :
154
- package = self .find (package_ref )
155
- if package is None :
156
- raise ValueError (f"Invalid package reference: '{ package_ref } '" )
157
- document .append (package )
158
- self .documents .append ((document , file_path ))
159
- return document
171
+ self .documents .append (DocumentConfig (file_path , packages ))
172
+
173
+ def set_document_root (self , directory : str ) -> None :
174
+ """
175
+ Sets root directory where documents are written
176
+ """
177
+ self .document_root = directory
160
178
161
179
def write_documents (self , scehema_version = ar_base .DEFAULT_SCHEMA_VERSION ) -> None :
162
180
"""
163
181
Writes all documents to file system
164
182
"""
165
183
writer = Writer ()
166
- for (document , file_path ) in self .documents :
184
+ for document_config in self .documents :
185
+ document = document_config .document
186
+ file_path = document_config .file_path
187
+ package_refs = document_config .package_refs
167
188
document .schema_version = scehema_version
189
+ for package_ref in package_refs :
190
+ package = self .find (package_ref )
191
+ if package is not None :
192
+ if not isinstance (package , ar_element .Package ):
193
+ raise ValueError (f"'{ package_ref } ' does not reference a package element" )
194
+ document .append (package )
195
+ if self .document_root is not None :
196
+ file_path = os .path .join (self .document_root , file_path )
168
197
writer .write_file (document , file_path )
169
198
199
+ def load_config (self , file_path : str ) -> None :
200
+ """
201
+ Loads (.toml) config file into workspace
202
+ """
203
+ with open (file_path , "rb" ) as fp : # pylint: disable=C0103
204
+ config = tomllib .load (fp )
205
+ namespace = config .get ("namespace" , None )
206
+ if namespace is not None :
207
+ for name , ns_config in namespace .items ():
208
+ self ._create_namespace_from_config (name , ns_config )
209
+ document = config .get ("document" , None )
210
+ if document is not None :
211
+ for name , doc_config in document .items ():
212
+ self ._create_document_from_config (name , doc_config )
213
+
214
+ def _create_namespace_from_config (self , name : str , config : dict ):
215
+ base_ref = None
216
+ package_map = {}
217
+ for key , value in config .items ():
218
+ if key == "base_ref" :
219
+ base_ref = value
220
+ else :
221
+ package_map [key ] = value
222
+ self .create_namespace (name , package_map , base_ref )
223
+
224
+ def _create_document_from_config (self , name : str , config : str | list [str ]):
225
+ file_name = f"{ name } .arxml"
226
+ self .create_document (file_name , config .get ("packages" , None ))
227
+
170
228
def _apply_element_template (self , template : ar_template .ElementTemplate , kwargs : dict ) -> ar_element .ARElement :
171
229
"""
172
230
Wrapper for element templates.
0 commit comments