Skip to content

Commit ede4875

Browse files
committed
xml parsing working
1 parent effde3d commit ede4875

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/pynxtools_xps/specs/sle/flatten_xml.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ def extract_device_info(elem: ET.Element) -> Dict[str, Any]:
103103

104104
device_settings = extract_devices(elem)
105105

106-
print(device_settings)
107-
108106
return {unique_name: device_settings}
109107

110108

@@ -270,7 +268,7 @@ def flatten_schedule(xml: ET.Element) -> List[Dict[str, Any]]:
270268
return collect
271269

272270

273-
def flatten_context(xml: ET.Element) -> List[Dict[str, Any]]:
271+
def flatten_context(xml: ET.Element) -> Dict[str, Any]:
274272
"""
275273
Flatten the nested XML context, keeping only the needed metadata.
276274
@@ -281,10 +279,13 @@ def flatten_context(xml: ET.Element) -> List[Dict[str, Any]]:
281279
282280
Returns
283281
-------
284-
collect : list
282+
Dict[str, Any]:
285283
Dictionary with device metadata.
286284
287285
"""
286+
if xml is not None:
287+
return {}
288+
288289
device_metadata: Dict[str, Any] = {}
289290

290291
for elem in xml.iter("DeviceContext"):
@@ -304,13 +305,16 @@ def flatten_metainfo(xml: ET.Element) -> List[Dict[str, Any]]:
304305
305306
Returns
306307
-------
307-
collect : list
308+
Dict[str, Any]:
308309
Dictionary with metainfo.
309310
310311
"""
312+
if xml is not None:
313+
return {}
314+
311315
metainfo: Dict[str, Any] = {}
312316

313-
for elem in xml.iter("DeviceContext"):
317+
for elem in xml.iter("Parameter"):
314318
process_xml_element(elem, metainfo)
315319

316320
return metainfo

src/pynxtools_xps/specs/sle/sle_specs.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import struct
3232
import copy
3333
import logging
34-
from typing import Dict, Any, List
34+
from pathlib import Path
35+
from typing import Dict, Any, List, Union
3536
import warnings
3637
from packaging.version import Version, InvalidVersion
3738
import sqlite3
@@ -46,6 +47,7 @@
4647
re_map_keys,
4748
re_map_values,
4849
drop_unused_keys,
50+
update_dict_without_overwrite,
4951
)
5052
from pynxtools_xps.value_mappers import (
5153
convert_energy_type,
@@ -101,12 +103,12 @@ def __init__(self):
101103
SleProdigyParser,
102104
]
103105

104-
self.sql_connection: str
106+
self.file: Union[str, Path] = ""
105107

106108
super().__init__()
107109

108110
def _get_sle_version(self):
109-
con = sqlite3.connect(self.sql_connection)
111+
con = sqlite3.connect(self.file)
110112
query = 'SELECT Value FROM Configuration WHERE Key="Version"'
111113
return execute_sql_query_on_con(con, query)[0][0]
112114

@@ -191,7 +193,7 @@ def is_version_supported(version, supported_version_ranges):
191193
f"Version {version} of SPECS Prodigy is currently not supported."
192194
)
193195

194-
return supporting_parsers[-1] # always use newest parser
196+
return supporting_parsers[-1]() # always use newest parser
195197

196198
def parse_file(self, file: str, **kwargs: Dict[str, Any]) -> Dict[str, Any]:
197199
"""
@@ -212,7 +214,7 @@ def parse_file(self, file: str, **kwargs: Dict[str, Any]) -> Dict[str, Any]:
212214
Dict with parsed data.
213215
214216
"""
215-
self.sql_connection = file
217+
self.file = file
216218
return super().parse_file(file, **kwargs)
217219

218220
def construct_data(self):
@@ -448,7 +450,7 @@ def __init__(self):
448450
self.spectra: List[Dict[str, Any]] = []
449451
self.xml_schedule: ET.Element = None
450452
self.xml_context: ET.Element = None
451-
self.xml_metainfo: ET.Element = None ######## TODO
453+
self.xml_metainfo: ET.Element = None
452454

453455
self.sum_channels: bool = False
454456
self.remove_align: bool = True
@@ -498,7 +500,12 @@ def parse_file(self, file: str, **kwargs: Dict[str, Any]) -> List[Dict[str, Any]
498500
self._get_xml_context()
499501
self._get_xml_metainfo()
500502

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+
502509
self._attach_node_ids()
503510
self._remove_empty_nodes()
504511
self._attach_device_protocols()
@@ -525,7 +532,7 @@ def initiate_file_connection(self, file: str):
525532
sql_connection = file
526533
self.con = sqlite3.connect(sql_connection)
527534

528-
def _execute_sql_query(self, con: sqlite3.Connection, query: str):
535+
def _execute_sql_query(self, query: str):
529536
return execute_sql_query_on_con(self.con, query)
530537

531538
def _get_version(self):
@@ -537,8 +544,11 @@ def _get_app_version(self):
537544
self.app_version = self._execute_sql_query(query)[0][0]
538545

539546
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
542552

543553
def _get_xml_schedule(self):
544554
"""Parse the schedule into an XML object."""
@@ -549,9 +559,8 @@ def _get_xml_context(self):
549559
self.xml_context = self._get_xml_from_key("Context")
550560

551561
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")
555564

556565
def _append_scan_data(self):
557566
"""

0 commit comments

Comments
 (0)