From 8bd8f1f2d09db33697fc61d5caaa3b37b856916d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 16 Jun 2016 02:40:51 +0300 Subject: track_status.py: move stuff to the package --- vk/utils/tracking/db/__init__.py | 0 vk/utils/tracking/db/record.py | 27 ++++++++++++++++++++++ vk/utils/tracking/db/writer/__init__.py | 5 +++++ vk/utils/tracking/db/writer/csv.py | 40 +++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 vk/utils/tracking/db/__init__.py create mode 100644 vk/utils/tracking/db/record.py create mode 100644 vk/utils/tracking/db/writer/__init__.py create mode 100644 vk/utils/tracking/db/writer/csv.py (limited to 'vk/utils/tracking/db') diff --git a/vk/utils/tracking/db/__init__.py b/vk/utils/tracking/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vk/utils/tracking/db/record.py b/vk/utils/tracking/db/record.py new file mode 100644 index 0000000..0a1a687 --- /dev/null +++ b/vk/utils/tracking/db/record.py @@ -0,0 +1,27 @@ +# Copyright 2016 Egor Tensin +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections import OrderedDict +from datetime import datetime + +from vk.user import Field + +class Record: + _FIELDS = ( + Field.UID, + Field.FIRST_NAME, + Field.LAST_NAME, + Field.SCREEN_NAME, + Field.ONLINE, + Field.LAST_SEEN, + ) + + def __init__(self, user): + self._fields = OrderedDict() + for field in self._FIELDS: + self._fields[field] = user[field] + self._timestamp = datetime.utcnow().replace(microsecond=0) + + def to_list(self): + return [self._timestamp.isoformat()] + list(self._fields.values()) diff --git a/vk/utils/tracking/db/writer/__init__.py b/vk/utils/tracking/db/writer/__init__.py new file mode 100644 index 0000000..330d5a8 --- /dev/null +++ b/vk/utils/tracking/db/writer/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2016 Egor Tensin +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +__all__ = 'csv', diff --git a/vk/utils/tracking/db/writer/csv.py b/vk/utils/tracking/db/writer/csv.py new file mode 100644 index 0000000..4594e09 --- /dev/null +++ b/vk/utils/tracking/db/writer/csv.py @@ -0,0 +1,40 @@ +# Copyright 2016 Egor Tensin +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +import csv +from datetime import datetime + +from ..record import Record + +class Writer: + def __init__(self, path, mode='w'): + if path is None: + self._fd = None + else: + self._fd = open(path, mode) + self._writer = csv.writer(self._fd, lineterminator='\n') + + def _is_valid(self): + return self._fd is not None + + def __enter__(self): + if not self._is_valid(): + return None + self._fd.__enter__() + return self + + def __exit__(self, *args): + if self._is_valid(): + self._fd.__exit__(*args) + + def flush(self): + if self._is_valid(): + self._fd.flush() + + def write_record(self, user): + self._write_row(Record(user).to_list()) + self.flush() + + def _write_row(self, row): + self._writer.writerow(row) -- cgit v1.2.3