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"""
11# =============================================================================
12# Imports
13# =============================================================================
14from xml.etree import cElementTree as et
15
16from mt_metadata.base import Base, get_schema
17from mt_metadata.base.helpers import element_to_string, write_lines
18from mt_metadata.common.mttime import MTime
19
20from .standards import SCHEMA_FN_PATHS
21
22
23# =============================================================================
24attr_dict = get_schema("comment", SCHEMA_FN_PATHS)
25# =============================================================================
26
27
28class Comment(Base):
29 __doc__ = write_lines(attr_dict)
30
31 def __init__(self, **kwargs):
32 self._dt = MTime()
33 super().__init__(attr_dict=attr_dict, **kwargs)
34
35 @property
36 def date(self):
37 return self._dt.isoformat()
38
39 @date.setter
40 def date(self, dt_str):
41 self._dt.parse(dt_str)
42
43 def read_dict(self, input_dict):
44 """
45
46 :param input_dict: DESCRIPTION
47 :type input_dict: TYPE
48 :return: DESCRIPTION
49 :rtype: TYPE
50
51 """
52 key = input_dict["comments"]
53 if isinstance(key, str):
54 self.value = key
55 elif isinstance(key, dict):
56 try:
57 self.value = key["value"]
58 except KeyError:
59 self.logger.debug("No value in comment")
60
61 try:
62 self.author = key["author"]
63 except KeyError:
64 self.logger.debug("No author of comment")
65 try:
66 self.date = key["date"]
67 except KeyError:
68 self.logger.debug("No date for comment")
69 else:
70 raise TypeError(f"Comment cannot parse type {type(key)}")
71
72 def to_xml(self, string=False, required=True):
73 """ """
74 if self.author is None:
75 self.author = ""
76 root = et.Element(self.__class__.__name__ + "s", {"author": self.author})
77 if self.value is None:
78 self.value = ""
79 root.text = self.value
80
81 if string:
82 return element_to_string(root)
83 return root