From c466e74a081cda57fd332c8182e6786ba0accdce Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Mon, 22 Apr 2024 10:15:36 +0300 Subject: [PATCH 1/3] Fix flake8 warnings --- posttroll/address_receiver.py | 10 ++-- posttroll/bbmcast.py | 22 +++++---- posttroll/logger.py | 56 +++++++++------------ posttroll/message.py | 84 +++++++++++++++----------------- posttroll/message_broadcaster.py | 31 +++++++----- posttroll/ns.py | 32 +++++++----- posttroll/subscriber.py | 2 +- posttroll/testing.py | 1 + posttroll/tests/test_message.py | 32 +++++------- posttroll/tests/test_testing.py | 1 + 10 files changed, 135 insertions(+), 136 deletions(-) diff --git a/posttroll/address_receiver.py b/posttroll/address_receiver.py index 27106ef..dc6112c 100644 --- a/posttroll/address_receiver.py +++ b/posttroll/address_receiver.py @@ -58,6 +58,7 @@ def get_local_ips(): + """Get local IP addresses.""" inet_addrs = [netifaces.ifaddresses(iface).get(netifaces.AF_INET) for iface in netifaces.interfaces()] ips = [] @@ -79,6 +80,7 @@ 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() @@ -196,8 +198,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) @@ -222,15 +223,16 @@ def _add(self, 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 diff --git a/posttroll/bbmcast.py b/posttroll/bbmcast.py index 358eff5..3aa6b70 100644 --- a/posttroll/bbmcast.py +++ b/posttroll/bbmcast.py @@ -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): @@ -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 = '' 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 @@ -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 @@ -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: @@ -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 diff --git a/posttroll/logger.py b/posttroll/logger.py index 22db60f..31fd152 100644 --- a/posttroll/logger.py +++ b/posttroll/logger.py @@ -1,27 +1,26 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +# # Copyright (c) 2012, 2014, 2015 Martin Raspaud - +# # Author(s): - +# # Martin Raspaud - +# # 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 . -"""Logger for pytroll system. -""" +"""Logger module for Posttroll.""" # TODO: remove old hanging subscriptions @@ -39,14 +38,14 @@ 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()) @@ -54,20 +53,21 @@ def format(self, record): 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) @@ -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]) @@ -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: @@ -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 @@ -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() diff --git a/posttroll/message.py b/posttroll/message.py index 9ff6958..1998377 100644 --- a/posttroll/message.py +++ b/posttroll/message.py @@ -23,8 +23,9 @@ # You should have received a copy of the GNU General Public License along with # pytroll. If not, see . -"""A Message goes like: - [mime-type data] +"""Module for Pytroll messages. + +A Message is formatted as: [mime-type data] :: @@ -34,12 +35,11 @@ pytroll://DC/juhu info henry@prodsat 2010-12-01T12:21:11.123456 v1.01 application/json "jhuuuu !!!" -Note: It's not optimized for BIG messages. +Note: the Message class is not optimized for BIG messages. """ import re from datetime import datetime -import _strptime try: import json except ImportError: @@ -52,11 +52,11 @@ class MessageError(Exception): + """This modules exceptions.""" - """This modules exceptions. - """ pass + # ----------------------------------------------------------------------------- # # Utillities. @@ -65,34 +65,39 @@ class MessageError(Exception): def is_valid_subject(obj): - """Currently we only check for empty strings. + """Check that the message subject is valid. + + Currently we only check for empty strings. """ return isinstance(obj, str) and bool(obj) def is_valid_type(obj): - """Currently we only check for empty strings. + """Check that the message type is valid. + + Currently we only check for empty strings. """ return isinstance(obj, str) and bool(obj) def is_valid_sender(obj): - """Currently we only check for empty strings. + """Check that the sender is valid. + + Currently we only check for empty strings. """ return isinstance(obj, str) and bool(obj) def is_valid_data(obj): - """Check if data is JSON serializable. - """ + """Check if data is JSON serializable.""" if obj: try: - tmp = json.dumps(obj, default=datetime_encoder) - del tmp + _ = json.dumps(obj, default=datetime_encoder) except (TypeError, UnicodeDecodeError): return False return True + # ----------------------------------------------------------------------------- # # Message class. @@ -101,7 +106,6 @@ def is_valid_data(obj): class Message(object): - """A Message. - Has to be initialized with a *rawstr* (encoded message to decode) OR @@ -113,9 +117,7 @@ class Message(object): """ def __init__(self, subject='', atype='', data='', binary=False, rawstr=None): - """Initialize a Message from a subject, type and data... - or from a raw string. - """ + """Initialize a Message from a subject, type and data, or from a raw string.""" if rawstr: self.__dict__ = _decode(rawstr) else: @@ -137,8 +139,7 @@ def __init__(self, subject='', atype='', data='', binary=False, rawstr=None): @property def user(self): - """Try to return a user from a sender. - """ + """Try to return a user from a sender.""" try: return self.sender[:self.sender.index('@')] except ValueError: @@ -146,8 +147,7 @@ def user(self): @property def host(self): - """Try to return a host from a sender. - """ + """Try to return a host from a sender.""" try: return self.sender[self.sender.index('@') + 1:] except ValueError: @@ -155,39 +155,37 @@ def host(self): @property def head(self): - """Return header of a message (a message without the data part). - """ + """Return header of a message (a message without the data part).""" self._validate() return _encode(self, head=True) @staticmethod def decode(rawstr): - """Decode a raw string into a Message. - """ + """Decode a raw string into a Message.""" return Message(rawstr=rawstr) def encode(self): - """Encode a Message to a raw string. - """ + """Encode a Message to a raw string.""" self._validate() return _encode(self, binary=self.binary) def __repr__(self): + """Return the textual representation of the Message.""" return self.encode() def __unicode__(self): + """Return the unicode representation of the Message.""" return self.encode() def __str__(self): + """Return the human readable representation of the Message.""" try: return unicode(self).encode('utf-8') except NameError: return self.encode() - def _validate(self): - """Validate a messages attributes. - """ + """Validate a messages attributes.""" if not is_valid_subject(self.subject): raise MessageError("Invalid subject: '%s'" % self.subject) if not is_valid_type(self.type): @@ -198,16 +196,16 @@ def _validate(self): raise MessageError("Invalid data: data is not JSON serializable: %s" % str(self.data)) - # - # Make it pickleable. - # def __getstate__(self): + """Get the Message state for pickle().""" return self.encode() def __setstate__(self, state): + """Set the Message when unpickling.""" self.__dict__.clear() self.__dict__ = _decode(state) + # ----------------------------------------------------------------------------- # # Decode / encode @@ -216,14 +214,12 @@ def __setstate__(self, state): def _is_valid_version(version): - """Check version. - """ + """Check version.""" return version == _VERSION def datetime_decoder(dct): - """Decode datetimes to python objects. - """ + """Decode datetimes to python objects.""" if isinstance(dct, list): pairs = enumerate(dct) elif isinstance(dct, dict): @@ -245,8 +241,7 @@ def datetime_decoder(dct): def _decode(rawstr): - """Convert a raw string to a Message. - """ + """Convert a raw string to a Message.""" # Check for the magick word. try: rawstr = rawstr.decode('utf-8') @@ -307,8 +302,7 @@ def _decode(rawstr): def datetime_encoder(obj): - """Encodes datetimes into iso format. - """ + """Encode datetimes into iso format.""" try: return obj.isoformat() except AttributeError: @@ -316,8 +310,7 @@ def datetime_encoder(obj): def _encode(msg, head=False, binary=False): - """Convert a Message to a raw string. - """ + """Convert a Message to a raw string.""" rawstr = str(_MAGICK) + u"{0:s} {1:s} {2:s} {3:s} {4:s}".format( msg.subject, msg.type, msg.sender, msg.time.isoformat(), msg.version) @@ -334,6 +327,7 @@ def _encode(msg, head=False, binary=False): 'binary/octet-stream' + ' ' + msg.data) return rawstr + # ----------------------------------------------------------------------------- # # Small internal helpers. @@ -343,8 +337,8 @@ def _encode(msg, head=False, binary=False): def _getsender(): """Return local sender. - Don't use the getpass module, it looks at various environment variables - and is unreliable. + + Don't use the getpass module, it looks at various environment variables and is unreliable. """ import os import pwd diff --git a/posttroll/message_broadcaster.py b/posttroll/message_broadcaster.py index 53bfe52..32103c8 100644 --- a/posttroll/message_broadcaster.py +++ b/posttroll/message_broadcaster.py @@ -1,26 +1,28 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2010-2012, 2014, 2015. - +# # Author(s): - +# # Lars Ø. Rasmussen # Martin Raspaud - +# # This file is part of pytroll. - +# # Pytroll 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. - +# # Pytroll 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 # pytroll. If not, see . +"""Message broadcast module.""" + import time import threading import logging @@ -42,11 +44,12 @@ class DesignatedReceiversSender(object): """Sends message to multiple *receivers* on *port*.""" def __init__(self, default_port, receivers): + """Set settings.""" self.default_port = default_port - self.receivers = receivers def __call__(self, data): + """Send messages from all receivers.""" for receiver in self.receivers: self._send_to_address(receiver, data) @@ -71,11 +74,13 @@ def _send_to_address(self, address, data, timeout=10): def close(self): """Close the sender.""" pass -#----------------------------------------------------------------------------- + + +# ---------------------------------------------------------------------------- # # General thread to broadcast messages. # -#----------------------------------------------------------------------------- +# ---------------------------------------------------------------------------- class MessageBroadcaster(object): @@ -85,6 +90,7 @@ class MessageBroadcaster(object): """ def __init__(self, msg, port, interval, designated_receivers=None): + """Initialize message broadcaster.""" if designated_receivers: self._sender = DesignatedReceiversSender(port, designated_receivers) @@ -140,17 +146,19 @@ def _run(self): self._is_running = False self._sender.close() -#----------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- # # General thread to broadcast addresses. # -#----------------------------------------------------------------------------- +# ---------------------------------------------------------------------------- class AddressBroadcaster(MessageBroadcaster): """Class to broadcast stuff.""" def __init__(self, name, address, interval, nameservers): + """Initialize address broadcasting.""" msg = message.Message("/address/%s" % name, "info", {"URI": "%s:%d" % address}).encode() MessageBroadcaster.__init__(self, msg, broadcast_port, interval, @@ -172,6 +180,7 @@ class AddressServiceBroadcaster(MessageBroadcaster): """Class to broadcast stuff.""" def __init__(self, name, address, data_type, interval=2, nameservers=None): + """Initialize broadcaster.""" msg = message.Message("/address/%s" % name, "info", {"URI": address, "service": data_type}).encode() diff --git a/posttroll/ns.py b/posttroll/ns.py index 12a1bd0..7aabc1e 100644 --- a/posttroll/ns.py +++ b/posttroll/ns.py @@ -47,17 +47,21 @@ nslock = Lock() -class TimeoutError(BaseException): +class TimeoutError(BaseException): """A timeout.""" + pass # Client functions. def get_pub_addresses(names=None, timeout=10, nameserver="localhost"): - """Get the address of the publisher for a given list of publisher *names* - from the nameserver on *nameserver* (localhost by default). + """Get the addresses of the publishers. + + Kwargs: + - names: names of the publishers + - nameserver: nameserver address to query the publishers from (default: localhost). """ addrs = [] if names is None: @@ -73,8 +77,12 @@ def get_pub_addresses(names=None, timeout=10, nameserver="localhost"): def get_pub_address(name, timeout=10, nameserver="localhost"): - """Get the address of the publisher for a given publisher *name* from the - nameserver on *nameserver* (localhost by default).""" + """Get the address of the named publisher. + + Kwargs: + - name: name of the publishers + - nameserver: nameserver address to query the publishers from (default: localhost). + """ # Socket to talk to server socket = get_context().socket(REQ) try: @@ -104,8 +112,7 @@ def get_pub_address(name, timeout=10, nameserver="localhost"): def get_active_address(name, arec): - """Get the addresses of the active modules for a given publisher *name*. - """ + """Get the addresses of the active modules for a given publisher *name*.""" addrs = arec.get(name) if addrs: return Message("/oper/ns", "info", addrs) @@ -116,16 +123,16 @@ def get_active_address(name, arec): class NameServer: """The name server.""" - def __init__(self, max_age=timedelta(minutes=10), multicast_enabled=True, restrict_to_localhost=False): + def __init__(self, max_age=None, multicast_enabled=True, restrict_to_localhost=False): + """Initialize nameserver.""" self.loop = True self.listener = None - self._max_age = max_age + self._max_age = max_age or timedelta(minutes=10) self._multicast_enabled = multicast_enabled self._restrict_to_localhost = restrict_to_localhost def run(self, *args): - """Run the listener and answer to requests. - """ + """Run the listener and answer to requests.""" del args arec = AddressReceiver(max_age=self._max_age, @@ -161,8 +168,7 @@ def run(self, *args): self.stop() def stop(self): - """Stop the name server. - """ + """Stop the name server.""" self.listener.setsockopt(LINGER, 1) self.loop = False with nslock: diff --git a/posttroll/subscriber.py b/posttroll/subscriber.py index 7ea5d80..9cc8c32 100644 --- a/posttroll/subscriber.py +++ b/posttroll/subscriber.py @@ -176,7 +176,6 @@ def _add_hook(self, socket, callback): self._hooks.append(socket) self._hooks_cb[socket] = callback - @property def addresses(self): """Get the addresses.""" @@ -365,6 +364,7 @@ class Subscribe: information how the selection is done. Example:: + del tmp from posttroll.subscriber import Subscribe diff --git a/posttroll/testing.py b/posttroll/testing.py index 66a2a62..d9cbc60 100644 --- a/posttroll/testing.py +++ b/posttroll/testing.py @@ -17,6 +17,7 @@ def interuptible_recv(self): with mock.patch("posttroll.subscriber.Subscriber.recv", interuptible_recv): yield + @contextmanager def patched_publisher(): """Patch the Subscriber object to return given messages.""" diff --git a/posttroll/tests/test_message.py b/posttroll/tests/test_message.py index dbe088c..3a4ee6e 100644 --- a/posttroll/tests/test_message.py +++ b/posttroll/tests/test_message.py @@ -21,8 +21,7 @@ # You should have received a copy of the GNU General Public License along with # pytroll. If not, see . -"""Test module for the message class. -""" +"""Test module for the message class.""" import os import sys @@ -47,13 +46,10 @@ class Test(unittest.TestCase): - - """Test class. - """ + """Test class.""" def test_encode_decode(self): - """Test the encoding/decoding of the message class. - """ + """Test the encoding/decoding of the message class.""" msg1 = Message('/test/whatup/doc', 'info', data='not much to say') sender = '%s@%s' % (msg1.user, msg1.host) @@ -64,8 +60,7 @@ def test_encode_decode(self): msg='Messaging, encoding, decoding failed') def test_decode(self): - """Test the decoding of a message. - """ + """Test the decoding of a message.""" rawstr = (_MAGICK + r'/test/1/2/3 info ras@hawaii 2008-04-11T22:13:22.123000 v1.01' + r' text/ascii "what' + r"'" + r's up doc"') @@ -75,8 +70,7 @@ def test_decode(self): msg='Messaging, decoding of message failed') def test_encode(self): - """Test the encoding of a message. - """ + """Test the encoding of a message.""" subject = '/test/whatup/doc' atype = "info" data = 'not much to say' @@ -114,7 +108,8 @@ def test_unicode(self): def test_iso(self): """Test handling of iso-8859-1.""" - msg = 'pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN 2019-01-07T12:52:19.872171 v1.01 application/json {"station": "norrköping"}' + msg = ('pytroll://oper/polar/direct_readout/norrköping pong sat@MERLIN ' + '2019-01-07T12:52:19.872171 v1.01 application/json {"station": "norrköping"}') try: iso_msg = msg.decode('utf-8').encode('iso-8859-1') except AttributeError: @@ -125,8 +120,7 @@ def test_iso(self): self.fail('Unexpected iso decoding error') def test_pickle(self): - """Test pickling. - """ + """Test pickling.""" import pickle msg1 = Message('/test/whatup/doc', 'info', data='not much to say') try: @@ -146,8 +140,7 @@ def test_pickle(self): pass def test_metadata(self): - """Test metadata encoding/decoding. - """ + """Test metadata encoding/decoding.""" metadata = copy.copy(SOME_METADATA) msg = Message.decode(Message('/sat/polar/smb/level1', 'file', data=metadata).encode()) @@ -156,8 +149,7 @@ def test_metadata(self): msg='Messaging, metadata decoding / encoding failed') def test_serialization(self): - """Test json serialization. - """ + """Test json serialization.""" compare_file = '/message_metadata.dumps' try: import json @@ -181,13 +173,13 @@ def test_serialization(self): def suite(): - """The suite for test_message. - """ + """Create the suite for test_message.""" loader = unittest.TestLoader() mysuite = unittest.TestSuite() mysuite.addTest(loader.loadTestsFromTestCase(Test)) return mysuite + if __name__ == '__main__': unittest.main() diff --git a/posttroll/tests/test_testing.py b/posttroll/tests/test_testing.py index c688086..a61d008 100644 --- a/posttroll/tests/test_testing.py +++ b/posttroll/tests/test_testing.py @@ -25,6 +25,7 @@ def test_fake_publisher_crashes_when_not_started(): with pytest.raises(RuntimeError): pub.send("bla") + def test_fake_publisher_crashes_when_send_is_used_with_non_string_type(): """Test fake publisher needs to be started.""" from posttroll.publisher import create_publisher_from_dict_config From be3bb7718c15352436dc2b41cb25e33121e57608 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Mon, 22 Apr 2024 10:29:47 +0300 Subject: [PATCH 2/3] Fix datetime imports --- posttroll/__init__.py | 8 ++++---- posttroll/address_receiver.py | 17 ++++++++--------- posttroll/message.py | 5 +++-- posttroll/ns.py | 8 ++++---- posttroll/publisher.py | 10 +++++----- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/posttroll/__init__.py b/posttroll/__init__.py index b77c34b..d812970 100644 --- a/posttroll/__init__.py +++ b/posttroll/__init__.py @@ -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 @@ -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: @@ -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) diff --git a/posttroll/address_receiver.py b/posttroll/address_receiver.py index dc6112c..dc8076b 100644 --- a/posttroll/address_receiver.py +++ b/posttroll/address_receiver.py @@ -27,14 +27,13 @@ //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 @@ -53,8 +52,8 @@ 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(): @@ -88,7 +87,7 @@ def __init__(self, max_age=ten_minutes, port=None, 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) @@ -127,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: @@ -218,7 +217,7 @@ 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 diff --git a/posttroll/message.py b/posttroll/message.py index 1998377..6c10c0a 100644 --- a/posttroll/message.py +++ b/posttroll/message.py @@ -38,8 +38,9 @@ Note: the Message class is not optimized for BIG messages. """ +import datetime as dt import re -from datetime import datetime + try: import json except ImportError: @@ -131,7 +132,7 @@ def __init__(self, subject='', atype='', data='', binary=False, rawstr=None): self.type = atype self.type = atype self.sender = _getsender() - self.time = datetime.utcnow() + self.time = dt.datetime.utcnow() self.data = data self.binary = binary self.version = _VERSION diff --git a/posttroll/ns.py b/posttroll/ns.py index 7aabc1e..1ecff05 100644 --- a/posttroll/ns.py +++ b/posttroll/ns.py @@ -25,10 +25,10 @@ Default port is 5557, if $NAMESERVER_PORT is not defined. """ +import datetime as dt import logging import os import time -from datetime import datetime, timedelta from threading import Lock # pylint: disable=E0611 @@ -67,8 +67,8 @@ def get_pub_addresses(names=None, timeout=10, nameserver="localhost"): if names is None: names = ["", ] for name in names: - then = datetime.now() + timedelta(seconds=timeout) - while datetime.now() < then: + then = dt.datetime.now() + dt.timedelta(seconds=timeout) + while dt.datetime.now() < then: addrs += get_pub_address(name, nameserver=nameserver, timeout=timeout) if addrs: break @@ -127,7 +127,7 @@ def __init__(self, max_age=None, multicast_enabled=True, restrict_to_localhost=F """Initialize nameserver.""" self.loop = True self.listener = None - self._max_age = max_age or timedelta(minutes=10) + self._max_age = max_age or dt.timedelta(minutes=10) self._multicast_enabled = multicast_enabled self._restrict_to_localhost = restrict_to_localhost diff --git a/posttroll/publisher.py b/posttroll/publisher.py index 47fb738..4a9bdec 100644 --- a/posttroll/publisher.py +++ b/posttroll/publisher.py @@ -23,9 +23,9 @@ """The publisher module gives high-level tools to publish messages on a port.""" +import datetime as dt import logging import socket -from datetime import datetime, timedelta from threading import Lock from urllib.parse import urlsplit, urlunsplit import zmq @@ -160,13 +160,13 @@ class _PublisherHeartbeat: def __init__(self, publisher): self.publisher = publisher self.subject = '/heartbeat/' + publisher.name - self.lastbeat = datetime(1900, 1, 1) + self.lastbeat = dt.datetime(1900, 1, 1) def __call__(self, min_interval=0): if not min_interval or ( - (datetime.utcnow() - self.lastbeat >= - timedelta(seconds=min_interval))): - self.lastbeat = datetime.utcnow() + (dt.datetime.utcnow() - self.lastbeat >= + dt.timedelta(seconds=min_interval))): + self.lastbeat = dt.datetime.utcnow() LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval) self.publisher.send(Message(self.subject, "beat", {"min_interval": min_interval}).encode()) From bf2863b316cf498f01f39b863f1a3c494dd4788e Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Mon, 22 Apr 2024 11:01:20 +0300 Subject: [PATCH 3/3] Add unsaved datetime import fixes --- posttroll/subscriber.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/posttroll/subscriber.py b/posttroll/subscriber.py index 9cc8c32..d966548 100644 --- a/posttroll/subscriber.py +++ b/posttroll/subscriber.py @@ -24,10 +24,10 @@ """Simple library to subscribe to messages.""" -from time import sleep +import datetime as dt import logging import time -from datetime import datetime, timedelta +from time import sleep from threading import Lock from urllib.parse import urlsplit @@ -311,8 +311,8 @@ def start(self): """Start the subscriber.""" def _get_addr_loop(service, timeout): """Try to get the address of *service* until for *timeout* seconds.""" - then = datetime.now() + timedelta(seconds=timeout) - while datetime.now() < then: + then = dt.datetime.now() + dt.timedelta(seconds=timeout) + while dt.datetime.now() < then: addrs = get_pub_address(service, nameserver=self._nameserver) if addrs: return [addr["URI"] for addr in addrs]