This repository has been archived by the owner on Sep 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathissue-21.diff
50 lines (45 loc) · 1.71 KB
/
issue-21.diff
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
diff --git a/radicale/storage/filesystem.py b/radicale/storage/filesystem.py
index deeba20..32a26a7 100644
--- a/radicale/storage/filesystem.py
+++ b/radicale/storage/filesystem.py
@@ -28,6 +28,7 @@ import json
import time
import sys
from contextlib import contextmanager
+from atomicwrites import AtomicWriter
from .. import config, ical, log, pathutils
@@ -42,6 +43,14 @@ except:
GIT_REPOSITORY = None
+class _EncodedAtomicWriter(AtomicWriter):
+ def __init__(self, path, encoding, mode="w", overwrite=True):
+ self._encoding = encoding
+ super().__init__(path, mode, overwrite)
+
+ def get_fileobject(self, **kwargs):
+ return super().get_fileobject(encoding=self._encoding, **kwargs)
+
# This function overrides the builtin ``open`` function for this module
# pylint: disable=W0622
@contextmanager
@@ -49,8 +58,12 @@ def open(path, mode="r"):
"""Open a file at ``path`` with encoding set in the configuration."""
# On enter
abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
- with codecs.open(abs_path, mode, config.get("encoding", "stock")) as fd:
- yield fd
+ if mode == "w":
+ with _EncodedAtomicWriter(abs_path, config.get("encoding", "stock"), mode).open() as fd:
+ yield fd
+ else:
+ with codecs.open(abs_path, mode, config.get("encoding", "stock")) as fd:
+ yield fd
# On exit
if GIT_REPOSITORY and mode == "w":
path = os.path.relpath(abs_path, FOLDER)
@@ -60,7 +73,6 @@ def open(path, mode="r"):
path.encode("utf-8"), committer=committer.encode("utf-8"))
# pylint: enable=W0622
-
class Collection(ical.Collection):
"""Collection stored in a flat ical file."""
@property