aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-01-29 07:49:42 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-01-29 07:49:42 +0300
commit6fec9eefe56b9e476d5faf0d57dc2a80b703c9d6 (patch)
treee4e71aefba74780e63792553676206e14688719c
parentadd connection errors to the db logger (diff)
downloadvk-scripts-6fec9eefe56b9e476d5faf0d57dc2a80b703c9d6.tar.gz
vk-scripts-6fec9eefe56b9e476d5faf0d57dc2a80b703c9d6.zip
don't overwrite database files
-rw-r--r--vk/tracking/db/format.py16
-rw-r--r--vk/tracking/db/io.py10
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)