aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vk/tracking/db
diff options
context:
space:
mode:
Diffstat (limited to 'vk/tracking/db')
-rw-r--r--vk/tracking/db/backend/csv.py2
-rw-r--r--vk/tracking/db/backend/log.py1
-rw-r--r--vk/tracking/db/backend/null.py2
-rw-r--r--vk/tracking/db/format.py29
-rw-r--r--vk/tracking/db/io.py8
-rw-r--r--vk/tracking/db/meta.py2
-rw-r--r--vk/tracking/db/record.py1
-rw-r--r--vk/tracking/db/timestamp.py1
8 files changed, 29 insertions, 17 deletions
diff --git a/vk/tracking/db/backend/csv.py b/vk/tracking/db/backend/csv.py
index 4943ff2..43038e4 100644
--- a/vk/tracking/db/backend/csv.py
+++ b/vk/tracking/db/backend/csv.py
@@ -8,6 +8,7 @@ from ..io import FileReaderCSV, FileWriterCSV
from ..record import Record
from ..timestamp import Timestamp
+
class Writer(meta.Writer):
def __init__(self, fd):
self._writer = FileWriterCSV(fd)
@@ -30,6 +31,7 @@ class Writer(meta.Writer):
def _record_to_row(record):
return [str(record.get_timestamp())] + [str(record[field]) for field in record]
+
class Reader(meta.Reader):
def __init__(self, fd):
self._reader = FileReaderCSV(fd)
diff --git a/vk/tracking/db/backend/log.py b/vk/tracking/db/backend/log.py
index 814cabc..d301856 100644
--- a/vk/tracking/db/backend/log.py
+++ b/vk/tracking/db/backend/log.py
@@ -7,6 +7,7 @@ import logging
from .. import meta
+
class Writer(meta.Writer):
def __init__(self, fd):
self._logger = logging.getLogger(__file__)
diff --git a/vk/tracking/db/backend/null.py b/vk/tracking/db/backend/null.py
index 663af10..80a66b4 100644
--- a/vk/tracking/db/backend/null.py
+++ b/vk/tracking/db/backend/null.py
@@ -5,6 +5,7 @@
from .. import meta
+
class Writer(meta.Writer):
def __init__(self):
pass
@@ -18,6 +19,7 @@ class Writer(meta.Writer):
def on_connection_error(self, e):
pass
+
class Reader(meta.Reader):
def __init__(self):
pass
diff --git a/vk/tracking/db/format.py b/vk/tracking/db/format.py
index f9a670c..028d403 100644
--- a/vk/tracking/db/format.py
+++ b/vk/tracking/db/format.py
@@ -8,6 +8,7 @@ import sys
from . import backend, io
+
class Format(Enum):
CSV = 'csv'
LOG = 'log'
@@ -19,22 +20,20 @@ class Format(Enum):
def create_writer(self, fd=sys.stdout):
if self is Format.CSV:
return backend.csv.Writer(fd)
- elif self is Format.LOG:
+ if self is Format.LOG:
return backend.log.Writer(fd)
- elif self is Format.NULL:
+ if self is Format.NULL:
return backend.null.Writer()
- else:
- raise NotImplementedError('unsupported database format: ' + str(self))
+ raise NotImplementedError('unsupported database format: ' + str(self))
def open_output_file(self, path=None):
if self is Format.CSV:
return self._open_output_database_file(path)
- elif self is Format.LOG:
+ if self is Format.LOG:
return self._open_output_log_file(path)
- elif self is Format.NULL:
+ if self is Format.NULL:
return self._open_output_database_file(None)
- else:
- raise NotImplementedError('unsupported database format: ' + str(self))
+ raise NotImplementedError('unsupported database format: ' + str(self))
@staticmethod
def _open_output_log_file(path):
@@ -47,19 +46,17 @@ class Format(Enum):
def create_reader(self, fd=sys.stdin):
if self is Format.CSV:
return backend.csv.Reader(fd)
- elif self is Format.LOG:
+ if self is Format.LOG:
return NotImplementedError('cannot read from a log file')
- elif self is Format.NULL:
+ if self is Format.NULL:
return backend.null.Reader()
- else:
- raise NotImplementedError('unsupported database format: ' + str(self))
+ raise NotImplementedError('unsupported database format: ' + str(self))
def open_input_file(self, path=None):
if self is Format.CSV:
return io.open_input_text_file(path)
- elif self is Format.LOG:
+ if self is Format.LOG:
raise NotImplementedError('cannot read from a log file')
- elif self is Format.NULL:
+ if self is Format.NULL:
return io.open_input_text_file(None)
- else:
- raise NotImplementedError('unsupported database format: ' + str(self))
+ raise NotImplementedError('unsupported database format: ' + str(self))
diff --git a/vk/tracking/db/io.py b/vk/tracking/db/io.py
index 37d9c53..a89865f 100644
--- a/vk/tracking/db/io.py
+++ b/vk/tracking/db/io.py
@@ -7,6 +7,7 @@ from contextlib import contextmanager
import csv
import sys
+
class FileWriterCSV:
def __init__(self, fd=sys.stdout):
self._fd = fd
@@ -14,7 +15,7 @@ class FileWriterCSV:
@staticmethod
def _convert_row_old_python(row):
- if isinstance(row, list) or isinstance(row, tuple):
+ if isinstance(row, (list, tuple)):
return row
return list(row)
@@ -24,6 +25,7 @@ class FileWriterCSV:
self._writer.writerow(row)
self._fd.flush()
+
class FileReaderCSV:
def __init__(self, fd=sys.stdin):
self._reader = csv.reader(fd)
@@ -31,6 +33,7 @@ class FileReaderCSV:
def __iter__(self):
return iter(self._reader)
+
@contextmanager
def _open_file(path=None, default=None, **kwargs):
if path is None:
@@ -39,12 +42,15 @@ def _open_file(path=None, default=None, **kwargs):
with open(path, **kwargs) as fd:
yield fd
+
_DEFAULT_ENCODING = 'utf-8'
+
def open_output_text_file(path=None, mode='w'):
return _open_file(path, default=sys.stdout, mode=mode,
encoding=_DEFAULT_ENCODING)
+
def open_input_text_file(path=None):
return _open_file(path, default=sys.stdin, mode='r',
encoding=_DEFAULT_ENCODING)
diff --git a/vk/tracking/db/meta.py b/vk/tracking/db/meta.py
index 024d9d8..eb15c74 100644
--- a/vk/tracking/db/meta.py
+++ b/vk/tracking/db/meta.py
@@ -6,6 +6,7 @@
import abc
from collections.abc import Iterable
+
class Writer(metaclass=abc.ABCMeta):
@abc.abstractmethod
def on_initial_status(self, user):
@@ -19,6 +20,7 @@ class Writer(metaclass=abc.ABCMeta):
def on_connection_error(self, e):
pass
+
class Reader(Iterable, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __iter__(self):
diff --git a/vk/tracking/db/record.py b/vk/tracking/db/record.py
index dfd47c6..6998238 100644
--- a/vk/tracking/db/record.py
+++ b/vk/tracking/db/record.py
@@ -12,6 +12,7 @@ from vk.user import User, UserField
from .timestamp import Timestamp
+
class Record(MutableMapping):
FIELDS = (
UserField.UID,
diff --git a/vk/tracking/db/timestamp.py b/vk/tracking/db/timestamp.py
index b2219ca..1309797 100644
--- a/vk/tracking/db/timestamp.py
+++ b/vk/tracking/db/timestamp.py
@@ -5,6 +5,7 @@
from datetime import datetime, timezone
+
class Timestamp:
@staticmethod
def _new():