def send_cmd(self, cmd, echo=False, wait_for=None, save_history=True): if hasattr(self, 'fics'): if wait_for: self._wait_for_txt = wait_for config.log('Waiting for: '+wait_for) if save_history: self.cmd_line.cmd_history_idx = 0 if (len(self.cmd_line.cmd_history) and self.cmd_line.cmd_history[-1] != cmd or not len(self.cmd_line.cmd_history)): self.cmd_line.cmd_history.append(cmd) data = cmd.translate(config.TRANS_TABLE) \ .encode("ascii", "ignore")+b'\n' config.log(data, sent=True) self.fics.write(data) if wait_for: if not self._wait_for_sem.acquire(timeout=5): self._wait_for_txt = None config.log('Waiting for failed') return False config.log('Waiting for succeed') self._wait_for_txt = None if echo: self.print('> '+cmd, urwid.AttrSpec(config.console.echo_color, 'default')) return True
def run(fics_pass): GObject.threads_init() if config.general.log: config.logfd = open(os.path.expanduser(config.general.log),'a') config.cli = CLI(fics_pass) config.gui = GUI() try: config.cli.connect_mainloop() except: txt = PrintException() if config.logfd: config.log(str(txt)) config.cli.exit(True) if config.logfd: config.logfd.close() Gtk.main_quit()
def connect_fics(self): if config.general.timeseal: self.fics = FicsTimesealConnection() else: self.fics = telnetlib.Telnet('freechess.org', port=5000) # login: data = fics_filter(self.fics.read_until(b'login: '******'<_>') self.cmd_line.insert_text('.') self.main_loop.draw_screen() # > login data = config.fics_user.encode('ascii') + b'\n' config.log(data, True) self.fics.write(data) # pass: data = fics_filter(self.fics.read_until(b':')) if config.fics_user == 'guest': config.fics_user = data.split()[-1].strip(b'":').decode('ascii', 'ignore') else: config.fics_user = data.split()[0].strip(b'"').decode('ascii', 'ignore') config.FICS_HANDLES.append(config.fics_user) self.re_rules() config.log(data) self.read_pipe(data+b'<_>') self.cmd_line.insert_text('.') self.main_loop.draw_screen() # > pass data = self.fics_pass.encode('ascii') + b'\n' config.log(data, True) self.fics.write(data) # prompt data = fics_filter(self.fics.read_until(b'fics% ')) config.log(data) self.read_pipe(data+b'<_>') self.cmd_line.insert_text('.') self.main_loop.draw_screen() # > startup commands for cmd in config.general.startup_command: self.send_cmd(cmd, save_history=False, record_handle=False) self.cmd_line.insert_text('.') self.main_loop.draw_screen() self.pipe = self.main_loop.watch_pipe(self.read_pipe) self.fics_thread = threading.Thread(target=self.fics_read) self.fics_thread.start()
def mouse_event(self, size, event, button, col, row, focus): # No sirve con vtwheel self.body_size = size if event == 'mouse press': if button == 5.0: self.keypress(size, 'page down') elif button == 4.0: self.keypress(size, 'page up') elif button == 1.0: eggs = super(CLI, self).mouse_event(size, event, button, col, row, focus) if isinstance(eggs, tuple): widget, txt_row, word, txt_line = eggs if txt_row and len(txt_row) and word: m = self.san_rule.match(word) if m and self.may_I_move(): moves = self.san_rule.findall(txt_line) moves = moves[:moves.index(word)+1] if (':[Game ' in txt_line and 'Book' not in txt_line and len(moves) > 1 and widget == self._last_AB): gn_start = txt_line.find(':[Game ')+7 gn_end = txt_line.find(']') gn = int(txt_line[gn_start:gn_end]) g = self.game_with_number(gn) config.log('AB moves: '+' '.join(moves)) p = Pgn(txt=' '.join(moves), ic=g._history[-1]) threading.Thread(target=self.send_moves, args=(p.main_line[1::], gn,) ).start() else: self.send_cmd(word, echo=True) else: m = self.handle_rule.match(word) if m: if self.handle_commands: self.handle_commands.destroy() self.handle_commands = \ HandleCommands(m.group()) return True
def fics_read(self): try: while not self.die: #data = self.fics.read_until(b'fics% ', data = self.fics.read_until(b'\n\r', timeout=(None if config.general.connection_test_timeout == 0 else config.general.connection_test_timeout)) if not data: threading.Thread(target=self.test_connection).start() elif data != b'fics% \n\r': config.log(data) data = fics_filter(data) if self._wait_for_txt and (self._wait_for_txt in data.decode('ascii','ignore')): self._wait_for_sem.release() else: os.write(self.pipe, data+b'\n') self.fics.close() except: del self.fics self.print('=== We got DISCONNECTED! try %c ===', urwid.AttrSpec(config.console.echo_color, 'default'))
def send_cmd(self, cmd, echo=False, wait_for=None, ans_buff=None, save_history=True, record_handle=True): if hasattr(self, 'fics'): if save_history: if (len(self.cmd_line.cmd_history) and self.cmd_line.cmd_history[-1] != cmd or not len(self.cmd_line.cmd_history)): self.cmd_line.cmd_history.append(cmd) if record_handle: if ' ' in cmd and len(cmd.split())>1: m = self.handle_rule.match(cmd.split()[1]) if m: config.update_handle(m.group()) data = cmd.translate(config.TRANS_TABLE) \ .encode("ascii", "ignore")+b'\n' config.log(data, sent=True) if wait_for: self._wait_for_buf.clear() self._wait_for_txt = wait_for config.log('Waiting for: '+str(wait_for)) self.fics.write(data) if wait_for: if not self._wait_for_sem.acquire(timeout=5): self._wait_for_txt = None self.ML_recording = False config.log('Waiting for failed') return False config.log('Waiting for succeed') self._wait_for_txt = None if isinstance(ans_buff, list): ans_buff.append(''.join(self._wait_for_buf)) if echo: self.print('> '+cmd, urwid.AttrSpec(config.console.echo_color, 'default')) return True
def __new__(cls, txt=None, path=None, hdr=None, it=None, ic=None): self = object.__new__(cls) if ic: last_move = GameState(ic) else: last_move = GameState() self.main_line = GameHistory([last_move]) self.header = list() if hdr: self.header.append(hdr) hdr_done = False var_stem = list() if path: with open(path, 'r') as fd: it = _pgn_re.finditer(fd.read()) elif txt: config.log('Pgn got txt="{}"'.format(txt)) it = _pgn_re.finditer(txt) for m in it: if m.group('hdr_name'): if hdr_done: self = [self] tail = Pgn(hdr=(m.group('hdr_name'), m.group('hdr')), it=it) if isinstance(tail, list): self.extend(tail) else: self.append(tail) break else: self.header.append((m.group('hdr_name'),m.group('hdr'))) if m.group('hdr_name').lower() == 'fen': last_move = GameState(m.group('hdr')) self.main_line = GameHistory([last_move]) elif m.group('vstart'): hdr_done = True var_stem.append(last_move) last_move = last_move.prev elif m.group('vend'): hdr_done = True last_move = var_stem.pop() elif m.group('move'): hdr_done = True s = last_move.make(San(m.group('move'))) # print(m.group('move'), end=' -> ') # print(s) if not s: config.cli.print('There was an error processing: {}'.format(m.group('move'))) config.log('There was an error processing: {}'.format(m.group('move'))) w = next((y[1] for y in self.header if y[0].lower() == 'white'), '') b = next((y[1] for y in self.header if y[0].lower() == 'black'), '') config.cli.print('{} v/s {}'.format(w, b)) break # last_move.next.append(s) last_move = s if not len(var_stem): self.main_line.append(s) else: self.main_line.update_reg(s) elif m.group('comment'): if last_move.comment: last_move.comment = last_move.comment + ' ' + m.group('comment') else: last_move.comment = m.group('comment') return self
def fics_read(self): try: while not self.die: data = self.fics.read_until(b'fics% ', # data = self.fics.read_until(b'\n\r', timeout=(None if config.general.connection_test_timeout == 0 else config.general.connection_test_timeout)) if not data: threading.Thread(target=self.test_connection).start() elif data != b'fics% \n\r': data = fics_filter(data) config.log(data) if self._wait_for_txt == WAIT_FOR_MOVELIST: odata = None if (not self.ML_recording and b'Movelist for game' in data): self.ML_recording = True idx = data.index(b'Movelist for game') odata = data[:idx] data = data[idx::] if self.ML_recording: txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) if b' {' in data: idx = data.index(b' {') oidx = data.find(b'\n', idx) if oidx > 0: odata = data[oidx::] else: odata = b'' self._wait_for_buf.pop() data = data[:idx] txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) self.ML_recording = False self._wait_for_sem.release() data = odata if odata else b'' elif self._wait_for_txt == WAIT_FOR_WHO: odata = None m = self.WHO_RULE.search(data.decode('ascii','ignore')) if not self.ML_recording and m: self.ML_recording = True idx = m.start() odata = data[:idx] data = data[idx::] if self.ML_recording: txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) if b'indicates system administrator.' in data: idx = data.index(b'indicates system administrator.') odata = data[idx+31::] self._wait_for_buf.pop() data = data[:idx] txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) self.ML_recording = False self._wait_for_sem.release() data = odata if odata else b'' elif self._wait_for_txt == WAIT_FOR_ANALYSIS: odata = None wf = ['Game {}: {} moves: '.format(self._wait_for_gn, config.fics_user).encode(), ':[Game {}] '.format(self._wait_for_gn).encode() ] m = next((x for x in wf if x in data), None) if m: if m == wf[1]: self._wait_for_sem.release() idx = data.find(m) odata = data[:idx] data = data[idx::] end = data.rfind(b'\n', idx) if end > -1: odata += data[end::] txt = data[:end].decode('ascii','ignore') else: txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) data = data if odata is None else odata elif self._wait_for_txt: txt = data.decode('ascii','ignore') self._wait_for_buf.append(txt) if self._wait_for_txt in txt: self._wait_for_sem.release() data = data+b'<_>' os.write(self.pipe, data) self.fics.close() except: del self.fics self.print('=== We got DISCONNECTED! try %c ===', urwid.AttrSpec(config.console.echo_color, 'default'))