aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/vk/tracking/db/backend/csv.py
blob: 5b2cc7d6a832c6c40eac81ddddc86e82688128fc (plain) (tree)
1
2
3
4
5
6
7
8



                                                             

                                    
 
                                                




                                 
                                          
 

                                      


                                     






                                     
                                                                           






                                                                                       
                                         
 








                                                         
# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "VK scripts" project.
# For details, see https://github.com/egor-tensin/vk-scripts.
# Distributed under the MIT License.

from collections.abc import Iterable

from ..io import InputReaderCSV, OutputWriterCSV
from ..record import Record
from ..timestamp import Timestamp

class Writer:
    def __init__(self, fd):
        self._writer = OutputWriterCSV(fd)

    def on_initial_status(self, user):
        self._write_record(user)

    def on_status_update(self, user):
        self._write_record(user)

    def on_connection_error(self, e):
        pass

    def _write_record(self, user):
        if not self:
            return
        self._writer.write_row(self._record_to_row(Record.from_user(user)))

    @staticmethod
    def _record_to_row(record):
        return [str(record.get_timestamp())] + [str(record[field]) for field in record]

class Reader(Iterable):
    def __init__(self, fd):
        self._reader = InputReaderCSV(fd)

    def __iter__(self):
        return map(Reader._record_from_row, self._reader)

    @staticmethod
    def _record_from_row(row):
        record = Record(Timestamp.from_string(row[0]))
        for i in range(len(Record.FIELDS)):
            record[Record.FIELDS[i]] = row[i + 1]
        return record