Пример #1
0
def test_serialise_game_tree(tc):
    serialised = ("(;AB[aa][ab][ac]C[comment \xa3];W[ab];C[];C[]"
                  "(;B[bc])(;B[bd];W[ca](;B[da])(;B[db];\n"
                  "W[ea])))\n")
    coarse_game = sgf_grammar.parse_sgf_game(serialised)
    tc.assertEqual(sgf_grammar.serialise_game_tree(coarse_game), serialised)
    tc.assertEqual(sgf_grammar.serialise_game_tree(coarse_game, wrap=None),
                   serialised.replace("\n", "") + "\n")
Пример #2
0
def test_serialise_game_tree(tc):
    serialised = ("(;AB[aa][ab][ac]C[comment \xa3];W[ab];C[];C[]"
                  "(;B[bc])(;B[bd];W[ca](;B[da])(;B[db];\n"
                  "W[ea])))\n")
    coarse_game = sgf_grammar.parse_sgf_game(serialised)
    tc.assertEqual(sgf_grammar.serialise_game_tree(coarse_game), serialised)
    tc.assertEqual(sgf_grammar.serialise_game_tree(coarse_game, wrap=None),
                   serialised.replace("\n", "")+"\n")
Пример #3
0
    def serialise(self, wrap=79):
        """Serialise the SGF data as a string.

        wrap -- int (default 79), or None

        Returns an 8-bit string, in the encoding specified by the CA property
        in the root node (defaulting to "ISO-8859-1").

        If the raw property encoding and the target encoding match (which is
        the usual case), the raw property values are included unchanged in the
        output (even if they are improperly encoded.)

        Otherwise, if any raw property value is improperly encoded,
        UnicodeDecodeError is raised, and if any property value can't be
        represented in the target encoding, UnicodeEncodeError is raised.

        If the target encoding doesn't identify a Python codec, ValueError is
        raised. Behaviour is unspecified if the target encoding isn't
        ASCII-compatible (eg, UTF-16).

        If 'wrap' is not None, makes some effort to keep output lines no longer
        than 'wrap'.

        """
        try:
            encoding = self.get_charset()
        except ValueError:
            raise ValueError("unsupported charset: %s" %
                             self.root.get_raw_list("CA"))
        coarse_tree = sgf_grammar.make_coarse_game_tree(
            self.root, lambda node:node, Node.get_raw_property_map)
        serialised = sgf_grammar.serialise_game_tree(coarse_tree, wrap)
        if encoding == self.root.get_encoding():
            return serialised
        else:
            return serialised.decode(self.root.get_encoding()).encode(encoding)