import ipaddress
import logging
import re
+from collections import OrderedDict
# Third party modules
-# from fb_tools.common import pp
+from fb_tools.common import pp
from fb_tools.obj import FbGenericBaseObject
# Own modules
_ = XLATOR.gettext
ngettext = XLATOR.ngettext
-__version__ = '0.4.1'
+__version__ = '0.4.2'
LOG = logging.getLogger(__name__)
else:
self._starttls = DataPair.from_str(val)
+ # -------------------------------------------------------------------------
+ def __str__(self):
+ """Typecast into a string object.
+
+ @return: structure as string
+ @rtype: str
+ """
+ return pp(self.as_dict(exportable=True))
+
# -------------------------------------------------------------------------
def __repr__(self):
"""Typecast into a string for reproduction."""
fields = []
- if self.client_host is not None:
- fields.append('client_host={!r}'.format(self.client_host))
- if self.client_addr is not None:
- fields.append('client_addr={!r}'.format(str(self.client_addr)))
- if self.start is not None:
- if isinstance(self.start, datetime.datetime):
- ts = self.start.isoformat(' ')
- else:
- ts = self.start
- fields.append('start={!r}'.format(ts))
- if self.end is not None:
- if isinstance(self.end, datetime.datetime):
- ts = self.end.isoformat(' ')
- else:
- ts = self.end
- fields.append('end={!r}'.format(ts))
- if self.message_id is not None:
- fields.append('message_id={!r}'.format(self.message_id))
- if self.postfix_id is not None:
- fields.append('postfix_id={!r}'.format(self.postfix_id))
- if self.ehlo is not None:
- fields.append('ehlo={!r}'.format(str(self.ehlo)))
- if self.starttls is not None:
- fields.append('starttls={!r}'.format(str(self.starttls)))
- if self.sent_quit is not None:
- fields.append('sent_quit={!r}'.format(str(self.sent_quit)))
- if self.auth is not None:
- fields.append('auth={!r}'.format(str(self.auth)))
- if self.commands is not None:
- fields.append('commands={!r}'.format(str(self.commands)))
- if self.rcpt is not None:
- fields.append('rcpt={!r}'.format(str(self.rcpt)))
+ attr_dict = self.as_dict(exportable=True)
+ for attr in attr_dict.keys():
+ value = attr_dict[attr]
+ if value is None:
+ continue
+ fields.append(f'{attr}={value!r}')
if fields:
out += ', '.join(fields)
return out
# -------------------------------------------------------------------------
- def as_dict(self, short=True):
+ def as_dict(self, short=True, exportable=False):
"""
Transform the elements of the object into a dict.
@return: structure as dict
@rtype: dict
"""
- res = super(PostfixLogchainInfo, self).as_dict(short=short)
-
- res['auth'] = self.auth
- res['client_addr'] = self.client_addr
- res['client_host'] = self.client_host
- res['commands'] = self.commands
- res['duration'] = self.duration
- res['ehlo'] = self.ehlo
- res['end'] = self.end
- res['message_id'] = self.message_id
- res['postfix_id'] = self.postfix_id
- res['rcpt'] = self.rcpt
- res['sent_quit'] = self.sent_quit
- res['start'] = self.start
- res['starttls'] = self.starttls
+ if exportable:
+ res = OrderedDict()
+ else:
+ res = super(PostfixLogchainInfo, self).as_dict(short=short)
+
+ atribs = (
+ 'client_host', 'client_addr', 'start', 'end', 'message_id', 'postfix_id', 'ehlo',
+ 'starttls', 'sent_quit', 'auth', 'commands', 'rcpt')
+ for attrib in atribs:
+ if not hasattr(self, attrib):
+ continue
+ value = getattr(self, attrib, None)
+ if value is None:
+ res[attrib] = None
+ continue
+ if isinstance(value, ipaddress._BaseAddress):
+ res[attrib] = str(value) if exportable else value
+ continue
+ if isinstance(value, datetime.datetime):
+ res[attrib] = value.isoformat(' ') if exportable else value
+ continue
+ if isinstance(value, DataPair):
+ res[attrib] = str(value) if exportable else value
+ continue
+
+ # Catch all
+ res[attrib] = value
+
+ # Non init properties
+ if not exportable:
+ res['duration'] = self.duration
return res
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib'))
sys.path.insert(0, libdir)
-from general import PpAdminToolsTestcase, get_arg_verbose, init_root_logger
-
# from fb_tools.common import pp, to_str, is_sequence
+from fb_tools.common import pp
+
+from general import PpAdminToolsTestcase, get_arg_verbose, init_root_logger
APPNAME = 'test-postfix-chain'
LOG = logging.getLogger(APPNAME)
chain = PostfixLogchainInfo()
LOG.debug('PostfixLogchainInfo %r: {!r}'.format(chain))
LOG.debug('PostfixLogchainInfo %s:\n{}'.format(chain))
+ LOG.debug('PostfixLogchainInfo as_dict():\n{}'.format(pp(chain.as_dict())))
# -------------------------------------------------------------------------
def test_filled_object(self):
)
LOG.debug('PostfixLogchainInfo %r: {!r}'.format(chain))
LOG.debug('PostfixLogchainInfo %s:\n{}'.format(chain))
+ LOG.debug('PostfixLogchainInfo as_dict():\n{}'.format(pp(chain.as_dict())))
# =============================================================================