From 6fec9eefe56b9e476d5faf0d57dc2a80b703c9d6 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 29 Jan 2017 07:49:42 +0300 Subject: don't overwrite database files --- vk/tracking/db/format.py | 16 +++++++++++++--- vk/tracking/db/io.py | 10 +++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/vk/tracking/db/format.py b/vk/tracking/db/format.py index 7b3f312..f9a670c 100644 --- a/vk/tracking/db/format.py +++ b/vk/tracking/db/format.py @@ -27,13 +27,23 @@ class Format(Enum): raise NotImplementedError('unsupported database format: ' + str(self)) def open_output_file(self, path=None): - if self is Format.CSV or self is Format.LOG: - return io.open_output_text_file(path) + if self is Format.CSV: + return self._open_output_database_file(path) + elif self is Format.LOG: + return self._open_output_log_file(path) elif self is Format.NULL: - return io.open_output_text_file(None) + return self._open_output_database_file(None) else: raise NotImplementedError('unsupported database format: ' + str(self)) + @staticmethod + def _open_output_log_file(path): + return io.open_output_text_file(path, mode='a') + + @staticmethod + def _open_output_database_file(path): + return io.open_output_text_file(path, mode='x') + def create_reader(self, fd=sys.stdin): if self is Format.CSV: return backend.csv.Reader(fd) diff --git a/vk/tracking/db/io.py b/vk/tracking/db/io.py index 805334d..d280634 100644 --- a/vk/tracking/db/io.py +++ b/vk/tracking/db/io.py @@ -36,8 +36,12 @@ def _open_file(path=None, default=None, **kwargs): if fd is not default: fd.close() -def open_output_text_file(path=None): - return _open_file(path, default=sys.stdout, mode='w', encoding='utf-8') +_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='utf-8') + return _open_file(path, default=sys.stdin, mode='r', + encoding=_DEFAULT_ENCODING) -- cgit v1.2.3