Exemplo n.º 1
0
    def handle_savesgf(self, args):
        try:
            pathname = args[0]
        except IndexError:
            gtp_engine.report_bad_arguments()

        sgf_game = sgf.Sgf_game(self.board_size)
        root = sgf_game.get_root()
        root.set('KM', self.komi)
        root.set('AP', ("gomill", __version__))
        sgf_game.set_date()
        if self.handicap is not None:
            root.set('HA', self.handicap)
        for arg in args[1:]:
            try:
                identifier, value = arg.split("=", 1)
                if not identifier.isalpha():
                    raise ValueError
                identifier = identifier.upper()
                value = value.replace("\\_", " ").replace("\\\\", "\\")
            except Exception:
                gtp_engine.report_bad_arguments()
            root.set_raw(identifier, sgf_grammar.escape_text(value))
        sgf_moves.set_initial_position(sgf_game, self.history_base)
        for history_move in self.move_history:
            node = sgf_game.extend_main_sequence()
            node.set_move(history_move.colour, history_move.move)
            if history_move.comments is not None:
                node.set("C", history_move.comments)
        sgf_moves.indicate_first_player(sgf_game)
        try:
            self._save_file(pathname, sgf_game.serialise())
        except EnvironmentError, e:
            raise GtpError("error writing file: %s" % e)
Exemplo n.º 2
0
def serialise_text(s, context):
    """Serialise a Text value.

    See sgf_grammar.escape_text() for details.

    s -- 8-bit utf-8 string

    """
    if context.encoding != "UTF-8":
        s = s.decode("utf-8").encode(context.encoding)
    return sgf_grammar.escape_text(s)
Exemplo n.º 3
0
def serialise_text(s, context):
    """Serialise a Text value.

    See sgf_grammar.escape_text() for details.

    s -- 8-bit utf-8 string

    """
    if context.encoding != "UTF-8":
        s = s.decode("utf-8").encode(context.encoding)
    return sgf_grammar.escape_text(s)
Exemplo n.º 4
0
 def roundtrip(s):
     return sgf_grammar.text_value(sgf_grammar.escape_text(s))
Exemplo n.º 5
0
def test_escape_text(tc):
    tc.assertEqual(sgf_grammar.escape_text("abc"), "abc")
    tc.assertEqual(sgf_grammar.escape_text(r"a\bc"), r"a\\bc")
    tc.assertEqual(sgf_grammar.escape_text(r"ab[c]"), r"ab[c\]")
    tc.assertEqual(sgf_grammar.escape_text(r"a\]bc"), r"a\\\]bc")
Exemplo n.º 6
0
 def roundtrip(s):
     return sgf_grammar.text_value(sgf_grammar.escape_text(s))
Exemplo n.º 7
0
def test_escape_text(tc):
    tc.assertEqual(sgf_grammar.escape_text("abc"), "abc")
    tc.assertEqual(sgf_grammar.escape_text(r"a\bc"), r"a\\bc")
    tc.assertEqual(sgf_grammar.escape_text(r"ab[c]"), r"ab[c\]")
    tc.assertEqual(sgf_grammar.escape_text(r"a\]bc"), r"a\\\]bc")