Skip to content
Browse files

use thread-local data, use safer tmpfile

  • Loading branch information
ralsina committed Jan 8, 2016
1 parent e29ac1a commit f5aefddbc789b9b3c0fd16871b2e8a7e5cd6fed7
Showing with 14 additions and 10 deletions.
  1. +14 −10 nikola/
@@ -29,6 +29,8 @@
import json
import os
import shutil
import tempfile
import threading

from . import utils

@@ -47,33 +49,35 @@ def __init__(self, path):
"""Where do you want it persisted."""
self._path = path
utils.makedirs(os.path.dirname(path)) = {}
self._local = threading.local() = {}

def get(self, key):
"""Get data stored in key."""

def set(self, key, value):
"""Store value in key."""
self._read()[key] = value[key] = value

def delete(self, key):
"""Delete key and the value it contains."""
if key in
if key in

def _read(self):
if os.path.isfile(self._path):
with open(self._path) as inf: = json.load(inf) = json.load(inf)

def _save(self):
tpath = self._path + '.tmp'
with open(tpath, 'w') as outf:
json.dump(, outf, sort_keys=True, indent=2)
shutil.move(tpath, self.path)
dname = os.path.dirname(self._path)
with tempfile.NamedTemporaryFile(dir=dname, delete=False) as outf:
tname =
json.dump(, outf, sort_keys=True, indent=2)
shutil.move(tname, self.path)

0 comments on commit f5aefdd

Please sign in to comment.
You can’t perform that action at this time.