Skip to content
Snippets Groups Projects
selector.py 6.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • Benoit Favre's avatar
    Benoit Favre committed
    from gi.repository import GObject, Gtk, Gdk
    
    import os, sys, glob, re
    
    Benoit Favre's avatar
    Benoit Favre committed
    import config
    
    class ModelSelector(Gtk.Dialog):
    
        def __init__(self, asr_model = ''):
    
    Benoit Favre's avatar
    Benoit Favre committed
            super(ModelSelector, self).__init__()
    
            self.options = {'asr_model': asr_model, 'osc_host': '127.0.0.1', 'osc_port': '1234', 'slu': '0', 'adaptation': 'macbook'}
    
            self.load_options()
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.set_title('Configuration')
    
            self.set_border_width(10)
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.add_button("Cancel", Gtk.ResponseType.CANCEL)
            self.add_button("OK", Gtk.ResponseType.OK)
            box = self.get_content_area()
    
    
            #xml_box = Gtk.HBox()
            #xml_box.pack_start(Gtk.Label('XML file:'), False, False, 5)
            #xml_entry = Gtk.Entry()
            #xml_entry.set_text(self.options['xml_filename'])
            #xml_entry.set_width_chars(len(self.options['xml_filename']))
            #self.xml_entry = xml_entry
            #xml_box.pack_start(xml_entry, True, True, 5)
            #xml_button = Gtk.Button("Choose...")
            #xml_button.connect('clicked', self.show_filechooser)
            #xml_box.pack_start(xml_button, False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
            #box.pack_start(xml_box, False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            model_box = Gtk.HBox()
    
            model_box.pack_start(Gtk.Label('ASR model:'), False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
            model_chooser = Gtk.ComboBoxText()
            model_chooser.set_entry_text_column(0)
    
    Benoit Favre's avatar
    Benoit Favre committed
            target_index = 0
    
    Benoit Favre's avatar
    Benoit Favre committed
            for i, model in enumerate(self.list_models()):
                model_chooser.append_text(model)
    
                if self.options['asr_model'] == self.models[i]:
    
    Benoit Favre's avatar
    Benoit Favre committed
                    target_index = i
            model_chooser.set_active(target_index)
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.model_chooser = model_chooser
    
            model_box.pack_start(model_chooser, True, True, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            box.pack_start(model_box, False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
            self.adaptations = ['show', 'macbook', 'none']
            adaptation_box = Gtk.HBox()
            adaptation_box.pack_start(Gtk.Label('Adaptation:'), False, False, 5)
            adaptation_chooser = Gtk.ComboBoxText()
            adaptation_chooser.set_entry_text_column(0)
            target_index = 0
            for i, adaptation in enumerate(self.adaptations):
                adaptation_chooser.append_text(adaptation)
                if self.options['adaptation'] == adaptation:
                    target_index = i
            adaptation_chooser.set_active(target_index)
            self.adaptation_chooser = adaptation_chooser
            adaptation_box.pack_start(adaptation_chooser, True, True, 5)
    
            box.pack_start(adaptation_box, False, False, 5)
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            slu_box =Gtk.HBox()
            slu_box.pack_start(Gtk.Label('SLU model:'), False, False, 5)
            slu_chooser = Gtk.ComboBoxText()
            slu_chooser.set_entry_text_column(0)
            for i, slu in enumerate(['Regular', 'Alternative']):
                slu_chooser.append_text(slu)
            slu_chooser.set_active(int(self.options['slu']))
            self.slu_chooser = slu_chooser
            slu_box.pack_start(slu_chooser, True, True, 5)
    
            box.pack_start(slu_box, False, False, 5)
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            osc_box = Gtk.HBox()
    
            osc_box.pack_start(Gtk.Label('OSC host:'), False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
            osc_host = Gtk.Entry()
    
            osc_host.set_text(self.options['osc_host'])
    
    Benoit Favre's avatar
    Benoit Favre committed
            osc_host.set_width_chars(len(osc_host.get_text()))
    
            self.osc_host = osc_host
            osc_box.pack_start(osc_host, True, True, 5)
            osc_box.pack_start(Gtk.Label('Port:'), False, False, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
            osc_port = Gtk.Entry()
    
            osc_port.set_text(self.options['osc_port'])
    
    Benoit Favre's avatar
    Benoit Favre committed
            osc_port.set_width_chars(len(osc_port.get_text()))
    
            self.osc_port = osc_port
            osc_box.pack_start(osc_port, True, True, 5)
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            box.pack_start(osc_box, False, False, 5)
    
    
            #self.xml_entry.set_activates_default(True)
    
    Benoit Favre's avatar
    Benoit Favre committed
            okButton = self.get_widget_for_response(response_id=Gtk.ResponseType.OK)
            okButton.set_can_default(True)
            okButton.grab_default()
    
            okButton.grab_focus()
    
    Benoit Favre's avatar
    Benoit Favre committed
    
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.show_all()
    
    
        def save_options(self):
            try:
                with open('.options.txt', 'w') as fp:
                    for key, value in self.options.items():
                        print >>fp, '%s: %s' % (key, str(value))
            except:
                print 'Warning: could not save options'
    
        def load_options(self):
            try:
                with open('.options.txt') as fp:
                    for line in fp:
                        line = line.strip()
                        found = re.search('^([^:]*):(.*)', line)
                        if found:
                            key = found.group(1).strip()
                            value = found.group(2).strip()
                            self.options[key] = value
            except:
                print 'Warning: could not load options'
    
    
    Benoit Favre's avatar
    Benoit Favre committed
        def show_filechooser(self, button):
            dialog = Gtk.FileChooserDialog("Please choose a file", self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
            filter_text = Gtk.FileFilter()
            filter_text.set_name("XML files")
            filter_text.add_mime_type("text/xml")
            dialog.add_filter(filter_text)
    
            dialog.set_current_folder('%s/data' % (os.path.dirname(__file__) or '.'))
    
    Benoit Favre's avatar
    Benoit Favre committed
    
            response = dialog.run()
            if response == Gtk.ResponseType.OK:
                self.xml_entry.set_text(dialog.get_filename())
            dialog.destroy()
    
        def list_models(self):
            self.models = []
            model_names = []
    
            for filename in glob.glob('asr/models/*.cfg'):
    
    Benoit Favre's avatar
    Benoit Favre committed
                try:
                    items = config.read(filename)
                    self.models.append(filename)
                    if 'name' in items:
                        model_names.append(items['name'])
                    else:
                        model_names.append(filename)
                except:
                   pass
            return model_names
    
        def run(self):
            response = super(ModelSelector, self).run()
            if response != Gtk.ResponseType.OK:
    
                return None, None, None, None, None
    
            self.options['asr_model'] = self.models[self.model_chooser.get_active()]
    
            self.options['adaptation'] = self.adaptations[self.adaptation_chooser.get_active()]
    
            #self.options['xml_filename'] = self.xml_entry.get_text()
    
            self.options['osc_host'] = self.osc_host.get_text()
            self.options['osc_port'] = self.osc_port.get_text()
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.options['slu'] = self.slu_chooser.get_active()
    
            self.save_options()
    
    Benoit Favre's avatar
    Benoit Favre committed
            self.destroy()
    
            return self.options['asr_model'], self.options['osc_host'], self.options['osc_port'], self.options['slu'], self.options['adaptation']
    
    Benoit Favre's avatar
    Benoit Favre committed