31
31
import struct
32
32
import copy
33
33
import logging
34
- from typing import Dict , Any , List
34
+ from pathlib import Path
35
+ from typing import Dict , Any , List , Union
35
36
import warnings
36
37
from packaging .version import Version , InvalidVersion
37
38
import sqlite3
46
47
re_map_keys ,
47
48
re_map_values ,
48
49
drop_unused_keys ,
50
+ update_dict_without_overwrite ,
49
51
)
50
52
from pynxtools_xps .value_mappers import (
51
53
convert_energy_type ,
@@ -101,12 +103,12 @@ def __init__(self):
101
103
SleProdigyParser ,
102
104
]
103
105
104
- self .sql_connection : str
106
+ self .file : Union [ str , Path ] = ""
105
107
106
108
super ().__init__ ()
107
109
108
110
def _get_sle_version (self ):
109
- con = sqlite3 .connect (self .sql_connection )
111
+ con = sqlite3 .connect (self .file )
110
112
query = 'SELECT Value FROM Configuration WHERE Key="Version"'
111
113
return execute_sql_query_on_con (con , query )[0 ][0 ]
112
114
@@ -191,7 +193,7 @@ def is_version_supported(version, supported_version_ranges):
191
193
f"Version { version } of SPECS Prodigy is currently not supported."
192
194
)
193
195
194
- return supporting_parsers [- 1 ] # always use newest parser
196
+ return supporting_parsers [- 1 ]() # always use newest parser
195
197
196
198
def parse_file (self , file : str , ** kwargs : Dict [str , Any ]) -> Dict [str , Any ]:
197
199
"""
@@ -212,7 +214,7 @@ def parse_file(self, file: str, **kwargs: Dict[str, Any]) -> Dict[str, Any]:
212
214
Dict with parsed data.
213
215
214
216
"""
215
- self .sql_connection = file
217
+ self .file = file
216
218
return super ().parse_file (file , ** kwargs )
217
219
218
220
def construct_data (self ):
@@ -448,7 +450,7 @@ def __init__(self):
448
450
self .spectra : List [Dict [str , Any ]] = []
449
451
self .xml_schedule : ET .Element = None
450
452
self .xml_context : ET .Element = None
451
- self .xml_metainfo : ET .Element = None ######## TODO
453
+ self .xml_metainfo : ET .Element = None
452
454
453
455
self .sum_channels : bool = False
454
456
self .remove_align : bool = True
@@ -498,7 +500,12 @@ def parse_file(self, file: str, **kwargs: Dict[str, Any]) -> List[Dict[str, Any]
498
500
self ._get_xml_context ()
499
501
self ._get_xml_metainfo ()
500
502
501
- self .spectra = flatten_schedule (self .xml )
503
+ self .spectra = flatten_schedule (self .xml_schedule )
504
+
505
+ for spectrum in self .spectra :
506
+ update_dict_without_overwrite (spectrum , flatten_context (self .xml_context ))
507
+ update_dict_without_overwrite (spectrum , flatten_metainfo (self .xml_metainfo ))
508
+
502
509
self ._attach_node_ids ()
503
510
self ._remove_empty_nodes ()
504
511
self ._attach_device_protocols ()
@@ -525,7 +532,7 @@ def initiate_file_connection(self, file: str):
525
532
sql_connection = file
526
533
self .con = sqlite3 .connect (sql_connection )
527
534
528
- def _execute_sql_query (self , con : sqlite3 . Connection , query : str ):
535
+ def _execute_sql_query (self , query : str ):
529
536
return execute_sql_query_on_con (self .con , query )
530
537
531
538
def _get_version (self ):
@@ -537,8 +544,11 @@ def _get_app_version(self):
537
544
self .app_version = self ._execute_sql_query (query )[0 ][0 ]
538
545
539
546
def _get_xml_from_key (self , key : str ):
540
- query = f"SELECT Value FROM Configuration WHERE Key={ key } "
541
- return ET .fromstring (self ._execute_sql_query (query )[0 ][0 ])
547
+ query = f"SELECT Value FROM Configuration WHERE Key='{ key } '"
548
+ try :
549
+ return ET .fromstring (self ._execute_sql_query (query )[0 ][0 ])
550
+ except IndexError :
551
+ return None
542
552
543
553
def _get_xml_schedule (self ):
544
554
"""Parse the schedule into an XML object."""
@@ -549,9 +559,8 @@ def _get_xml_context(self):
549
559
self .xml_context = self ._get_xml_from_key ("Context" )
550
560
551
561
def _get_xml_metainfo (self ):
552
- XML = self ._get_xml_from_key ("MetaInfo" )
553
- for i in XML .iter ("Parameter" ):
554
- self .metainfo [i .attrib ["name" ].replace (" " , "_" )] = i .text
562
+ """Parse the metainfo into an XML object."""
563
+ self .xml_metainfo = self ._get_xml_from_key ("MetaInfo" )
555
564
556
565
def _append_scan_data (self ):
557
566
"""
0 commit comments