Kaynağa Gözat

Merge pull request #478 from Unrud/patch-33

Correctly write line endings to files
Guillaume Ayoub 9 yıl önce
ebeveyn
işleme
f395e256ff
1 değiştirilmiş dosya ile 6 ekleme ve 6 silme
  1. 6 6
      radicale/storage.py

+ 6 - 6
radicale/storage.py

@@ -391,11 +391,11 @@ class Collection(BaseCollection):
         return os.path.join(filesystem_folder, "collection-root")
 
     @contextmanager
-    def _atomic_write(self, path, mode="w"):
+    def _atomic_write(self, path, mode="w", newline=None):
         directory = os.path.dirname(path)
         tmp = NamedTemporaryFile(
             mode=mode, dir=directory, encoding=self.encoding,
-            delete=False, prefix=".Radicale.tmp-")
+            delete=False, prefix=".Radicale.tmp-", newline=newline)
         try:
             yield tmp
             if self.configuration.getboolean("storage", "fsync"):
@@ -606,7 +606,7 @@ class Collection(BaseCollection):
         path = path_to_filesystem(self._filesystem_path, href)
         if not os.path.isfile(path):
             return None
-        with open(path, encoding=self.encoding) as fd:
+        with open(path, encoding=self.encoding, newline="") as fd:
             text = fd.read()
         last_modified = time.strftime(
             "%a, %d %b %Y %H:%M:%S GMT",
@@ -623,7 +623,7 @@ class Collection(BaseCollection):
         if os.path.exists(path):
             raise ComponentExistsError(href)
         item = Item(self, vobject_item, href)
-        with self._atomic_write(path) as fd:
+        with self._atomic_write(path, newline="") as fd:
             fd.write(item.serialize())
         return item
 
@@ -634,7 +634,7 @@ class Collection(BaseCollection):
         if not os.path.isfile(path):
             raise ComponentNotFoundError(href)
         item = Item(self, vobject_item, href)
-        with self._atomic_write(path) as fd:
+        with self._atomic_write(path, newline="") as fd:
             fd.write(item.serialize())
         return item
 
@@ -695,7 +695,7 @@ class Collection(BaseCollection):
                 continue
             path = os.path.join(self._filesystem_path, href)
             if os.path.isfile(path):
-                with open(path, encoding=self.encoding) as fd:
+                with open(path, encoding=self.encoding, newline="") as fd:
                     items.append(vobject.readOne(fd.read()))
         if self.get_meta("tag") == "VCALENDAR":
             collection = vobject.iCalendar()