GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit af849a06 authored by juga  's avatar juga

chg: state: Encode/decode datetimes

parent f19738f1
...@@ -2,6 +2,8 @@ from sbws.util.filelock import FileLock ...@@ -2,6 +2,8 @@ from sbws.util.filelock import FileLock
import os import os
import json import json
from .json import CustomDecoder, CustomEncoder
class State: class State:
""" """
...@@ -48,12 +50,12 @@ class State: ...@@ -48,12 +50,12 @@ class State:
return {} return {}
with FileLock(self._fname): with FileLock(self._fname):
with open(self._fname, 'rt') as fd: with open(self._fname, 'rt') as fd:
return json.load(fd) return json.load(fd, cls=CustomDecoder)
def _write(self): def _write(self):
with FileLock(self._fname): with FileLock(self._fname):
with open(self._fname, 'wt') as fd: with open(self._fname, 'wt') as fd:
return json.dump(self._state, fd, indent=4) return json.dump(self._state, fd, indent=4, cls=CustomEncoder)
def __len__(self): def __len__(self):
self._state = self._read() self._state = self._read()
......
...@@ -83,3 +83,11 @@ def test_two_instances(tmpdir): ...@@ -83,3 +83,11 @@ def test_two_instances(tmpdir):
s1["x"] = "foo" s1["x"] = "foo"
s2["y"] = "bar" s2["y"] = "bar"
assert s2["x"] == "foo" assert s2["x"] == "foo"
def test_datetime_values(tmpdir):
import datetime
state = State(os.path.join(str(tmpdir), 'state.dat'))
now = datetime.datetime.utcnow().replace(microsecond=0)
state["datetimes"] = now
assert now == state["datetimes"]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment