Ejemplo n.º 1
0
def main(data=None):
    sys.stdout = codecs.getwriter('mbcs' if os.name == 'nt' else 'utf-8')(sys.stdout, 'replace')
    if data is None:
        data = os.path.join(get_main_dir(), 'data')
    if not os.path.exists(data):
        raise SystemExit('Your need a full package with the data folder')
    print '[SYSTEM]', 'Booted on', get_system_info(), '[/SYSTEM]'
    print
    print '[SYSTEM]'
    hal = HAL(write=True, speak=True)
    print '[/SYSTEM]'
    print
    print '-HAL: Hello %s. I am HAL %s.'%(hal.user, hal.version)
    print
    prompt = '-%s:'%hal.user
    halpro = '-HAL:'
    length = max(len(prompt), len(halpro))
    if len(prompt) < length:
        prompt += ' '*(length-len(prompt))
    if len(halpro) < length:
        halpro += ' '*(length-len(halpro))
    prompt += ' '
    try:
        while hal.running:
            line = raw_input(prompt)
            for i in hal.ask(line):
                print halpro, i
            print
        raise EOFError
    except EOFError:
        print '-HAL:', hal.shutdown()
Ejemplo n.º 2
0
class MainWin(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainWin.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, -1, _("HAL"), style=wx.ALIGN_CENTRE)
        self.datetime = wx.TextCtrl(self, -1, _("Date: (Unknown)\nTime: (Unknown)"), style=wx.TE_MULTILINE | wx.TE_READONLY)
        self.output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_WORDWRAP)
        self.input = wx.TextCtrl(self, 6, "", style=wx.TE_PROCESS_ENTER)
        self.ask_btn = wx.Button(self, 1, _("&Ask"))
        self.stop_talking_btn = wx.Button(self, 3, _("Stop Talking!"))
        self.clear_out_btn = wx.Button(self, 4, _("Clear Output"))
        self.save_btn = wx.Button(self, 7, _("Save Output"))
        self.options_btn = wx.Button(self, 5, _("&Options"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.Ask, id=1)
        self.Bind(wx.EVT_BUTTON, self.stop_talking, id=3)
        self.Bind(wx.EVT_BUTTON, self.clear_output, id=4)
        self.Bind(wx.EVT_BUTTON, self.save_output, id=7)
        self.Bind(wx.EVT_BUTTON, self.open_options, id=5)
        # end wxGlade
        if os.name == 'nt':
            font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, self.font_name)
            self.output.SetFont(font)
            self.input.SetFont(font)
        thread = threading.Thread(target=self.start_hal)
        thread.daemon = True
        thread.start()
        
        self.time_lock = threading.Lock()
        thread = threading.Thread(target=self.timer)
        thread.start()
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.Bind(EVT_GUI_PRINT_EVENT, self.ThreadPrint)

    def ThreadPrint(self, event):
        self.output.AppendText(event.string)
    
    def OnCloseWindow(self, event):
        try:
            self.closewindowstarted
        except AttributeError:
            self.closewindowstarted = True
            self.time_lock.acquire()
            self.Destroy()
    
    def timer(self):
        while True:
            if not self.time_lock.acquire(0):
                break
            self.datetime.SetValue(time.strftime('Date: %B %d, %Y\nTime: %H:%M:%S'))
            if hasattr(self, 'lasttalk') and not self.working and time.time()-self.lasttalk > 60:
                msg = self.hal.autotalk()
                print self.halpro, msg
                print
                self.lasttalk = time.time()
                self.hal.do_speech(msg)
            time.sleep(1)
            self.time_lock.release()
    
    def __set_properties(self):
        # begin wxGlade: MainWin.__set_properties
        self.SetTitle(_("HAL"))
        self.SetSize((700, 530))
        self.SetBackgroundColour(wx.Colour(240, 240, 240))
        self.label_1.SetForegroundColour(wx.Colour(0, 0, 255))
        self.label_1.SetFont(wx.Font(20, wx.MODERN, wx.NORMAL, wx.BOLD, 0, ""))
        self.datetime.SetMinSize((150, 40))
        self.input.SetFocus()
        self.ask_btn.Enable(False)
        self.options_btn.Enable(False)
        # end wxGlade
        if os.name == 'nt':
            self.label_1.SetFont(wx.Font(25, wx.MODERN, wx.NORMAL, wx.BOLD, 0, 'Consolas'))
        sys.stdout = RedirectText(self.output, threading.current_thread(), self)

    def __do_layout(self):
        # begin wxGlade: MainWin.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6.Add(self.label_1, 1, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_6.Add(self.datetime, 0, 0, 0)
        sizer_1.Add(sizer_6, 0, wx.EXPAND, 0)
        sizer_3.Add(self.output, 1, wx.EXPAND, 0)
        sizer_4.Add(self.input, 1, wx.EXPAND, 0)
        sizer_4.Add(self.ask_btn, 0, 0, 0)
        sizer_3.Add(sizer_4, 0, wx.EXPAND, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_5.Add(self.stop_talking_btn, 0, 0, 0)
        sizer_5.Add(self.clear_out_btn, 0, 0, 0)
        sizer_5.Add(self.save_btn, 0, 0, 0)
        sizer_5.Add((0, 0), 1, wx.EXPAND, 0)
        sizer_5.Add(self.options_btn, 0, wx.EXPAND, 0)
        sizer_2.Add(sizer_5, 0, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
    
    def start_hal(self):
        data = os.path.join(get_main_dir(), 'data')
        print '[SYSTEM]', 'Booted on', get_system_info(), '[/SYSTEM]'
        print
        print 'Loading data files...'
        self.hal = HAL(speak=True, write=True)
        if not os.path.exists(data):
            print 'Your need a full package with the data folder'
        prompt = '-%s:'%self.hal.user
        halpro = '-HAL:'
        length = max(len(prompt), len(halpro))
        self.prompt = prompt.ljust(length)
        self.halpro = halpro.ljust(length)
        self.output.SetValue('')
        print self.halpro, 'Hello %s. I am HAL %s.'%(self.hal.user, self.hal.version)
        print
        self.options_btn.Enable(True)
        self.ask_btn.Enable(True)
        self.working = True
        self.Bind(wx.EVT_TEXT_ENTER, self.input_enter, id=6)
        self.lasttalk = time.time()

    def Ask(self, event):  # wxGlade: MainWin.<event_handler>
        self.input.Enable(False)
        self.working = True
        input = self.input.GetValue()
        try:
            if not input.strip():
                event.Skip()
                raise Done
            print self.prompt, input
            for i in self.hal.ask(input.encode('utf-8')):
                print self.halpro, i
            if not self.hal.running:
                print self.halpro, self.hal.shutdown()
                self.hal.running = True
            print
            raise Done
        except Done:
            self.working = False
            self.input.Clear()
            self.input.Enable(True)
            self.input.SetFocus()
        self.lasttalk = time.time()

    def mute_changed(self, event):  # wxGlade: MainWin.<event_handler>
        if self.mute.IsChecked():
            self.hal.speak = False
            self.stop_talking(None)
        else:
            self.hal.speak = True

    def stop_talking(self, event):  # wxGlade: MainWin.<event_handler>
        stop_speaking(self.hal.sphandle)

    def clear_output(self, event):  # wxGlade: MainWin.<event_handler>
        self.output.Clear()

    def input_enter(self, event):  # wxGlade: MainWin.<event_handler>
        if self.working:
            event.Skip()
        self.Ask(event)

    def open_options(self, event):  # wxGlade: MainWin.<event_handler>
        HALOptions(self).Show()

    def save_output(self, event):  # wxGlade: MainWin.<event_handler>
        dialog = wx.FileDialog(self, "Save Output...", "", "",
                               "Text File (*.txt)|*.txt", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)

        if dialog.ShowModal() == wx.ID_CANCEL:
            return
        
        with open(dialog.GetPath(), 'w') as f:
            f.write(self.output.GetValue())