Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Flake8 complaints and datetime imports #65

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions posttroll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

"""Posttroll packages."""

import datetime as dt
import logging
import os
import sys
from datetime import datetime

import zmq
from donfig import Config
Expand Down Expand Up @@ -58,7 +58,7 @@ def strp_isoformat(strg):

We handle input like: 2011-11-14T12:51:25.123456
"""
if isinstance(strg, datetime):
if isinstance(strg, dt.datetime):
return strg
if len(strg) < 19 or len(strg) > 26:
if len(strg) > 30:
Expand All @@ -67,10 +67,10 @@ def strp_isoformat(strg):
if strg.find(".") == -1:
strg += '.000000'
if sys.version[0:3] >= '2.6':
return datetime.strptime(strg, "%Y-%m-%dT%H:%M:%S.%f")
return dt.datetime.strptime(strg, "%Y-%m-%dT%H:%M:%S.%f")
else:
dat, mis = strg.split(".")
dat = datetime.strptime(dat, "%Y-%m-%dT%H:%M:%S")
dat = dt.datetime.strptime(dat, "%Y-%m-%dT%H:%M:%S")
mis = int(float('.' + mis)*1000000)
return dat.replace(microsecond=mis)

Expand Down
27 changes: 14 additions & 13 deletions posttroll/address_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
/<server-name>/address info ... host:port
"""
import copy
import datetime as dt
import errno
import logging
import os
import threading
import errno
import time

from datetime import datetime, timedelta

import netifaces
from zmq import REP, LINGER

Expand All @@ -53,11 +52,12 @@

default_publish_port = 16543

ten_minutes = timedelta(minutes=10)
zero_seconds = timedelta(seconds=0)
ten_minutes = dt.timedelta(minutes=10)
zero_seconds = dt.timedelta(seconds=0)


def get_local_ips():
"""Get local IP addresses."""
inet_addrs = [netifaces.ifaddresses(iface).get(netifaces.AF_INET)
for iface in netifaces.interfaces()]
ips = []
Expand All @@ -79,14 +79,15 @@ class AddressReceiver(object):

def __init__(self, max_age=ten_minutes, port=None,
do_heartbeat=True, multicast_enabled=True, restrict_to_localhost=False):
"""Initialize addres receiver."""
self._max_age = max_age
self._port = port or default_publish_port
self._address_lock = threading.Lock()
self._addresses = {}
self._subject = '/address'
self._do_heartbeat = do_heartbeat
self._multicast_enabled = multicast_enabled
self._last_age_check = datetime(1900, 1, 1)
self._last_age_check = dt.datetime(1900, 1, 1)
self._do_run = False
self._is_running = False
self._thread = threading.Thread(target=self._run)
Expand Down Expand Up @@ -125,11 +126,11 @@ def get(self, name=""):

def _check_age(self, pub, min_interval=zero_seconds):
"""Check the age of the receiver."""
now = datetime.utcnow()
now = dt.datetime.utcnow()
if (now - self._last_age_check) <= min_interval:
return

LOGGER.debug("%s - checking addresses", str(datetime.utcnow()))
LOGGER.debug("%s - checking addresses", str(dt.datetime.utcnow()))
self._last_age_check = now
to_del = []
with self._address_lock:
Expand Down Expand Up @@ -196,8 +197,7 @@ def _run(self):
pub.heartbeat(min_interval=29)
msg = Message.decode(data)
name = msg.subject.split("/")[1]
if(msg.type == 'info' and
msg.subject.lower().startswith(self._subject)):
if msg.type == 'info' and msg.subject.lower().startswith(self._subject):
addr = msg.data["URI"]
msg.data['status'] = True
metadata = copy.copy(msg.data)
Expand All @@ -217,20 +217,21 @@ def _run(self):
def _add(self, adr, metadata):
"""Add an address."""
with self._address_lock:
metadata["receive_time"] = datetime.utcnow()
metadata["receive_time"] = dt.datetime.utcnow()
self._addresses[adr] = metadata


class _SimpleReceiver(object):

""" Simple listing on port for address messages."""
"""Simple listing on port for address messages."""

def __init__(self, port=None):
"""Initialize receiver."""
self._port = port or default_publish_port
self._socket = get_context().socket(REP)
self._socket.bind("tcp://*:" + str(port))

def __call__(self):
"""Receive and return a message."""
message = self._socket.recv_string()
self._socket.send_string("ok")
return message, None
Expand Down
22 changes: 12 additions & 10 deletions posttroll/bbmcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class MulticastSender(object):
"""Multicast sender on *port* and *mcgroup*."""

def __init__(self, port, mcgroup=MC_GROUP):
"""Initialize multicast sending."""
self.port = port
self.group = mcgroup
self.socket, self.group = mcast_sender(mcgroup)
logger.debug('Started multicast group %s', mcgroup)

def __call__(self, data):
"""Send data to a socket."""
self.socket.sendto(data.encode(), (self.group, self.port))

def close(self):
Expand All @@ -76,16 +78,14 @@ def close(self):


def mcast_sender(mcgroup=MC_GROUP):
"""Non-object interface for sending multicast messages.
"""
"""Non-object interface for sending multicast messages."""
sock = socket(AF_INET, SOCK_DGRAM)
try:
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
if _is_broadcast_group(mcgroup):
group = '<broadcast>'
sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
elif((int(mcgroup.split(".")[0]) > 239) or
(int(mcgroup.split(".")[0]) < 224)):
elif int(mcgroup.split(".")[0]) > 239 or int(mcgroup.split(".")[0]) < 224:
raise IOError("Invalid multicast address.")
else:
group = mcgroup
Expand All @@ -105,20 +105,25 @@ def mcast_sender(mcgroup=MC_GROUP):

class MulticastReceiver(object):
"""Multicast receiver on *port* for an *mcgroup*."""

BUFSIZE = 1024

def __init__(self, port, mcgroup=MC_GROUP):
"""Initialize multicast receiver."""
# Note: a multicast receiver will also receive broadcast on same port.
self.port = port
self.socket, self.group = mcast_receiver(port, mcgroup)

def settimeout(self, tout=None):
"""A timeout will throw a 'socket.timeout'.
"""Set timeout.

A timeout will throw a 'socket.timeout'.
"""
self.socket.settimeout(tout)
return self

def __call__(self):
"""Receive data from a socket."""
data, sender = self.socket.recvfrom(self.BUFSIZE)
return data.decode(), sender

Expand All @@ -131,9 +136,7 @@ def close(self):


def mcast_receiver(port, mcgroup=MC_GROUP):
"""Open a UDP socket, bind it to a port and select a multicast group.
"""

"""Open a UDP socket, bind it to a port and select a multicast group."""
if _is_broadcast_group(mcgroup):
group = None
else:
Expand Down Expand Up @@ -184,8 +187,7 @@ def mcast_receiver(port, mcgroup=MC_GROUP):


def _is_broadcast_group(group):
"""Check if *group* is a valid multicasting group.
"""
"""Check if *group* is a valid multicasting group."""
if not group or gethostbyname(group) in ('0.0.0.0', '255.255.255.255'):
return True
return False
56 changes: 24 additions & 32 deletions posttroll/logger.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2012, 2014, 2015 Martin Raspaud

#
# Author(s):

#
# Martin Raspaud <[email protected]>

#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Logger for pytroll system.
"""
"""Logger module for Posttroll."""


# TODO: remove old hanging subscriptions
Expand All @@ -39,35 +38,36 @@


class PytrollFormatter(logging.Formatter):

"""Formats a pytroll message inside a log record.
"""
"""Formats a pytroll message inside a log record."""

def __init__(self, fmt, datefmt):
"""Initialize formatter."""
logging.Formatter.__init__(self, fmt, datefmt)

def format(self, record):
"""Format the message."""
subject = "/".join(("log", record.levelname, str(record.name)))
mesg = Message(subject, "log." + str(record.levelname).lower(),
record.getMessage())
return str(mesg)


class PytrollHandler(logging.Handler):

"""Sends the record through a pytroll publisher.
"""
"""Sends the record through a pytroll publisher."""

def __init__(self, name, port=0):
"""Initialize the handler."""
logging.Handler.__init__(self)
self._publisher = NoisyPublisher(name, port)
self._publisher.start()

def emit(self, record):
"""Emit the message."""
message = self.format(record)
self._publisher.send(message)

def close(self):
"""Close the handler."""
self._publisher.stop()
logging.Handler.close(self)

Expand All @@ -87,15 +87,15 @@ def close(self):


class ColoredFormatter(logging.Formatter):

"""Adds a color for the levelname.
"""
"""Adds a color for the levelname."""

def __init__(self, msg, use_color=True):
"""Initialize the colored formatter."""
logging.Formatter.__init__(self, msg)
self.use_color = use_color

def format(self, record):
"""Format the message."""
levelname = record.levelname
if self.use_color and levelname in COLORS:
levelname_color = (COLOR_SEQ % (30 + COLORS[levelname])
Expand All @@ -105,30 +105,24 @@ def format(self, record):
return logging.Formatter.format(self, record2)


# logging.basicConfig(format='[%(asctime)s %(levelname)s] %(message)s',
# level=logging.DEBUG)


class Logger(object):

"""The logging machine.

Contains a thread listening to incomming messages, and a thread logging.
"""

def __init__(self, nameserver_address="localhost", nameserver_port=16543):
"""Initialize the logger."""
del nameserver_address, nameserver_port
self.log_thread = Thread(target=self.log)
self.loop = True

def start(self):
"""Starts the logging.
"""
"""Start the logging."""
self.log_thread.start()

def log(self):
"""Log stuff.
"""
"""Log stuff."""
with Subscribe(services=[""], addr_listener=True) as sub:
for msg in sub.recv(1):
if msg:
Expand Down Expand Up @@ -156,14 +150,12 @@ def log(self):
break

def stop(self):
"""Stop the machine.
"""
"""Stop the machine."""
self.loop = False


def run():
"""Main function
"""
"""Run the logger."""
import argparse

global LOGGER
Expand Down Expand Up @@ -209,8 +201,8 @@ def run():
time.sleep(1)
except KeyboardInterrupt:
tlogger.stop()
print("Thanks for using pytroll/logger. "
"See you soon on www.pytroll.org!")
print("Thanks for using pytroll/logger. See you soon on www.pytroll.org!")


if __name__ == '__main__':
run()
Loading