コード例 #1
0
ファイル: framework.py プロジェクト: TaoZong/Python
 def schedule(self, msgs_to_process=None, timers_to_process=None):
     """Schedule given number of pending messages"""
     if msgs_to_process is None:
         msgs_to_process = 32768
     if timers_to_process is None:
         timers_to_process = 32768
     while self.queue:
         msg = self.queue.popleft()
         try:
             c = zerorpc.Client(timeout=1)
             c.connect('tcp://' + msg.to_node)
         except zerorpc.TimeoutExpired:
             _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg)
             History.add("drop", msg)
             self.dynamo.retry_request(msg)
         if isinstance(msg, ResponseMessage):
                     # figure out the original request this is a response to
             try:
                 reqmsg = msg.response_to.original_msg
             except Exception:
                 reqmsg = msg.response_to
         History.add("deliver", msg)
         m = pickle.dumps(msg)
         try:
             c.rcvmsg(m)
             c.close()
         except:
             print 'time out'
             print msg.to_node
             self.dynamo.retry_request(msg)
コード例 #2
0
ファイル: timer.py プロジェクト: TaoZong/Python
 def cancel_timer(cls, tmsg):
     """Cancel the given timer"""
     for (this_prio, this_tmsg) in cls.pending:
         if this_tmsg == tmsg:
             _logger.debug("Cancel timer %s for node %s reason %s", id(tmsg), tmsg.from_node, tmsg.reason)
             cls.pending.remove((this_prio, this_tmsg))
             History.add("cancel", tmsg)
             return
コード例 #3
0
ファイル: timer.py プロジェクト: thtrieu/pynamo
 def cancel_timer(cls, tmsg):
     """Cancel the given timer"""
     for (this_prio, this_tmsg) in cls.pending:
         if this_tmsg == tmsg:
             _logger.debug("Cancel timer %s for node %s reason %s",
                           id(tmsg), tmsg.from_node, tmsg.reason)
             cls.pending.remove((this_prio, this_tmsg))
             History.add("cancel", tmsg)
             return
コード例 #4
0
ファイル: frameworkBack.py プロジェクト: TaoZong/Python
 def forward_message(cls, msg, new_to_node):
     """Forward a message"""
     _logger.info("Enqueue(fwd) %s->%s: %s", msg.to_node, new_to_node, msg)
     fwd_msg = copy.copy(msg)
     fwd_msg.intermediate_node = fwd_msg.to_node
     fwd_msg.original_msg = msg
     fwd_msg.to_node = new_to_node
     cls.queue.append(fwd_msg)
     History.add("forward", fwd_msg)
コード例 #5
0
ファイル: framework.py プロジェクト: thtrieu/pynamo
 def forward_message(cls, msg, new_to_node):
     """Forward a message"""
     _logger.info("Enqueue(fwd) %s->%s: %s", msg.to_node, new_to_node, msg)
     fwd_msg = copy.copy(msg)
     fwd_msg.intermediate_node = fwd_msg.to_node
     fwd_msg.original_msg = msg
     fwd_msg.to_node = new_to_node
     cls.queue.append(fwd_msg)
     History.add("forward", fwd_msg)
コード例 #6
0
ファイル: test_dynamo.py プロジェクト: thtrieu/pynamo
    def partition_repair(self):
        # Repair the partition
        History.add("announce", "Repair network partition")
        Framework.cuts = []
        Framework.schedule(timers_to_process=12)

        # Get from node a
        a = Node.node['a']
        a.get('K1')
        Framework.schedule(timers_to_process=0)
コード例 #7
0
ファイル: test_dynamo.py プロジェクト: TaoZong/Python
    def partition_repair(self):
        # Repair the partition
        History.add("announce", "Repair network partition")
        Framework.cuts = []
        Framework.schedule(timers_to_process=12)

        # Get from node a
        a = Node.node['a']
        a.get('K1')
        Framework.schedule(timers_to_process=0)
コード例 #8
0
 def send_message(cls, msg, expect_reply=True):
     """Send a message"""
     _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg)
     cls.queue.append(msg)
     History.add("send", msg)
     # Automatically run timers for request messages if the sender can cope
     # with retry timer pops
     if (expect_reply and
         not isinstance(msg, ResponseMessage) and
         'rsp_timer_pop' in msg.from_node.__class__.__dict__ and
         callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])):
         cls.pending_timers[msg] = TimerManager.start_timer(msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
コード例 #9
0
ファイル: framework.py プロジェクト: thtrieu/pynamo
 def send_message(cls, msg, expect_reply=True):
     """Send a message"""
     _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg)
     cls.queue.append(msg)
     History.add("send", msg)
     # Automatically run timers for request messages if the sender can cope
     # with retry timer pops
     if (expect_reply and not isinstance(msg, ResponseMessage)
             and 'rsp_timer_pop' in msg.from_node.__class__.__dict__ and
             callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])):
         cls.pending_timers[msg] = TimerManager.start_timer(
             msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
コード例 #10
0
ファイル: main.py プロジェクト: mrhitman/poulpe
class MainWindow(QMainWindow):
    tray_icon = None

    def __init__(self):
        QMainWindow.__init__(self)
        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(QIcon('icon.png'))
        self.hide()

        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)

        quit_action = QAction("quit", self)
        quit_action.triggered.connect(qApp.quit)
        tray_menu = QMenu()
        tray_menu.addAction(quit_action)

        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

        self.pouple = Pouple()
        self.history = History()
        self.bind_keys()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.tray_icon.hide()

    def command(self, cmd):
        def execute():
            print(cmd.__name__)
            self.history.add(cmd)
            cmd()

        return execute

    def bind_keys(self):
        cfg = configparser.RawConfigParser()
        cfg.read('settings.cfg')

        hot_keys = {
            'align_left': self.pouple.align_left,
            'align_right': self.pouple.align_right,
            'align_top': self.pouple.align_top,
            'align_bottom': self.pouple.align_bottom,
            'center': self.pouple.center,
            'screen': self.pouple.screen,
            'fullscreen': self.pouple.fullscreen,
            'undo': self.history.undo,
            'redo': self.history.redo,
        }
        for name in hot_keys:
            keyboard.add_hotkey(cfg.get('keys', name), self.command(hot_keys[name]))
コード例 #11
0
def course_page(dept, course_id):
    try:  #format id, else tell user that id is invalid
        formatted_id = id_from_url(course_id)
    except ValueError:
        return '\'' + course_id + '\' is not a valid course id.'

    try:
        res = dbsession.query(Course)
        course = res.filter(Course.id == formatted_id).one()
    except:
        return 'Course \'' + course_id + '\' does not exist.'

    #get alt descs for course
    res = dbsession.query(AltDesc)
    alt_desc_list = list(res.filter(AltDesc.course_id == formatted_id))

    #get alt descs for courses that are the same (like CS220 and MATH220)
    #and concatenate them together
    for same_course in listify(course.same_as):
        additional_alt_descs = dbsession.query(AltDesc).filter(
            AltDesc.course_id == same_course)
        alt_desc_list += list(additional_alt_descs)

    #remove unapproved alt descs
    alt_desc_list = filter(lambda alt_desc: alt_desc.approved, alt_desc_list)

    sections = dbsession.query(Section).\
                         filter( Section.course_id == formatted_id )
    term_offerings = OrderedDict()
    for term in terms:
        term_offerings[term] = sections.filter(Section.term == term)
        term_offerings[term] = list(term_offerings[term])

    course = search(dbsession, course_id=course.id)
    course = course[course.keys()[0]]

    form = AltDescForm()

    #Appends course title to history
    history = History()
    history.add(course)
    #Sets history cookie
    resp = make_response(
        render_template("course.html",
                        course=course,
                        form=form,
                        alt_descs=alt_desc_list,
                        terms=term_offerings,
                        history=history))
    resp.set_cookie('history', str(history),
                    max_age=365 * 24 * 60 * 60)  #cookie lasts a year
    return resp
コード例 #12
0
 def __init__(self, name=None):
     if name is None:
         self.name = Node.next_name()
     else:
         self.name = name
     self.next_sequence_number = 0
     self.included = True  # Whether this node is included in lists of nodes
     self.failed = False  # Indicates current failure
     # Keep track of node object <-> node name
     Node.node[self.name] = self
     Node.name[self] = self.name
     _logger.debug("Create node %s", self)
     History.add('add', NodeAction(self))
コード例 #13
0
ファイル: timer.py プロジェクト: TaoZong/Python
 def pop_timer(cls):
     """Pop the first pending timer"""
     while True:
         (_, tmsg) = cls.pending.pop(0)
         if tmsg.from_node.failed:
             continue
         _logger.debug("Pop timer %s for node %s reason %s", id(tmsg), tmsg.from_node, tmsg.reason)
         History.add("pop", tmsg)
         if tmsg.callback is None:
             # Default to calling Node.timer_pop()
             tmsg.from_node.timer_pop(tmsg.reason)
         else:
             tmsg.callback(tmsg.reason)
         return
コード例 #14
0
def course_page(dept, course_id):
    try: #format id, else tell user that id is invalid
        formatted_id = id_from_url(course_id)
    except ValueError:
        return '\''+course_id+'\' is not a valid course id.'

    try:
        res = dbsession.query(Course)
        course = res.filter(Course.id == formatted_id).one()
    except:
        return 'Course \'' + course_id + '\' does not exist.'

    #get alt descs for course
    res = dbsession.query(AltDesc)
    alt_desc_list = list(res.filter(AltDesc.course_id == formatted_id))

    #get alt descs for courses that are the same (like CS220 and MATH220)
    #and concatenate them together
    for same_course in listify(course.same_as):
        additional_alt_descs = dbsession.query(AltDesc).filter(AltDesc.course_id == same_course)
        alt_desc_list += list(additional_alt_descs)

    #remove unapproved alt descs
    alt_desc_list = filter(lambda alt_desc: alt_desc.approved, alt_desc_list)

    sections = dbsession.query(Section).\
                         filter( Section.course_id == formatted_id )
    term_offerings = OrderedDict()
    for term in terms:
        term_offerings[term] = sections.filter(Section.term==term)
        term_offerings[term] = list(term_offerings[term])

    course = search(dbsession, course_id = course.id)
    course = course[course.keys()[0]]

    form = AltDescForm()

    #Appends course title to history
    history = History()
    history.add(course)
    #Sets history cookie
    resp = make_response( render_template("course.html",
                                          course=course,
                                          form=form,
                                          alt_descs=alt_desc_list,
                                          terms=term_offerings,
                                          history=history) )
    resp.set_cookie('history', str(history), max_age=365*24*60*60) #cookie lasts a year
    return resp
コード例 #15
0
ファイル: timer.py プロジェクト: thtrieu/pynamo
 def pop_timer(cls):
     """Pop the first pending timer"""
     while True:
         (_, tmsg) = cls.pending.pop(0)
         if tmsg.from_node.failed:
             continue
         _logger.debug("Pop timer %s for node %s reason %s", id(tmsg),
                       tmsg.from_node, tmsg.reason)
         History.add("pop", tmsg)
         if tmsg.callback is None:
             # Default to calling Node.timer_pop()
             tmsg.from_node.timer_pop(tmsg.reason)
         else:
             tmsg.callback(tmsg.reason)
         return
コード例 #16
0
ファイル: timer.py プロジェクト: TaoZong/Python
 def start_timer(cls, node, reason=None, callback=None, priority=None):
     """Start a timer for the given node, with an option reason code"""
     if node.failed:
         return None
     tmsg = Timer(node, reason, callback=callback)
     History.add("start", tmsg)
     if priority is None:  # default to priority of the node
         priority = _priority(tmsg)
     _logger.debug("Start timer %s prio %d for node %s reason %s", id(tmsg), priority, node, reason)
     # Figure out where in the list to insert
     for ii in range(len(cls.pending)):
         if priority > cls.pending[ii][0]:
             cls.pending.insert(ii, (priority, tmsg))
             return tmsg
     cls.pending.append((priority, tmsg))
     return tmsg
コード例 #17
0
ファイル: framework.py プロジェクト: terencez127/mrpynamo
 def forward_message(cls, msg, con, new_to_node):
     """Forward a message"""
     _logger.info("Enqueue(fwd) %s->%s: %s", msg.to_node, new_to_node, msg)
     fwd_msg = copy.copy(msg)
     
     fwd_msg.intermediate_node = fwd_msg.to_node
     fwd_msg.original_msg = msg
     fwd_msg.to_node = new_to_node
     cls.queue.append(fwd_msg)
     History.add("forward", fwd_msg)
     #modified
     try:
         pmsg = pickle.dumps(msg)
         return con.rcvmsg(pmsg)
     except zerorpc.TimeoutExpired:
         print "forward message time out when", msg.from_node, "calls", new_to_node
         return False
コード例 #18
0
ファイル: timer.py プロジェクト: thtrieu/pynamo
 def start_timer(cls, node, reason=None, callback=None, priority=None):
     """Start a timer for the given node, with an option reason code"""
     if node.failed:
         return None
     tmsg = Timer(node, reason, callback=callback)
     History.add("start", tmsg)
     if priority is None:  # default to priority of the node
         priority = _priority(tmsg)
     _logger.debug("Start timer %s prio %d for node %s reason %s", id(tmsg),
                   priority, node, reason)
     # Figure out where in the list to insert
     for ii in range(len(cls.pending)):
         if priority > cls.pending[ii][0]:
             cls.pending.insert(ii, (priority, tmsg))
             return tmsg
     cls.pending.append((priority, tmsg))
     return tmsg
コード例 #19
0
    def play(self, n_step=10000, n_episode=100, test_ep=None, render=False):
        if test_ep == None:
            test_ep = self.ep_end

        test_history = History(self.config)

        best_reward, best_idx = 0, 0
        for idx in range(n_episode):
            screen, reward, action, terminal = self.env.new_random_game()
            current_reward = 0

            for _ in range(self.history_length):
                test_history.add(screen)

            for t in tqdm(range(n_step), ncols=70):
                # 1. predict
                action = self.predict(test_history.get(), test_ep)
                # 2. act
                screen, reward, terminal = self.env.act(action,
                                                        is_training=False)

                cv2.imshow(
                    'test',
                    cv2.resize(screen, (500, 500),
                               interpolation=cv2.INTER_NEAREST))
                cv2.waitKey(100)

                # 3. observe
                test_history.add(screen)

                current_reward += reward
                if terminal:
                    break

            if current_reward > best_reward:
                best_reward = current_reward
                best_idx = idx

            print("=" * 30)
            print(" [%d] Best reward : %d" % (best_idx, best_reward))
            print("=" * 30)

        cv2.waitKey(0)
        cv2.destroyAllWindows()
コード例 #20
0
ファイル: framework.py プロジェクト: terencez127/mrpynamo
 def send_message(cls, msg, con, expect_reply=True):
     """receive a message"""
     _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg)
     cls.queue.append(msg)
     History.add("receive", msg)
     # Automatically run timers for request messages if the sender can cope
     # with retry timer pops
     if (expect_reply and
         not isinstance(msg, ResponseMessage) and
         'rsp_timer_pop' in msg.from_node.__class__.__dict__ and
         callable(msg.from_node.__class__.__dict__['rsp_timer_pop'])):
         cls.pending_timers[msg] = TimerManager.start_timer(msg.from_node, reason=msg, callback=Framework.rsp_timer_pop)
     #modified
     try:
         pmsg = pickle.dumps(msg)
         return con.rcvmsg(pmsg)
     except zerorpc.TimeoutExpired:
         print "time out when", msg.from_node, "calls", msg.to_node
         return False
コード例 #21
0
    def play(self, n_step=10000, n_episode=100, test_ep=None, render=False):
        if test_ep == None:
            test_ep = self.ep_end

        test_history = History(self.config)

        if not self.display:
            gym_dir = '/tmp/%s-%s' % (self.env_name, get_time())
            self.env.env.monitor.start(gym_dir)

        best_reward, best_idx = 0, 0
        for idx in range(n_episode):
            screen, reward, action, terminal = self.env.new_random_game()
            current_reward = 0

            for _ in range(self.history_length):
                test_history.add(screen)

            for t in tqdm(range(n_step), ncols=72):
                # 1. predict
                action = self.predict(test_history.get(), test_ep)
                # 2. act
                screen, reward, terminal = self.env.act(action,
                                                        is_training=False)
                # 3. observe
                test_history.add(screen)

                current_reward += reward
                if terminal:
                    break

            if current_reward > best_reward:
                best_reward = current_reward
                best_idx = idx

            print("=" * 30)
            print(" [%d] Best reward : %d" % (best_idx, best_reward))
            print("=" * 30)

        if not self.display:
            self.env.env.monitor.close()
コード例 #22
0
    def rollout(self, s, h, simulator, depth):
        # Check significance of update
        if self.gamma**depth < self.epsilon:
            return 0

        legal_actions = self.simulator.get_legal_actions_given_state(s)

        # Uniform random rollout policy
        action = choice(legal_actions)

        # Generate states and observations from the simulator
        successor_state, observation, reward, is_terminal = self.simulator.step(
            s, action)

        if is_terminal:
            return reward
        if h == -1:
            h = History()
        h.add(action, observation)
        return reward + self.gamma * self.rollout(successor_state, h,
                                                  simulator, depth + 1)
コード例 #23
0
ファイル: framework.py プロジェクト: thtrieu/pynamo
    def schedule(cls, msgs_to_process=None, timers_to_process=None):
        """Schedule given number of pending messages"""
        if msgs_to_process is None:
            msgs_to_process = 32768
        if timers_to_process is None:
            timers_to_process = 32768

        while cls._work_to_do():
            _logger.info(
                "Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers",
                len(cls.queue), msgs_to_process, TimerManager.pending_count(),
                timers_to_process)
            # Process all the queued up messages (which may enqueue more along the way)
            while cls.queue:
                msg = cls.queue.popleft()
                if msg.to_node.failed:
                    _logger.info("Drop %s->%s: %s as destination down",
                                 msg.from_node, msg.to_node, msg)
                    History.add("drop", msg)
                elif not Framework.reachable(msg.from_node, msg.to_node):
                    _logger.info("Drop %s->%s: %s as route down",
                                 msg.from_node, msg.to_node, msg)
                    History.add("cut", msg)
                else:
                    _logger.info("Dequeue %s->%s: %s", msg.from_node,
                                 msg.to_node, msg)
                    if isinstance(msg, ResponseMessage):
                        # figure out the original request this is a response to
                        try:
                            reqmsg = msg.response_to.original_msg
                        except Exception:
                            reqmsg = msg.response_to
                        # cancel any timer associated with the original request
                        cls.remove_req_timer(reqmsg)
                    History.add("deliver", msg)
                    msg.to_node.rcvmsg(msg)
                msgs_to_process = msgs_to_process - 1
                if msgs_to_process == 0:
                    return

            # No pending messages; potentially pop a (single) timer
            if TimerManager.pending_count() > 0 and timers_to_process > 0:
                # Pop the first pending timer; this may enqueue work
                TimerManager.pop_timer()
                timers_to_process = timers_to_process - 1
            if timers_to_process == 0:
                return
コード例 #24
0
ファイル: framework.py プロジェクト: terencez127/mrpynamo
    def schedule(cls, msgs_to_process=None, timers_to_process=None):
        """Schedule given number of pending messages"""
        if msgs_to_process is None:
            msgs_to_process = 32768
        if timers_to_process is None:
            timers_to_process = 32768

        while cls._work_to_do():
            _logger.info("Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers",
                         len(cls.queue), msgs_to_process, TimerManager.pending_count(), timers_to_process)
            # Process all the queued up messages (which may enqueue more along the way)
            while cls.queue:
                #modify
                msg, con = cls.queue.popleft()
                ###############################
                
                if msg.to_node.failed:
                    _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg)
                    History.add("drop", msg)
                elif not Framework.reachable(msg.from_node, msg.to_node):
                    _logger.info("Drop %s->%s: %s as route down", msg.from_node, msg.to_node, msg)
                    History.add("cut", msg)
                else:
                    _logger.info("Dequeue %s->%s: %s", msg.from_node, msg.to_node, msg)
                    if isinstance(msg, ResponseMessage):
                        # figure out the original request this is a response to
                        try:
                            reqmsg = msg.response_to.original_msg
                        except Exception:
                            reqmsg = msg.response_to
                        # cancel any timer associated with the original request
                        cls.remove_req_timer(reqmsg)
                    History.add("deliver", msg)                  
                    
                msgs_to_process = msgs_to_process - 1
                if msgs_to_process == 0:
                    return

            # No pending messages; potentially pop a (single) timer
            if TimerManager.pending_count() > 0 and timers_to_process > 0:
                # Pop the first pending timer; this may enqueue work
                TimerManager.pop_timer()
                timers_to_process = timers_to_process - 1
            if timers_to_process == 0:
                return
コード例 #25
0
 def remove(self):
     """Remove this Node from the system-wide lists of Nodes"""
     self.included = False
     _logger.debug("Node %s removed from system", self)
     History.add('remove', NodeAction(self))
コード例 #26
0
ファイル: engine.py プロジェクト: victorhook/qchess
class Engine:
    def __init__(self, cli_display=False):

        self._pwhite = None
        self._pblack = None

        self._w_king_moved = False
        self._b_king_moved = False

        self._w_en_passant = False
        self._b_en_passant = False

        self._w_ressign = False
        self._b_ressign = False

        self._promotion_sq = None

        self.history = History()

        self._board = [[None for i in range(8)] for i in range(8)]
        self._init_pieces()
        self._current_color = Color.WHITE

        self._cli_display = cli_display
        if self._cli_display:
            self._display()

    """ Public """

    def get_status(self):
        # |  is_checked  | is_check_mate | is_draw | enemy_resign |
        # |  white score |  black score  | player turn |
        return [
            self.is_checked(),
            self.is_check_mate(),
            self.is_draw(),
            self.enemy_resign(),
            self.player_color(),
            self.get_score(Color.WHITE),
            self.get_score(Color.BLACK)
        ]

    def get_score(self, color):
        START_SCORE = 40  # Standard points
        not_capured = 0
        for row in range(8):
            for col in range(8):
                piece = self._get(row, col)
                if (piece != PieceType.EMPTY
                        and piece.piece_type != PieceType.King):
                    if piece.color != color:
                        not_capured += PIECE_SCORE[piece.piece_type]

        return START_SCORE - not_capured

    def get_str(self, row, col):
        piece = self._get(row, col)
        string = ' ' if piece is None else piece.symbol()
        return string

    def get_all_available_moves(self):
        start_squares = self._get_squares_of_color(self._current_color)
        moves = []
        for move_from in start_squares:
            moves.extend(self.get_available_moves(move_from))
        return moves

    def get_available_moves(self, move_from):
        square = self._parse_square(move_from)
        moves = []
        for row in range(8):
            for col in range(8):
                move_to = (row, col)
                move = Move(square, move_to)
                if self.valid_move(move):
                    moves.append(move)

        return moves

    def get_winner(self):
        color = self.self._current_color
        self._current_color = Color.WHITE
        white_moves = len(self.get_all_available_moves())
        self._current_color = Color.BLACK
        black_moves = len(self.get_all_available_moves())
        self._current_color = color

        if white_moves == 0:
            winner = Color.WHITE
        elif black_moves == 0:
            winner = Color.BLACK
        else:
            winner = None

        return winner

    def player_color(self):
        return self._current_color

    def enemy_resign(self):
        if self._current_color == Color.WHITE:
            resign = self._w_ressign
        else:
            resign = self._b_ressign
        return resign

    def is_resigned(self):
        return self._w_ressign or self._b_ressign

    def resign(self):
        if self._current_color == Color.WHITE:
            self._w_ressign = True
        else:
            self._b_ressign = True

    def is_checked(self):
        king_square = self._find_king()
        if king_square is None:
            raise Exception('Failed to find king!!!')
        king_color = self._current_color

        check = self._under_attack(king_square, king_color)

        return check

    def is_draw(self):
        return self.is_stale_mate() or self.is_dead_pos()

    def is_stale_mate(self):
        return self.is_dead_pos()

    def is_dead_pos(self):
        """
        1. king against king;
        2. king against king and bishop;
        3. king against king and knight;
        4. king and bishop against king and bishop, with both bishops on
            squares of the same color (see King and two bishops).
        """
        return (self._king_against_king()
                or self._king_against_king_and_bishop()
                or self._king_against_king_and_knight()
                or self._king_and_bishop_against_king_and_bishop())

    def clear(self):
        """ Clears the entire chess board. """
        for row in range(8):
            for col in range(8):
                self._put(row, col, PieceType.EMPTY)

    def set_board_from_string(self, string):
        self.clear()
        row, col = 0, 0
        for char in string:

            WHITE_PIECES_ASCII = ['P', 'B', 'N', 'R', 'Q', 'K']
            BLACK_PIECES_ASCII = ['p', 'b', 'n', 'r', 'q', 'k']
            if char == '.' or char == ' ':
                piece = PieceType.EMPTY

            elif char > 'Z':
                # Black pieces
                piece_type = BLACK_PIECES_ASCII_TABLE[char]
                piece = Piece(piece_type, Color.BLACK)
            else:
                # White pieces
                piece_type = WHITE_PIECES_ASCII_TABLE[char]
                piece = Piece(piece_type, Color.WHITE)

            self._put(row, col, piece)

            if col == 7:
                row += 1
                col = 0
            else:
                col += 1

    def get_board_as_string(self):
        return self._stringify()

    def is_check_mate(self):
        """ Must be check and no positions for one of the opponents. """
        check = self.is_checked()
        self.swap_color()
        check |= self.is_checked()
        self.swap_color()
        return check and self._no_moves_available()

    def make_move(self, move):
        """ Tries to make the given move.
            move: ((row_from, col_to), (row_to, col_to))
            returns: A MoveResult, containing information if the move
            was valid, if it caused a promotion and if it was a capture.
        """

        result = MoveResult()

        if (self.is_check_mate() or self.is_draw() or self.is_resigned()
                or self.is_promotion()):
            result.is_ok = False
        else:
            move = self._parse_move(move)
            valid = self.valid_move(move)

            if valid:
                result.capture = self.place(move)
                if self._cli_display:
                    self._display()
                result.is_ok = True
            else:
                result.is_ok = False

        result.promotion = self.is_promotion()
        print(result)

        return result

    def is_promotion(self):
        return self._promotion_sq is not None

    def make_promotion(self, piece_type):
        if piece_type in [
                PieceType.Bishop, PieceType.Rock, PieceType.Knight,
                PieceType.Queen
        ]:
            piece = Piece(piece_type, self._current_color)
            row, col = self._promotion_sq
            self._put(row, col, piece)
            self._promotion_sq = None
            return True
        return False

    def place(self, move):
        from_r, from_c = move.move_from
        to_r, to_c = move.move_to

        if self._king_move(move) or self._rock_move(move):
            self._update_castle_rules(move)

        if self._en_passant_move(move):
            self._take_en_passant(move)
            capture = True
        else:
            capture = self._is_capture(move)

        if self._is_promotion_move(move):
            self._promotion_sq = move.move_to

        elif self._castle_move(move):
            #print('castle')
            self._move_rocks(move)

        piece = self._get(from_r, from_c)
        self._put(to_r, to_c, piece)
        self._put(from_r, from_c, PieceType.EMPTY)
        self.history.add(move)
        return capture

    def run(self):
        while True:
            move = input('Move: ')
            res = self.make_move(move)

    def valid_move(self, move):
        valid = self._valid_move(move)
        return valid and not self._causes_check(move)

    def swap_color(self):
        if self._current_color == Color.WHITE:
            self._current_color = Color.BLACK
        else:
            self._current_color = Color.WHITE

    """ Private """

    def _init_pieces(self):
        # 'RNBQKBNR'
        # 'rnbqkbnr'
        self._board[0][0] = Piece(PieceType.Rock, Color.WHITE)
        self._board[0][1] = Piece(PieceType.Knight, Color.WHITE)
        self._board[0][2] = Piece(PieceType.Bishop, Color.WHITE)
        self._board[0][3] = Piece(PieceType.Queen, Color.WHITE)
        self._board[0][4] = Piece(PieceType.King, Color.WHITE)
        self._board[0][5] = Piece(PieceType.Bishop, Color.WHITE)
        self._board[0][6] = Piece(PieceType.Knight, Color.WHITE)
        self._board[0][7] = Piece(PieceType.Rock, Color.WHITE)

        self._board[7][0] = Piece(PieceType.Rock, Color.BLACK)
        self._board[7][1] = Piece(PieceType.Knight, Color.BLACK)
        self._board[7][2] = Piece(PieceType.Bishop, Color.BLACK)
        self._board[7][3] = Piece(PieceType.Queen, Color.BLACK)
        self._board[7][4] = Piece(PieceType.King, Color.BLACK)
        self._board[7][5] = Piece(PieceType.Bishop, Color.BLACK)
        self._board[7][6] = Piece(PieceType.Knight, Color.BLACK)
        self._board[7][7] = Piece(PieceType.Rock, Color.BLACK)

        for i in range(8):
            self._board[1][i] = Piece(PieceType.Pawn, Color.WHITE)
            self._board[6][i] = Piece(PieceType.Pawn, Color.BLACK)

    def _display(self):
        print()
        for row in self._board[::-1]:
            print('|', end='')
            for sq in row:
                sq = ' ' if sq is None else sq.symbol()
                print(' %s |' % sq, end='')
            print()

    def _parse_square(self, square):
        if type(square) is str:
            square = square.upper()
            square = (ord(square[1]) - 49, ord(square[0]) - 65)
        return square

    def _is_promotion_move(self, move):
        row = move.move_to[0]
        return (self._type(move.move_from) == PieceType.Pawn
                and (row == 0 or row == 7))

    def _is_capture(self, move):
        return self._piece_at(move.move_to)

    def _causes_check(self, move):
        """ Checks if the given move causes check by playing it on the board.
            check if checked, and then undo-ing the move again. """

        if self._type(move.move_to) == PieceType.King:
            return False

        piece_from = self._get(move.move_from)
        piece_to = self._get(move.move_to)

        self._put(move.move_from, PieceType.EMPTY)  # Clear old
        self._put(move.move_to, piece_from)  # Put piece to new sq
        checked = self.is_checked()

        self._put(move.move_from, piece_from)  # Reset moves
        self._put(move.move_to, piece_to)
        return checked

    def _rock_move(self, move):
        return self._type(move.move_from) == PieceType.Rock

    def _king_move(self, move):
        return self._type(move.move_from) == PieceType.King

    def _update_castle_rules(self, move):
        if self._color(move.move_from) == Color.WHITE:
            self._w_king_moved = True
        else:
            self._b_king_moved = True

    def _move_rocks(self, move):
        row, col = move.move_to[0], move.move_to[1]
        if col == 6:
            old_c, new_c = 7, 5
        else:
            old_c, new_c = 0, 3

        rock = self._get(row, old_c)
        self._put(row, old_c, PieceType.EMPTY)
        self._put(row, new_c, rock)

    def _castle_move(self, move):
        if (self._type(move.move_from) == PieceType.King
                and abs(self._dr_dc(move.move_from, move.move_to)[1]) > 1):
            return True

    def _take_en_passant(self, move):
        color = self._color(move.move_from)
        col = move.move_to[1]
        if color == Color.WHITE:
            row = move.move_to[0] - 1
        else:
            row = move.move_to[0] + 1
        self._put(row, col, PieceType.EMPTY)

    def _en_passant_move(self, move):
        dr, dc = self._dr_dc(move.move_from, move.move_to)
        if (self._type(move.move_from) == PieceType.Pawn
                and self._type(move.move_to) == PieceType.EMPTY
                and abs(dr) == 1 and abs(dc) == 1):
            return True
        return False

    def _stringify(self):
        board = ''
        for row in range(8):
            for col in range(8):
                piece = self._get(row, col)
                if piece != PieceType.EMPTY:
                    symbol = piece.ascii()
                else:
                    symbol = '.'
                board += symbol
        return board

    def _same_color(self, sq1, sq2):
        r1, c1 = sq1
        r2, c2 = sq2

        # Black
        if ((r1 % 2 == 0 and c1 % 2 == 0) or (r1 % 2 != 0 and c1 % 2 != 0)):
            return ((r2 % 2 == 0 and c2 % 2 == 0)
                    or (r2 % 2 != 0 and c2 % 2 != 0))
        # White
        elif ((r1 % 2 == 0 and c1 % 2 != 0) or (r1 % 2 != 0 and c1 % 2 == 0)):
            return ((r2 % 2 == 0 and c2 % 2 != 0)
                    or (r2 % 2 != 0 and c2 % 2 == 0))

    def _king_and_bishop_against_king_and_bishop(self):
        string = self._stringify().replace('.', '')

        if (string.count('b') == 1 and string.count('B') == 1
                and ('k' in string) and ('K' in string)):

            b1_r, b1_c = 0, 0
            found = False
            for row in range(8):
                for col in range(8):
                    piece = self._get(row, col)
                    if (piece != PieceType.EMPTY
                            and piece.piece_type == PieceType.Bishop):
                        if found:
                            if self._same_color((b1_r, b1_c), (row, col)):
                                return True
                            return False
                        else:
                            found = True
                            b1_r, b1_c = row, col
        return False

    def _king_against_king_and_knight(self):
        string = self._stringify().replace('.', '').lower()

        for char in 'rbqp':
            if char in string:
                return False

        if string.count('n') == 1 and string.count('k') == 2:
            return True

        return False

    def _king_against_king_and_bishop(self):
        string = self._stringify().replace('.', '').lower()

        for char in 'rnqp':
            if char in string:
                return False

        if string.count('b') == 1 and string.count('k') == 2:
            return True

        return False

    def _king_against_king(self):
        return self._stringify().lower().replace('.', '') == 'kk'

    def _no_moves_available(self):
        m1 = self.get_all_available_moves()
        self.swap_color()
        m2 = self.get_all_available_moves()
        self.swap_color()
        return len(m1) == 0 or len(m2) == 0

    def _valid_move(self, move):
        color = self._current_color

        # Ensure square is not empty and that we're not trying to move
        # the opponents pieces.
        if (not self._piece_at(move.move_from)
                or self._color(move.move_from) != color):
            return False

        # Can't place out of bounds
        if self._out_of_bounds(move.move_from):
            return False

        piece_type = self._type(move.move_from)

        if piece_type == PieceType.Pawn:
            return self._valid_pawn(move, color)
        elif piece_type == PieceType.Knight:
            return self._valid_knight(move, color)
        elif piece_type == PieceType.Bishop:
            return self._valid_bishop(move, color)
        elif piece_type == PieceType.Rock:
            return self._valid_rock(move, color)
        elif piece_type == PieceType.Queen:
            return self._valid_queen(move, color)
        elif piece_type == PieceType.King:
            return self._valid_king(move, color)

    def _get_attackers(self, square, color):
        attackers = []
        self.swap_color()

        for row in range(8):
            for col in range(8):
                piece = self._get(row, col)
                if piece != PieceType.EMPTY and piece.color != color:
                    from_square = (row, col)
                    if self.valid_move(Move(from_square, square)):
                        attackers.append(from_square)

        self.swap_color()
        return attackers

    def _under_attack(self, square, color):
        return len(self._get_attackers(square, color)) > 0

    def _get_squares_of_color(self, color):
        squares = []
        for row in range(8):
            for col in range(8):
                if self._color((row, col)) == color:
                    squares.append((row, col))

        return squares

    def _find_king(self):
        for row in range(8):
            for col in range(8):
                square = (row, col)
                if (self._type(square) == PieceType.King
                        and self._color(square) == self._current_color):
                    return square
        return None

    def _pawn_max_moves(self, sq, color):
        row = sq[0]
        max_moves = 1
        if ((row == 1 and color == Color.WHITE)
                or (row == 6 and color == Color.BLACK)):
            max_moves = 2
        return max_moves

    def _valid_pawn(self, move, color):
        max_moves = self._pawn_max_moves(move.move_from, color)
        dr, dc = self._dr_dc(move.move_from, move.move_to)

        direction = 1 if self._color(move.move_from) == Color.WHITE else -1

        if ((abs(dr) > max_moves) or  # Max moves
            (abs(dc) > 1) or  # Sideways cant be > 1
            (abs(dc) == 1 and dr == 0) or  # Cant move only sideways
            (self._color(move.move_to) == color)):  # Moving to same color
            return False

        if max_moves == 2 and direction * abs(dr) == dr and abs(dr) == 2:
            if (dc != 0 or self._in_the_way(move.move_from, move.move_to)
                    or self._piece_at(move.move_to)):
                return False

        if abs(dr) == 1:
            if dc == 0:
                if self._piece_at(move.move_to) or direction != dr:
                    return False
            elif abs(dc) == 1:
                if direction * abs(dr) != dr:
                    return False
                if self._can_en_passant(move, color):
                    return True
                elif not self._piece_at(move.move_to):
                    return False

        return True

    def _valid_knight(self, move, color):
        if self._color(move.move_to) == color:  # Same color
            return False

        dr, dc = self._dr_dc(move.move_from, move.move_to)
        if ((abs(dr) == 2 and abs(dc) == 1)
                or (abs(dr) == 1 and abs(dc) == 2)):
            return True

        return False

    def _valid_bishop(self, move, color):
        dr, dc = self._dr_dc(move.move_from, move.move_to)

        if (abs(dr) != abs(dc) or  # Need to move only diagonally
                self._in_the_way(move.move_from, move.move_to) or self._color(
                    move.move_to) == color):
            return False

        return True

    def _valid_rock(self, move, color):
        dr, dc = self._dr_dc(move.move_from, move.move_to)

        if ((dr != 0 and dc != 0) or self._color(move.move_to) == color
                or self._in_the_way(move.move_from, move.move_to)):
            return False

        return True

    def _valid_queen(self, move, color):
        dr, dc = self._dr_dc(move.move_from, move.move_to)
        if (self._color(move.move_to) == color
                or self._in_the_way(move.move_from, move.move_to)):
            return False

        diagonal = abs(dr) == abs(dc)
        straight = (dr != 0 and dc == 0) or (dr == 0 and dc != 0)

        return diagonal or straight

    def _valid_king(self, move, color):
        dr, dc = self._dr_dc(move.move_from, move.move_to)

        if dr == 0 and abs(dc) > 1:
            # Check for possible castle
            if self._can_castle(move, color):
                return True
            else:
                return False

        if ((abs(dc) > 1 or abs(dr) > 1)
                or self._color(move.move_to) == color):
            return False

        return True

    def _can_castle(self, move, color):
        col = move.move_to[1]
        row = move.move_to[0]

        if (self._under_attack(move.move_from, color)
                or self._piece_at(move.move_to)):
            return False

        if col != 2 and col != 6:
            return False

        if self._in_the_way(move.move_from, move.move_to):
            return False

        if col == 2:
            if self._piece_at(row, 1):  # Knight in the way
                return False
            sq1, sq2 = (row, 2), (row, 3)
        else:
            sq1, sq2 = (row, 5), (row, 6)

        if (self._under_attack(sq1, self._current_color)
                or self._under_attack(sq2, self._current_color)):
            return False

        if color == Color.WHITE:
            if self._w_king_moved:
                return False
        elif color == Color.BLACK:
            if self._b_king_moved:
                return False

        return True

    def _dr_dc(self, sq1, sq2):
        old_r, old_c = sq1
        new_r, new_c = sq2
        return new_r - old_r, new_c - old_c

    def _in_the_way(self, sq1, sq2):
        dr, dc = self._dr_dc(sq1, sq2)
        r, c = sq1
        if dc == 0:
            sign = 1 if dr > 0 else -1
            for i in range(abs(dr) - 1):
                if self._piece_at(r + sign + sign * i, c):
                    return True
        elif dr == 0:
            sign = 1 if dc > 0 else -1
            for i in range(abs(dc) - 1):
                if self._piece_at(r, c + sign + sign * i):
                    return True

        if abs(dr) == abs(dc):
            sign_r = 1 if dr > 0 else -1
            sign_c = 1 if dc > 0 else -1
            for i in range(abs(dr) - 1):
                if self._piece_at(r + sign_r + sign_r * i,
                                  c + sign_c + sign_c * i):
                    return True

        return False

    def _out_of_bounds(self, sq):
        r, c = sq
        return r < 0 or r > 7 or c < 0 or c > 7

    def _can_en_passant(self, move, color):
        last = self.history.peek()
        if last is None:
            return False

        row = last.move_to[0]

        old_dr, _ = self._dr_dc(last.move_from, last.move_to)

        if (self._type(last.move_to) != PieceType.Pawn
                or (row != 4 and row != 3) or abs(old_dr) != 2):
            return False

        to_c, to_r = move.move_to[1], move.move_to[0]
        last_c, last_r = last.move_to[1], last.move_to[0]

        if to_c == last_c:
            if color == Color.WHITE:
                if last_r + 1 == to_r:
                    return True
            else:
                if last_r - 1 == to_r:
                    return True

        return False

    def _parse_move(self, move):
        move_t = type(move)
        if move_t is str:
            move = move.upper().split(' ')
            from_c = ord(move[0][0]) - 65
            from_r = ord(move[0][1]) - 49
            to_c = ord(move[1][0]) - 65
            to_r = ord(move[1][1]) - 49
            move = Move((from_r, from_c), (to_r, to_c))
        elif move_t is tuple:
            move = Move(move[0], move[1])

        return move

    def _get(self, row, *col):
        if type(row) is tuple:
            row, col = row[0], row[1]
        else:
            col = col[0]
        return self._board[row][col]

    def _put(self, row, col, *piece):
        if type(row) is tuple:
            piece = col
            row, col = row[0], row[1]
        else:
            piece = piece[0]
        self._board[row][col] = piece

    def _piece_at(self, sq, *sq2):
        if sq2:
            sq = (sq, sq2[0])
        return self._get(sq[0], sq[1]) != PieceType.EMPTY

    def _type(self, piece):
        if type(piece) is tuple:
            piece = self._get(piece[0], piece[1])
        if piece is None:
            piece_type = PieceType.EMPTY
        else:
            piece_type = piece.piece_type
        return piece_type

    def _color(self, piece):
        if type(piece) is tuple:
            piece = self._get(piece[0], piece[1])
        if piece is None:
            return -1

        return Color.WHITE if piece.color == Color.WHITE else Color.BLACK

    def do_random_move(self):
        moves = self.get_all_available_moves()
        if moves:
            move = random.choice(moves)
            self.make_move(move)
        else:
            pass
コード例 #27
0
ファイル: main.py プロジェクト: KOLEGA007/sms_sender
class sms_sender:
    def __init__(self):
        self.history = History(db_name=DATABASE)
        self.contacts = Contacts(db_name=DATABASE)
        self.builder = gtk.Builder()
        self.builder.add_from_file("ui.glade")
        window = self.builder.get_object("window")
        self.message = self.builder.get_object("text")
        ###
        # Menu --> Connect
        self.builder.get_object("history").connect("activate", self.history_browsing)
        self.builder.get_object("contact").connect("activate", self.contact_browsing)
        ###
        window.show_all()
        window.connect("destroy", gtk.main_quit)
        ###
        cancel = self.builder.get_object("cancel")
        cancel.connect("clicked", gtk.main_quit)
        self.builder.get_object("exit").connect("activate", gtk.main_quit)
        ###
        ok = self.builder.get_object("ok")
        ok.connect("clicked", self.ok_clicked)
        ###
        self.check_box = self.builder.get_object("history_check")
        ###
        self.number = self.builder.get_object("number")
        self.number.connect("changed", self.on_number_changed)
        # Počítání znaků
        self.charcounter = self.builder.get_object("charcounter")
        self.message.get_buffer().connect("changed", self.on_message_changed)
        # Doplňování
        self.completion = gtk.EntryCompletion()
        self.store = gtk.TreeStore(str, str)
        self.completion.set_model(self.store)
        # Model creating
        self.completion.set_text_column(0)
        name_cell = gtk.CellRendererText()
        self.completion.pack_start(name_cell)
        self.completion.add_attribute(name_cell, 'text', 1)
        self.number.set_completion(self.completion)
        # About dialog
        self.about_dialog = self.builder.get_object("aboutdialog")
        self.builder.get_object("about").connect("activate", self.on_about_activate)
        # Progress dialog
        self.progress_dialog = self.builder.get_object("progressdialog")
        self.progress_ok = self.builder.get_object("progressok")
        self.progress_ok.connect("clicked", self.on_progressok_clicked)
        self.progress_bar = self.builder.get_object("progressbar")
        #gtkmainloop
        gtk.main()
    def send(self, target, what):
        test = 0
        ##Commend next row for production use
        test = 1
        if test == 1:
            return True
        print "Odesílám %s do %d" % (what, target)
        self.progress_dialog.hide()
        self.progress_dialog.show()
        self.progress_ok.set_sensitive(False)
        self.progress_bar.set_fraction(0.33)
        self.progress_bar.set_text("Kontaktuji web")
        timestamp = int(time.time())
        data = {
            'timestamp' : timestamp,
            'action'    : 'send',
            'sendingProfile1' : 11,
            'sendingProfile2' : 20,
            'sendingProfile3' : 32,
            'textsms' : what,
            'cislo-prijemce' : target
        }
        data = urllib.urlencode(data) 
        print('http://www.poslatsms.cz/', data)
        try:
            req = urllib2.Request('http://www.poslatsms.cz/', data)
            self.progress_bar.set_fraction(0.66)
            self.progress_bar.set_text("Odesílám data")
            response = urllib2.urlopen(req)
            the_page = str(response.read())
        except urllib2.error as e:
            print "error", e
            self.progress_bar.hide()
        print the_page
        if 'SMS zprávy přijaty k odeslání!' in the_page:
            self.progress_bar.set_text("Hotovo")
            self.progress_ok.set_sensitive(True)
            self.progress_bar.set_fraction(1.00)
            return True
        return False
    def on_message_changed(self, model):
        text = self.message.get_buffer().get_text(self.message.get_buffer().get_start_iter(), self.message.get_buffer().get_end_iter())
        chars = len(text)
        colour = "darkgreen"
        if chars > 625:
            self.message.get_buffer().set_text(text[:625])
            self.alert(self.message, "Překročena maximální délka zprávy!")
            text = self.message.get_buffer().get_text(self.message.get_buffer().get_start_iter(), self.message.get_buffer().get_end_iter())
            chars = len(text)
            colour = "red"
        label = "Napsáno %d/125" % (chars % 125)
        if chars > 125:
            label += "Počet zpráv %d/5" % (((chars - 1) / 125) + 1)
            
            preformat = '<span foreground="%s">' % colour
            postformat= '</span>'
            label = preformat + label + postformat
        self.charcounter.set_markup(label)
    def on_number_changed(self, model):
        cislo = True
        try:
            text = int(self.number.get_text())
        except ValueError:
            cislo = False
        self.store.clear()
        self.update_model(self.store, cislo)
    def update_model(self, model, number):
        #GET FROM CONTACTS
        try:
          for i in self.contacts.list_all():
              if number:
                  model.append(None, [i[0], i[1]])
              else:
                  model.append(None, [i[1], i[0]])
        except TypeError:
            print "No contacts stored"
        #GET FROM HISTORY
        try:
          for i in self.history.disctinct_contacts():
              model.append(None, [i[0], ""])
        except TypeError:
            print "History doesnt contain \"non-contact\" numbers"
        return model
    def info(self, msg):
        dialog = gtk.MessageDialog(None, 0,
                    gtk.MESSAGE_INFO,
                    gtk.BUTTONS_OK,
                    msg)
        choice = dialog.run()
        if choice != None:
            dialog.hide()      
    def alert(self, what, msg):
        #call alert from "what" with message "msg"
        dialog = gtk.MessageDialog(None, 0,
                    gtk.MESSAGE_WARNING,
                    gtk.BUTTONS_OK,
                    msg)
        choice = dialog.run()
        if choice != None:
            dialog.hide()
        if what:
            what.grab_focus()
    def on_about_activate(self, widget):
        self.about_dialog.run()
        self.about_dialog.hide()
    def history_browsing(self, widget):
        self.history_window = History_UI(parent=self)
        self.history_window.builder.get_object("history_dialog").show()
        if self.history_window.result:
          self.number.set_text(str(self.history_window.result[0]))
          self.message.get_buffer().set_text(self.history_window.result[1])
    def contact_browsing(self, widget):
        self.contact_window = Contacts_UI(parent=self)
        
    def ok_clicked(self, widget):
        if isInteger(self.number.get_text()):
            cislo = int(self.number.get_text())
            if (len(self.number.get_text()) != 9):
                self.alert(self.nubmer, "Číslo příjemce není 9 místné číslo")
                return 1
        else:
            cislo = self.contacts.get_num(self.number.get_text())
            if cislo == None:
                self.alert(self.number, "Uvedený kontakt nebyl nalezen")
                return 1
        text = self.message.get_buffer().get_text(self.message.get_buffer().get_start_iter(), self.message.get_buffer().get_end_iter())
        if (text == ""):
            self.alert(self.message, "Nelze odeslat prázdnou zprávu!")
            return 1
        while text <> "":
            if not(self.send(cislo, text[:125])):
                self.alert(None, "Chyba při odesílání! Změna enginu poskytovatele?")
                text = text[125:]
            else:
                # ukládání do historie
                if (self.check_box.get_active()):
                    self.history.add(cislo, text[:125])
                text = text[125:]
                self.message.get_buffer().set_text("")
                self.number.set_text("")
    def on_progressok_clicked(self,widget):
        self.progress_dialog.hide()
コード例 #28
0
ファイル: entry.py プロジェクト: sixtyfive/gedit-plugins
class Entry(Gtk.Box):
    __gtype_name__ = "CommanderEntry"

    def _show(self):
        self._reveal.set_reveal_child(True)

    def _hide(self):
        self._reveal.set_reveal_child(False)

    def __init__(self, view):
        super(Entry, self).__init__()

        self._view = view
        view.connect("destroy", self._on_view_destroyed)

        self._history = History(
            os.path.join(GLib.get_user_config_dir(),
                         'gedit/commander/history'))
        self._history_prefix = None

        self._prompt_text = None
        self._accel_group = None

        self._wait_timeout = 0
        self._cancel_button = None
        self._info = None
        self._info_revealer = None

        self._suspended = None

        self._handlers = [[0, Gdk.KEY_Up, self._on_history_move, -1],
                          [0, Gdk.KEY_Down, self._on_history_move, 1],
                          [None, Gdk.KEY_Return, self._on_execute, None],
                          [None, Gdk.KEY_KP_Enter, self._on_execute, None],
                          [0, Gdk.KEY_Tab, self._on_complete, None],
                          [0, Gdk.KEY_ISO_Left_Tab, self._on_complete, None]]

        self._re_complete = re.compile(
            '("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

        self.connect('destroy', self._on_destroy)

        self._build_ui()
        self._setup_keybindings()

        self._attach()

    def view(self):
        return self._view

    def _setup_keybindings(self):
        css = Gtk.CssProvider()
        css.load_from_data(
            bytes(
                """
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    background-image: none;
    box-shadow: 0 0;
    transition: none;
    border: 0;
}

""", 'utf-8'))

        self._entry.get_style_context().add_provider(
            css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    def _find_overlay(self, view):
        parent = view.get_parent()

        while not isinstance(parent, Gtk.Overlay):
            parent = parent.get_parent()

        return parent

    def _build_ui(self):
        self.set_orientation(Gtk.Orientation.VERTICAL)

        self._overlay = self._find_overlay(self._view)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        hbox.show()

        self.pack_end(hbox, False, False, 0)

        self._info_revealer = Gtk.Revealer()

        self._info_revealer.set_transition_type(
            Gtk.RevealerTransitionType.SLIDE_UP)
        self._info_revealer.set_transition_duration(150)

        self.pack_start(self._info_revealer, False, False, 0)
        self._info_revealer.connect('notify::child-revealed',
                                    self._on_info_revealer_child_revealed)

        self._prompt_label = Gtk.Label(label='<b>&gt;&gt;&gt;</b>',
                                       use_markup=True)
        self._prompt_label.set_margin_top(3)
        self._prompt_label.set_margin_bottom(3)
        self._prompt_label.set_margin_start(3)

        self._prompt_label.show()
        hbox.add(self._prompt_label)

        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name('gedit-commander-entry')
        self._entry.set_hexpand(True)
        self._entry.set_margin_top(3)
        self._entry.set_margin_bottom(3)
        self._entry.set_margin_end(3)

        self._entry.connect('key-press-event', self._on_entry_key_press)

        self._entry.show()
        hbox.add(self._entry)

        self._copy_style_from_view()
        self._view_style_updated_id = self._view.connect(
            'style-updated', self._on_view_style_updated)

    def _on_view_destroyed(self, widget, user_data=None):
        self._view.disconnect(self._view_style_updated_id)
        self._view_style_updated_id = None

    def _on_view_style_updated(self, widget):
        self._copy_style_from_view()

    @property
    def _border_color(self):
        style = self._view.get_buffer().get_style_scheme().get_style(
            'right-margin')

        if not style is None and style.props.foreground_set:
            color = Gdk.RGBA()
            color.parse(style.props.foreground)
        else:
            color = self._get_background_color(Gtk.StateFlags.NORMAL,
                                               'bottom').copy()
            color.red = 1 - color.red
            color.green = 1 - color.green
            color.blue = 1 - color.blue

        color.alpha = 0.3
        return color

    def _get_background_color(self, state, cls=None):
        context = self._view.get_style_context()

        context.save()

        if not cls is None:
            context.add_class(cls)

        ret = context.get_background_color(state)

        context.restore()

        return ret

    def _get_foreground_color(self, state, cls=None):
        context = self._view.get_style_context()

        context.save()

        if not cls is None:
            context.add_class(cls)

        ret = context.get_color(state)

        context.restore()

        return ret

    def _get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def _styled_widgets(self):
        widgets = [self, self._entry, self._prompt_label]

        if not self._info is None:
            widgets.append(self._info)
            widgets.append(self._info.text_view)

        return widgets

    def _modify_bg(self, col, widget):
        if self._info is None or (self._info.text_view != widget
                                  and self._info != widget):
            return col

        d = 0.1

        h, l, s = colorsys.rgb_to_hls(col.red, col.green, col.blue)

        if l < 0.5:
            factor = 1 + d
        else:
            factor = 1 - d

        l = max(0, min(1, l * factor))
        s = max(0, min(1, s * factor))

        r, g, b = colorsys.hls_to_rgb(h, l, s)

        return Gdk.RGBA(r, g, b, col.alpha)

    def _copy_style_from_view(self, widgets=None):
        font = self._get_font()
        fg = self._get_foreground_color(Gtk.StateFlags.NORMAL, 'bottom')
        bg = self._get_background_color(Gtk.StateFlags.NORMAL, 'bottom')

        fgsel = self._get_foreground_color(Gtk.StateFlags.SELECTED)
        bgsel = self._get_background_color(Gtk.StateFlags.SELECTED)

        cursor = self._view.style_get_property('cursor-color')

        if not cursor is None:
            cursor = Gdk.RGBA.from_color(cursor)

        secondary_cursor = self._view.style_get_property(
            'secondary-cursor-color')

        if not secondary_cursor is None:
            secondary_cursor = Gdk.RGBA.from_color(secondary_cursor)

        if widgets is None:
            widgets = self._styled_widgets()

        for widget in widgets:
            widget.override_color(Gtk.StateFlags.NORMAL, fg)
            widget.override_background_color(Gtk.StateFlags.NORMAL,
                                             self._modify_bg(bg, widget))

            widget.override_color(Gtk.StateFlags.SELECTED, fgsel)
            widget.override_background_color(Gtk.StateFlags.SELECTED,
                                             self._modify_bg(bgsel, widget))

            widget.override_font(font)
            widget.override_cursor(cursor, secondary_cursor)

    def _attach(self):
        reveal = Gtk.Revealer()
        reveal.add(self)
        self.show()

        reveal.set_transition_type(Gtk.RevealerTransitionType.SLIDE_UP)
        reveal.set_transition_duration(200)

        reveal.set_valign(Gtk.Align.END)
        reveal.set_halign(Gtk.Align.FILL)

        self._overlay.add_overlay(reveal)
        reveal.show()

        reveal.set_reveal_child(True)
        self._reveal = reveal

        self._entry.grab_focus()

    def grab_focus(self):
        self._entry.grab_focus()

    def _on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if not self._info is None:
                if not self._suspended is None:
                    self._suspended.resume()

                if not self._info is None:
                    self._info_revealer.set_reveal_child(False)

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self._prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self._prompt()
            else:
                self._view.grab_focus()
                self._reveal.set_reveal_child(False)

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self._prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self._run_command(
                    lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0]
                    == state) and evnt.keyval == handler[1] and handler[2](
                        handler[3], state):
                return True

        if not self._info is None and self._info.is_empty:
            self._info_revealer.set_reveal_child(False)

        self._history_prefix = None
        return False

    def _on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def _prompt(self, pr=''):
        self._prompt_text = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(
                self._accel_group.full_name()), )

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def _make_info(self):
        if self._info is None:
            self._info = Info()

            self._copy_style_from_view([self._info, self._info.text_view])

            self._info_revealer.add(self._info)
            self._info.show()

            self._info_revealer.show()
            self._info_revealer.set_reveal_child(True)

            self._info.connect('destroy', self._on_info_destroy)

    def _on_info_revealer_child_revealed(self, widget, pspec):
        if not self._info_revealer.get_child_revealed():
            self._info.destroy()
            self._info_revealer.hide()

    def _on_info_destroy(self, widget):
        self._info = None

    def info_show(self, text='', use_markup=False):
        self._make_info()
        self._info.add_lines(text, use_markup)

    def info_status(self, text):
        self._make_info()
        self._info.status(text)

    def _info_add_action(self, stock, callback, data=None):
        self._make_info()
        return self._info.add_action(stock, callback, data)

    def _command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def _on_wait_cancel(self):
        self._on_execute(None, 0)

    def _show_wait_cancel(self):
        self._cancel_button = self._info_add_action('process-stop-symbolic',
                                                    self._on_wait_cancel)
        self.info_status('<i>Waiting to finish\u2026</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def _on_suspend_resume(self):
        if self._wait_timeout:
            GLib.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            if not self._cancel_button is None:
                self._cancel_button.destroy()
                self._cancel_button = None

            self.info_status(None)

        self._entry.set_sensitive(True)
        self._command_history_done()

        if self._entry.props.has_focus or (not self._info is None
                                           and not self._info.is_empty):
            self._entry.grab_focus()

    def _run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception as e:
            sys.stderr.write(self._format_trace() + '\n')

            self._command_history_done()
            self._command_state.clear()

            self._prompt()

            # Show error in info
            self.info_show(
                '<b><span color="#f66">Error:</span></b> ' +
                saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self._format_trace(), False)

            return None

        mod = sys.modules['commander.commands.result']

        if ret == mod.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self._on_suspend_resume)

            self._wait_timeout = GLib.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self._command_history_done()
            self._prompt('')

            if ret == mod.Result.PROMPT:
                self._prompt(ret.prompt)
            elif (ret == None
                  or ret == mod.HIDE) and not self._prompt_text and (
                      self._info is None or self._info.is_empty):
                self._command_state.clear()
                self._view.grab_focus()
                self._reveal.set_reveal_child(False)
            else:
                self._entry.grab_focus()

        return ret

    def _format_trace(self):
        tp, val, tb = sys.exc_info()

        origtb = tb

        thisdir = os.path.dirname(__file__)

        # Skip frames up until after the last entry.py...
        while not tb is None:
            filename = tb.tb_frame.f_code.co_filename

            dname = os.path.dirname(filename)

            if not dname.startswith(thisdir):
                break

            tb = tb.tb_next

        msg = traceback.format_exception(tp, val, tb)
        r = ''.join(msg[0:-1])

        # This is done to prevent cyclic references, see python
        # documentation on sys.exc_info
        del origtb

        return r

    def _on_execute(self, dummy, modifier):
        if not self._info is None and not self._suspended:
            self._info_revealer.set_reveal_child(False)

        text = self._entry.get_text().strip()
        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        if not wordsstr and not self._command_state:
            self._entry.set_text('')
            return

        self._run_command(lambda: commands.Commands().execute(
            self._command_state, text, words, wordsstr, self, modifier))

        return True

    def _on_complete(self, dummy, modifier):
        # First split all the text in words
        text = self._entry.get_text()
        pos = self._entry.get_position()

        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        # Find out at which word the cursor actually is
        # Examples:
        #  * hello world|
        #  * hello| world
        #  * |hello world
        #  * hello wor|ld
        #  * hello  |  world
        #  * "hello world|"
        posidx = None

        for idx in range(0, len(words)):
            spec = self._complete_word_match(words[idx])

            if words[idx].start(0) > pos:
                # Empty space, new completion
                wordsstr.insert(idx, '')
                words.insert(idx, None)
                posidx = idx
                break
            elif spec[2] == pos:
                # At end of word, resume completion
                posidx = idx
                break
            elif spec[1] <= pos and spec[2] > pos:
                # In middle of word, do not complete
                return True

        if posidx == None:
            wordsstr.append('')
            words.append(None)
            posidx = len(wordsstr) - 1

        # First word completes a command, if not in any special 'mode'
        # otherwise, relay completion to the command, or complete by advice
        # from the 'mode' (prompt)
        cmds = commands.Commands()

        if not self._command_state and posidx == 0:
            # Complete the first command
            ret = commands.completion.command(words=wordsstr, idx=posidx)
        else:
            complete = None
            realidx = posidx

            if not self._command_state:
                # Get the command first
                cmd = commands.completion.single_command(wordsstr, 0)
                realidx -= 1

                ww = wordsstr[1:]
            else:
                cmd = self._command_state.top()
                ww = wordsstr

            if cmd:
                complete = cmd.autocomplete_func()

            if not complete:
                return True

            # 'complete' contains a dict with arg -> func to do the completion
            # of the named argument the command (or stack item) expects
            args, varargs = cmd.args()

            # Remove system arguments
            s = ['argstr', 'args', 'entry', 'view']
            args = list(filter(lambda x: not x in s, args))

            if realidx < len(args):
                arg = args[realidx]
            elif varargs:
                arg = '*'
            else:
                return True

            if not arg in complete:
                return True

            func = complete[arg]

            try:
                spec = utils.getargspec(func)

                if not ww:
                    ww = ['']

                kwargs = {'words': ww, 'idx': realidx, 'view': self._view}

                if not spec.keywords:
                    for k in list(kwargs.keys()):
                        if not k in spec.args:
                            del kwargs[k]

                ret = func(**kwargs)
            except Exception as e:
                # Can be number of arguments, or return values or simply buggy
                # modules
                print(e)
                traceback.print_exc()
                return True

        if not ret or not ret[0]:
            return True

        res = ret[0]
        completed = ret[1]

        if len(ret) > 2:
            after = ret[2]
        else:
            after = ' '

        # Replace the word
        if words[posidx] == None:
            # At end of everything, just append
            spec = None

            self._entry.insert_text(completed, self._entry.get_text_length())
            self._entry.set_position(-1)
        else:
            spec = self._complete_word_match(words[posidx])

            self._entry.delete_text(spec[1], spec[2])
            self._entry.insert_text(completed, spec[1])
            self._entry.set_position(spec[1] + len(completed))

        if len(res) == 1:
            # Full completion
            lastpos = self._entry.get_position()

            if not isinstance(res[0],
                              commands.module.Module) or not res[0].commands():
                if words[posidx] and after == ' ' and (
                        words[posidx].group(2) != None
                        or words[posidx].group(3) != None):
                    lastpos = lastpos + 1

                self._entry.insert_text(after, lastpos)
                self._entry.set_position(lastpos + 1)
            elif completed == wordsstr[posidx] or not res[0].method:
                self._entry.insert_text('.', lastpos)
                self._entry.set_position(lastpos + 1)

            if not self._info is None:
                self._info_revealer.set_reveal_child(False)
        else:
            # Show popup with completed items
            if not self._info is None:
                self._info.clear()

            ret = []

            for x in res:
                if isinstance(x, commands.method.Method):
                    ret.append('<b>' + saxutils.escape(x.name) + '</b> (<i>' +
                               x.oneline_doc() + '</i>)')
                else:
                    ret.append(str(x))

            self.info_show("\n".join(ret), True)

        return True

    def do_draw(self, ctx):
        ret = Gtk.Box.do_draw(self, ctx)

        col = self._border_color

        ctx.set_line_width(1)
        ctx.set_source_rgba(col.red, col.green, col.blue, col.alpha)

        w = self.get_allocated_width()

        ctx.move_to(0, 0.5)
        ctx.line_to(w, 0.5)
        ctx.stroke()

        if not self._info is None:
            alloc = self._info_revealer.get_allocation()
            y = alloc.y + alloc.height + 0.5

            if y >= 3:
                ctx.move_to(0, y)
                ctx.line_to(w, y)
                ctx.stroke()

        return ret

    def _on_destroy(self, widget):
        # Note we do this not as an override because somehow something
        # goes wrong when finalizing in that case, maybe self is NULL
        # or something like that, and then gets some new empty instance?
        if self._view_style_updated_id:
            self._view.disconnect(self._view_style_updated_id)

        self._history.save()

        self._view = None
        self._view_style_updated_id = None
コード例 #29
0
ファイル: frameworkBack.py プロジェクト: TaoZong/Python
 def cut_wires(cls, from_nodes, to_nodes):
     History.add("announce", "Cut %s -> %s" % ([str(x) for x in from_nodes], [str(x) for x in to_nodes]))
     cls.cuts.append((from_nodes, to_nodes))
コード例 #30
0
ファイル: env.py プロジェクト: wxw0/rl
class Env(object):
    def __init__(self, config):
        self.cf = config
        self.game = self.cf.game
        self.history = History(self.cf)
        self.env = gym.make(self.game + self.cf.env_versions)
        self.action_n = self.env.action_space.n
        self._obs = np.zeros((2, ) + self.env.observation_space.shape,
                             dtype=np.uint8)
        self.real_done = True
        print('env: ', self.game, self.action_n)

    def reset(self):
        self.history.clear()
        self._obs[:] = 0
        if not self.real_done:
            s, r, d = self.act(0)
        else:
            obs = self.env.reset()
            self._obs_add(obs)
            no_op = random.randint(1, self.cf.no_op_max - 1)
            s, r, d = self.act(0, no_op)
        if d:
            return self.reset()
        for i in range(self.cf.history_length):
            self.history.add(s)
        return s

    def act(self, action, action_repeat=None, is_training=True):
        if action_repeat == None:
            action_repeat = self.cf.action_repeat
        start_lives = self.lives
        total_r = 0
        for i in range(action_repeat):
            obs, r, d, info = self.env.step(action)
            s = self._obs_add(obs)
            self.real_done = d
            total_r += r

            if is_training:
                if self.lives < start_lives:
                    d = True
                if d:
                    total_r = -1
            if d:
                break

        self.history.add(s)
        return s, total_r, d

    def sample_action(self):
        return self.env.action_space.sample()

    def close(self):
        return self.env.close()

    def _obs_add(self, obs):
        self._obs[0] = self._obs[1]
        self._obs[1] = obs
        state = np.max(self._obs, axis=0)

        state = cv2.resize(cv2.cvtColor(state, cv2.COLOR_RGB2GRAY),
                           tuple(self.cf.state_shape),
                           interpolation=cv2.INTER_AREA)
        return (state / 255.).astype(self.cf.state_dtype)

    @property
    def lives(self):
        return self.env.env.ale.lives()

    @property
    def recent_states(self):
        return self.history.get()
コード例 #31
0
    h.add_only_observation(observation)
    print('Action from POCMP: ', action, 'Real observation: ', observation)
    #Save the 'old' particle list to update afterwards
    old_particle_list = copy.deepcopy(
        pomcp.tree.nodes[pomcp.tree.root_key].particle_list)
    #print('tamanho old list: ', len(old_particle_list))
    pomcp.tree.prune_and_make_new_root(action, observation)
    #print('Historico oficial')
    #h.print_history()
    state_from_history, _ = simulator.get_dummy_state_and_legal_actions_given_history(
        h)
    #Now update the belief state
    pomcp.tree.nodes[pomcp.tree.root_key].particle_list = particle_list_update(
        simulator, old_particle_list, int(pomcp.n_simulations),
        state_from_history, action, observation, 100)
    if len(pomcp.tree.nodes[pomcp.tree.root_key].particle_list) == 0:
        break
print('Out of particles, finishing episode with SelectRandom')
time = 0
while time < 100:
    action = choice(valid_actions(real_initial_state))
    successor_state, observation, reward, is_terminal = simulator.step(
        real_initial_state, action)
    if is_terminal:
        print('Finished')
        break
    h.add(action, observation)

print('Historico oficial')
h.print_history()
コード例 #32
0
 def fail(self):
     """Mark this Node as currently failed; all messages to it will be dropped"""
     self.failed = True
     _logger.debug("Node %s fails", self)
     History.add('fail', NodeAction(self))
コード例 #33
0
ファイル: entry.py プロジェクト: onia/pygi
class Entry(Gtk.EventBox):
    __gtype_name__ = "CommanderEntry"

    def __init__(self, view):
        Gtk.EventBox.__init__(self)
        self._view = view

        self.set_visible_window(False)

        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
        hbox.show()
        hbox.set_border_width(3)

        # context for the view
        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name('gedit-commander-entry')
        self._entry.show()

        css = Gtk.CssProvider()
        css.load_from_data("""
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    /* Override background to anything. This is weird, but doing this we can
       then in code use widget.override_background to set the color dynamically
       to the same color as the gedit view */
    background: transparent;
    border-width: 0;
    box-shadow: 0 0 transparent;
}
""")

        # FIXME: remove hardcopy of 600 (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
        # https://bugzilla.gnome.org/show_bug.cgi?id=646860
        self._entry.get_style_context().add_provider(css, 600)

        self._prompt_label = Gtk.Label(label='<b>&gt;&gt;&gt;</b>',
                                       use_markup=True)
        self._prompt_label.show()

        self._entry.connect('focus-out-event', self.on_entry_focus_out)
        self._entry.connect('key-press-event', self.on_entry_key_press)

        self._history = History(
            os.path.join(GLib.get_user_config_dir(),
                         'gedit/commander/history'))
        self._prompt = None

        self._accel_group = None

        hbox.pack_start(self._prompt_label, False, False, 0)
        hbox.pack_start(self._entry, True, True, 0)

        self.copy_style_from_view()
        self.view_style_updated_id = self._view.connect(
            'style-updated', self.on_view_style_updated)

        self.add(hbox)
        self.attach()
        self._entry.grab_focus()

        self._wait_timeout = 0
        self._info_window = None

        self.connect('destroy', self.on_destroy)
        self.connect_after('size-allocate', self.on_size_allocate)
        self.view_draw_id = self._view.connect_after('draw', self.on_draw)

        self._history_prefix = None
        self._suspended = None
        self._handlers = [[0, Gdk.KEY_Up, self.on_history_move, -1],
                          [0, Gdk.KEY_Down, self.on_history_move, 1],
                          [None, Gdk.KEY_Return, self.on_execute, None],
                          [None, Gdk.KEY_KP_Enter, self.on_execute, None],
                          [0, Gdk.KEY_Tab, self.on_complete, None],
                          [0, Gdk.KEY_ISO_Left_Tab, self.on_complete, None]]

        self._re_complete = re.compile(
            '("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

    def on_view_style_updated(self, widget):
        self.copy_style_from_view()

    def get_border_color(self):
        color = self.get_background_color().copy()
        color.red = 1 - color.red
        color.green = 1 - color.green
        color.blue = 1 - color.blue
        color.alpha = 0.5

        return color

    def get_background_color(self):
        context = self._view.get_style_context()
        return context.get_background_color(Gtk.StateFlags.NORMAL)

    def get_foreground_color(self):
        context = self._view.get_style_context()
        return context.get_color(Gtk.StateFlags.NORMAL)

    def get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def copy_style_from_view(self, widget=None):
        if widget != None:
            context = self._view.get_style_context()
            font = context.get_font(Gtk.StateFlags.NORMAL)

            widget.override_color(Gtk.StateFlags.NORMAL,
                                  self.get_foreground_color())
            widget.override_font(self.get_font())
        else:
            if self._entry:
                self.copy_style_from_view(self._entry)

            if self._prompt_label:
                self.copy_style_from_view(self._prompt_label)

    def view(self):
        return self._view

    def on_size_allocate(self, widget, alloc):
        alloc = self.get_allocation()
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM,
                                          alloc.height)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)
        self.set_size_request(win.get_width(), -1)

        # NOTE: we need to do this explicitly somehow, otherwise the window
        # size will not be updated unless something else happens, not exactly
        # sure what. This might be caused by the multi notebook, or custom
        # animation layouting?
        self._view.get_parent().resize_children()

    def attach(self):
        # Attach ourselves in the text view, and position just above the
        # text window
        win = self._view.get_window(Gtk.TextWindowType.TEXT)
        alloc = self.get_allocation()

        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM,
                                          max(alloc.height, 1))
        self._view.add_child_in_window(self, Gtk.TextWindowType.BOTTOM, 0, 0)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)

        self.show()
        self.set_size_request(win.get_width(), -1)

    def on_entry_focus_out(self, widget, evnt):
        if self._entry.get_sensitive():
            self.destroy()

    def on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if self._info_window:
                if self._suspended:
                    self._suspended.resume()

                if self._info_window:
                    self._info_window.destroy()

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self.prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self.prompt()
            else:
                self._view.grab_focus()
                self.destroy()

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self.prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self.run_command(
                    lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0]
                    == state) and evnt.keyval == handler[1] and handler[2](
                        handler[3], state):
                return True

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()

        self._history_prefix = None
        return False

    def on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def prompt(self, pr=''):
        self._prompt = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(
                self._accel_group.full_name()), )

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def make_info(self):
        if self._info_window == None:
            self._info_window = Info(self)
            self._info_window.show()

            self._info_window.connect('destroy', self.on_info_window_destroy)

    def on_info_window_destroy(self, widget):
        self._info_window = None

    def info_show(self, text='', use_markup=False):
        self.make_info()
        self._info_window.add_lines(text, use_markup)

    def info_status(self, text):
        self.make_info()
        self._info_window.status(text)

    def info_add_action(self, stock, callback, data=None):
        self.make_info()
        return self._info_window.add_action(stock, callback, data)

    def command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def on_wait_cancel(self):
        if self._suspended:
            self._suspended.resume()

        if self._cancel_button:
            self._cancel_button.destroy()

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()
            self._entry.grab_focus()
            self._entry.set_sensitive(True)

    def _show_wait_cancel(self):
        self._cancel_button = self.info_add_action(Gtk.STOCK_STOP,
                                                   self.on_wait_cancel)
        self.info_status('<i>Waiting to finish...</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def on_suspend_resume(self):
        if self._wait_timeout:
            GObject.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            self._cancel_button.destroy()
            self._cancel_button = None
            self.info_status(None)

        self._entry.set_sensitive(True)
        self.command_history_done()

        if self._entry.props.has_focus or (self._info_window
                                           and not self._info_window.empty()):
            self._entry.grab_focus()

        self.on_execute(None, 0)

    def ellipsize(self, s, size):
        if len(s) <= size:
            return s

        mid = (size - 4) / 2
        return s[:mid] + '...' + s[-mid:]

    def destroy(self):
        self.hide()
        Gtk.EventBox.destroy(self)

    def run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception, e:
            self.command_history_done()
            self._command_state.clear()

            self.prompt()

            # Show error in info
            self.info_show(
                '<b><span color="#f66">Error:</span></b> ' +
                saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self.format_trace(), False)

            return None

        if ret == commands.result.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self.on_suspend_resume)

            self._wait_timeout = GObject.timeout_add(500,
                                                     self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self.command_history_done()
            self.prompt('')

            if ret == commands.result.Result.PROMPT:
                self.prompt(ret.prompt)
            elif (ret == None
                  or ret == commands.result.HIDE) and not self._prompt and (
                      not self._info_window or self._info_window.empty()):
                self._command_state.clear()
                self._view.grab_focus()
                self.destroy()
            else:
                self._entry.grab_focus()

        return ret
コード例 #34
0
 def restore(self):
     """Restore this Node to the system-wide lists of Nodes"""
     self.included = True
     _logger.debug("Node %s restored to system", self)
     History.add('add', NodeAction(self))
コード例 #35
0
                    vs, vf, tg = [], [], []

        avg_loss = tot_loss / n_batches
        avg_acc = tot_acc / n_batches
        print(
            'Training log before start training--->avg_loss: %.3f' % avg_loss,
            'avg_acc: %.3f' % avg_acc, ' ptp01: %.3f' % np.mean(Ptp01),
            'ptp05: %.3f' % np.mean(Ptp05), ' auc: %.3f' % np.mean(AUC))
        train_logs = {
            'loss': avg_loss,
            'acc': avg_acc,
            'ptp01': np.mean(Ptp01),
            'ptp05': np.mean(Ptp05),
            'auc': np.mean(AUC)
        }
        train_hist.add(logs=train_logs, epoch=0)
        # ---------  Validation logs before start training -----------------
        # model.eval()
        # logisticReg.eval()
        tot_loss, tot_acc = 0, 0
        n_batches = len(validation_loader)
        Ptp01, Ptp05, Ptp1, AUC = np.zeros(
            n_batches // n_batch_verif), np.zeros(
                n_batches // n_batch_verif), np.zeros(
                    n_batches // n_batch_verif), np.zeros(n_batches //
                                                          n_batch_verif)
        vs, vf, tg = [], [], []
        idx = -1
        with torch.no_grad():
            for batch_idx, (data, target, img_file,
                            class_id) in enumerate(validation_loader):
コード例 #36
0
ファイル: entry.py プロジェクト: purpleidea/gedit-plugins
class Entry(Gtk.EventBox):
    __gtype_name__ = "CommanderEntry"

    def __init__(self, view):
        Gtk.EventBox.__init__(self)
        self._view = view

        self.set_visible_window(False)

        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
        hbox.show()
        hbox.set_border_width(3)

        # context for the view
        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name("gedit-commander-entry")
        self._entry.show()

        css = Gtk.CssProvider()
        css.load_from_data(
            bytes(
                """
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    /* Override background to anything. This is weird, but doing this we can
       then in code use widget.override_background to set the color dynamically
       to the same color as the gedit view */
    background: transparent;
    border-width: 0;
    box-shadow: 0 0 transparent;
    transition: none;
}
""",
                "utf-8",
            )
        )

        # FIXME: remove hardcopy of 600 (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
        # https://bugzilla.gnome.org/show_bug.cgi?id=646860
        self._entry.get_style_context().add_provider(css, 600)

        self._prompt_label = Gtk.Label(label="<b>&gt;&gt;&gt;</b>", use_markup=True)
        self._prompt_label.show()

        self._entry.connect("focus-out-event", self.on_entry_focus_out)
        self._entry.connect("key-press-event", self.on_entry_key_press)

        self._history = History(os.path.join(GLib.get_user_config_dir(), "gedit/commander/history"))
        self._prompt = None

        self._accel_group = None

        hbox.pack_start(self._prompt_label, False, False, 0)
        hbox.pack_start(self._entry, True, True, 0)

        self.copy_style_from_view()
        self.view_style_updated_id = self._view.connect("style-updated", self.on_view_style_updated)

        self.add(hbox)
        self.attach()
        self._entry.grab_focus()

        self._wait_timeout = 0
        self._info_window = None

        self.connect("destroy", self.on_destroy)
        self.connect_after("size-allocate", self.on_size_allocate)
        self.view_draw_id = self._view.connect_after("draw", self.on_draw)

        self._history_prefix = None
        self._suspended = None
        self._handlers = [
            [0, Gdk.KEY_Up, self.on_history_move, -1],
            [0, Gdk.KEY_Down, self.on_history_move, 1],
            [None, Gdk.KEY_Return, self.on_execute, None],
            [None, Gdk.KEY_KP_Enter, self.on_execute, None],
            [0, Gdk.KEY_Tab, self.on_complete, None],
            [0, Gdk.KEY_ISO_Left_Tab, self.on_complete, None],
        ]

        self._re_complete = re.compile("(\"((?:\\\\\"|[^\"])*)\"?|'((?:\\\\'|[^'])*)'?|[^\s]+)")
        self._command_state = commands.Commands.State()

    def on_view_style_updated(self, widget):
        self.copy_style_from_view()

    def get_border_color(self):
        color = self.get_background_color().copy()
        color.red = 1 - color.red
        color.green = 1 - color.green
        color.blue = 1 - color.blue
        color.alpha = 0.5

        return color

    def get_background_color(self):
        context = self._view.get_style_context()
        return context.get_background_color(Gtk.StateFlags.NORMAL)

    def get_foreground_color(self):
        context = self._view.get_style_context()
        return context.get_color(Gtk.StateFlags.NORMAL)

    def get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def copy_style_from_view(self, widget=None):
        if widget != None:
            context = self._view.get_style_context()
            font = context.get_font(Gtk.StateFlags.NORMAL)

            widget.override_color(Gtk.StateFlags.NORMAL, self.get_foreground_color())
            widget.override_font(self.get_font())
        else:
            if self._entry:
                self.copy_style_from_view(self._entry)

            if self._prompt_label:
                self.copy_style_from_view(self._prompt_label)

    def view(self):
        return self._view

    def on_size_allocate(self, widget, alloc):
        alloc = self.get_allocation()
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, alloc.height)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)
        self.set_size_request(win.get_width(), -1)

        # NOTE: we need to do this explicitly somehow, otherwise the window
        # size will not be updated unless something else happens, not exactly
        # sure what. This might be caused by the multi notebook, or custom
        # animation layouting?
        self._view.get_parent().resize_children()

    def attach(self):
        # Attach ourselves in the text view, and position just above the
        # text window
        win = self._view.get_window(Gtk.TextWindowType.TEXT)
        alloc = self.get_allocation()

        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, max(alloc.height, 1))
        self._view.add_child_in_window(self, Gtk.TextWindowType.BOTTOM, 0, 0)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)

        self.show()
        self.set_size_request(win.get_width(), -1)

    def on_entry_focus_out(self, widget, evnt):
        if self._entry.get_sensitive():
            self.destroy()

    def on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if self._info_window:
                if self._suspended:
                    self._suspended.resume()

                if self._info_window:
                    self._info_window.destroy()

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self.prompt()
            elif text:
                self._entry.set_text("")
            elif self._command_state:
                self._command_state.clear()
                self.prompt()
            else:
                self._view.grab_focus()
                self.destroy()

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text("")
                self._entry.set_editable(False)
                self.prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self.run_command(lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (
                (handler[0] == None or handler[0] == state)
                and evnt.keyval == handler[1]
                and handler[2](handler[3], state)
            ):
                return True

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()

        self._history_prefix = None
        return False

    def on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ""

        if self._history_prefix == None:
            hist = ""
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def prompt(self, pr=""):
        self._prompt = pr

        if self._accel_group != None:
            pr = "<i>%s</i>" % (saxutils.escape(self._accel_group.full_name()),)

        if not pr:
            pr = ""
        else:
            pr = " " + pr

        self._prompt_label.set_markup("<b>&gt;&gt;&gt;</b>%s" % pr)

    def make_info(self):
        if self._info_window == None:
            self._info_window = Info(self)
            self._info_window.show()

            self._info_window.connect("destroy", self.on_info_window_destroy)

    def on_info_window_destroy(self, widget):
        self._info_window = None

    def info_show(self, text="", use_markup=False):
        self.make_info()
        self._info_window.add_lines(text, use_markup)

    def info_status(self, text):
        self.make_info()
        self._info_window.status(text)

    def info_add_action(self, stock, callback, data=None):
        self.make_info()
        return self._info_window.add_action(stock, callback, data)

    def command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text("")

    def on_wait_cancel(self):
        if self._suspended:
            self._suspended.resume()

        if self._cancel_button:
            self._cancel_button.destroy()

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()
            self._entry.grab_focus()
            self._entry.set_sensitive(True)

    def _show_wait_cancel(self):
        self._cancel_button = self.info_add_action(Gtk.STOCK_STOP, self.on_wait_cancel)
        self.info_status("<i>Waiting to finish...</i>")

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def on_suspend_resume(self):
        if self._wait_timeout:
            GLib.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            self._cancel_button.destroy()
            self._cancel_button = None
            self.info_status(None)

        self._entry.set_sensitive(True)
        self.command_history_done()

        if self._entry.props.has_focus or (self._info_window and not self._info_window.empty()):
            self._entry.grab_focus()

        self.on_execute(None, 0)

    def ellipsize(self, s, size):
        if len(s) <= size:
            return s

        mid = (size - 4) / 2
        return s[:mid] + "..." + s[-mid:]

    def destroy(self):
        self.hide()
        Gtk.EventBox.destroy(self)

    def run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception as e:
            self.command_history_done()
            self._command_state.clear()

            self.prompt()

            # Show error in info
            self.info_show('<b><span color="#f66">Error:</span></b> ' + saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self.format_trace(), False)

            return None

        mod = sys.modules["commander.commands.result"]

        if ret == mod.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self.on_suspend_resume)

            self._wait_timeout = GLib.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self.command_history_done()
            self.prompt("")

            if ret == mod.Result.PROMPT:
                self.prompt(ret.prompt)
            elif (
                (ret == None or ret == mod.HIDE)
                and not self._prompt
                and (not self._info_window or self._info_window.empty())
            ):
                self._command_state.clear()
                self._view.grab_focus()
                self.destroy()
            else:
                self._entry.grab_focus()

        return ret

    def format_trace(self):
        tp, val, tb = sys.exc_info()

        origtb = tb

        thisdir = os.path.dirname(__file__)

        # Skip frames up until after the last entry.py...
        while True:
            filename = tb.tb_frame.f_code.co_filename

            dname = os.path.dirname(filename)

            if not dname.startswith(thisdir):
                break

            tb = tb.tb_next

        msg = traceback.format_exception(tp, val, tb)
        r = "".join(msg[0:-1])

        # This is done to prevent cyclic references, see python
        # documentation on sys.exc_info
        del origtb

        return r

    def on_execute(self, dummy, modifier):
        if self._info_window and not self._suspended:
            self._info_window.destroy()

        text = self._entry.get_text().strip()
        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        if not wordsstr and not self._command_state:
            self._entry.set_text("")
            return

        self.run_command(
            lambda: commands.Commands().execute(self._command_state, text, words, wordsstr, self, modifier)
        )

        return True

    def on_complete(self, dummy, modifier):
        # First split all the text in words
        text = self._entry.get_text()
        pos = self._entry.get_position()

        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        # Find out at which word the cursor actually is
        # Examples:
        #  * hello world|
        #  * hello| world
        #  * |hello world
        #  * hello wor|ld
        #  * hello  |  world
        #  * "hello world|"
        posidx = None

        for idx in range(0, len(words)):
            spec = self._complete_word_match(words[idx])

            if words[idx].start(0) > pos:
                # Empty space, new completion
                wordsstr.insert(idx, "")
                words.insert(idx, None)
                posidx = idx
                break
            elif spec[2] == pos:
                # At end of word, resume completion
                posidx = idx
                break
            elif spec[1] <= pos and spec[2] > pos:
                # In middle of word, do not complete
                return True

        if posidx == None:
            wordsstr.append("")
            words.append(None)
            posidx = len(wordsstr) - 1

        # First word completes a command, if not in any special 'mode'
        # otherwise, relay completion to the command, or complete by advice
        # from the 'mode' (prompt)
        cmds = commands.Commands()

        if not self._command_state and posidx == 0:
            # Complete the first command
            ret = commands.completion.command(words=wordsstr, idx=posidx)
        else:
            complete = None
            realidx = posidx

            if not self._command_state:
                # Get the command first
                cmd = commands.completion.single_command(wordsstr, 0)
                realidx -= 1

                ww = wordsstr[1:]
            else:
                cmd = self._command_state.top()
                ww = wordsstr

            if cmd:
                complete = cmd.autocomplete_func()

            if not complete:
                return True

            # 'complete' contains a dict with arg -> func to do the completion
            # of the named argument the command (or stack item) expects
            args, varargs = cmd.args()

            # Remove system arguments
            s = ["argstr", "args", "entry", "view"]
            args = list(filter(lambda x: not x in s, args))

            if realidx < len(args):
                arg = args[realidx]
            elif varargs:
                arg = "*"
            else:
                return True

            if not arg in complete:
                return True

            func = complete[arg]

            try:
                spec = utils.getargspec(func)

                if not ww:
                    ww = [""]

                kwargs = {"words": ww, "idx": realidx, "view": self._view}

                if not spec.keywords:
                    for k in list(kwargs.keys()):
                        if not k in spec.args:
                            del kwargs[k]

                ret = func(**kwargs)
            except Exception as e:
                # Can be number of arguments, or return values or simply buggy
                # modules
                print(e)
                traceback.print_exc()
                return True

        if not ret or not ret[0]:
            return True

        res = ret[0]
        completed = ret[1]

        if len(ret) > 2:
            after = ret[2]
        else:
            after = " "

        # Replace the word
        if words[posidx] == None:
            # At end of everything, just append
            spec = None

            self._entry.insert_text(completed, self._entry.get_text_length())
            self._entry.set_position(-1)
        else:
            spec = self._complete_word_match(words[posidx])

            self._entry.delete_text(spec[1], spec[2])
            self._entry.insert_text(completed, spec[1])
            self._entry.set_position(spec[1] + len(completed))

        if len(res) == 1:
            # Full completion
            lastpos = self._entry.get_position()

            if not isinstance(res[0], commands.module.Module) or not res[0].commands():
                if (
                    words[posidx]
                    and after == " "
                    and (words[posidx].group(2) != None or words[posidx].group(3) != None)
                ):
                    lastpos = lastpos + 1

                self._entry.insert_text(after, lastpos)
                self._entry.set_position(lastpos + 1)
            elif completed == wordsstr[posidx] or not res[0].method:
                self._entry.insert_text(".", lastpos)
                self._entry.set_position(lastpos + 1)

            if self._info_window:
                self._info_window.destroy()
        else:
            # Show popup with completed items
            if self._info_window:
                self._info_window.clear()

            ret = []

            for x in res:
                if isinstance(x, commands.method.Method):
                    ret.append("<b>" + saxutils.escape(x.name) + "</b> (<i>" + x.oneline_doc() + "</i>)")
                else:
                    ret.append(str(x))

            self.info_show("\n".join(ret), True)

        return True

    def on_draw(self, widget, ct):
        win = widget.get_window(Gtk.TextWindowType.BOTTOM)

        if not Gtk.cairo_should_draw_window(ct, win):
            return False

        Gtk.cairo_transform_to_window(ct, widget, win)

        color = self.get_border_color()
        width = win.get_width()

        ct.set_source_rgba(color.red, color.green, color.blue, color.alpha)
        ct.move_to(0, 0)
        ct.line_to(width, 0)
        ct.stroke()

        return False

    def on_destroy(self, widget):
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, 0)
        self._view.disconnect(self.view_style_updated_id)
        self._view.disconnect(self.view_draw_id)

        if self._info_window:
            self._info_window.destroy()

        self._history.save()
コード例 #37
0
ファイル: frameworkBack.py プロジェクト: TaoZong/Python
    def schedule(cls, msgs_to_process=None, timers_to_process=None):
        """Schedule given number of pending messages"""
        if msgs_to_process is None:
            msgs_to_process = 32768
        if timers_to_process is None:
            timers_to_process = 32768
        while cls._work_to_do():
            _logger.info("Start of schedule: %d (limit %d) pending messages, %d (limit %d) pending timers",
                         len(cls.queue), msgs_to_process, TimerManager.pending_count(), timers_to_process)
            # Process all the queued up messages (which may enqueue more along the way)
            while cls.queue:
                msg = cls.queue.popleft()
                if msg.to_node in cls.block:
                    _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg)
                    History.add("drop", msg)
                else:
                    
                    try:
                        c = zerorpc.Client(timeout=1)
                        c.connect('tcp://' + msg.to_node)
                    except zerorpc.TimeoutExpired:
                        _logger.info("Drop %s->%s: %s as destination down", msg.from_node, msg.to_node, msg)
                        History.add("drop", msg)
                        continue
                    if not Framework.reachable(msg.from_node, msg.to_node):
                        _logger.info("Drop %s->%s: %s as route down", msg.from_node, msg.to_node, msg)
                        History.add("cut", msg)
                        _logger.info("Dequeue %s->%s: %s", msg.from_node, msg.to_node, msg)
                    elif isinstance(msg, ResponseMessage):
                            # figure out the original request this is a response to
                        try:
                            reqmsg = msg.response_to.original_msg
                        except Exception:
                            reqmsg = msg.response_to
                            # cancel any timer associated with the original request
                        cls.remove_req_timer(reqmsg)
                    History.add("deliver", msg)
                    m = pickle.dumps(msg)
                    try:
                       
                        c.rcvmsg(m)
                        c.close()
                    except:
                        print 'time out'
                        cls.queue.append(msg)
                        
                        print cls.block
                        for node in cls.nodeList:
                            if node != msg.to_node:
                                    
                                bmsg = BlockRsp(from_node=msg.from_node, to_node=node, key=msg.to_node, msg_id=None)
                                cls.queue.append(bmsg)
                    msgs_to_process = msgs_to_process - 1
                    if msgs_to_process == 0:
                        return

            # No pending messages; potentially pop a (single) timer
            if TimerManager.pending_count() > 0 and timers_to_process > 0:
                # Pop the first pending timer; this may enqueue work
                TimerManager.pop_timer()
                timers_to_process = timers_to_process - 1
            if timers_to_process == 0:
                return
コード例 #38
0
class Entry(gtk.EventBox):
    def __init__(self, view):
        gtk.EventBox.__init__(self)
        self._view = view

        hbox = gtk.HBox(False, 3)
        hbox.show()
        hbox.set_border_width(3)

        self._entry = gtk.Entry()
        self._entry.modify_font(self._view.style.font_desc)
        self._entry.set_has_frame(False)
        self._entry.set_name('command-bar')
        self._entry.modify_text(gtk.STATE_NORMAL,
                                self._view.style.text[gtk.STATE_NORMAL])
        self._entry.set_app_paintable(True)

        self._entry.connect('realize', self.on_realize)
        self._entry.connect('expose-event', self.on_entry_expose)

        self._entry.show()

        self._prompt_label = gtk.Label('<b>&gt;&gt;&gt;</b>')
        self._prompt_label.set_use_markup(True)
        self._prompt_label.modify_font(self._view.style.font_desc)
        self._prompt_label.show()
        self._prompt_label.modify_fg(gtk.STATE_NORMAL,
                                     self._view.style.text[gtk.STATE_NORMAL])

        self.modify_bg(gtk.STATE_NORMAL, self.background_gdk())
        self._entry.modify_base(gtk.STATE_NORMAL, self.background_gdk())

        self._entry.connect('focus-out-event', self.on_entry_focus_out)
        self._entry.connect('key-press-event', self.on_entry_key_press)

        self.connect_after('size-allocate', self.on_size_allocate)
        self.connect_after('expose-event', self.on_expose)
        self.connect_after('realize', self.on_realize)

        self._history = History(
            os.path.expanduser('~/.config/pluma/commander/history'))
        self._prompt = None

        self._accel_group = None

        hbox.pack_start(self._prompt_label, False, False, 0)
        hbox.pack_start(self._entry, True, True, 0)

        self.add(hbox)
        self.attach()

        self._entry.grab_focus()
        self._wait_timeout = 0
        self._info_window = None

        self.connect('destroy', self.on_destroy)

        self._history_prefix = None
        self._suspended = None
        self._handlers = [[0, gtk.keysyms.Up, self.on_history_move, -1],
                          [0, gtk.keysyms.Down, self.on_history_move, 1],
                          [None, gtk.keysyms.Return, self.on_execute, None],
                          [None, gtk.keysyms.KP_Enter, self.on_execute, None],
                          [0, gtk.keysyms.Tab, self.on_complete, None],
                          [
                              0, gtk.keysyms.ISO_Left_Tab, self.on_complete,
                              None
                          ]]

        self._re_complete = re.compile(
            '("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

    def view(self):
        return self._view

    def on_realize(self, widget):
        widget.window.set_back_pixmap(None, False)

    def on_entry_expose(self, widget, evnt):
        ct = evnt.window.cairo_create()
        ct.rectangle(evnt.area.x, evnt.area.y, evnt.area.width,
                     evnt.area.height)

        bg = self.background_color()
        ct.set_source_rgb(bg[0], bg[1], bg[2])
        ct.fill()

        return False

    def on_expose(self, widget, evnt):
        ct = evnt.window.cairo_create()
        color = self.background_color()

        ct.rectangle(evnt.area.x, evnt.area.y, evnt.area.width,
                     evnt.area.height)
        ct.clip()

        # Draw separator line
        ct.move_to(0, 0)
        ct.set_line_width(1)
        ct.line_to(self.allocation.width, 0)

        ct.set_source_rgb(1 - color[0], 1 - color[1], 1 - color[2])
        ct.stroke()
        return False

    def on_size_allocate(self, widget, alloc):
        vwwnd = self._view.get_window(gtk.TEXT_WINDOW_BOTTOM).get_parent()
        size = vwwnd.get_size()
        position = vwwnd.get_position()

        self._view.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, alloc.height)

    def attach(self):
        # Attach ourselves in the text view, and position just above the
        # text window
        self._view.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, 1)
        alloc = self._view.allocation

        self.show()
        self._view.add_child_in_window(self, gtk.TEXT_WINDOW_BOTTOM, 0, 0)
        self.set_size_request(alloc.width, -1)

    def background_gdk(self):
        bg = self.background_color()

        bg = map(lambda x: int(x * 65535), bg)
        return gtk.gdk.Color(bg[0], bg[1], bg[2])

    def background_color(self):
        bg = self._view.get_style().base[self._view.state]

        vals = [bg.red, bg.green, bg.blue, 1]

        for i in range(3):
            val = vals[i] / 65535.0

            if val < 0.0001:
                vals[i] = 0.1
            elif val > 0.9999:
                vals[i] = 0.9
            elif val < 0.1:
                vals[i] = val * 1.2
            else:
                vals[i] = val * 0.8

        return vals

    def on_entry_focus_out(self, widget, evnt):
        if self._entry.flags() & gtk.SENSITIVE:
            self.destroy()

    def on_entry_key_press(self, widget, evnt):
        state = evnt.state & gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == gtk.keysyms.Escape:
            if self._info_window:
                if self._suspended:
                    self._suspended.resume()

                if self._info_window:
                    self._info_window.destroy()

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self.prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self.prompt()
            else:
                self._view.grab_focus()
                self.destroy()

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self.prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self.run_command(
                    lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0]
                    == state) and evnt.keyval == handler[1] and handler[2](
                        handler[3], state):
                return True

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()

        self._history_prefix = None
        return False

    def on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def prompt(self, pr=''):
        self._prompt = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(
                self._accel_group.full_name()), )

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def make_info(self):
        if self._info_window == None:
            self._info_window = Info(self)
            self._info_window.show()

            self._info_window.connect('destroy', self.on_info_window_destroy)

    def on_info_window_destroy(self, widget):
        self._info_window = None

    def info_show(self, text='', use_markup=False):
        self.make_info()
        self._info_window.add_lines(text, use_markup)

    def info_status(self, text):
        self.make_info()
        self._info_window.status(text)

    def info_add_action(self, stock, callback, data=None):
        self.make_info()
        return self._info_window.add_action(stock, callback, data)

    def command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def on_wait_cancel(self):
        if self._suspended:
            self._suspended.resume()

        if self._cancel_button:
            self._cancel_button.destroy()

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()
            self._entry.grab_focus()
            self._entry.set_sensitive(True)

    def _show_wait_cancel(self):
        self._cancel_button = self.info_add_action(gtk.STOCK_STOP,
                                                   self.on_wait_cancel)
        self.info_status('<i>Waiting to finish...</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def on_suspend_resume(self):
        if self._wait_timeout:
            glib.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            self._cancel_button.destroy()
            self._cancel_button = None
            self.info_status(None)

        self._entry.set_sensitive(True)
        self.command_history_done()

        if self._entry.props.has_focus or (self._info_window
                                           and not self._info_window.empty()):
            self._entry.grab_focus()

        self.on_execute(None, 0)

    def ellipsize(self, s, size):
        if len(s) <= size:
            return s

        mid = (size - 4) / 2
        return s[:mid] + '...' + s[-mid:]

    def destroy(self):
        self.hide()
        gtk.EventBox.destroy(self)

    def run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception, e:
            self.command_history_done()
            self._command_state.clear()

            self.prompt()

            # Show error in info
            self.info_show(
                '<b><span color="#f66">Error:</span></b> ' +
                saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(traceback.format_exc(), False)

            return None

        if ret == commands.result.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self.on_suspend_resume)

            self._wait_timeout = glib.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self.command_history_done()
            self.prompt('')

            if ret == commands.result.Result.PROMPT:
                self.prompt(ret.prompt)
            elif (ret == None
                  or ret == commands.result.HIDE) and not self._prompt and (
                      not self._info_window or self._info_window.empty()):
                self._command_state.clear()
                self._view.grab_focus()
                self.destroy()
            else:
                self._entry.grab_focus()

        return ret
コード例 #39
0
ファイル: frameworkBack.py プロジェクト: TaoZong/Python
 def send_message(cls, msg, expect_reply=True):
     """Send a message"""
     _logger.info("Enqueue %s->%s: %s", msg.from_node, msg.to_node, msg)
     cls.queue.append(msg)
     History.add("send", msg)
コード例 #40
0
class Agent(BaseModel):
    def __init__(self, config, environment, sess):
        super(Agent, self).__init__(config)
        self.sess = sess

        self.env = environment
        self.history = History(self.config)
        self.memory = ReplayMemory(self.config, self.checkpoint_dir)

        with tf.variable_scope('step'):
            self.step_op = tf.Variable(0, trainable=False, name='step')
            self.step_input = tf.placeholder('int32', None, name='step_input')
            self.step_assign_op = self.step_op.assign(self.step_input)

        self.build_dqn()

    def train(self):
        start_step = self.step_op.eval()
        start_time = time.time()

        num_game, self.update_count, ep_reward = 0, 0, 0.
        total_reward, self.total_loss, self.total_q = 0., 0., 0.
        ep_rewards, actions = [], []

        screen, reward, action, terminal = self.env.new_random_game()
        for _ in range(self.history_length):
            self.history.add(screen)

        for self.step in tqdm(range(start_step, self.max_step),
                              ncols=72,
                              initial=start_step):
            if self.step == self.learn_start:
                num_game, self.update_count, ep_reward = 0, 0, 0.
                total_reward, self.total_loss, self.total_q = 0., 0., 0.
                ep_rewards, actions = [], []

            # 1. predict
            action = self.predict(self.history.get())
            # 2. act
            screen, reward, terminal = self.env.act(action, is_training=True)
            # 3. observe
            self.observe(screen, reward, action, terminal)

            if terminal:
                screen, reward, action, terminal = self.env.new_random_game()

                num_game += 1
                ep_rewards.append(ep_reward)
                ep_reward = 0.
            else:
                ep_reward += reward

            actions.append(action)
            total_reward += reward

            if self.step > self.learn_start:
                if self.step % self.test_step == 0:
                    avg_reward = total_reward / self.test_step
                    avg_loss = self.total_loss / self.update_count
                    avg_q = self.total_q / self.update_count

                    try:
                        max_ep_reward = np.max(ep_rewards)
                        min_ep_reward = np.min(ep_rewards)
                        avg_ep_reward = np.mean(ep_rewards)
                    except:
                        max_ep_reward, min_ep_reward, avg_ep_reward = 0, 0, 0

                    print(
                        '\navg_r: %.4f, avg_l: %.6f, avg_q: %3.6f, avg_ep_r: %.4f, max_ep_r: %.4f, min_ep_r: %.4f, # game: %d'
                        % (avg_reward, avg_loss, avg_q, avg_ep_reward,
                           max_ep_reward, min_ep_reward, num_game))

                    self.step_assign_op.eval({self.step_input: self.step + 1})
                    self.save_model(self.step + 1)

                    if self.step > 180:
                        self.inject_summary(
                            {
                                'average.reward':
                                avg_reward,
                                'average.loss':
                                avg_loss,
                                'average.q':
                                avg_q,
                                'episode.max reward':
                                max_ep_reward,
                                'episode.min reward':
                                min_ep_reward,
                                'episode.avg reward':
                                avg_ep_reward,
                                'episode.num of game':
                                num_game,
                                'episode.rewards':
                                ep_rewards,
                                'episode.actions':
                                actions,
                                'training.learning_rate':
                                self.learning_rate_op.eval(
                                    {self.learning_rate_step: self.step}),
                            }, self.step)

                    num_game = 0
                    total_reward = 0.
                    self.total_loss = 0.
                    self.total_q = 0.
                    self.update_count = 0
                    ep_reward = 0.
                    ep_rewards = []
                    actions = []

    def predict(self, s_t, test_ep=None):
        ep = test_ep or (self.ep_end + max(
            0., (self.ep_start - self.ep_end) *
            (self.ep_end_t - max(0., self.step - self.learn_start)) /
            self.ep_end_t))

        if random.random() < ep:
            action = random.randrange(self.env.action_size)
        else:
            action = self.q_action.eval({self.s_t: [s_t]})[0]

        return action

    def observe(self, screen, reward, action, terminal):
        reward = max(self.min_reward, min(self.max_reward, reward))

        self.history.add(screen)
        self.memory.add(screen, reward, action, terminal)

        if self.step > self.learn_start:
            if self.step % self.train_frequency == 0:
                self.q_learning_mini_batch()

            if self.step % self.target_q_update_step == self.target_q_update_step - 1:
                self.update_target_q_network()

    def q_learning_mini_batch(self):
        if self.memory.count < self.history_length:
            return
        else:
            s_t, action, reward, s_t_plus_1, terminal = self.memory.sample()

        t = time.time()
        if self.double_q:
            # Double Q-learning
            pred_action = self.q_action.eval({self.s_t: s_t_plus_1})

            q_t_plus_1_with_pred_action = self.target_q_with_idx.eval({
                self.target_s_t:
                s_t_plus_1,
                self.target_q_idx:
                [[idx, pred_a] for idx, pred_a in enumerate(pred_action)]
            })
            target_q_t = (1. - terminal) * self.discount * \
                q_t_plus_1_with_pred_action + reward
        else:
            q_t_plus_1 = self.target_q.eval({self.target_s_t: s_t_plus_1})

            terminal = np.array(terminal) + 0.
            max_q_t_plus_1 = np.max(q_t_plus_1, axis=1)
            target_q_t = (1. - terminal) * self.discount * \
                max_q_t_plus_1 + reward

        _, q_t, loss, summary_str = self.sess.run(
            [self.optim, self.q, self.loss, self.q_summary], {
                self.target_q_t: target_q_t,
                self.action: action,
                self.s_t: s_t,
                self.learning_rate_step: self.step,
            })

        self.writer.add_summary(summary_str, self.step)
        self.total_loss += loss
        self.total_q += q_t.mean()
        self.update_count += 1

    def build_dqn(self):
        self.w = {}
        self.t_w = {}

        # initializer = tf.contrib.layers.xavier_initializer()
        initializer = tf.truncated_normal_initializer(0, 0.02)
        activation_fn = tf.nn.relu

        # training network
        with tf.variable_scope('prediction'):
            if self.cnn_format == 'NHWC':
                self.s_t = tf.placeholder('float32', [
                    None, self.screen_height, self.screen_width,
                    self.history_length
                ],
                                          name='s_t')
            else:
                self.s_t = tf.placeholder('float32', [
                    None, self.history_length, self.screen_height,
                    self.screen_width
                ],
                                          name='s_t')

            self.l1, self.w['l1_w'], self.w['l1_b'] = conv2d(self.s_t,
                                                             32, [8, 8],
                                                             [4, 4],
                                                             initializer,
                                                             activation_fn,
                                                             self.cnn_format,
                                                             name='l1')
            self.l2, self.w['l2_w'], self.w['l2_b'] = conv2d(self.l1,
                                                             64, [4, 4],
                                                             [2, 2],
                                                             initializer,
                                                             activation_fn,
                                                             self.cnn_format,
                                                             name='l2')
            self.l3, self.w['l3_w'], self.w['l3_b'] = conv2d(self.l2,
                                                             64, [3, 3],
                                                             [1, 1],
                                                             initializer,
                                                             activation_fn,
                                                             self.cnn_format,
                                                             name='l3')

            shape = self.l3.get_shape().as_list()
            self.l3_flat = tf.reshape(
                self.l3, [-1, reduce(lambda x, y: x * y, shape[1:])])

            if self.dueling:
                self.value_hid, self.w['l4_val_w'], self.w['l4_val_b'] = \
                    linear(self.l3_flat, 512,
                           activation_fn=activation_fn, name='value_hid')

                self.adv_hid, self.w['l4_adv_w'], self.w['l4_adv_b'] = \
                    linear(self.l3_flat, 512,
                           activation_fn=activation_fn, name='adv_hid')

                self.value, self.w['val_w_out'], self.w['val_w_b'] = \
                    linear(self.value_hid, 1, name='value_out')

                self.advantage, self.w['adv_w_out'], self.w['adv_w_b'] = \
                    linear(self.adv_hid, self.env.action_size, name='adv_out')

                # Average Dueling
                self.q = self.value + (self.advantage - tf.reduce_mean(
                    self.advantage, reduction_indices=1, keep_dims=True))
            else:
                self.l4, self.w['l4_w'], self.w['l4_b'] = linear(
                    self.l3_flat, 512, activation_fn=activation_fn, name='l4')
                self.q, self.w['q_w'], self.w['q_b'] = linear(
                    self.l4, self.env.action_size, name='q')

            self.q_action = tf.argmax(self.q, dimension=1)

            q_summary = []
            avg_q = tf.reduce_mean(self.q, 0)
            for idx in range(self.env.action_size):
                q_summary.append(tf.histogram_summary('q/%s' % idx,
                                                      avg_q[idx]))
            self.q_summary = tf.merge_summary(q_summary, 'q_summary')

        # target network
        with tf.variable_scope('target'):
            if self.cnn_format == 'NHWC':
                self.target_s_t = tf.placeholder('float32', [
                    None, self.screen_height, self.screen_width,
                    self.history_length
                ],
                                                 name='target_s_t')
            else:
                self.target_s_t = tf.placeholder('float32', [
                    None, self.history_length, self.screen_height,
                    self.screen_width
                ],
                                                 name='target_s_t')

            self.target_l1, self.t_w['l1_w'], self.t_w['l1_b'] = conv2d(
                self.target_s_t,
                32, [8, 8], [4, 4],
                initializer,
                activation_fn,
                self.cnn_format,
                name='target_l1')
            self.target_l2, self.t_w['l2_w'], self.t_w['l2_b'] = conv2d(
                self.target_l1,
                64, [4, 4], [2, 2],
                initializer,
                activation_fn,
                self.cnn_format,
                name='target_l2')
            self.target_l3, self.t_w['l3_w'], self.t_w['l3_b'] = conv2d(
                self.target_l2,
                64, [3, 3], [1, 1],
                initializer,
                activation_fn,
                self.cnn_format,
                name='target_l3')

            shape = self.target_l3.get_shape().as_list()
            self.target_l3_flat = tf.reshape(
                self.target_l3, [-1, reduce(lambda x, y: x * y, shape[1:])])

            if self.dueling:
                self.t_value_hid, self.t_w['l4_val_w'], self.t_w['l4_val_b'] = \
                    linear(self.target_l3_flat, 512,
                           activation_fn=activation_fn, name='target_value_hid')

                self.t_adv_hid, self.t_w['l4_adv_w'], self.t_w['l4_adv_b'] = \
                    linear(self.target_l3_flat, 512,
                           activation_fn=activation_fn, name='target_adv_hid')

                self.t_value, self.t_w['val_w_out'], self.t_w['val_w_b'] = \
                    linear(self.t_value_hid, 1, name='target_value_out')

                self.t_advantage, self.t_w['adv_w_out'], self.t_w['adv_w_b'] = \
                    linear(self.t_adv_hid, self.env.action_size,
                           name='target_adv_out')

                # Average Dueling
                self.target_q = self.t_value + (
                    self.t_advantage - tf.reduce_mean(
                        self.t_advantage, reduction_indices=1, keep_dims=True))
            else:
                self.target_l4, self.t_w['l4_w'], self.t_w['l4_b'] = \
                    linear(self.target_l3_flat, 512,
                           activation_fn=activation_fn, name='target_l4')
                self.target_q, self.t_w['q_w'], self.t_w['q_b'] = \
                    linear(self.target_l4, self.env.action_size, name='target_q')

            self.target_q_idx = tf.placeholder('int32', [None, None],
                                               'outputs_idx')
            self.target_q_with_idx = tf.gather_nd(self.target_q,
                                                  self.target_q_idx)

        with tf.variable_scope('pred_to_target'):
            self.t_w_input = {}
            self.t_w_assign_op = {}

            for name in self.w.keys():
                self.t_w_input[name] = tf.placeholder(
                    'float32', self.t_w[name].get_shape().as_list(), name=name)
                self.t_w_assign_op[name] = self.t_w[name].assign(
                    self.t_w_input[name])

        # optimizer
        with tf.variable_scope('optimizer'):
            self.target_q_t = tf.placeholder('float32', [None],
                                             name='target_q_t')
            self.action = tf.placeholder('int64', [None], name='action')

            action_one_hot = tf.one_hot(self.action,
                                        self.env.action_size,
                                        1.0,
                                        0.0,
                                        name='action_one_hot')
            q_acted = tf.reduce_sum(self.q * action_one_hot,
                                    reduction_indices=1,
                                    name='q_acted')

            self.delta = self.target_q_t - q_acted
            self.clipped_delta = tf.clip_by_value(self.delta,
                                                  self.min_delta,
                                                  self.max_delta,
                                                  name='clipped_delta')

            self.global_step = tf.Variable(0, trainable=False)

            self.loss = tf.reduce_mean(tf.square(self.clipped_delta),
                                       name='loss')
            self.learning_rate_step = tf.placeholder('int64',
                                                     None,
                                                     name='learning_rate_step')
            self.learning_rate_op = tf.maximum(
                self.learning_rate_minimum,
                tf.train.exponential_decay(self.learning_rate,
                                           self.learning_rate_step,
                                           self.learning_rate_decay_step,
                                           self.learning_rate_decay,
                                           staircase=True))
            self.optim = tf.train.RMSPropOptimizer(self.learning_rate_op,
                                                   momentum=0.95,
                                                   epsilon=0.01).minimize(
                                                       self.loss)

        with tf.variable_scope('summary'):
            scalar_summary_tags = [
                'average.reward', 'average.loss', 'average.q',
                'episode.max reward', 'episode.min reward',
                'episode.avg reward', 'episode.num of game',
                'training.learning_rate'
            ]

            self.summary_placeholders = {}
            self.summary_ops = {}

            for tag in scalar_summary_tags:
                self.summary_placeholders[tag] = tf.placeholder(
                    'float32', None, name=tag.replace(' ', '_'))
                self.summary_ops[tag] = tf.scalar_summary(
                    "%s/%s" % (self.env_name, tag),
                    self.summary_placeholders[tag])

            histogram_summary_tags = ['episode.rewards', 'episode.actions']

            for tag in histogram_summary_tags:
                self.summary_placeholders[tag] = tf.placeholder(
                    'float32', None, name=tag.replace(' ', '_'))
                self.summary_ops[tag] = tf.histogram_summary(
                    tag, self.summary_placeholders[tag])

            self.writer = tf.train.SummaryWriter('./logs/%s' % self.model_dir,
                                                 self.sess.graph)

        tf.initialize_all_variables().run()

        self._saver = tf.train.Saver(list(self.w.values()) + [self.step_op],
                                     max_to_keep=30)

        self.load_model()
        self.update_target_q_network()

    def update_target_q_network(self):
        for name in self.w.keys():
            self.t_w_assign_op[name].eval(
                {self.t_w_input[name]: self.w[name].eval()})

    def inject_summary(self, tag_dict, step):
        summary_str_lists = self.sess.run(
            [self.summary_ops[tag] for tag in tag_dict.keys()], {
                self.summary_placeholders[tag]: value
                for tag, value in tag_dict.items()
            })
        for summary_str in summary_str_lists:
            self.writer.add_summary(summary_str, self.step)

    def play(self, n_step=10000, n_episode=100, test_ep=None, render=False):
        if test_ep == None:
            test_ep = self.ep_end

        test_history = History(self.config)

        if not self.display:
            gym_dir = '/tmp/%s-%s' % (self.env_name, get_time())
            self.env.env.monitor.start(gym_dir)

        best_reward, best_idx = 0, 0
        for idx in range(n_episode):
            screen, reward, action, terminal = self.env.new_random_game()
            current_reward = 0

            for _ in range(self.history_length):
                test_history.add(screen)

            for t in tqdm(range(n_step), ncols=72):
                # 1. predict
                action = self.predict(test_history.get(), test_ep)
                # 2. act
                screen, reward, terminal = self.env.act(action,
                                                        is_training=False)
                # 3. observe
                test_history.add(screen)

                current_reward += reward
                if terminal:
                    break

            if current_reward > best_reward:
                best_reward = current_reward
                best_idx = idx

            print("=" * 30)
            print(" [%d] Best reward : %d" % (best_idx, best_reward))
            print("=" * 30)

        if not self.display:
            self.env.env.monitor.close()

    def save_model(self, step=None):
        super(Agent, self).save_model(step)
        self.memory.save()

    def load_model(self):
        if super(Agent, self).load_model():
            # Only try to load the replay memory if we successfully loaded
            # a checkpoint file.
            self.memory.load()
コード例 #41
0
ファイル: framework.py プロジェクト: thtrieu/pynamo
 def cut_wires(cls, from_nodes, to_nodes):
     History.add(
         "announce", "Cut %s -> %s" %
         ([str(x) for x in from_nodes], [str(x) for x in to_nodes]))
     cls.cuts.append((from_nodes, to_nodes))
コード例 #42
0
ファイル: entry.py プロジェクト: GNOME/gedit-plugins
class Entry(Gtk.Box):
    __gtype_name__ = "CommanderEntry"

    def _show(self):
        self._reveal.set_reveal_child(True)

    def _hide(self):
        self._reveal.set_reveal_child(False)

    def __init__(self, view):
        super(Entry, self).__init__()

        self._view = view
        view.connect("destroy", self._on_view_destroyed)

        self._history = History(os.path.join(GLib.get_user_config_dir(), 'gedit/commander/history'))
        self._history_prefix = None

        self._prompt_text = None
        self._accel_group = None

        self._wait_timeout = 0
        self._cancel_button = None
        self._info = None
        self._info_revealer = None

        self._suspended = None

        self._handlers = [
            [0, Gdk.KEY_Up, self._on_history_move, -1],
            [0, Gdk.KEY_Down, self._on_history_move, 1],
            [None, Gdk.KEY_Return, self._on_execute, None],
            [None, Gdk.KEY_KP_Enter, self._on_execute, None],
            [0, Gdk.KEY_Tab, self._on_complete, None],
            [0, Gdk.KEY_ISO_Left_Tab, self._on_complete, None]
        ]

        self._re_complete = re.compile('("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

        self.connect('destroy', self._on_destroy)

        self._build_ui()
        self._setup_keybindings()

        self._attach()

    def view(self):
        return self._view

    def _setup_keybindings(self):
        css = Gtk.CssProvider()
        css.load_from_data(bytes("""
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    background-image: none;
    box-shadow: 0 0;
    transition: none;
    border: 0;
}

""", 'utf-8'))

        self._entry.get_style_context().add_provider(css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    def _find_overlay(self, view):
        parent = view.get_parent()

        while not isinstance(parent, Gtk.Overlay):
            parent = parent.get_parent()

        return parent

    def _build_ui(self):
        self.set_orientation(Gtk.Orientation.VERTICAL)

        self._overlay = self._find_overlay(self._view)

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        hbox.show()

        self.pack_end(hbox, False, False, 0)

        self._info_revealer = Gtk.Revealer()

        self._info_revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_UP)
        self._info_revealer.set_transition_duration(150)

        self.pack_start(self._info_revealer, False, False, 0)
        self._info_revealer.connect('notify::child-revealed', self._on_info_revealer_child_revealed)

        self._prompt_label = Gtk.Label(label='<b>&gt;&gt;&gt;</b>', use_markup=True)
        self._prompt_label.set_margin_top(3)
        self._prompt_label.set_margin_bottom(3)
        self._prompt_label.set_margin_start(3)

        self._prompt_label.show()
        hbox.add(self._prompt_label)

        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name('gedit-commander-entry')
        self._entry.set_hexpand(True)
        self._entry.set_margin_top(3)
        self._entry.set_margin_bottom(3)
        self._entry.set_margin_end(3)

        self._entry.connect('key-press-event', self._on_entry_key_press)

        self._entry.show()
        hbox.add(self._entry)

        self._copy_style_from_view()
        self._view_style_updated_id = self._view.connect('style-updated', self._on_view_style_updated)

    def _on_view_destroyed (self, widget, user_data=None):
        self._view.disconnect(self._view_style_updated_id)
        self._view_style_updated_id = None

    def _on_view_style_updated(self, widget):
        self._copy_style_from_view()

    @property
    def _border_color(self):
        style = self._view.get_buffer().get_style_scheme().get_style('right-margin')

        if not style is None and style.props.foreground_set:
            color = Gdk.RGBA()
            color.parse(style.props.foreground)
        else:
            color = self._get_background_color(Gtk.StateFlags.NORMAL, 'bottom').copy()
            color.red = 1 - color.red
            color.green = 1 - color.green
            color.blue = 1 - color.blue

        color.alpha = 0.3
        return color

    def _get_background_color(self, state, cls=None):
        context = self._view.get_style_context()

        context.save()

        if not cls is None:
            context.add_class(cls)

        ret = context.get_background_color(state)

        context.restore()

        return ret

    def _get_foreground_color(self, state, cls=None):
        context = self._view.get_style_context()

        context.save()

        if not cls is None:
            context.add_class(cls)

        ret = context.get_color(state)

        context.restore()

        return ret

    def _get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def _styled_widgets(self):
        widgets = [self, self._entry, self._prompt_label]

        if not self._info is None:
            widgets.append(self._info)
            widgets.append(self._info.text_view)

        return widgets

    def _modify_bg(self, col, widget):
        if self._info is None or (self._info.text_view != widget and self._info != widget):
            return col

        d = 0.1

        h, l, s = colorsys.rgb_to_hls(col.red, col.green, col.blue)

        if l < 0.5:
            factor = 1 + d
        else:
            factor = 1 - d

        l = max(0, min(1, l * factor))
        s = max(0, min(1, s * factor))

        r, g, b = colorsys.hls_to_rgb(h, l, s)

        return Gdk.RGBA(r, g, b, col.alpha)

    def _copy_style_from_view(self, widgets=None):
        font = self._get_font()
        fg = self._get_foreground_color(Gtk.StateFlags.NORMAL, 'bottom')
        bg = self._get_background_color(Gtk.StateFlags.NORMAL, 'bottom')

        fgsel = self._get_foreground_color(Gtk.StateFlags.SELECTED)
        bgsel = self._get_background_color(Gtk.StateFlags.SELECTED)

        cursor = self._view.style_get_property('cursor-color')

        if not cursor is None:
            cursor = Gdk.RGBA.from_color(cursor)

        secondary_cursor = self._view.style_get_property('secondary-cursor-color')

        if not secondary_cursor is None:
            secondary_cursor = Gdk.RGBA.from_color(secondary_cursor)

        if widgets is None:
            widgets = self._styled_widgets()

        for widget in widgets:
            widget.override_color(Gtk.StateFlags.NORMAL, fg)
            widget.override_background_color(Gtk.StateFlags.NORMAL, self._modify_bg(bg, widget))

            widget.override_color(Gtk.StateFlags.SELECTED, fgsel)
            widget.override_background_color(Gtk.StateFlags.SELECTED, self._modify_bg(bgsel, widget))

            widget.override_font(font)
            widget.override_cursor(cursor, secondary_cursor)

    def _attach(self):
        reveal = Gtk.Revealer()
        reveal.add(self)
        self.show()

        reveal.set_transition_type(Gtk.RevealerTransitionType.SLIDE_UP)
        reveal.set_transition_duration(200)

        reveal.set_valign(Gtk.Align.END)
        reveal.set_halign(Gtk.Align.FILL)

        self._overlay.add_overlay(reveal)
        reveal.show()

        reveal.set_reveal_child(True)
        self._reveal = reveal

        self._entry.grab_focus()

    def grab_focus(self):
        self._entry.grab_focus()

    def _on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if not self._info is None:
                if not self._suspended is None:
                    self._suspended.resume()

                if not self._info is None:
                    self._info_revealer.set_reveal_child(False)

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self._prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self._prompt()
            else:
                self._view.grab_focus()
                self._reveal.set_reveal_child(False)

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self._prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self._run_command(lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0] == state) and evnt.keyval == handler[1] and handler[2](handler[3], state):
                return True

        if not self._info is None and self._info.is_empty:
            self._info_revealer.set_reveal_child(False)

        self._history_prefix = None
        return False

    def _on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def _prompt(self, pr=''):
        self._prompt_text = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(self._accel_group.full_name()),)

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def _make_info(self):
        if self._info is None:
            self._info = Info()

            self._copy_style_from_view([self._info, self._info.text_view])

            self._info_revealer.add(self._info)
            self._info.show()

            self._info_revealer.show()
            self._info_revealer.set_reveal_child(True)

            self._info.connect('destroy', self._on_info_destroy)

    def _on_info_revealer_child_revealed(self, widget, pspec):
        if not self._info_revealer.get_child_revealed():
            self._info.destroy()
            self._info_revealer.hide()

    def _on_info_destroy(self, widget):
        self._info = None

    def info_show(self, text='', use_markup=False):
        self._make_info()
        self._info.add_lines(text, use_markup)

    def info_status(self, text):
        self._make_info()
        self._info.status(text)

    def _info_add_action(self, stock, callback, data=None):
        self._make_info()
        return self._info.add_action(stock, callback, data)

    def _command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def _on_wait_cancel(self):
        self._on_execute(None, 0)

    def _show_wait_cancel(self):
        self._cancel_button = self._info_add_action('process-stop-symbolic', self._on_wait_cancel)
        self.info_status('<i>Waiting to finish\u2026</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def _on_suspend_resume(self):
        if self._wait_timeout:
            GLib.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            if not self._cancel_button is None:
                self._cancel_button.destroy()
                self._cancel_button = None

            self.info_status(None)

        self._entry.set_sensitive(True)
        self._command_history_done()

        if self._entry.props.has_focus or (not self._info is None and not self._info.is_empty):
            self._entry.grab_focus()

    def _run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception as e:
            sys.stderr.write(self._format_trace() + '\n')

            self._command_history_done()
            self._command_state.clear()

            self._prompt()

            # Show error in info
            self.info_show('<b><span color="#f66">Error:</span></b> ' + saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self._format_trace(), False)

            return None

        mod = sys.modules['commander.commands.result']

        if ret == mod.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self._on_suspend_resume)

            self._wait_timeout = GLib.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self._command_history_done()
            self._prompt('')

            if ret == mod.Result.PROMPT:
                self._prompt(ret.prompt)
            elif (ret == None or ret == mod.HIDE) and not self._prompt_text and (self._info is None or self._info.is_empty):
                self._command_state.clear()
                self._view.grab_focus()
                self._reveal.set_reveal_child(False)
            else:
                self._entry.grab_focus()

        return ret

    def _format_trace(self):
        tp, val, tb = sys.exc_info()

        origtb = tb

        thisdir = os.path.dirname(__file__)

        # Skip frames up until after the last entry.py...
        while not tb is None:
            filename = tb.tb_frame.f_code.co_filename

            dname = os.path.dirname(filename)

            if not dname.startswith(thisdir):
                break

            tb = tb.tb_next

        msg = traceback.format_exception(tp, val, tb)
        r = ''.join(msg[0:-1])

        # This is done to prevent cyclic references, see python
        # documentation on sys.exc_info
        del origtb

        return r

    def _on_execute(self, dummy, modifier):
        if not self._info is None and not self._suspended:
            self._info_revealer.set_reveal_child(False)

        text = self._entry.get_text().strip()
        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        if not wordsstr and not self._command_state:
            self._entry.set_text('')
            return

        self._run_command(lambda: commands.Commands().execute(self._command_state, text, words, wordsstr, self, modifier))

        return True

    def _on_complete(self, dummy, modifier):
        # First split all the text in words
        text = self._entry.get_text()
        pos = self._entry.get_position()

        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        # Find out at which word the cursor actually is
        # Examples:
        #  * hello world|
        #  * hello| world
        #  * |hello world
        #  * hello wor|ld
        #  * hello  |  world
        #  * "hello world|"
        posidx = None

        for idx in range(0, len(words)):
            spec = self._complete_word_match(words[idx])

            if words[idx].start(0) > pos:
                # Empty space, new completion
                wordsstr.insert(idx, '')
                words.insert(idx, None)
                posidx = idx
                break
            elif spec[2] == pos:
                # At end of word, resume completion
                posidx = idx
                break
            elif spec[1] <= pos and spec[2] > pos:
                # In middle of word, do not complete
                return True

        if posidx == None:
            wordsstr.append('')
            words.append(None)
            posidx = len(wordsstr) - 1

        # First word completes a command, if not in any special 'mode'
        # otherwise, relay completion to the command, or complete by advice
        # from the 'mode' (prompt)
        cmds = commands.Commands()

        if not self._command_state and posidx == 0:
            # Complete the first command
            ret = commands.completion.command(words=wordsstr, idx=posidx)
        else:
            complete = None
            realidx = posidx

            if not self._command_state:
                # Get the command first
                cmd = commands.completion.single_command(wordsstr, 0)
                realidx -= 1

                ww = wordsstr[1:]
            else:
                cmd = self._command_state.top()
                ww = wordsstr

            if cmd:
                complete = cmd.autocomplete_func()

            if not complete:
                return True

            # 'complete' contains a dict with arg -> func to do the completion
            # of the named argument the command (or stack item) expects
            args, varargs = cmd.args()

            # Remove system arguments
            s = ['argstr', 'args', 'entry', 'view']
            args = list(filter(lambda x: not x in s, args))

            if realidx < len(args):
                arg = args[realidx]
            elif varargs:
                arg = '*'
            else:
                return True

            if not arg in complete:
                return True

            func = complete[arg]

            try:
                spec = utils.getargspec(func)

                if not ww:
                    ww = ['']

                kwargs = {
                    'words': ww,
                    'idx': realidx,
                    'view': self._view
                }

                if not spec.keywords:
                    for k in list(kwargs.keys()):
                        if not k in spec.args:
                            del kwargs[k]

                ret = func(**kwargs)
            except Exception as e:
                # Can be number of arguments, or return values or simply buggy
                # modules
                print(e)
                traceback.print_exc()
                return True

        if not ret or not ret[0]:
            return True

        res = ret[0]
        completed = ret[1]

        if len(ret) > 2:
            after = ret[2]
        else:
            after = ' '

        # Replace the word
        if words[posidx] == None:
            # At end of everything, just append
            spec = None

            self._entry.insert_text(completed, self._entry.get_text_length())
            self._entry.set_position(-1)
        else:
            spec = self._complete_word_match(words[posidx])

            self._entry.delete_text(spec[1], spec[2])
            self._entry.insert_text(completed, spec[1])
            self._entry.set_position(spec[1] + len(completed))

        if len(res) == 1:
            # Full completion
            lastpos = self._entry.get_position()

            if not isinstance(res[0], commands.module.Module) or not res[0].commands():
                if words[posidx] and after == ' ' and (words[posidx].group(2) != None or words[posidx].group(3) != None):
                    lastpos = lastpos + 1

                self._entry.insert_text(after, lastpos)
                self._entry.set_position(lastpos + 1)
            elif completed == wordsstr[posidx] or not res[0].method:
                self._entry.insert_text('.', lastpos)
                self._entry.set_position(lastpos + 1)

            if not self._info is None:
                self._info_revealer.set_reveal_child(False)
        else:
            # Show popup with completed items
            if not self._info is None:
                self._info.clear()

            ret = []

            for x in res:
                if isinstance(x, commands.method.Method):
                    ret.append('<b>' + saxutils.escape(x.name) + '</b> (<i>' + x.oneline_doc() + '</i>)')
                else:
                    ret.append(str(x))

            self.info_show("\n".join(ret), True)

        return True

    def do_draw(self, ctx):
        ret = Gtk.Box.do_draw(self, ctx)

        col = self._border_color

        ctx.set_line_width(1)
        ctx.set_source_rgba(col.red, col.green, col.blue, col.alpha)

        w = self.get_allocated_width()

        ctx.move_to(0, 0.5)
        ctx.line_to(w, 0.5)
        ctx.stroke()

        if not self._info is None:
            alloc = self._info_revealer.get_allocation()
            y = alloc.y + alloc.height + 0.5

            if y >= 3:
                ctx.move_to(0, y)
                ctx.line_to(w, y)
                ctx.stroke()

        return ret

    def _on_destroy(self, widget):
        # Note we do this not as an override because somehow something
        # goes wrong when finalizing in that case, maybe self is NULL
        # or something like that, and then gets some new empty instance?
        if self._view_style_updated_id:
            self._view.disconnect(self._view_style_updated_id)

        self._history.save()

        self._view = None
        self._view_style_updated_id = None
コード例 #43
0
ファイル: bot.py プロジェクト: abatilo/cscq_bot
 all_history = history.get_history()
 
 log.write('Looping through recent comments...')
 for comment in comments:
   id = comment.id
   log.write('Handling comment ' + str(id) + '...')
   if id not in all_history:
     log.write('Parsing new comment ' + str(id) + '...')
     body = comment.body
     links = LinkExtractor.extract_links(body)
     if len(links) > 0:
       for link in links:
         log.write('Storing new link ' + str(link) + '...')
         link_file.add(str(link))
       log.write('Adding ' + str(link) + ' to history...')
       history.add(id)
     else:
       log.write('No links found in ' + str(id) + '...')
   else:
     log.write('Comment seen before...skipping...')
 
 log.write('Looping through 10 hottest posts...')
 for submission in submissions:
   id = submission.id
   log.write('Handling post ' + str(id) + '...')
   if id not in all_history:
     log.write('Parsing new post ' + str(id) + '...')
     body = submission.selftext
     links = LinkExtractor.extract_links(body)
     if len(links) > 0:
       for link in links:
コード例 #44
0
ファイル: entry.py プロジェクト: alexkv/gedit-commander
class Entry(gtk.EventBox):
	def __init__(self, view):
		gtk.EventBox.__init__(self)
		self._view = view
		
		hbox = gtk.HBox(False, 3)
		hbox.show()
		hbox.set_border_width(3)
		
		self._entry = gtk.Entry()
		self._entry.modify_font(self._view.style.font_desc)
		self._entry.set_has_frame(False)
		self._entry.set_name('command-bar')
		self._entry.modify_text(gtk.STATE_NORMAL, self._view.style.text[gtk.STATE_NORMAL])
		self._entry.set_app_paintable(True)
		
		self._entry.connect('realize', self.on_realize)
		self._entry.connect('expose-event', self.on_entry_expose)

		self._entry.show()

		self._prompt_label = gtk.Label('<b>&gt;&gt;&gt;</b>')
		self._prompt_label.set_use_markup(True)
		self._prompt_label.modify_font(self._view.style.font_desc)
		self._prompt_label.show()
		self._prompt_label.modify_fg(gtk.STATE_NORMAL, self._view.style.text[gtk.STATE_NORMAL])
		
		self.modify_bg(gtk.STATE_NORMAL, self.background_gdk())
		self._entry.modify_base(gtk.STATE_NORMAL, self.background_gdk())
		
		self._entry.connect('focus-out-event', self.on_entry_focus_out)
		self._entry.connect('key-press-event', self.on_entry_key_press)
		
		self.connect_after('size-allocate', self.on_size_allocate)
		self.connect_after('expose-event', self.on_expose)
		self.connect_after('realize', self.on_realize)
		
		self._history = History(os.path.expanduser('~/.gnome2/gedit/commander/history'))
		self._prompt = None
		
		hbox.pack_start(self._prompt_label, False, False, 0)
		hbox.pack_start(self._entry, True, True, 0)
		
		self.add(hbox)
		self.attach()

		self._entry.grab_focus()
		self._wait_timeout = 0
		self._info_window = None
		
		self.connect('destroy', self.on_destroy)
		
		self._history_prefix = None		
		self._suspended = None
		self._handlers = [
			[0, gtk.keysyms.Up, self.on_history_move, -1],
			[0, gtk.keysyms.Down, self.on_history_move, 1],
			[None, gtk.keysyms.Return, self.on_execute, None],
			[None, gtk.keysyms.KP_Enter, self.on_execute, None],
			[0, gtk.keysyms.Tab, self.on_complete, None],
			[0, gtk.keysyms.ISO_Left_Tab, self.on_complete, None]
		]
		
		self._re_complete = re.compile('("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
		self._command_state = commands.Commands.State()
	
	def view(self):
		return self._view

	def on_realize(self, widget):
		widget.window.set_back_pixmap(None, False)
	
	def on_entry_expose(self, widget, evnt):
		ct = evnt.window.cairo_create()
		ct.rectangle(evnt.area.x, evnt.area.y, evnt.area.width, evnt.area.height)
		
		bg = self.background_color()
		ct.set_source_rgb(bg[0], bg[1], bg[1])
		ct.fill()
		return False

	def on_expose(self, widget, evnt):
		ct = evnt.window.cairo_create()
		color = self.background_color()
		
		ct.rectangle(evnt.area.x, evnt.area.y, evnt.area.width, evnt.area.height)
		ct.clip()
		
		# Draw separator line
		ct.move_to(0, 0)
		ct.set_line_width(1)
		ct.line_to(self.allocation.width, 0)

		ct.set_source_rgb(1 - color[0], 1 - color[1], 1 - color[2])
		ct.stroke()
		return False
		
	def on_size_allocate(self, widget, alloc):
		vwwnd = self._view.get_window(gtk.TEXT_WINDOW_BOTTOM).get_parent()
		size = vwwnd.get_size()
		position = vwwnd.get_position()

		self._view.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, alloc.height)
	
	def attach(self):
		# Attach ourselves in the text view, and position just above the 
		# text window
		self._view.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, 1)
		alloc = self._view.allocation
		
		self.show()
		self._view.add_child_in_window(self, gtk.TEXT_WINDOW_BOTTOM, 0, 0)
		self.set_size_request(alloc.width, -1)

	def background_gdk(self):
		bg = self.background_color()
		
		bg = map(lambda x: int(x * 65535), bg)
		return gtk.gdk.Color(bg[0], bg[1], bg[2])
		
	def background_color(self):
		bg = self._view.get_style().base[self._view.state]
		
		return [bg.red / 65535.0 * 1.1, bg.green / 65535.0 * 1.1, bg.blue / 65535.0 * 0.9, 0.8]

	def on_entry_focus_out(self, widget, evnt):
		if self._entry.flags() & gtk.SENSITIVE:
			self.destroy()
	
	def on_entry_key_press(self, widget, evnt):
		state = evnt.state & gtk.accelerator_get_default_mod_mask()
		text = self._entry.get_text()
		
		if evnt.keyval == gtk.keysyms.Escape and self._info_window:
			if self._suspended:
				self._suspended.resume()

			if self._info_window:
				self._info_window.destroy()

			self._entry.set_sensitive(True)
			return True

		if evnt.keyval == gtk.keysyms.Escape:
			if text:
				self._entry.set_text('')
			elif self._command_state:
				self._command_state.clear()
				self.prompt()
			else:
				self._view.grab_focus()
				self.destroy()

			return True

		for handler in self._handlers:
			if (handler[0] == None or handler[0] == state) and evnt.keyval == handler[1] and handler[2](handler[3], state):
				return True

		if self._info_window and self._info_window.empty():
			self._info_window.destroy()
		
		self._history_prefix = None
		return False
	
	def on_history_move(self, direction, modifier):
		pos = self._entry.get_position()
		
		self._history.update(self._entry.get_text())
		
		if self._history_prefix == None:
			if len(self._entry.get_text()) == pos:
				self._history_prefix = self._entry.get_chars(0, pos)
			else:
				self._history_prefix = ''
		
		if self._history_prefix == None:
			hist = ''
		else:
			hist = self._history_prefix
			
		next = self._history.move(direction, hist)
		
		if next != None:
			self._entry.set_text(next)
			self._entry.set_position(-1)
		
		return True
	
	def prompt(self, pr=''):
		self._prompt = pr

		if not pr:
			pr = ''
		else:
			pr = ' ' + pr
		
		self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)
	
	def make_info(self):
		if self._info_window == None:
			self._info_window = Info(self)
			self._info_window.show()
			
			self._info_window.connect('destroy', self.on_info_window_destroy)
	
	def on_info_window_destroy(self, widget):
		self._info_window = None
	
	def info_show(self, text='', use_markup=False):
		self.make_info()
		self._info_window.add_lines(text, use_markup)
	
	def info_status(self, text):
		self.make_info()
		self._info_window.status(text)
	
	def info_add_action(self, stock, callback, data=None):
		self.make_info()
		return self._info_window.add_action(stock, callback, data)

	def command_history_done(self):
		self._history.update(self._entry.get_text())
		self._history.add()
		self._history_prefix = None
		self._entry.set_text('')
	
	def on_wait_cancel(self):
		if self._suspended:
			self._suspended.resume()
		
		if self._cancel_button:
			self._cancel_button.destroy()

		if self._info_window and self._info_window.empty():
			self._info_window.destroy()
			self._entry.grab_focus()
			self._entry.set_sensitive(True)			
	
	def _show_wait_cancel(self):
		self._cancel_button = self.info_add_action(gtk.STOCK_STOP, self.on_wait_cancel)
		self.info_status('<i>Waiting to finish...</i>')
		
		self._wait_timeout = 0
		return False

	def _complete_word_match(self, match):
		for i in (3, 2, 0):
			if match.group(i) != None:
				return [match.group(i), match.start(i), match.end(i)]

	def on_suspend_resume(self):
		if self._wait_timeout:
			glib.source_remove(self._wait_timeout)
			self._wait_timeout = 0
		else:
			self._cancel_button.destroy()
			self._cancel_button = None
			self.info_status(None)
		
		self._entry.set_sensitive(True)
		self.command_history_done()

		if self._entry.props.has_focus or (self._info_window and not self._info_window.empty()):
			self._entry.grab_focus()

		self.on_execute(None, 0)

	def ellipsize(self, s, size):
		if len(s) <= size:
			return s
		
		mid = (size - 4) / 2
		return s[:mid] + '...' + s[-mid:]

	def destroy(self):
		self.hide()
		gtk.EventBox.destroy(self)

	def on_execute(self, dummy, modifier):
		if self._info_window and not self._suspended:
			self._info_window.destroy()

		text = self._entry.get_text().strip()
		words = list(self._re_complete.finditer(text))
		wordsstr = []
		
		for word in words:
			spec = self._complete_word_match(word)
			wordsstr.append(spec[0])

		if not wordsstr and not self._command_state:
			self._entry.set_text('')
			return
		
		self._suspended = None

		try:
			ret = commands.Commands().execute(self._command_state, text, words, wordsstr, self, modifier)
		except Exception, e:
			self.command_history_done()
			self._command_state.clear()
			
			self.prompt()
			
			# Show error in info
			self.info_show('<b><span color="#f66">Error:</span></b> ' + saxutils.escape(str(e)), True)

			if not isinstance(e, commands.exceptions.Execute):
				self.info_show(traceback.format_exc(), False)

			return True

		if ret == commands.result.Result.SUSPEND:
			# Wait for it...
			self._suspended = ret
			ret.register(self.on_suspend_resume)

			self._wait_timeout = glib.timeout_add(500, self._show_wait_cancel)
			self._entry.set_sensitive(False)
		else:
			self.command_history_done()
			self.prompt('')
			
			if ret == commands.result.Result.PROMPT:
				self.prompt(ret.prompt)
			elif (ret == None or ret == commands.result.HIDE) and not self._prompt and (not self._info_window or self._info_window.empty()):
				self._command_state.clear()
				self._view.grab_focus()
				self.destroy()
			else:
				self._entry.grab_focus()

		return True
コード例 #45
0
ファイル: entry.py プロジェクト: onia/pygi
class Entry(Gtk.EventBox):
    __gtype_name__ = "CommanderEntry"

    def __init__(self, view):
        Gtk.EventBox.__init__(self)
        self._view = view

        self.set_visible_window(False)

        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
        hbox.show()
        hbox.set_border_width(3)

        # context for the view
        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name('gedit-commander-entry')
        self._entry.show()

        css = Gtk.CssProvider()
        css.load_from_data("""
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    /* Override background to anything. This is weird, but doing this we can
       then in code use widget.override_background to set the color dynamically
       to the same color as the gedit view */
    background: transparent;
    border-width: 0;
    box-shadow: 0 0 transparent;
}
""")

        # FIXME: remove hardcopy of 600 (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
        # https://bugzilla.gnome.org/show_bug.cgi?id=646860
        self._entry.get_style_context().add_provider(css, 600)

        self._prompt_label = Gtk.Label(label='<b>&gt;&gt;&gt;</b>', use_markup=True)
        self._prompt_label.show()

        self._entry.connect('focus-out-event', self.on_entry_focus_out)
        self._entry.connect('key-press-event', self.on_entry_key_press)

        self._history = History(os.path.join(GLib.get_user_config_dir(), 'gedit/commander/history'))
        self._prompt = None

        self._accel_group = None

        hbox.pack_start(self._prompt_label, False, False, 0)
        hbox.pack_start(self._entry, True, True, 0)

        self.copy_style_from_view()
        self.view_style_updated_id = self._view.connect('style-updated', self.on_view_style_updated)

        self.add(hbox)
        self.attach()
        self._entry.grab_focus()

        self._wait_timeout = 0
        self._info_window = None

        self.connect('destroy', self.on_destroy)
        self.connect_after('size-allocate', self.on_size_allocate)
        self.view_draw_id = self._view.connect_after('draw', self.on_draw)

        self._history_prefix = None
        self._suspended = None
        self._handlers = [
            [0, Gdk.KEY_Up, self.on_history_move, -1],
            [0, Gdk.KEY_Down, self.on_history_move, 1],
            [None, Gdk.KEY_Return, self.on_execute, None],
            [None, Gdk.KEY_KP_Enter, self.on_execute, None],
            [0, Gdk.KEY_Tab, self.on_complete, None],
            [0, Gdk.KEY_ISO_Left_Tab, self.on_complete, None]
        ]

        self._re_complete = re.compile('("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

    def on_view_style_updated(self, widget):
        self.copy_style_from_view()

    def get_border_color(self):
        color = self.get_background_color().copy()
        color.red = 1 - color.red
        color.green = 1 - color.green
        color.blue = 1 - color.blue
        color.alpha = 0.5

        return color

    def get_background_color(self):
        context = self._view.get_style_context()
        return context.get_background_color(Gtk.StateFlags.NORMAL)
    
    def get_foreground_color(self):
        context = self._view.get_style_context()
        return context.get_color(Gtk.StateFlags.NORMAL)

    def get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def copy_style_from_view(self, widget=None):
        if widget != None:
            context = self._view.get_style_context()
            font = context.get_font(Gtk.StateFlags.NORMAL)

            widget.override_color(Gtk.StateFlags.NORMAL, self.get_foreground_color())
            widget.override_font(self.get_font())
        else:
            if self._entry:
                self.copy_style_from_view(self._entry)

            if self._prompt_label:
                self.copy_style_from_view(self._prompt_label)

    def view(self):
        return self._view

    def on_size_allocate(self, widget, alloc):
        alloc = self.get_allocation()
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, alloc.height)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)
        self.set_size_request(win.get_width(), -1)

        # NOTE: we need to do this explicitly somehow, otherwise the window
        # size will not be updated unless something else happens, not exactly
        # sure what. This might be caused by the multi notebook, or custom
        # animation layouting?
        self._view.get_parent().resize_children()

    def attach(self):
        # Attach ourselves in the text view, and position just above the
        # text window
        win = self._view.get_window(Gtk.TextWindowType.TEXT)
        alloc = self.get_allocation()

        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, max(alloc.height, 1))
        self._view.add_child_in_window(self, Gtk.TextWindowType.BOTTOM, 0, 0)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)

        self.show()
        self.set_size_request(win.get_width(), -1)

    def on_entry_focus_out(self, widget, evnt):
        if self._entry.get_sensitive():
            self.destroy()

    def on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if self._info_window:
                if self._suspended:
                    self._suspended.resume()

                if self._info_window:
                    self._info_window.destroy()

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self.prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self.prompt()
            else:
                self._view.grab_focus()
                self.destroy()

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self.prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self.run_command(lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0] == state) and evnt.keyval == handler[1] and handler[2](handler[3], state):
                return True

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()

        self._history_prefix = None
        return False

    def on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def prompt(self, pr=''):
        self._prompt = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(self._accel_group.full_name()),)

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def make_info(self):
        if self._info_window == None:
            self._info_window = Info(self)
            self._info_window.show()

            self._info_window.connect('destroy', self.on_info_window_destroy)

    def on_info_window_destroy(self, widget):
        self._info_window = None

    def info_show(self, text='', use_markup=False):
        self.make_info()
        self._info_window.add_lines(text, use_markup)

    def info_status(self, text):
        self.make_info()
        self._info_window.status(text)

    def info_add_action(self, stock, callback, data=None):
        self.make_info()
        return self._info_window.add_action(stock, callback, data)

    def command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def on_wait_cancel(self):
        if self._suspended:
            self._suspended.resume()

        if self._cancel_button:
            self._cancel_button.destroy()

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()
            self._entry.grab_focus()
            self._entry.set_sensitive(True)

    def _show_wait_cancel(self):
        self._cancel_button = self.info_add_action(Gtk.STOCK_STOP, self.on_wait_cancel)
        self.info_status('<i>Waiting to finish...</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def on_suspend_resume(self):
        if self._wait_timeout:
            GObject.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            self._cancel_button.destroy()
            self._cancel_button = None
            self.info_status(None)

        self._entry.set_sensitive(True)
        self.command_history_done()

        if self._entry.props.has_focus or (self._info_window and not self._info_window.empty()):
            self._entry.grab_focus()

        self.on_execute(None, 0)

    def ellipsize(self, s, size):
        if len(s) <= size:
            return s

        mid = (size - 4) / 2
        return s[:mid] + '...' + s[-mid:]

    def destroy(self):
        self.hide()
        Gtk.EventBox.destroy(self)

    def run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception, e:
            self.command_history_done()
            self._command_state.clear()

            self.prompt()

            # Show error in info
            self.info_show('<b><span color="#f66">Error:</span></b> ' + saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self.format_trace(), False)

            return None

        if ret == commands.result.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self.on_suspend_resume)

            self._wait_timeout = GObject.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self.command_history_done()
            self.prompt('')

            if ret == commands.result.Result.PROMPT:
                self.prompt(ret.prompt)
            elif (ret == None or ret == commands.result.HIDE) and not self._prompt and (not self._info_window or self._info_window.empty()):
                self._command_state.clear()
                self._view.grab_focus()
                self.destroy()
            else:
                self._entry.grab_focus()

        return ret
コード例 #46
0
class DQN:
    def __init__(self, config):

        self.dic  =  pickle.load(open("embedTeacher"+str(config.game_num)+".p","rb"))
        #init replay memory
        conf = tf.ConfigProto()
        conf.gpu_options.allow_growth=True
        self.session = tf.Session(config=conf)

        self.config = config

        self.memory = self.load_replay_memory(config)
        self.history = History()
        #init parameters
        self.timeStep = 0
        self.epsilon = config.INITIAL_EPSILON

        # self.stateInput = tf.placeholder(tf.int32, [None, self.config.seq_length,self.config.embed_dim])
        # self.stateInputT = tf.placeholder(tf.int32, [None, self.config.seq_length,self.config.embed_dim])

        self.stateInput = tf.placeholder(tf.float32, [None, self.config.seq_length,self.config.embed_dim])
        self.stateInputT = tf.placeholder(tf.float32, [None, self.config.seq_length,self.config.embed_dim])

        # self.stateInput = tf.placeholder(tf.int32, [self.config.seq_length, self.config.BATCH_SIZE, self.config.embed_dim])
        # self.stateInputT = tf.placeholder(tf.int32, [self.config.seq_length, self.config.BATCH_SIZE, self.config.embed_dim])

        self.word_embeds = self.stateInput
        self.word_embedsT = self.stateInputT
        # print '$'*100
        self.initializer = tf.truncated_normal_initializer(stddev = 0.02)
        # self.initializer = tf.random_uniform_initializer(minval=-1.0, maxval=1.0, seed=None, dtype=tf.float32)
        # self.initializer = tf.contrib.layers.xavier_initializer()
        # print '$'*100
        self.cell = tf.nn.rnn_cell.LSTMCell(self.config.rnn_size, initializer = self.initializer, state_is_tuple=True)
        self.cellT = tf.nn.rnn_cell.LSTMCell(self.config.rnn_size, initializer = self.initializer, state_is_tuple=True)
        # print '$'*100
        initial_state = self.cell.zero_state(self.config.BATCH_SIZE, tf.float32)
        initial_stateT = self.cellT.zero_state(self.config.BATCH_SIZE, tf.float32)
        # print '$'*100
        # early_stop = tf.constant(self.config.seq_length, dtype = tf.int32)
        # print '$'*100
        outputs, _ = tf.nn.rnn(self.cell, [tf.reshape(embed_t, [-1, self.config.embed_dim]) for embed_t in tf.split(1, self.config.seq_length, self.word_embeds)], dtype=tf.float32, initial_state = initial_state, scope = "LSTMN")
        outputsT, _ = tf.nn.rnn(self.cellT, [tf.reshape(embed_tT, [-1, self.config.embed_dim]) for embed_tT in tf.split(1, self.config.seq_length, self.word_embedsT)], dtype=tf.float32, initial_state = initial_stateT, scope = "LSTMT")
        # outputs, _ = tf.nn.rnn(self.cell, self.word_embeds, dtype=tf.float32, initial_state = initial_state, scope = "LSTMN")
        # outputsT, _ = tf.nn.rnn(self.cellT, self.word_embedsT, dtype=tf.float32, initial_state = initial_stateT, scope = "LSTMT")
        # print '$'*100
        self.output_embed = tf.transpose(tf.pack(outputs), [1, 0, 2])
        self.output_embedT = tf.transpose(tf.pack(outputsT), [1, 0, 2])
        # print '$'*100
        mean_pool = tf.reduce_mean(self.output_embed, 1)
        mean_poolT = tf.reduce_mean(self.output_embedT, 1)
        # print '$'*100
        linear_output = tf.nn.relu(tf.nn.rnn_cell._linear(mean_pool, int(self.output_embed.get_shape()[2]), 1,0.01, scope="linearN"))
        linear_outputT = tf.nn.relu(tf.nn.rnn_cell._linear(mean_poolT, int(self.output_embedT.get_shape()[2]),1, 0.01, scope="linearT"))
        # print '$'*100

        self.action_value = tf.nn.rnn_cell._linear(linear_output, self.config.num_actions, 1,0.01, scope="actionN")
        self.action_valueT = tf.nn.rnn_cell._linear(linear_outputT, self.config.num_actions, 1,0.01, scope="actionT")

        self.object_value = tf.nn.rnn_cell._linear(linear_output, self.config.num_objects, 1,0.01, scope="objectN")
        self.object_valueT = tf.nn.rnn_cell._linear(linear_outputT, self.config.num_objects, 1,0.01, scope="objectT")

        self.target_action_value = tf.placeholder(tf.float32, [None])
        self.target_object_value = tf.placeholder(tf.float32, [None])

        self.action_indicator = tf.placeholder(tf.float32, [None, self.config.num_actions])
        self.object_indicator = tf.placeholder(tf.float32, [None, self.config.num_objects])

        self.pred_action_value = tf.reduce_sum(tf.mul(self.action_indicator, self.action_value), 1)
        self.pred_object_value = tf.reduce_sum(tf.mul(self.object_indicator, self.object_value), 1)

        self.target_qpred = tf.truediv(tf.add(self.target_action_value,self.target_object_value),2.0)

        # self.qpred = tf.truediv(tf.add(self.pred_action_value,self.pred_object_value),2.0)

        summary_list = []
        with tf.name_scope('delta'):
            # self.delta_a = self.target_action_value - self.pred_action_value
            # self.delta_o = self.target_object_value - self.pred_object_value
            self.delta_a = self.target_qpred - self.pred_action_value
            self.delta_o = self.target_qpred - self.pred_object_value
            self.variable_summaries(self.delta_a, 'delta_a',summary_list)
            self.variable_summaries(self.delta_o, 'delta_o',summary_list)
            # self.delta = self.target_qpred - self.qpred
            # self.variable_summaries(self.delta, 'delta',summary_list)

        if self.config.clipDelta:
                with tf.name_scope('clippeddelta'):
                    # self.delta = tf.clip_by_value(self.delta, self.config.minDelta, self.config.maxDelta, name='clipped_delta')

                    self.quadratic_part_a = tf.minimum(abs(self.delta_a), config.maxDelta)
                    self.linear_part_a = abs(self.delta_a) - self.quadratic_part_a


                    self.quadratic_part_o = tf.minimum(abs(self.delta_o), config.maxDelta)
                    self.linear_part_o = abs(self.delta_o) - self.quadratic_part_o

                    self.quadratic_part = tf.concat(0,[self.quadratic_part_a,self.quadratic_part_o])
                    self.linear_part = tf.concat(0,[self.linear_part_a,self.linear_part_o])

                    # self.quadratic_part = tf.minimum(abs(self.delta), config.maxDelta)
                    # self.linear_part = abs(self.delta) - self.quadratic_part

                    # self.variable_summaries(self.delta, 'clippeddelta',summary_list)

                    # self.variable_summaries(self.linear_part_a, 'linear_part_a',summary_list)
                    # self.variable_summaries(self.quadratic_part_a, 'quadratic_part_a',summary_list)

                    # self.variable_summaries(self.linear_part_o, 'linear_part_o',summary_list)
                    # self.variable_summaries(self.quadratic_part_o, 'quadratic_part_o',summary_list)

                    self.variable_summaries(self.linear_part, 'linear_part',summary_list)
                    self.variable_summaries(self.quadratic_part, 'quadratic_part',summary_list)





        with tf.name_scope('loss'):
            #self.loss = 0.5*tf.reduce_mean(tf.square(self.delta), name='loss')
            # self.loss_a = tf.reduce_mean(0.5*tf.square(self.quadratic_part_a) + config.clipDelta * self.linear_part_a, name='loss_a')
            # self.variable_summaries(self.loss_a, 'loss_a',summary_list)

            # self.loss_o = tf.reduce_mean(0.5*tf.square(self.quadratic_part_o) + config.clipDelta * self.linear_part_o, name='loss_o')
            # self.variable_summaries(self.loss_o, 'loss_o',summary_list)

            self.loss = tf.reduce_mean(0.5*tf.square(self.quadratic_part) + config.clipDelta * self.linear_part, name='loss')
            self.variable_summaries(self.loss, 'loss',summary_list)

        self.W = ["LSTMN", "linearN", "actionN", "objectN"]
        self.target_W = ["LSTMT", "linearT", "actionT", "objectT"]

        # for i in range(len(self.W)):
        #     vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.W[i])
        #     varsT = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.target_W[i])

        #     with tf.name_scope('activationsN'):
        #         summary_list.extend(map(lambda x:tf.histogram_summary('activations/'+str(x.name), x), vars))
        #     with tf.name_scope('activationsT'):
        #         summary_list.extend(map(lambda x:tf.histogram_summary('activations/'+str(x.name), x), varsT))

        self.summary_placeholders = {}
        self.summary_ops = {}
        if self.config.TUTORIAL_WORLD:
            scalar_summary_tags = ['average.q_a','average.q_o','average.q','average_reward','average_numrewards','number_of_episodes','quest1_average_reward_cnt', \
                    'quest2_average_reward_cnt','quest3_average_reward_cnt']
        else:
            scalar_summary_tags = ['average.q_a','average.q_o','average.q','average_reward','average_numrewards','number_of_episodes','quest1_average_reward_cnt']

        for tag in scalar_summary_tags:
            self.summary_placeholders[tag] = tf.placeholder('float32', None, name=tag.replace(' ', '_'))
            self.summary_ops[tag]  = tf.scalar_summary('evaluation_data/'+tag, self.summary_placeholders[tag])

        # Clipping gradients

        # self.optim_ = tf.train.RMSPropOptimizer(learning_rate = self.config.LEARNING_RATE)
        # tvars = tf.trainable_variables()
        # def ClipIfNotNone(grad,var):
        #     if grad is None:
        #         return (grad, var)
        #     return (tf.clip_by_norm(grad,10), var)
        # grads = [ClipIfNotNone(i,var) for i,var in self.optim_.compute_gradients(self.loss, tvars)]

        # self.optim = self.optim_.apply_gradients(grads)
        # self.optim = tf.train.RMSPropOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss_a + self.loss_o)
        # self.optim = tf.train.RMSPropOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss)
        # self.optim = tf.train.AdagradOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss)
        # self.optim_a = tf.train.AdagradOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss_a)
        # self.optim_o = tf.train.AdagradOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss_o)
        # self.optim1 = tf.train.AdamOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss_a)
        # self.optim2 = tf.train.AdamOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss_o)

        self.optim = tf.train.AdamOptimizer(learning_rate = self.config.LEARNING_RATE).minimize(self.loss)
        self.saver = tf.train.Saver()

        if not(self.config.LOAD_WEIGHTS and self.load_weights()):
            self.session.run(tf.initialize_all_variables())

        # self.merged = tf.merge_all_summaries()
        self.merged = tf.merge_summary(summary_list)
        self.train_writer = tf.train.SummaryWriter(self.config.summaries_dir + '/train/'+str(self.config.game_num),self.session.graph)

        self.copyTargetQNetworkOperation()



    def variable_summaries(self, var, name,list_summary):
        """Attach a lot of summaries to a Tensor."""
        with tf.name_scope('summaries'):
            mean = tf.reduce_mean(var)
            list_summary.append(tf.scalar_summary('training_data/mean/' + name, mean))
            # with tf.name_scope('stddev'):
            #     stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))
            # list_summary.append(tf.scalar_summary('training_data/sttdev/' + name, stddev))
            list_summary.append(tf.scalar_summary('training_data/max/' + name, tf.reduce_max(var)))
            list_summary.append(tf.scalar_summary('training_data/min/' + name, tf.reduce_min(var)))
            # list_summary.append(tf.histogram_summary(name, var))

    def inject_summary(self, tag_dict, step):
        summary_str_lists = self.session.run([self.summary_ops[tag] for tag in tag_dict.keys()], { \
        self.summary_placeholders[tag]: value for tag, value in tag_dict.items()})
        for summary_str in summary_str_lists:
            self.train_writer.add_summary(summary_str, self.timeStep)

    def copyTargetQNetworkOperation(self):
        for i in range(len(self.W)):
            vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.W[i])
            varsT = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.target_W[i])
            # for i in vars:
            # 	print i.name
            # print "Over ."
            # for i in varsT:
            # 	print i.name
            # print len(vars)
            # print len(varsT)
            copy_op = zip(varsT, vars)
            self.session.run(map(lambda (x,y): x.assign(y.eval(session = self.session)),copy_op))

            # with tf.name_scope('activations'):
            #     map(lambda x:tf.histogram_summary('activations/'+str(x.name), x), vars)
            # value1 = self.session.run(vars)
            # value2 = self.session.run(varsT)
            # print len(value1)
            # print len(value2)
            # val_op = zip(value1, value2)
            # for x, y in val_op:
            # 	res = x - y
            # 	print sum(res)





    def train(self):

        s_t, action, obj, reward, s_t_plus_1, terminal = self.memory.sample()
        state_batch = s_t
        action_batch = action
        obj_batch = obj
        reward_batch = reward
        nextState_batch = s_t_plus_1

        # Step 2: calculate y
        target_action_batch = []
        target_object_batch = []

        QValue_action_batch = self.action_valueT.eval(feed_dict={self.stateInputT:self.convert(nextState_batch)},session = self.session)
        QValue_object_batch = self.object_valueT.eval(feed_dict={self.stateInputT:self.convert(nextState_batch)},session = self.session)


        for i in range(0,self.config.BATCH_SIZE):
            if terminal[i]:
                target_action_batch.append(reward_batch[i])
                target_object_batch.append(reward_batch[i])
            else:
                target_action_batch.append(reward_batch[i] + self.config.GAMMA* np.max(QValue_action_batch[i]))
                target_object_batch.append(reward_batch[i] + self.config.GAMMA* np.max(QValue_object_batch[i]))

        if self.timeStep%self.config.EVAL == self.config.trainfreq:
            _ , summary = self.session.run([self.optim, self.merged],feed_dict={
                    self.target_action_value : target_action_batch,
                    self.target_object_value : target_object_batch,
                    self.action_indicator : action_batch,
                    self.object_indicator : obj_batch,
                    self.stateInput : self.convert(state_batch)
                    })
            self.train_writer.add_summary(summary, self.timeStep)

            # _ , summary = self.session.run([self.optim2, self.merged],feed_dict={
            #         self.target_action_value : target_action_batch,
            #         self.target_object_value : target_object_batch,
            #         self.action_indicator : action_batch,
            #         self.object_indicator : obj_batch,
            #         self.stateInput : state_batch
            #         })
            # self.train_writer.add_summary(summary, self.timeStep)
        else:
            _ = self.session.run([self.optim],feed_dict={
                    self.target_action_value : target_action_batch,
                    self.target_object_value : target_object_batch,
                    self.action_indicator : action_batch,
                    self.object_indicator : obj_batch,
                    self.stateInput : self.convert(state_batch)
                    })
            # _ = self.session.run([self.optim2],feed_dict={
            #         self.target_action_value : target_action_batch,
            #         self.target_object_value : target_object_batch,
            #         self.action_indicator : action_batch,
            #         self.object_indicator : obj_batch,
            #         self.stateInput : state_batch
            #         })


        if self.timeStep % self.config.UPDATE_FREQUENCY == 0:
            # print "Copying weights."
            self.copyTargetQNetworkOperation()

    def setPerception(self, state, reward, action_indicator, object_indicator, nextstate,terminal,evaluate = False): #nextObservation,action,reward,terminal):
        self.history.add(nextstate)
        if not evaluate:
            self.memory.add(state, action_indicator, object_indicator, reward, nextstate, terminal)
        if self.timeStep > self.config.REPLAY_START_SIZE and self.memory.count > self.config.REPLAY_START_SIZE:
            # Train the network
            if (not evaluate ) and (self.timeStep % self.config.trainfreq == 0):
            	# print "Started Training."
                self.train()
        if not evaluate:
            self.timeStep += 1


    def getAction(self, availableObjects, evaluate = False):
        action_index = 0
        object_index = 0
        curr_epsilon = self.epsilon
        if evaluate:
            curr_epsilon = self.config.testepsilon

        if random.random() <= curr_epsilon:
            action_index = random.randrange(self.config.num_actions)
            object_index = random.randrange(self.config.num_objects)
        else:
            state_batch = np.zeros([self.config.batch_size, self.config.seq_length])
            state_batch[0] = self.history.get()
            QValue_action = self.action_value.eval(feed_dict={self.stateInput:self.convert(state_batch)},session = self.session)[0]
            bestAction = np.where(QValue_action == np.max(QValue_action))[0]
            QValue_object = self.object_value.eval(feed_dict={self.stateInput:self.convert(state_batch)},session = self.session)[0]
            for i in range(QValue_object.size):
                if i not in availableObjects:
                    QValue_object[i] = -sys.maxint - 1
            bestObject = np.where(QValue_object == np.max(QValue_object))[0]
            action_index = bestAction[random.randrange(0,bestAction.shape[0])]
            object_index = bestObject[random.randrange(0,bestObject.shape[0])]


        if not evaluate:
            self.epsilon = self.config.FINAL_EPSILON + max(0, (self.config.INITIAL_EPSILON - self.config.FINAL_EPSILON) * (self.config.EXPLORE - max(0, self.timeStep - self.config.REPLAY_START_SIZE))/self.config.EXPLORE)


        return action_index, object_index

    def getQValues(self, availableObjects, evaluate = False):

        state_batch = np.zeros([self.config.batch_size, self.config.seq_length])
        state_batch[0] = self.history.get()
        QValue_action = self.action_valueT.eval(feed_dict={self.stateInputT:self.convert(state_batch)},session = self.session)[0]
        QValue_object = self.object_valueT.eval(feed_dict={self.stateInputT:self.convert(state_batch)},session = self.session)[0]

        return QValue_action, QValue_object

    def load_weights(self):
        print 'inload weights'
        if not os.path.exists(os.getcwd()+'/Savednetworks'):
            return False

        list_dir = sorted(os.listdir(os.getcwd()+'/Savednetworks'))
        if not any(item.startswith('network-dqn') for item in list_dir):
            return False

        print 'weights loaded'
        self.saver.restore(self.session, os.getcwd()+'/Savednetworks/'+list_dir[-2])
        return True


    def load_replay_memory(self,config):
        if os.path.exists(config.model_dir+'/replay_file.save'):
            fp = open(config.model_dir+'/replay_file.save','rb')
            memory = pickle.load(fp)
            fp.close()
        else:
            memory = ReplayMemory(config)
        return memory

    def convert(self,state):
        output = np.zeros([len(state),self.config.seq_length,self.config.embed_dim])
        for i in range(len(state)):
            for j in range(self.config.seq_length):
                output[i,j] = self.dic[state[i][j]]
        return output
コード例 #47
0
class DQN:
    def __init__(self, config):

        #init replay memory
        self.config = config
        
        self.memory = self.load_replay_memory(config)
        self.history = History(config)
        #init parameters
        self.timeStep = 0
        self.epsilon = config.INITIAL_EPSILON
        self.actions = config.NUM_ACTIONS

        self.stateInput = tf.placeholder(tf.int32, [None, self.config.seq_length])
        self.stateInputT = tf.placeholder(tf.int32, [None, self.config.seq_length])


        embed = tf.get_variable("embed", [self.config.vocab_size, self.config.embed_dim])
        embedT = tf.get_variable("embed", [self.config.vocab_size, self.config.embed_dim])

        word_embeds = tf.nn.embedding_lookup(embed, self.stateInput) # @codewalk: What is this line doing ?
        word_embedsT = tf.nn.embedding_lookup(embedT, self.stateInputT) # @codewalk: What is this line doing ?

        self.initializer = tf.truncated_normal(shape, stddev = 0.02)

        self.cell = tf.nn.rnn_cell.LSTMCell(self.config.rnn_size, initializer = self.initializer)
        self.cellT = tf.nn.rnn_cell.LSTMCell(self.config.rnn_size, initializer = self.initializer)

        initial_state = self.cell.zero_state(None, tf.float32)
        initial_stateT = self.cellT.zero_state(None, tf.float32)

        early_stop = tf.constant(self.config.seq_length, dtype = tf.int32)

        outputs, _ = tf.nn.rnn(self.cell, [tf.reshape(embed_t, [-1, self.config.embed_dim]) for embed_t in tf.split(1, self.config.seq_length, word_embeds)], dtype=tf.float32, initial_state = initial_state, sequence_length = early_stop, scope = "LSTM")
        outputsT, _ = tf.nn.rnn(self.cellT, [tf.reshape(embed_tT, [-1, self.config.embed_dim]) for embed_tT in tf.split(1, self.config.seq_length, word_embedsT)], dtype=tf.float32, initial_state = initial_stateT, sequence_length = early_stop, scope = "LSTMT")

        output_embed = tf.transpose(tf.pack(outputs), [1, 0, 2])
        output_embedT = tf.transpose(tf.pack(outputsT), [1, 0, 2])

        mean_pool = tf.nn.relu(tf.reduce_mean(output_embed, 1))
        mean_poolT = tf.nn.relu(tf.reduce_mean(output_embedT, 1))

        linear_output = tf.nn.relu(tf.nn.rnn_cell._linear(mean_pool, int(output_embed.get_shape()[2]), 0.0, scope="linear"))
        linear_outputT = tf.nn.relu(tf.nn.rnn_cell._linear(mean_poolT, int(output_embedT.get_shape()[2]), 0.0, scope="linearT"))


        self.action_value = tf.nn.rnn_cell._linear(linear_output, self.config.num_action, 0.0, scope="action")
        self.action_valueT = tf.nn.rnn_cell._linear(linear_outputT, self.config.num_action, 0.0, scope="actionT")

        self.object_value = tf.nn.rnn_cell._linear(linear_output, self.config.num_object, 0.0, scope="object")
        self.object_valueT = tf.nn.rnn_cell._linear(linear_outputT, self.config.num_object, 0.0, scope="objectT")

        self.target_action_value = tf.placeholder(tf.float32, [None])
        self.target_object_value = tf.placeholder(tf.float32, [None])

        self.action_indicator = tf.placeholder(tf.float32, [None, self.config.num_action])
        self.object_indicator = tf.placeholder(tf.float32, [None, self.config.num_object])

        self.pred_action_value = tf.reduce_sum(tf.mul(self.action_indicator, self.action_value), 1)
        self.pred_object_value = tf.reduce_sum(tf.mul(self.object_indicator, self.object_value), 1)

        self.target_qpred = (self.target_action_value + self.target_object_value)/2
        self.qpred = (self.pred_action_value + self.pred_object_value)/2

        self.delta = self.target_qpred - self.qpred

        if self.config.clipDelta:
            self.delta = tf.clip_by_value(self.delta, self.config.minDelta, self.config.maxDelta, name='clipped_delta')

        self.loss = tf.reduce_mean(tf.square(self.delta), name='loss')

        self.W = ["LSTM", "linear", "action", "object"]
        self.target_W = ["LSTMT", "linearT", "actionT", "objectT"]


        # Clipping gradients

        self.optim_ = tf.train.RMSPropOptimizer(learning_rate = self.config.LEARNING_RATE, decay = 1, momentum = self.config.GRADIENT_MOMENTUM)
        tvars = tf.trainable_variables()
        def ClipIfNotNone(grad,var):
            if grad is None:
                return grad
            return tf.clip_by_norm(grad,20)
        grads = [ClipIfNotNone(i,var) for i,var in zip(tf.gradients(self.loss, tvars),tvars)]
        self.optim = self.optim_.apply_gradients(zip(grads, tvars))


        if not(self.config.LOAD_WEIGHTS and self.load_weights()):
            self.session.run(tf.initialize_all_variables())
            # self.copyTargetQNetworkOperation()


    def copyTargetQNetworkOperation(self):
        for i in range(len(self.W)):
            vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.W[i])
            varsT = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope = self.target_W[i])
            copy_op = zip(vars, varsT)
            self.session.run(map(lambda (x,y): x.assign(y.eval(session = self.session)),copy_op))


    def train(self):

        s_t, action, obj, reward, s_t_plus_1, terminal = self.memory.sample()
        state_batch = s_t
        action_batch = action
        obj_batch = obj
        reward_batch = reward
        nextState_batch = s_t_plus_1

        # Step 2: calculate y
        target_action_batch = []
        target_object_batch = []
        QValue_action_batch = self.action_valueT.eval(feed_dict={self.stateInputT:nextState_batch},session = self.session)
        QValue_object_batch = self.object_valueT.eval(feed_dict={self.stateInputT:nextState_batch},session = self.session)


        for i in range(0,self.config.BATCH_SIZE):
            if terminal[i]:
                target_action_batch.append(reward_batch[i])
                target_object_batch.append(reward_batch[i])
            else:
                target_action_batch.append(reward_batch[i] + self.config.GAMMA* np.max(QValue_action_batch[i]))
                target_object_batch.append(reward_batch[i] + self.config.GAMMA* np.max(QValue_object_batch[i]))

        self.optim.run(feed_dict={
                self.target_action_value : target_action_batch,
                self.target_object_value : target_object_batch,
                self.action_indicator : action_batch,
                self.object_indicator : obj_batch,
                self.stateInput : state_batch
                },session = self.session)

        # save network every 10000 iteration
        if self.timeStep % 10000 == 0:
            if not os.path.exists(os.getcwd()+'/Savednetworks'):
                os.makedirs(os.getcwd()+'/Savednetworks')
            self.saver.save(self.session, os.getcwd()+'/Savednetworks/'+'network' + '-dqn', global_step = self.timeStep)

        if self.timeStep % self.config.UPDATE_FREQUENCY == 0:
            self.copyTargetQNetworkOperation()

    def setPerception(self,state,action,reward,nextstate,terminal,evaluate = False): #nextObservation,action,reward,terminal):
        self.history.add(nextstate)
        if not evaluate:
            self.memory.add(state,reward,action,nextstate,terminal)
        if self.timeStep > self.config.REPLAY_START_SIZE and self.memory.count > self.config.REPLAY_START_SIZE:
            # Train the network
            if not evaluate and self.timeStep % self.config.trainfreq ==0:
                self.train()
        if not evaluate:
            self.timeStep += 1


    def getAction(self, evaluate = False):
        action_index = 0
        object_index = 0
        curr_epsilon = self.epsilon
        if evaluate:
            curr_epsilon = 0.05
            
        if random.random() <= curr_epsilon:
            action_index = random.randrange(self.actions)
            object_index = random.randrange(self.objects)
        else:
            QValue_action = self.action_value.eval(feed_dict={self.stateInput:[self.history.get()]},session = self.session)[0]
            Qvalue_object = self.object_value.eval(feed_dict={self.stateInput:[self.history.get()]},session = self.session)[0]
            action_index = np.argmax(QValue_action)
            object_index = np.argmax(QValue_object)


        if not evaluate:
            if self.epsilon > self.config.FINAL_EPSILON and self.timeStep > self.config.REPLAY_START_SIZE:
                self.epsilon -= (self.config.INITIAL_EPSILON - self.config.FINAL_EPSILON) / self.config.EXPLORE

        return action_index, object_index
    
    def load_weights(self):
        print 'inload weights'
        if not os.path.exists(os.getcwd()+'/Savednetworks'):
            return False    
        
        list_dir = sorted(os.listdir(os.getcwd()+'/Savednetworks'))
        if not any(item.startswith('network-dqn') for item in list_dir):
            return False
        
        print 'weights loaded'
        self.saver.restore(self.session, os.getcwd()+'/Savednetworks/'+list_dir[-2])        
        return True

    
    def load_replay_memory(self,config):
        if os.path.exists(config.model_dir+'/replay_file.save'):
            fp = open(config.model_dir+'/replay_file.save','rb')
            memory = pickle.load(fp)
            fp.close()
        else:
            memory = ReplayMemory(config)
        return memory
コード例 #48
0

from constants import *
from cube import Cube
from fitness import *
from history import History
from validate import is_even, is_solved


## For history.py


h = History()
moves = [1, 5, 1, 9, 9, 8, 8, 12, 14, 15, 17, 12, 2, 3, 6, 5, 7, 7, 9, 10]
for move in moves:
    h.add(move)
assert h.get() == ["R'", 'B2', 'F2', 'U', "L'", 'R', 'F', "R'", "B'"]


## For cube.py


c = Cube()
for move in moves:
    c.move(move)
assert c.get_cube() == [[1, 1, 5, 4, 3, 1, 4, 4], [3, 3, 0, 4, 2, 3, 4, 0],
                        [3, 5, 0, 2, 1, 2, 4, 2], [5, 5, 2, 3, 0, 3, 1, 0],
                        [4, 2, 2, 5, 5, 0, 1, 4], [0, 1, 3, 1, 5, 0, 2, 5]]


## For fitness.py
コード例 #49
0
class Entry(Gtk.EventBox):
    __gtype_name__ = "CommanderEntry"

    def __init__(self, view):
        Gtk.EventBox.__init__(self)
        self._view = view

        self.set_visible_window(False)

        hbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 3)
        hbox.show()
        hbox.set_border_width(3)

        # context for the view
        self._entry = Gtk.Entry()
        self._entry.set_has_frame(False)
        self._entry.set_name('gedit-commander-entry')
        self._entry.show()

        css = Gtk.CssProvider()
        css.load_from_data(
            bytes(
                """
@binding-set terminal-like-bindings {
    unbind "<Control>A";

    bind "<Control>W" { "delete-from-cursor" (word-ends, -1) };
    bind "<Control>A" { "move-cursor" (buffer-ends, -1, 0) };
    bind "<Control>U" { "delete-from-cursor" (display-line-ends, -1) };
    bind "<Control>K" { "delete-from-cursor" (display-line-ends, 1) };
    bind "<Control>E" { "move-cursor" (buffer-ends, 1, 0) };
    bind "Escape" { "delete-from-cursor" (display-lines, 1) };
}

GtkEntry#gedit-commander-entry {
    gtk-key-bindings: terminal-like-bindings;

    /* Override background to anything. This is weird, but doing this we can
       then in code use widget.override_background to set the color dynamically
       to the same color as the gedit view */
    background: transparent;
    border-width: 0;
    box-shadow: 0 0 transparent;
    transition: none;
}
""", 'utf-8'))

        # FIXME: remove hardcopy of 600 (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
        # https://bugzilla.gnome.org/show_bug.cgi?id=646860
        self._entry.get_style_context().add_provider(css, 600)

        self._prompt_label = Gtk.Label(label='<b>&gt;&gt;&gt;</b>',
                                       use_markup=True)
        self._prompt_label.show()

        self._entry.connect('focus-out-event', self.on_entry_focus_out)
        self._entry.connect('key-press-event', self.on_entry_key_press)

        self._history = History(
            os.path.join(GLib.get_user_config_dir(),
                         'gedit/commander/history'))
        self._prompt = None

        self._accel_group = None

        hbox.pack_start(self._prompt_label, False, False, 0)
        hbox.pack_start(self._entry, True, True, 0)

        self.copy_style_from_view()
        self.view_style_updated_id = self._view.connect(
            'style-updated', self.on_view_style_updated)

        self.add(hbox)
        self.attach()
        self._entry.grab_focus()

        self._wait_timeout = 0
        self._info_window = None

        self.connect('destroy', self.on_destroy)
        self.connect_after('size-allocate', self.on_size_allocate)
        self.view_draw_id = self._view.connect_after('draw', self.on_draw)

        self._history_prefix = None
        self._suspended = None
        self._handlers = [[0, Gdk.KEY_Up, self.on_history_move, -1],
                          [0, Gdk.KEY_Down, self.on_history_move, 1],
                          [None, Gdk.KEY_Return, self.on_execute, None],
                          [None, Gdk.KEY_KP_Enter, self.on_execute, None],
                          [0, Gdk.KEY_Tab, self.on_complete, None],
                          [0, Gdk.KEY_ISO_Left_Tab, self.on_complete, None]]

        self._re_complete = re.compile(
            '("((?:\\\\"|[^"])*)"?|\'((?:\\\\\'|[^\'])*)\'?|[^\s]+)')
        self._command_state = commands.Commands.State()

    def on_view_style_updated(self, widget):
        self.copy_style_from_view()

    def get_border_color(self):
        color = self.get_background_color().copy()
        color.red = 1 - color.red
        color.green = 1 - color.green
        color.blue = 1 - color.blue
        color.alpha = 0.5

        return color

    def get_background_color(self):
        context = self._view.get_style_context()
        return context.get_background_color(Gtk.StateFlags.NORMAL)

    def get_foreground_color(self):
        context = self._view.get_style_context()
        return context.get_color(Gtk.StateFlags.NORMAL)

    def get_font(self):
        context = self._view.get_style_context()
        return context.get_font(Gtk.StateFlags.NORMAL)

    def copy_style_from_view(self, widget=None):
        if widget != None:
            context = self._view.get_style_context()
            font = context.get_font(Gtk.StateFlags.NORMAL)

            widget.override_color(Gtk.StateFlags.NORMAL,
                                  self.get_foreground_color())
            widget.override_font(self.get_font())
        else:
            if self._entry:
                self.copy_style_from_view(self._entry)

            if self._prompt_label:
                self.copy_style_from_view(self._prompt_label)

    def view(self):
        return self._view

    def on_size_allocate(self, widget, alloc):
        alloc = self.get_allocation()
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM,
                                          alloc.height)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)
        self.set_size_request(win.get_width(), -1)

        # NOTE: we need to do this explicitly somehow, otherwise the window
        # size will not be updated unless something else happens, not exactly
        # sure what. This might be caused by the multi notebook, or custom
        # animation layouting?
        self._view.get_parent().resize_children()

    def attach(self):
        # Attach ourselves in the text view, and position just above the
        # text window
        win = self._view.get_window(Gtk.TextWindowType.TEXT)
        alloc = self.get_allocation()

        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM,
                                          max(alloc.height, 1))
        self._view.add_child_in_window(self, Gtk.TextWindowType.BOTTOM, 0, 0)

        win = self._view.get_window(Gtk.TextWindowType.BOTTOM)

        self.show()
        self.set_size_request(win.get_width(), -1)

    def on_entry_focus_out(self, widget, evnt):
        if self._entry.get_sensitive():
            self.destroy()

    def on_entry_key_press(self, widget, evnt):
        state = evnt.state & Gtk.accelerator_get_default_mod_mask()
        text = self._entry.get_text()

        if evnt.keyval == Gdk.KEY_Escape:
            if self._info_window:
                if self._suspended:
                    self._suspended.resume()

                if self._info_window:
                    self._info_window.destroy()

                self._entry.set_sensitive(True)
            elif self._accel_group:
                self._accel_group = self._accel_group.parent

                if not self._accel_group or not self._accel_group.parent:
                    self._entry.set_editable(True)
                    self._accel_group = None

                self.prompt()
            elif text:
                self._entry.set_text('')
            elif self._command_state:
                self._command_state.clear()
                self.prompt()
            else:
                self._view.grab_focus()
                self.destroy()

            return True

        if state or self._accel_group:
            # Check if it should be handled by the accel group
            group = self._accel_group

            if not self._accel_group:
                group = commands.Commands().accelerator_group()

            accel = group.activate(evnt.keyval, state)

            if isinstance(accel, commands.accel_group.AccelGroup):
                self._accel_group = accel
                self._entry.set_text('')
                self._entry.set_editable(False)
                self.prompt()

                return True
            elif isinstance(accel, commands.accel_group.AccelCallback):
                self._entry.set_editable(True)
                self.run_command(
                    lambda: accel.activate(self._command_state, self))
                return True

        if not self._entry.get_editable():
            return True

        for handler in self._handlers:
            if (handler[0] == None or handler[0]
                    == state) and evnt.keyval == handler[1] and handler[2](
                        handler[3], state):
                return True

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()

        self._history_prefix = None
        return False

    def on_history_move(self, direction, modifier):
        pos = self._entry.get_position()

        self._history.update(self._entry.get_text())

        if self._history_prefix == None:
            if len(self._entry.get_text()) == pos:
                self._history_prefix = self._entry.get_chars(0, pos)
            else:
                self._history_prefix = ''

        if self._history_prefix == None:
            hist = ''
        else:
            hist = self._history_prefix

        next = self._history.move(direction, hist)

        if next != None:
            self._entry.set_text(next)
            self._entry.set_position(-1)

        return True

    def prompt(self, pr=''):
        self._prompt = pr

        if self._accel_group != None:
            pr = '<i>%s</i>' % (saxutils.escape(
                self._accel_group.full_name()), )

        if not pr:
            pr = ''
        else:
            pr = ' ' + pr

        self._prompt_label.set_markup('<b>&gt;&gt;&gt;</b>%s' % pr)

    def make_info(self):
        if self._info_window == None:
            self._info_window = Info(self)
            self._info_window.show()

            self._info_window.connect('destroy', self.on_info_window_destroy)

    def on_info_window_destroy(self, widget):
        self._info_window = None

    def info_show(self, text='', use_markup=False):
        self.make_info()
        self._info_window.add_lines(text, use_markup)

    def info_status(self, text):
        self.make_info()
        self._info_window.status(text)

    def info_add_action(self, stock, callback, data=None):
        self.make_info()
        return self._info_window.add_action(stock, callback, data)

    def command_history_done(self):
        self._history.add(self._entry.get_text())
        self._history_prefix = None
        self._entry.set_text('')

    def on_wait_cancel(self):
        if self._suspended:
            self._suspended.resume()

        if self._cancel_button:
            self._cancel_button.destroy()

        if self._info_window and self._info_window.empty():
            self._info_window.destroy()
            self._entry.grab_focus()
            self._entry.set_sensitive(True)

    def _show_wait_cancel(self):
        self._cancel_button = self.info_add_action(Gtk.STOCK_STOP,
                                                   self.on_wait_cancel)
        self.info_status('<i>Waiting to finish...</i>')

        self._wait_timeout = 0
        return False

    def _complete_word_match(self, match):
        for i in (3, 2, 0):
            if match.group(i) != None:
                return [match.group(i), match.start(i), match.end(i)]

    def on_suspend_resume(self):
        if self._wait_timeout:
            GLib.source_remove(self._wait_timeout)
            self._wait_timeout = 0
        else:
            self._cancel_button.destroy()
            self._cancel_button = None
            self.info_status(None)

        self._entry.set_sensitive(True)
        self.command_history_done()

        if self._entry.props.has_focus or (self._info_window
                                           and not self._info_window.empty()):
            self._entry.grab_focus()

        self.on_execute(None, 0)

    def ellipsize(self, s, size):
        if len(s) <= size:
            return s

        mid = (size - 4) / 2
        return s[:mid] + '...' + s[-mid:]

    def destroy(self):
        self.hide()
        Gtk.EventBox.destroy(self)

    def run_command(self, cb):
        self._suspended = None

        try:
            ret = cb()
        except Exception as e:
            self.command_history_done()
            self._command_state.clear()

            self.prompt()

            # Show error in info
            self.info_show(
                '<b><span color="#f66">Error:</span></b> ' +
                saxutils.escape(str(e)), True)

            if not isinstance(e, commands.exceptions.Execute):
                self.info_show(self.format_trace(), False)

            return None

        mod = sys.modules['commander.commands.result']

        if ret == mod.Result.SUSPEND:
            # Wait for it...
            self._suspended = ret
            ret.register(self.on_suspend_resume)

            self._wait_timeout = GLib.timeout_add(500, self._show_wait_cancel)
            self._entry.set_sensitive(False)
        else:
            self.command_history_done()
            self.prompt('')

            if ret == mod.Result.PROMPT:
                self.prompt(ret.prompt)
            elif (ret == None or ret == mod.HIDE) and not self._prompt and (
                    not self._info_window or self._info_window.empty()):
                self._command_state.clear()
                self._view.grab_focus()
                self.destroy()
            else:
                self._entry.grab_focus()

        return ret

    def format_trace(self):
        tp, val, tb = sys.exc_info()

        origtb = tb

        thisdir = os.path.dirname(__file__)

        # Skip frames up until after the last entry.py...
        while True:
            filename = tb.tb_frame.f_code.co_filename

            dname = os.path.dirname(filename)

            if not dname.startswith(thisdir):
                break

            tb = tb.tb_next

        msg = traceback.format_exception(tp, val, tb)
        r = ''.join(msg[0:-1])

        # This is done to prevent cyclic references, see python
        # documentation on sys.exc_info
        del origtb

        return r

    def on_execute(self, dummy, modifier):
        if self._info_window and not self._suspended:
            self._info_window.destroy()

        text = self._entry.get_text().strip()
        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        if not wordsstr and not self._command_state:
            self._entry.set_text('')
            return

        self.run_command(lambda: commands.Commands().execute(
            self._command_state, text, words, wordsstr, self, modifier))

        return True

    def on_complete(self, dummy, modifier):
        # First split all the text in words
        text = self._entry.get_text()
        pos = self._entry.get_position()

        words = list(self._re_complete.finditer(text))
        wordsstr = []

        for word in words:
            spec = self._complete_word_match(word)
            wordsstr.append(spec[0])

        # Find out at which word the cursor actually is
        # Examples:
        #  * hello world|
        #  * hello| world
        #  * |hello world
        #  * hello wor|ld
        #  * hello  |  world
        #  * "hello world|"
        posidx = None

        for idx in range(0, len(words)):
            spec = self._complete_word_match(words[idx])

            if words[idx].start(0) > pos:
                # Empty space, new completion
                wordsstr.insert(idx, '')
                words.insert(idx, None)
                posidx = idx
                break
            elif spec[2] == pos:
                # At end of word, resume completion
                posidx = idx
                break
            elif spec[1] <= pos and spec[2] > pos:
                # In middle of word, do not complete
                return True

        if posidx == None:
            wordsstr.append('')
            words.append(None)
            posidx = len(wordsstr) - 1

        # First word completes a command, if not in any special 'mode'
        # otherwise, relay completion to the command, or complete by advice
        # from the 'mode' (prompt)
        cmds = commands.Commands()

        if not self._command_state and posidx == 0:
            # Complete the first command
            ret = commands.completion.command(words=wordsstr, idx=posidx)
        else:
            complete = None
            realidx = posidx

            if not self._command_state:
                # Get the command first
                cmd = commands.completion.single_command(wordsstr, 0)
                realidx -= 1

                ww = wordsstr[1:]
            else:
                cmd = self._command_state.top()
                ww = wordsstr

            if cmd:
                complete = cmd.autocomplete_func()

            if not complete:
                return True

            # 'complete' contains a dict with arg -> func to do the completion
            # of the named argument the command (or stack item) expects
            args, varargs = cmd.args()

            # Remove system arguments
            s = ['argstr', 'args', 'entry', 'view']
            args = list(filter(lambda x: not x in s, args))

            if realidx < len(args):
                arg = args[realidx]
            elif varargs:
                arg = '*'
            else:
                return True

            if not arg in complete:
                return True

            func = complete[arg]

            try:
                spec = utils.getargspec(func)

                if not ww:
                    ww = ['']

                kwargs = {'words': ww, 'idx': realidx, 'view': self._view}

                if not spec.keywords:
                    for k in list(kwargs.keys()):
                        if not k in spec.args:
                            del kwargs[k]

                ret = func(**kwargs)
            except Exception as e:
                # Can be number of arguments, or return values or simply buggy
                # modules
                print(e)
                traceback.print_exc()
                return True

        if not ret or not ret[0]:
            return True

        res = ret[0]
        completed = ret[1]

        if len(ret) > 2:
            after = ret[2]
        else:
            after = ' '

        # Replace the word
        if words[posidx] == None:
            # At end of everything, just append
            spec = None

            self._entry.insert_text(completed, self._entry.get_text_length())
            self._entry.set_position(-1)
        else:
            spec = self._complete_word_match(words[posidx])

            self._entry.delete_text(spec[1], spec[2])
            self._entry.insert_text(completed, spec[1])
            self._entry.set_position(spec[1] + len(completed))

        if len(res) == 1:
            # Full completion
            lastpos = self._entry.get_position()

            if not isinstance(res[0],
                              commands.module.Module) or not res[0].commands():
                if words[posidx] and after == ' ' and (
                        words[posidx].group(2) != None
                        or words[posidx].group(3) != None):
                    lastpos = lastpos + 1

                self._entry.insert_text(after, lastpos)
                self._entry.set_position(lastpos + 1)
            elif completed == wordsstr[posidx] or not res[0].method:
                self._entry.insert_text('.', lastpos)
                self._entry.set_position(lastpos + 1)

            if self._info_window:
                self._info_window.destroy()
        else:
            # Show popup with completed items
            if self._info_window:
                self._info_window.clear()

            ret = []

            for x in res:
                if isinstance(x, commands.method.Method):
                    ret.append('<b>' + saxutils.escape(x.name) + '</b> (<i>' +
                               x.oneline_doc() + '</i>)')
                else:
                    ret.append(str(x))

            self.info_show("\n".join(ret), True)

        return True

    def on_draw(self, widget, ct):
        win = widget.get_window(Gtk.TextWindowType.BOTTOM)

        if not Gtk.cairo_should_draw_window(ct, win):
            return False

        Gtk.cairo_transform_to_window(ct, widget, win)

        color = self.get_border_color()
        width = win.get_width()

        ct.set_source_rgba(color.red, color.green, color.blue, color.alpha)
        ct.move_to(0, 0)
        ct.line_to(width, 0)
        ct.stroke()

        return False

    def on_destroy(self, widget):
        self._view.set_border_window_size(Gtk.TextWindowType.BOTTOM, 0)
        self._view.disconnect(self.view_style_updated_id)
        self._view.disconnect(self.view_draw_id)

        if self._info_window:
            self._info_window.destroy()

        self._history.save()
コード例 #50
0
 def recover(self):
     """Mark this Node as not failed"""
     self.failed = False
     _logger.debug("Node %s recovers", self)
     History.add('recover', NodeAction(self))