1
0
mirror of https://github.com/gryf/slack-backup.git synced 2025-12-17 11:30:25 +01:00

Moved fromtimestamp function to utils module

This commit is contained in:
2018-05-03 18:44:25 +02:00
parent 43b830c3d1
commit c33d2fad50
2 changed files with 16 additions and 13 deletions

View File

@@ -1,20 +1,12 @@
""" """
Convinient object mapping from slack API reponses Convinient object mapping from slack API reponses
""" """
from datetime import datetime
from sqlalchemy import Column, Integer, Text, Boolean, ForeignKey from sqlalchemy import Column, Integer, Text, Boolean, ForeignKey
from sqlalchemy import DateTime from sqlalchemy import DateTime
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from slack_backup.db import Base from slack_backup.db import Base
from slack_backup import utils
def fromtimestamp(timestamp):
past = datetime.utcfromtimestamp(0)
if timestamp is None:
return past
return datetime.fromtimestamp(timestamp)
class Purpose(Base): class Purpose(Base):
@@ -35,7 +27,7 @@ class Purpose(Base):
def update(self, data_dict): def update(self, data_dict):
data_dict = data_dict or {} data_dict = data_dict or {}
self.last_set = fromtimestamp(data_dict.get('last_set')) self.last_set = utils.fromtimestamp(data_dict.get('last_set'))
self.value = data_dict.get('value') self.value = data_dict.get('value')
def __repr__(self): def __repr__(self):
@@ -63,7 +55,7 @@ class Topic(Base):
def update(self, data_dict): def update(self, data_dict):
data_dict = data_dict or {} data_dict = data_dict or {}
self.last_set = fromtimestamp(data_dict.get('last_set')) self.last_set = utils.fromtimestamp(data_dict.get('last_set'))
self.value = data_dict.get('value') self.value = data_dict.get('value')
def __repr__(self): def __repr__(self):
@@ -98,7 +90,7 @@ class Channel(Base):
self.slackid = data_dict.get('id', '') self.slackid = data_dict.get('id', '')
self.name = data_dict.get('name', '') self.name = data_dict.get('name', '')
self.created = fromtimestamp(data_dict.get('created')) self.created = utils.fromtimestamp(data_dict.get('created'))
self.is_archived = data_dict.get('is_archived', False) self.is_archived = data_dict.get('is_archived', False)
def __repr__(self): def __repr__(self):
@@ -243,7 +235,7 @@ class Message(Base):
self.update(data_dict) self.update(data_dict)
def datetime(self): def datetime(self):
return datetime.fromtimestamp(float(self.ts)) return utils.fromtimestamp(float(self.ts))
def update(self, data_dict): def update(self, data_dict):
data_dict = data_dict or {} data_dict = data_dict or {}

View File

@@ -1,6 +1,7 @@
""" """
Some utils functions. Jsut to not copypaste the code around Some utils functions. Jsut to not copypaste the code around
""" """
from datetime import datetime
import errno import errno
import os import os
import logging import logging
@@ -42,3 +43,13 @@ def same_files(file1, file2):
hash2 = hashlib.sha256(fobj.read()) hash2 = hashlib.sha256(fobj.read())
return hash1.hexdigest() == hash2.hexdigest() return hash1.hexdigest() == hash2.hexdigest()
def fromtimestamp(timestamp):
"""
Return datetime object from provided timestamp. If timestamp argument is
falsy, datetime object placed in January 1970 will be retuned.
"""
if not timestamp:
return datetime.utcfromtimestamp(0)
return datetime.fromtimestamp(timestamp)