Commit 7c64cf0f authored by Morten Brekkevold's avatar Morten Brekkevold

format IP addresses in Slack output

also ditch IPy in favor of ipaddress, which is built-in in Python 3
parent a9b505b2
......@@ -11,12 +11,12 @@ import os
import socket
from collections import defaultdict
import logging
from ipaddress import ip_address
from operator import attrgetter, itemgetter
import requests
import argparse
from IPy import IP
from slack_log_handler import SlackLogHandler
from kindnavsync.navapi import NAVAPI, ObjectNotFound
......@@ -133,12 +133,25 @@ class SlackFormatter(logging.Formatter):
if isinstance(arg, (KindDevice, NAVBox)):
return "<{url}|{text}>".format(url=arg.get_absolute_url(),
text=str(arg))
elif (isinstance(arg, list) and
any(isinstance(i, (KindDevice, NAVBox)) for i in arg)):
newarg = [cls._format_arg(i) for i in arg]
return '\n'.join(" • {}".format(s) for s in newarg)
else:
return arg
elif isinstance(arg, str) and _is_valid_ip(arg):
return "`{}`".format(arg)
elif isinstance(arg, list):
if any(isinstance(i, (KindDevice, NAVBox)) for i in arg):
newarg = [cls._format_arg(i) for i in arg]
return '\n'.join(" • {}".format(s) for s in newarg)
elif any(_is_valid_ip(i) for i in arg):
newarg = [cls._format_arg(i) for i in arg]
return ', '.join(newarg)
return arg
def _is_valid_ip(addr):
try:
ip_address(addr)
return True
except ValueError:
return False
def get_kind_data():
......@@ -228,7 +241,8 @@ def lookup_dns(names):
except socket.gaierror:
pass
else:
result[name] = [a for a in addresslist if IP(a).version() == 4]
result[name] = [a for a in addresslist
if ip_address(a).version == 4]
return dict(result)
......
......@@ -13,7 +13,6 @@ setup(
scripts=['bin/kindnavsync', 'bin/kindnavsyncdiff'],
packages=['kindnavsync'],
install_requires=[
'IPy>=0.81',
'requests',
'slack_log_handler',
],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment