Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ transfer_functions \ io \ emtfxml \ metadata \ instrument.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# -*- coding: utf-8 -*- 

2""" 

3Created on Wed Dec 23 21:30:36 2020 

4 

5:copyright: 

6 Jared Peacock (jpeacock@usgs.gov) 

7 

8:license: MIT 

9 

10""" 

11from typing import Annotated 

12 

13# ============================================================================= 

14# Imports 

15# ============================================================================= 

16from xml.etree import cElementTree as et 

17 

18from pydantic import Field 

19 

20from mt_metadata import NULL_VALUES 

21from mt_metadata.base.helpers import element_to_string 

22from mt_metadata.common import Instrument as CommonInstrument 

23 

24 

25# ============================================================================= 

26class Instrument(CommonInstrument): 

27 settings: Annotated[ 

28 str | None, 

29 Field( 

30 default=None, 

31 description="Settings for the instrument, such as configuration or calibration details.", 

32 alias=None, 

33 json_schema_extra={ 

34 "units": None, 

35 "required": False, 

36 "examples": ["calibration settings", "configuration details"], 

37 }, 

38 ), 

39 ] = None 

40 

41 def to_xml(self, string: bool = False, required: bool = False) -> str | et.Element: 

42 """ 

43 Convert the Instrument object to an XML element or string. 

44 :param string: If True, return as a string; otherwise, return as an XML element. 

45 :type string: bool, optional 

46 :param required: If True, include only required fields; otherwise, include all fields. 

47 :type required: bool, optional 

48 :return: XML representation of the Instrument object. 

49 :rtype: str | et.Element 

50 

51 """ 

52 

53 root = et.Element(self.__class__.__name__) 

54 if self.type not in [None, ""]: 

55 root.attrib["type"] = self.type 

56 

57 for key in ["manufacturer", "name", "id", "settings"]: 

58 value = getattr(self, key) 

59 if value not in NULL_VALUES: 

60 et.SubElement(root, key).text = value 

61 

62 if string: 

63 return element_to_string(root) 

64 return root