blob: e1b34a1d392afa7e0f492a4a6943a7f5eba61bf0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# 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 contextlib import contextmanager
from enum import Enum
import sys
from . import backend
class Format(Enum):
CSV = 'csv'
LOG = 'log'
NULL = 'null'
@contextmanager
def create_writer(self, path=None):
with self._open_output_file(path) as fd:
if self is Format.CSV:
yield backend.csv.Writer(fd)
elif self is Format.LOG:
yield backend.log.Writer(fd)
elif self is Format.NULL:
yield backend.null.Writer()
else:
raise NotImplementedError('unsupported database format: ' + str(self))
@contextmanager
def _open_output_file(self, path=None):
fd = sys.stdout
if path is None:
pass
elif self is Format.CSV or self is Format.LOG:
fd = open(path, 'w', encoding='utf-8')
elif self is Format.NULL:
pass
else:
raise NotImplementedError('unsupported database format: ' + str(self))
try:
yield fd
finally:
if fd is not sys.stdout:
fd.close()
@contextmanager
def create_reader(self, path=None):
with self._open_input_file(path) as fd:
if self is Format.CSV:
yield backend.csv.Reader(fd)
elif self is Format.LOG:
raise NotImplementedError('cannot read from a log file')
elif self is Format.NULL:
yield backend.null.Reader()
else:
raise NotImplementedError('unsupported database format: ' + str(self))
@contextmanager
def _open_input_file(self, path=None):
fd = sys.stdin
if path is None:
pass
elif self is Format.CSV:
fd = open(path, encoding='utf-8')
elif self is Format.LOG:
raise NotImplementedError('cannot read from a log file')
elif self is Format.NULL:
pass
else:
raise NotImplementedError('unsupported database format: ' + str(self))
try:
yield fd
finally:
if fd is not sys.stdin:
fd.close()
def __str__(self):
return self.value
|