From bc07966df1831a73df715bec4ef7014631c9dadb Mon Sep 17 00:00:00 2001 From: "Clayton G. Hobbs" Date: Fri, 12 Feb 2016 19:27:32 -0500 Subject: 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 --- kaylee.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'kaylee.py') 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: -- cgit 1.4.1