commit b1ffbb8827ea55e68a13ced877a9e44d83a8e22b Author: Ossi Herrala oherrala@gmail.com Date: Thu Nov 20 23:04:02 2014 +0200
Replace calls to datetime.datetime.strptime() with stem.util.str_tools._parse_timestamp() --- stem/control.py | 3 +-- stem/descriptor/networkstatus.py | 9 ++++----- stem/descriptor/router_status_entry.py | 3 +-- stem/descriptor/server_descriptor.py | 3 +-- stem/descriptor/tordnsel.py | 8 +++----- stem/response/events.py | 7 +++---- 6 files changed, 13 insertions(+), 20 deletions(-)
diff --git a/stem/control.py b/stem/control.py index d815fcc..320fcab 100644 --- a/stem/control.py +++ b/stem/control.py @@ -221,7 +221,6 @@ If you're fine with allowing your script to raise exceptions then this can be mo
import calendar import collections -import datetime import inspect import io import os @@ -1273,7 +1272,7 @@ class Controller(BaseController): used = self.get_info('accounting/bytes') left = self.get_info('accounting/bytes-left')
- interval_end = datetime.datetime.strptime(interval_end, '%Y-%m-%d %H:%M:%S') + interval_end = stem.util.str_tools._parse_timestamp(interval_end) used_read, used_written = [int(val) for val in used.split(' ', 1)] left_read, left_written = [int(val) for val in left.split(' ', 1)]
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py index 1e516d3..6a15caf 100644 --- a/stem/descriptor/networkstatus.py +++ b/stem/descriptor/networkstatus.py @@ -49,7 +49,6 @@ For more information see :func:`~stem.descriptor.__init__.DocumentHandler`... DirectoryAuthority - Directory authority as defined in a v3 network status document """
-import datetime import io
import stem.descriptor.router_status_entry @@ -417,7 +416,7 @@ class NetworkStatusDocumentV2(NetworkStatusDocument): self.server_versions.append(version_str) elif keyword == 'published': try: - self.published = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + self.published = stem.util.str_tools._parse_timestamp(value) except ValueError: if validate: raise ValueError("Version 2 network status document's 'published' time wasn't parsable: %s" % value) @@ -699,7 +698,7 @@ class _DocumentHeader(object): raise ValueError("A network status document's consensus-method must be an integer, but was '%s'" % value) elif keyword in ('published', 'valid-after', 'fresh-until', 'valid-until'): try: - date_value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + date_value = stem.util.str_tools._parse_timestamp(value)
if keyword == 'published': self.published = date_value @@ -1362,7 +1361,7 @@ class KeyCertificate(Descriptor): # "dir-key-expires" YYYY-MM-DD HH:MM:SS
try: - date_value = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + date_value = stem.util.str_tools._parse_timestamp(value)
if keyword == 'dir-key-published': self.published = date_value @@ -1498,7 +1497,7 @@ class BridgeNetworkStatusDocument(NetworkStatusDocument): published_line = published_line.split(' ', 1)[1].strip()
try: - self.published = datetime.datetime.strptime(published_line, '%Y-%m-%d %H:%M:%S') + self.published = stem.util.str_tools._parse_timestamp(published_line) except ValueError: if validate: raise ValueError("Bridge network status document's 'published' time wasn't parsable: %s" % published_line) diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py index 75ad646..3dd9026 100644 --- a/stem/descriptor/router_status_entry.py +++ b/stem/descriptor/router_status_entry.py @@ -21,7 +21,6 @@ sources...
import base64 import binascii -import datetime
import stem.exit_policy import stem.prereq @@ -523,7 +522,7 @@ def _parse_r_line(desc, value, validate, include_digest = True):
try: published = '%s %s' % (r_comp[3], r_comp[4]) - desc.published = datetime.datetime.strptime(published, '%Y-%m-%d %H:%M:%S') + desc.published = stem.util.str_tools._parse_timestamp(published) except ValueError: if validate: raise ValueError("Publication time time wasn't parsable: r %s" % value) diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py index c1fed66..e92f0e0 100644 --- a/stem/descriptor/server_descriptor.py +++ b/stem/descriptor/server_descriptor.py @@ -34,7 +34,6 @@ etc). This information is provided from a few sources...
import base64 import codecs -import datetime import hashlib import re
@@ -460,7 +459,7 @@ class ServerDescriptor(Descriptor): # "published" YYYY-MM-DD HH:MM:SS
try: - self.published = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + self.published = stem.util.str_tools._parse_timestamp(value) except ValueError: if validate: raise ValueError("Published line's time wasn't parsable: %s" % line) diff --git a/stem/descriptor/tordnsel.py b/stem/descriptor/tordnsel.py index 72b3806..951b72f 100644 --- a/stem/descriptor/tordnsel.py +++ b/stem/descriptor/tordnsel.py @@ -10,8 +10,6 @@ exit list files. TorDNSEL - Exit list provided by TorDNSEL """
-import datetime - import stem.util.connection import stem.util.str_tools import stem.util.tor_tools @@ -89,13 +87,13 @@ class TorDNSEL(Descriptor): self.fingerprint = value elif keyword == 'Published': try: - self.published = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + self.published = stem.util.str_tools._parse_timestamp(value) except ValueError: if validate: raise ValueError("Published time wasn't parsable: %s" % value) elif keyword == 'LastStatus': try: - self.last_status = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') + self.last_status = stem.util.str_tools._parse_timestamp(value) except ValueError: if validate: raise ValueError("LastStatus time wasn't parsable: %s" % value) @@ -110,7 +108,7 @@ class TorDNSEL(Descriptor): raise ValueError('Unexpected block content: %s' % block_content)
try: - date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') + date = stem.util.str_tools._parse_timestamp(date) self.exit_addresses.append((address, date)) except ValueError: if validate: diff --git a/stem/response/events.py b/stem/response/events.py index 0df830b..92dd281 100644 --- a/stem/response/events.py +++ b/stem/response/events.py @@ -1,7 +1,6 @@ # Copyright 2012-2014, Damian Johnson and The Tor Project # See LICENSE for licensing information
-import datetime import io import re import time @@ -190,12 +189,12 @@ class AddrMapEvent(Event): self.expiry = None else: try: - self.expiry = datetime.datetime.strptime(self.expiry, '%Y-%m-%d %H:%M:%S') + self.expiry = stem.util.str_tools._parse_timestamp(self.expiry) except ValueError: raise stem.ProtocolError('Unable to parse date in ADDRMAP event: %s' % self)
if self.utc_expiry is not None: - self.utc_expiry = datetime.datetime.strptime(self.utc_expiry, '%Y-%m-%d %H:%M:%S') + self.utc_expiry = stem.util.str_tools._parse_timestamp(self.utc_expiry)
if self.cached is not None: if self.cached == 'YES': @@ -487,7 +486,7 @@ class ClientsSeenEvent(Event):
def _parse(self): if self.start_time is not None: - self.start_time = datetime.datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S') + self.start_time = stem.util.str_tools._parse_timestamp(self.start_time)
if self.locales is not None: locale_to_count = {}