Beispiel #1
0
def on_pick_date(button, date_entry):
    # Parse the existing date
    date = date_entry.get_text()
    year, month, day = parseDateTag(date)

    # Prepare the date of the picker
    calendar = Gtk.Calendar()
    curyear, curmonth, curday = calendar.get_date()
    year = curyear if year is None else year
    month = curmonth if month is None else month - 1
    day = curday if day is None else day
    calendar.select_month(month, year)
    calendar.select_day(day)

    # Show the dialog
    dialog = Gtk.Dialog(
        _("Pick a date"), None,
        Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK,
         Gtk.ResponseType.ACCEPT))

    sw = Gtk.ScrolledWindow()
    sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
    sw.add(calendar)

    dialog.get_content_area().pack_start(sw, True, True, 0)
    dialog.resize(300, 200)
    dialog.show_all()

    response = dialog.run()
    dialog.destroy()

    if response == Gtk.ResponseType.ACCEPT:
        year, month, day = calendar.get_date()
        date_entry.set_text("%04d.%02d.%02d" % (year, month + 1, day))
Beispiel #2
0
def on_pick_date(button, date_entry):
    # Parse the existing date
    date = date_entry.get_text()
    year, month, day = parseDateTag(date)

    # Prepare the date of the picker
    calendar = Gtk.Calendar()
    curyear, curmonth, curday = calendar.get_date()
    year = curyear if year is None else year
    month = curmonth if month is None else month - 1
    day = curday if day is None else day
    calendar.select_month(month, year)
    calendar.select_day(day)

    # Show the dialog
    dialog = Gtk.Dialog(_("Pick a date"),
                        mainwindow(),
                        Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT))

    sw = Gtk.ScrolledWindow()
    sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
    sw.add(calendar)

    dialog.get_content_area().pack_start(sw, True, True, 0)
    dialog.resize(300, 200)
    dialog.show_all()

    response = dialog.run()
    dialog.destroy()

    if response == Gtk.ResponseType.ACCEPT:
        year, month, day = calendar.get_date()
        date_entry.set_text("%04d.%02d.%02d" % (year, month + 1, day))
Beispiel #3
0
def save(handle, model, position=None, flip=False):
    """ Saves the game from GameModel to .pgn """
    processed_tags = []

    def write_tag(tag, value, roster=False):
        nonlocal processed_tags
        if tag in processed_tags or (not roster and not value):
            return
        try:
            pval = str(value)
            pval = pval.replace("\\", "\\\\")
            pval = pval.replace("\"", "\\\"")
            print('[%s "%s"]' % (tag, pval), file=handle)
        except UnicodeEncodeError:
            pval = bytes(pval, "utf-8").decode(PGN_ENCODING, errors="ignore")
            print('[%s "%s"]' % (tag, pval), file=handle)
        processed_tags = processed_tags + [tag]

    # Mandatory ordered seven-tag roster
    status = reprResult[model.status]
    for tag in mandatory_tags:
        value = model.tags[tag]
        if tag == "Date":
            y, m, d = parseDateTag(value)
            y = "%04d" % y if y is not None else "????"
            m = "%02d" % m if m is not None else "??"
            d = "%02d" % d if d is not None else "??"
            value = "%s.%s.%s" % (y, m, d)
        elif value == "":
            value = "?"
        write_tag(tag, value, roster=True)
    write_tag("Result", reprResult[model.status], roster=True)

    # Variant
    if model.variant.variant != NORMALCHESS:
        write_tag("Variant", model.variant.cecp_name.capitalize())

    # Initial position
    if model.boards[0].asFen() != FEN_START:
        write_tag("SetUp", "1")
        write_tag("FEN", model.boards[0].asFen())

    # Number of moves
    write_tag("PlyCount", model.ply - model.lowply)

    # Final position
    if model.reason in ABORTED_REASONS:
        value = "abandoned"
    elif model.reason == WON_ADJUDICATION and model.isEngine2EngineGame():
        value = "rules infraction"
    elif model.reason in ADJUDICATION_REASONS:
        value = "adjudication"
    elif model.reason in DEATH_REASONS:
        value = "death"
    elif model.reason in CALLFLAG_REASONS:
        value = "time forfeit"
    elif model.reason in ADJOURNED_REASONS or status == "*":
        value = "unterminated"
    else:
        value = "normal"
    write_tag("Termination", value)

    # ELO and its variation
    if conf.get("saveRatingChange"):
        welo = model.tags["WhiteElo"]
        belo = model.tags["BlackElo"]
        if welo != "" and belo != "":
            write_tag("WhiteRatingDiff",
                      get_elo_rating_change_pgn(model, WHITE))  # Unofficial
            write_tag("BlackRatingDiff",
                      get_elo_rating_change_pgn(model, BLACK))  # Unofficial

    # Time
    if model.timed:
        write_tag(
            'WhiteClock',
            msToClockTimeTag(int(model.timemodel.getPlayerTime(WHITE) * 1000)))
        write_tag(
            'BlackClock',
            msToClockTimeTag(int(model.timemodel.getPlayerTime(BLACK) * 1000)))

    # Write all the unprocessed tags
    for tag in model.tags:
        # Debug: print(">> %s = %s" % (tag, str(model.tags[tag])))
        write_tag(tag, model.tags[tag])

    # Discovery of the moves and comments
    save_emt = conf.get("saveEmt")
    save_eval = conf.get("saveEval")
    result = []
    walk(model.boards[0].board, result, model, save_emt, save_eval)

    # Alignment of the fetched elements
    indented = conf.get("indentPgn")
    if indented:
        buffer = ""
        depth = 0
        crlf = False
        for text in result:
            # De/Indentation
            crlf = (buffer[-1:] if len(buffer) > 0 else "") in ["\r", "\n"]
            if text == "(":
                depth += 1
                if indented and not crlf:
                    buffer += os.linesep
                    crlf = True
            # Space between each term
            last = buffer[-1:] if len(buffer) > 0 else ""
            crlf = last in ["\r", "\n"]
            if not crlf and last != " " and last != "\t" and last != "(" and not text.startswith(
                    "\r") and not text.startswith(
                        "\n") and text != ")" and len(buffer) > 0:
                buffer += " "
            # New line for a new main move
            if len(buffer) == 0 or (indented and depth == 0 and last != "\r"
                                    and last != "\n" and re.match(
                                        r"^[0-9]+\.", text) is not None):
                buffer += os.linesep
                crlf = True
            # Alignment
            if crlf and depth > 0:
                for j in range(0, depth):
                    buffer += "    "
            # Term
            buffer += text
            if indented and text == ")":
                buffer += os.linesep
                crlf = True
                depth -= 1
    else:
        # Add new line to separate tag section and movetext
        print('', file=handle)
        buffer = textwrap.fill(" ".join(result), width=80)

    # Final
    status = reprResult[model.status]
    print(buffer, status, file=handle)
    # Add new line to separate next game
    print('', file=handle)

    output = handle.getvalue() if isinstance(handle, StringIO) else ""
    handle.close()
    return output
Beispiel #4
0
def save(handle, model, position=None):
    """ Saves the game from GameModel to .pgn """
    processed_tags = []

    def write_tag(tag, value, roster=False):
        nonlocal processed_tags
        if tag in processed_tags or (not roster and not value):
            return
        try:
            pval = str(value)
            pval = pval.replace("\\", "\\\\")
            pval = pval.replace("\"", "\\\"")
            print('[%s "%s"]' % (tag, pval), file=handle)
            processed_tags = processed_tags + [tag]
        except Exception:
            pass

    # Mandatory ordered seven-tag roster
    status = reprResult[model.status]
    for tag in mandatory_tags:
        value = model.tags[tag]
        if tag == "Date":
            y, m, d = parseDateTag(value)
            y = "%04d" % y if y is not None else "????"
            m = "%02d" % m if m is not None else "??"
            d = "%02d" % d if d is not None else "??"
            value = "%s.%s.%s" % (y, m, d)
        elif value == "":
            value = "?"
        write_tag(tag, value, roster=True)
    write_tag("Result", reprResult[model.status], roster=True)

    # Variant
    if model.variant.variant != NORMALCHESS:
        write_tag("Variant", model.variant.cecp_name.capitalize())

    # Initial position
    if model.boards[0].asFen() != FEN_START:
        write_tag("SetUp", "1")
        write_tag("FEN", model.boards[0].asFen())

    # Number of moves
    write_tag("PlyCount", model.ply - model.lowply)

    # Final position
    if model.reason == WON_CALLFLAG:
        value = "time forfeit"
    elif model.reason == WON_ADJUDICATION and model.isEngine2EngineGame():
        value = "rules infraction"
    elif model.reason in (DRAW_ADJUDICATION, WON_ADJUDICATION):
        value = "adjudication"
    elif model.reason == WHITE_ENGINE_DIED:
        value = "white engine died"
    elif model.reason == BLACK_ENGINE_DIED:
        value = "black engine died"
    elif model.reason in ABORTED_REASONS:
        value = "abandoned"
    elif model.reason in ADJOURNED_REASONS:
        value = "unterminated"
    else:
        value = "unterminated" if status == "*" else None
    if value is not None:
        write_tag("Termination", value)

    # ELO and its variation
    if conf.get("saveRatingChange", False):
        welo = model.tags["WhiteElo"]
        belo = model.tags["BlackElo"]
        if welo != "" and belo != "":
            write_tag("WhiteRatingDiff", get_elo_rating_change_pgn(model, WHITE))  # Unofficial
            write_tag("BlackRatingDiff", get_elo_rating_change_pgn(model, BLACK))  # Unofficial

    # Time
    if model.timed:
        write_tag('WhiteClock', msToClockTimeTag(int(model.timemodel.getPlayerTime(WHITE) * 1000)))
        write_tag('BlackClock', msToClockTimeTag(int(model.timemodel.getPlayerTime(BLACK) * 1000)))

    # Write all the unprocessed tags
    for tag in model.tags:
        # Debug: print(">> %s = %s" % (tag, str(model.tags[tag])))
        write_tag(tag, model.tags[tag])

    # Discovery of the moves and comments
    save_emt = conf.get("saveEmt", False)
    save_eval = conf.get("saveEval", False)
    result = []
    walk(model.boards[0].board, result, model, save_emt, save_eval)

    # Alignment of the fetched elements
    indented = conf.get("indentPgn", False)
    if indented:
        buffer = ""
        depth = 0
        crlf = False
        for text in result:
            # De/Indentation
            crlf = (buffer[-1:] if len(buffer) > 0 else "") in ["\r", "\n"]
            if text == "(":
                depth += 1
                if indented and not crlf:
                    buffer += os.linesep
                    crlf = True
            # Space between each term
            last = buffer[-1:] if len(buffer) > 0 else ""
            crlf = last in ["\r", "\n"]
            if not crlf and last != " " and last != "\t" and last != "(" and not text.startswith("\r") and not text.startswith("\n") and text != ")" and len(buffer) > 0:
                buffer += " "
            # New line for a new main move
            if len(buffer) == 0 or (indented and depth == 0 and last != "\r" and last != "\n" and re.match("^[0-9]+\.", text) is not None):
                buffer += os.linesep
                crlf = True
            # Alignment
            if crlf and depth > 0:
                for j in range(0, depth):
                    buffer += "    "
            # Term
            buffer += text
            if indented and text == ")":
                buffer += os.linesep
                crlf = True
                depth -= 1
    else:
        # Add new line to separate tag section and movetext
        print('', file=handle)
        buffer = textwrap.fill(" ".join(result), width=80)

    # Final
    status = reprResult[model.status]
    print(buffer, status, file=handle)
    # Add new line to separate next game
    print('', file=handle)

    output = handle.getvalue() if isinstance(handle, StringIO) else ""
    handle.close()
    return output