Exemple #1
0
 def __init__(self, columns=80):
     self.lines = []
     self.columns = columns
     self.current_line = ""
     self.nag_table = NagHashTable()
Exemple #2
0
    def __init__(self, columns=80):
        self.lines = []
        self.columns = columns
        self.current_line = ""

        self.nag_table = NagHashTable()
Exemple #3
0
class StringExporter(object):
    """
    Allows exporting a game as a string.

    The export method of `Game` also provides options to include or exclude
    headers, variations or comments. By default everything is included.

    >>> exporter = chess.pgn.StringExporter()
    >>> game.export(exporter, headers=True, variations=True, comments=True)
    >>> pgn_string = str(exporter)

    Only `columns` characters are written per line. If `columns` is `None` then
    the entire movetext will be on a single line. This does not affect header
    tags and comments.

    There will be no newlines at the end of the string.
    """

    def __init__(self, columns=80):
        self.lines = []
        self.columns = columns
        self.current_line = ""
        self.nag_table = NagHashTable()

    def flush_current_line(self):
        if self.current_line:
            self.lines.append(self.current_line.rstrip())
        self.current_line = ""

    def write_token(self, token):
        if self.columns is not None and self.columns - len(self.current_line) < len(token):
            self.flush_current_line()
        self.current_line += token

    def write_line(self, line=""):
        self.flush_current_line()
        self.lines.append(line.rstrip())

    def start_game(self):
        pass

    def end_game(self):
        self.write_line()

    def start_headers(self):
        pass

    def put_header(self, tagname, tagvalue):
        self.write_line("[{0} \"{1}\"]".format(tagname, tagvalue))

    def end_headers(self):
        self.write_line()

    def start_variation(self):
        self.write_token("( ")

    def end_variation(self):
        self.write_token(") ")

    def start_snd_variation(self):
        #self.write_token('<dd><em><span style="color:gray">[ ')
        self.write_token('\n╔ ')

    def end_snd_variation(self):
        self.write_token('╚\n')

    def put_starting_comment(self, comment):
        self.put_comment(comment)

    def put_comment(self, comment):
        self.write_token("{ " + comment.replace("}", "").strip() + " } ")

    def return_comment(self, comment):
        return "{ " + comment.replace("}", "").strip() + " } "

    def put_nags(self, nags):
        for nag in sorted(nags):
            self.put_nag(nag)

    def put_nags_as_char(self, nags):
        for nag in sorted(nags):
            self.put_nag_as_char(nag)

    def put_nag(self, nag):
        self.write_token("$" + str(nag) + " ")

    def put_nag_as_char(self, nag):
        self.write_token(self.nag_table.nag_to_str(nag)+" ")

    def return_nags_as_char(self, nags):
        s = ""
        for nag in sorted(nags):
            s += self.nag_table.nag_to_str(nag) + " "
        return s

    def put_fullmove_number(self, turn, fullmove_number, variation_start):
        if turn == chess.WHITE:
            self.write_token(str(fullmove_number) + ". ")
        elif variation_start:
            self.write_token(str(fullmove_number) + ". ... ")


    def return_fullmove_number(self, turn, fullmove_number, variation_start, is_root):
        s = ""
        if turn == chess.WHITE:
            return str(fullmove_number) + "."
        elif variation_start:
            return str(fullmove_number) + ". ... "
        elif is_root and turn == chess.BLACK:
            return str(fullmove_number) + ". ... "
        else:
            return ""

    def put_move(self, board, move):
        self.write_token(board.san(move) + " ")

    def return_move(self, board, move):
        return board.san(move) + " "

    def put_move_highlighted(self,board,move):
        self.write_token('<span style="color:darkgoldenrod">')
        self.write_token(board.san(move) + " ")
        self.write_token('</span><a name="current"></a>')

    def return_move_highlighted(self,board,move):
        return '<span style="color:darkgoldenrod">'+board.san(move)+' </span><a name="current"></a>'
        #return board.san(move)


    def put_result(self, result):
        self.write_token(result + " ")

    def __str__(self):
        if self.current_line:
            return "\n".join(itertools.chain(self.lines, [ self.current_line.rstrip() ] )).rstrip()
        else:
            return "\n".join(self.lines).rstrip()
Exemple #4
0
class StringExporter(object):
    """
    Allows exporting a game as a string.

    :func:`chess.pgn.Game.export()` also provides options to include or exclude
    headers, variations or comments. By default everything is included.

    >>> exporter = chess.pgn.StringExporter()
    >>> game.export(exporter, headers=True, variations=True, comments=True)
    >>> pgn_string = str(exporter)

    Only `columns` characters are written per line. If `columns` is ``None``
    then the entire movetext will be on a single line. This does not affect
    header tags and comments.

    There will be no newlines at the end of the string.
    """

    def __init__(self, columns=80):
        self.lines = []
        self.columns = columns
        self.current_line = ""

        self.nag_table = NagHashTable()

    def flush_current_line(self):
        if self.current_line:
            self.lines.append(self.current_line.rstrip())
        self.current_line = ""

    def write_token(self, token):
        if self.columns is not None and self.columns - len(self.current_line) < len(token):
            self.flush_current_line()
        self.current_line += token

    def write_line(self, line=""):
        self.flush_current_line()
        self.lines.append(line.rstrip())

    def start_game(self):
        pass

    def end_game(self):
        self.write_line()

    def start_headers(self):
        pass


    def put_header(self, tagname, tagvalue):
        self.write_line("[{0} \"{1}\"]".format(tagname, tagvalue))

    def end_headers(self):
        self.write_line()

    def start_variation(self):
        self.write_token("( ")

    def end_variation(self):
        self.write_token(") ")

    def put_starting_comment(self, comment):
        self.put_comment(comment)

    def put_comment(self, comment):
        self.write_token("{ " + comment.replace("}", "").strip() + " } ")

    def put_nags(self, nags):
        for nag in sorted(nags):
            self.put_nag(nag)

    def put_nag(self, nag):
        self.write_token("$" + str(nag) + " ")

    def put_fullmove_number(self, turn, fullmove_number, variation_start):
        if turn == chess.WHITE:
            self.write_token(str(fullmove_number) + ". ")
        elif variation_start:
            self.write_token(str(fullmove_number) + "... ")

    def put_move(self, board, move):
        self.write_token(board.san(move) + " ")

    def put_result(self, result):
        self.write_token(result + " ")

    def __str__(self):
        if self.current_line:
            return "\n".join(itertools.chain(self.lines, [self.current_line.rstrip()])).rstrip()
        else:
            return "\n".join(self.lines).rstrip()

    # CUSTOM PATCH START
    def start_snd_variation(self):
        #self.write_token('<dd><em><span style="color:gray">[ ')
        self.write_token('\n╔ ')

    def end_snd_variation(self):
        self.write_token('╚\n')

    def return_fullmove_number(self, turn, fullmove_number, variation_start, is_root):
        s = ""
        if turn == chess.WHITE:
            return str(fullmove_number) + "."
        elif variation_start:
            return str(fullmove_number) + ". ... "
        elif is_root and turn == chess.BLACK:
            return str(fullmove_number) + ". ... "
        else:
            return ""

    def put_move_highlighted(self,board,move):
        self.write_token('<span style="color:darkgoldenrod">')
        self.write_token(board.san(move) + " ")
        self.write_token('</span><a name="current"></a>')

    def return_move_highlighted(self,board,move):
        return '<span style="color:darkgoldenrod">'+board.san(move)+' </span><a name="current"></a>'
        #return board.san(move)

    def return_move(self,board,move):
        return board.san(move) + " "

    def put_nags_as_char(self, nags):
        for nag in sorted(nags):
            self.put_nag_as_char(nag)

    def put_nag_as_char(self, nag):
        self.write_token(self.nag_table.nag_to_str(nag)+" ")

    def return_nags_as_char(self, nags):
        s = ""
        for nag in sorted(nags):
            s += self.nag_table.nag_to_str(nag) + " "
        return s

    def return_comment(self, comment):
        return "{ " + comment.replace("}", "").strip() + " } "