summary refs log tree commit diff
path: root/hasher.py
diff options
context:
space:
mode:
authorClayton G. Hobbs <clay@lakeserv.net>2016-02-12 19:27:32 -0500
committerClayton G. Hobbs <clay@lakeserv.net>2016-02-12 19:27:32 -0500
commitbc07966df1831a73df715bec4ef7014631c9dadb (patch)
tree6862e58851579f6b92740c01ace47e8b01e56540 /hasher.py
parent9b9cc013ab324b98fcb3ec883c97d57df232a808 (diff)
Only write the strings file if necessary
Now we check a hash of the voice commands before writing the strings
file to reduce how much we write to the hard disk.  In implementing
this, I realized that some code was being duplicated in an easily
fixable way, so I created a Hasher object that keeps track of the
hash.json file.

Resolves #6
Diffstat (limited to 'hasher.py')
-rw-r--r--hasher.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/hasher.py b/hasher.py
new file mode 100644
index 0000000..4aebd51
--- /dev/null
+++ b/hasher.py
@@ -0,0 +1,37 @@
+# This is part of Kaylee
+# -- this code is licensed GPLv3
+# Copyright 2015-2016 Clayton G. Hobbs
+# Portions Copyright 2013 Jezra
+
+import json
+import hashlib
+
+class Hasher:
+    """Keep track of hashes for Kaylee"""
+
+    def __init__(self, config):
+        self.config = config
+        try:
+            with open(self.config.hash_file, 'r') as f:
+                self.hashes = json.load(f)
+        except IOError:
+            # No stored hash
+            self.hashes = {}
+
+    def __getitem__(self, hashname):
+        try:
+            return self.hashes[hashname]
+        except (KeyError, TypeError):
+            return None
+
+    def __setitem__(self, hashname, value):
+        self.hashes[hashname] = value
+
+    def get_hash_object(self):
+        """Returns an object to compute a new hash"""
+        return hashlib.sha256()
+
+    def store(self):
+        """Store the current hashes into a the hash file"""
+        with open(self.config.hash_file, 'w') as f:
+            json.dump(self.hashes, f)