1# -*- coding: utf-8 -*-
2"""
3Created on Mon Sep 6 12:04:35 2021
4
5@author: jpeacock
6"""
7# =============================================================================
8# Imports
9# =============================================================================
10from typing import Annotated
11from xml.etree import ElementTree as et
12
13from loguru import logger
14from pydantic import Field
15
16from mt_metadata.base import MetadataBase
17from mt_metadata.transfer_functions.io.emtfxml.metadata import helpers
18
19from . import FieldNotes, Site
20
21
22class RemoteInfo(MetadataBase):
23 site: Annotated[
24 Site,
25 Field(
26 default_factory=Site, # type: ignore
27 description="Site information",
28 alias=None,
29 json_schema_extra={
30 "units": None,
31 "required": True,
32 "examples": ["Site(name='Test Site', location='Test Location')"],
33 },
34 ),
35 ]
36 field_notes: Annotated[
37 FieldNotes,
38 Field(
39 default_factory=FieldNotes, # type: ignore
40 description="Field notes information",
41 alias=None,
42 json_schema_extra={
43 "units": None,
44 "required": True,
45 "examples": ["FieldNotes(...)"],
46 },
47 ),
48 ]
49
50 _order: list = ["site", "field_notes"]
51
52 def read_dict(self, input_dict: dict) -> None:
53 """
54 Read metadata from a dictionary.
55
56 Parameters
57 ----------
58 input_dict : dict
59 A dictionary containing metadata information.
60 """
61 try:
62 remote_info_dict = input_dict["remote_info"]
63 except KeyError:
64 return
65 for key in ["site", "field_notes"]:
66 try:
67 pop_dict = {key: remote_info_dict.pop(key)}
68 getattr(self, key).read_dict(pop_dict)
69 except KeyError:
70 logger.debug(f"No {key} information in xml.")
71 except AttributeError:
72 # This handles the case where the key is not an attribute of the class
73 # or the attribute does not have a read_dict method.
74 logger.warning(
75 f"Failed access {key} from remote_info_dict {remote_info_dict}."
76 )
77 return
78
79 def to_xml(self, string: bool = False, required: bool = True) -> str | et.Element:
80 """
81 Convert the RemoteInfo object to XML format.
82
83 Parameters
84 ----------
85 string : bool, optional
86 Whether to return the XML as a string (default is False).
87 required : bool, optional
88 Whether to include required fields (default is True).
89
90 Returns
91 -------
92 str | et.Element
93 The XML representation of the RemoteInfo object.
94 """
95 return helpers.to_xml(
96 self,
97 string=string,
98 required=required,
99 order=self._order,
100 )