diff options
-rw-r--r-- | hasher.py | 37 | ||||
-rwxr-xr-x | kaylee.py | 26 | ||||
-rw-r--r-- | languageupdater.py | 23 | ||||
-rw-r--r-- | recognizer.py | 3 |
4 files changed, 69 insertions, 20 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) diff --git a/kaylee.py b/kaylee.py index 4774137..3f25a5b 100755 --- a/kaylee.py +++ b/kaylee.py @@ -11,12 +11,12 @@ import signal import os.path import subprocess from gi.repository import GObject, GLib -import json from recognizer import Recognizer from config import Config from languageupdater import LanguageUpdater from numberparser import NumberParser +from hasher import Hasher class Kaylee: @@ -27,8 +27,6 @@ class Kaylee: ui_continuous_listen = False self.continuous_listen = False - self.commands = {} - # Load configuration self.config = Config() self.options = vars(self.config.options) @@ -37,8 +35,11 @@ class Kaylee: # Create number parser for later use self.number_parser = NumberParser() + # Create a hasher + self.hasher = Hasher(self.config) + # Create the strings file - self.create_strings_file() + self.update_strings_file_if_changed() if self.options['interface']: if self.options['interface'] == "g": @@ -71,6 +72,23 @@ class Kaylee: self.recognizer = Recognizer(self.config) self.recognizer.connect('finished', self.recognizer_finished) + def update_voice_commands_if_changed(self): + """Use hashes to test if the voice commands have changed""" + stored_hash = self.hasher['voice_commands'] + + # Calculate the hash the voice commands have right now + hasher = self.hasher.get_hash_object() + for voice_cmd in self.commands.keys(): + hasher.update(voice_cmd.encode('utf-8')) + # Add a separator to avoid odd behavior + hasher.update('\n'.encode('utf-8')) + new_hash = hasher.hexdigest() + + if new_hash != stored_hash: + self.create_strings_file() + self.hasher['voice_commands'] = new_hash + self.hasher.store() + def create_strings_file(self): # Open the strings file with open(self.config.strings_file, 'w') as strings: diff --git a/languageupdater.py b/languageupdater.py index 98397c7..3f36d06 100644 --- a/languageupdater.py +++ b/languageupdater.py @@ -3,16 +3,17 @@ # Copyright 2015-2016 Clayton G. Hobbs # Portions Copyright 2013 Jezra -import hashlib -import json import re import requests +from hasher import Hasher + class LanguageUpdater: def __init__(self, config): self.config = config + self.hasher = Hasher(config) def update_language_if_changed(self): """Test if the language has changed, and if it has, update it""" @@ -21,18 +22,11 @@ class LanguageUpdater: self.save_language_hash() def language_has_changed(self): - """Use SHA256 hashes to test if the language has changed""" - # Load the stored hash from the hash file - try: - with open(self.config.hash_file, 'r') as f: - hashes = json.load(f) - self.stored_hash = hashes['language'] - except (IOError, KeyError, TypeError): - # No stored hash - self.stored_hash = '' + """Use hashes to test if the language has changed""" + self.stored_hash = self.hasher['language'] # Calculate the hash the language file has right now - hasher = hashlib.sha256() + hasher = self.hasher.get_hash_object() with open(self.config.strings_file, 'rb') as sfile: buf = sfile.read() hasher.update(buf) @@ -75,9 +69,8 @@ class LanguageUpdater: self._download_file(dic_url, self.config.dic_file) def save_language_hash(self): - new_hashes = {'language': self.new_hash} - with open(self.config.hash_file, 'w') as f: - json.dump(new_hashes, f) + self.hasher['language'] = self.new_hash + self.hasher.store() def _download_file(self, url, path): r = requests.get(url, stream=True) diff --git a/recognizer.py b/recognizer.py index 2ab1945..b54c055 100644 --- a/recognizer.py +++ b/recognizer.py @@ -15,7 +15,8 @@ Gst.init(None) class Recognizer(GObject.GObject): __gsignals__ = { - 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)) + 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, + (GObject.TYPE_STRING,)) } def __init__(self, config): |