From b4e6591c9dd41fd7f30a2be101561d6d8b65bd8a Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 4 Sep 2015 22:00:22 -0400 Subject: [PATCH] Refactor parse_time into its own utils file. --- discord/client.py | 16 +++++++++------- discord/message.py | 13 +++---------- discord/server.py | 4 ++-- discord/utils.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 discord/utils.py diff --git a/discord/client.py b/discord/client.py index 58e7e31b0..098f16f74 100644 --- a/discord/client.py +++ b/discord/client.py @@ -24,18 +24,20 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import requests -import json, re, time, copy -from collections import deque -from threading import Timer -from ws4py.client.threadedclient import WebSocketClient -from sys import platform as sys_platform from . import endpoints from .errors import InvalidEventName, InvalidDestination, GatewayNotFound from .user import User from .channel import Channel, PrivateChannel from .server import Server, Member, Permissions, Role from .message import Message +from .utils import parse_time + +import requests +import json, re, time, copy +from collections import deque +from threading import Timer +from ws4py.client.threadedclient import WebSocketClient +from sys import platform as sys_platform def _null_event(*args, **kwargs): pass @@ -238,7 +240,7 @@ class Client(object): continue value = data[attr] if 'time' in attr: - setattr(message, attr, message._parse_time(value)) + setattr(message, attr, parse_time(value)) else: setattr(message, attr, value) self._invoke_event('on_message_edit', older_message, message) diff --git a/discord/message.py b/discord/message.py index 5fe871cd9..01dbc37ee 100644 --- a/discord/message.py +++ b/discord/message.py @@ -24,8 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import datetime -import re +from .utils import parse_time from .user import User class Message(object): @@ -75,11 +74,8 @@ class Message(object): # we can use this to our advantage to use strptime instead of a complicated parsing routine. # example timestamp: 2015-08-21T12:03:45.782000+00:00 # sometimes the .%f modifier is missing - self.edited_timestamp = kwargs.get('edited_timestamp') - if self.edited_timestamp is not None: - self.edited_timestamp = self._parse_time(edited_timestamp) - - self.timestamp = self._parse_time(kwargs.get('timestamp')) + self.edited_timestamp = parse_time(kwargs.get('edited_timestamp')) + self.timestamp = parse_time(kwargs.get('timestamp')) self.tts = kwargs.get('tts') self.content = kwargs.get('content') self.mention_everyone = kwargs.get('mention_everyone') @@ -90,6 +86,3 @@ class Message(object): self.mentions = [User(**mention) for mention in kwargs.get('mentions', {})] self.attachments = kwargs.get('attachments') - def _parse_time(self, time_string): - return datetime.datetime(*map(int, re.split(r'[^\d]', time_string.replace('+00:00', '')))) - diff --git a/discord/server.py b/discord/server.py index 022430069..00c69dece 100644 --- a/discord/server.py +++ b/discord/server.py @@ -26,7 +26,7 @@ DEALINGS IN THE SOFTWARE. from .user import User from .permissions import Permissions -import datetime, re +from .utils import parse_time class Role(object): """Represents a Discord role in a :class:`Server`. @@ -85,7 +85,7 @@ class Member(User): super(Member, self).__init__(**user) self.deaf = deaf self.mute = mute - self.joined_at = datetime.datetime(*map(int, re.split(r'[^\d]', joined_at.replace('+00:00', '')))) + self.joined_at = parse_time(joined_at) self.roles = roles self.status = 'offline' self.game_id = kwargs.get('game_id', None) diff --git a/discord/utils.py b/discord/utils.py new file mode 100644 index 000000000..0db280914 --- /dev/null +++ b/discord/utils.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +""" +The MIT License (MIT) + +Copyright (c) 2015 Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +from re import split as re_split +import datetime + + +def parse_time(timestamp): + if timestamp: + return datetime.datetime(*map(int, re_split(r'[^\d]', timestamp.replace('+00:00', '')))) + return None