示例#1
0
    def cypher(self, ui):
        """Interactive version of cypher()."""
        txt = ""
        ui.message("===== Cypher Mode =====")

        while 1:
            done = False
            while 1:
                exhaustive = False
                threshold = 0.7

                txt = ui.text_input("Text to cypher to Morse/Wabun",
                                    sub_type=ui.UPPER)
                if txt is None:
                    break  # Go back to main Cypher menu.

                # Get cyphering method.
                options = [(morse_wabun.INTER,
                            "$international morse cyphering", ""),
                           (morse_wabun.WABUN, "or *wabun one", "")]
                method = ui.get_choice("Do you want to use",
                                       options,
                                       oneline=True)

                # Get cyphering variant.
                options = [(morse_wabun.STANDARD,
                            "$standard variant (=.===...=.=.......===.=)", ""),
                           (morse_wabun.FAST, "or *fast one (. -.- / ..--)",
                            "")]
                variant = ui.get_choice("Do you want to use",
                                        options,
                                        oneline=True)

                # What algo to use (wabun only).
                if method == morse_wabun.WABUN:
                    options = [("exhst", "*exhaustive cyphering", ""),
                               ("simple", "or $simple one", "")]
                    answ = ui.get_choice("Do you want to use",
                                         options,
                                         oneline=True)
                    if answ == "exhst":
                        exhaustive = True
                        t = ui.get_data("Cypher threshold (nothing to use "
                                        "default {} one): ".format(threshold),
                                        sub_type=ui.FLOAT,
                                        allow_void=True)
                        if t is not None:
                            threshold = t

                try:
                    # Will also raise an exception if data is None.
                    txt = morse_wabun.cypher(txt,
                                             method,
                                             variant,
                                             exhaustive=exhaustive,
                                             min_cypher=threshold)
                    if exhaustive:
                        w_sep = ''
                        if variant == morse_wabun.FAST:
                            w_sep = " / "
                        txt = self._get_exhaustive_txt(txt,
                                                       w_sep,
                                                       ui,
                                                       min_cypher=threshold)
                    done = True  # Out of those loops, output result.
                    break
                except Exception as e:
                    if utils.DEBUG:
                        import traceback
                        traceback.print_tb(sys.exc_info()[2])
                    ui.message(str(e), level=ui.ERROR)
                    options = [("retry", "*try again", ""),
                               ("menu", "or go back to *menu", "")]
                    answ = ui.get_choice(
                        "Could not convert that data into "
                        "morse, please",
                        options,
                        oneline=True)
                    if answ in {None, "menu"}:
                        return

            if done:
                ui.text_output("Data successfully converted", txt,
                               "Morse form of data")

            options = [("redo", "*cypher another data", ""),
                       ("quit", "or go back to *menu", "")]
            answ = ui.get_choice("Do you want to", options, oneline=True)
            if answ in {None, "quit"}:
                return
示例#2
0
    def cypher(self, ui):
        """Interactive version of cypher()."""
        txt = ""
        ui.message("===== Cypher Mode =====")

        while 1:
            done = False
            while 1:
                exhaustive = False
                threshold = 0.7

                txt = ui.text_input("Text to cypher to Morse/Wabun",
                                    sub_type=ui.UPPER)
                if txt is None:
                    break  # Go back to main Cypher menu.

                # Get cyphering method.
                options = [(morse_wabun.INTER,
                            "$international morse cyphering", ""),
                           (morse_wabun.WABUN, "or *wabun one", "")]
                method = ui.get_choice("Do you want to use", options,
                                       oneline=True)

                # Get cyphering variant.
                options = [(morse_wabun.STANDARD,
                            "$standard variant (=.===...=.=.......===.=)", ""),
                           (morse_wabun.FAST,
                            "or *fast one (. -.- / ..--)", "")]
                variant = ui.get_choice("Do you want to use", options,
                                        oneline=True)

                # What algo to use (wabun only).
                if method == morse_wabun.WABUN:
                    options = [("exhst", "*exhaustive cyphering", ""),
                               ("simple", "or $simple one", "")]
                    answ = ui.get_choice("Do you want to use", options,
                                         oneline=True)
                    if answ == "exhst":
                        exhaustive = True
                        t = ui.get_data("Cypher threshold (nothing to use "
                                        "default {} one): ".format(threshold),
                                        sub_type=ui.FLOAT, allow_void=True)
                        if t is not None:
                            threshold = t

                try:
                    # Will also raise an exception if data is None.
                    txt = morse_wabun.cypher(txt, method, variant,
                                             exhaustive=exhaustive,
                                             min_cypher=threshold)
                    if exhaustive:
                        w_sep = ''
                        if variant == morse_wabun.FAST:
                            w_sep = " / "
                        txt = self._get_exhaustive_txt(txt, w_sep, ui,
                                                       min_cypher=threshold)
                    done = True  # Out of those loops, output result.
                    break
                except Exception as e:
                    if utils.DEBUG:
                        import traceback
                        traceback.print_tb(sys.exc_info()[2])
                    ui.message(str(e), level=ui.ERROR)
                    options = [("retry", "*try again", ""),
                               ("menu", "or go back to *menu", "")]
                    answ = ui.get_choice("Could not convert that data into "
                                         "morse, please", options,
                                         oneline=True)
                    if answ in {None, "menu"}:
                        return

            if done:
                ui.text_output("Data successfully converted", txt,
                               "Morse form of data")

            options = [("redo", "*cypher another data", ""),
                       ("quit", "or go back to *menu", "")]
            answ = ui.get_choice("Do you want to", options, oneline=True)
            if answ in {None, "quit"}:
                return
示例#3
0
    def demo(self, ui):
        ui.message("===== Demo Mode =====")
        ui.message("Running a small demo/testing!")
        ui.message("")

        ui.message("--- Cyphering to Morse ---")
        text = "VANCOUVER! VANCOUVER! THIS IS IT!"
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text, morse_wabun.INTER, morse_wabun.STANDARD)
        ui.message("International standard morse: {}".format(out))
        out = morse_wabun.cypher(text, morse_wabun.INTER, morse_wabun.FAST)
        ui.message("International fast morse: {}".format(out))
        ui.message("")

        ui.message("--- Cyphering to Wabun ---")
        text = "LE KATAKANA DU SAMOURAI ETAIT EN FER BLANC."
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text, morse_wabun.WABUN, morse_wabun.STANDARD)
        ui.message("Wabun standard morse: {}".format(out))
        out = morse_wabun.cypher(text, morse_wabun.WABUN, morse_wabun.FAST)
        ui.message("Wabun fast morse: {}".format(out))
        ui.message("")

        ui.message("--- Decyphering ---")
        htext = ".. -.-. .. / ...- .- -. -.-. --- ..- ...- . .-. .-.-.- / " \
                "-.-. --- -- -- . -. - / .- .-.. .-.. . --.. -....- ...- " \
                "--- ..- ... --..-- / .--. .- .-. .. ... ..--.."
        ui.message("Note that most of the time, you can use default options "
                   "here (i.e. automatic detection)!")
        ui.message("First input: {}".format(htext))
        out = morse_wabun.decypher(htext)
        ui.message("The decyphered data is: {}".format(out))
        htext = "T...=.......===.===.=.===.=...=.=.===.===..." \
                "===.===.=.=.===.=.=.......=.===...S......." \
                "S...===.===.=.=.=.=.===.===.=.===...=.===.=.===.===.=.=..." \
                "===.===.=...N...G.......=.===.=.=.=...N.......T...=......." \
                "===.=.===...L...L"
        ui.message("Second input: {}".format(htext))
        out = morse_wabun.decypher(htext)
        ui.message("The decyphered data is: {}".format(out))
        ui.message("")

        ui.message("--- Notes ---")
        ui.message("+ For Wabun cyphering, you can choose the optionnal "
                   "Exhaustive option, to get all possible encodings of each "
                   "words higher than the given threshold of cyphering (or "
                   "the highest possible):")
        text = "THE SHOGUN IS DRINKING GREEN TEA."
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text,
                                 morse_wabun.WABUN,
                                 morse_wabun.FAST,
                                 exhaustive=True,
                                 min_cypher=0.6)
        out = self._get_exhaustive_txt(out,
                                       ' / ',
                                       ui,
                                       min_cypher=0.6,
                                       act="all")
        ui.message("Cyphered solutions with cypher factor higher than 0.6:")
        ui.message(out)
        ui.message("")

        ui.message("--- Won’t work ---")
        ui.message("+ As usual, only valid letters/codes can be given "
                   "(always uppercase).")
        text = "\"HELLŌ 2012 WØRLD…\""
        ui.message("* International Morse supports numbers, and some special "
                   "chars and accentuated occidental chars")
        ui.message("Data to cypher in international morse: {}" "".format(text))
        try:
            out = morse_wabun.cypher(text, morse_wabun.INTER)
            ui.message("Morse cyphered data: {}".format(out))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("* Wabun Morse supports only ASCII chars, spaces, dots, "
                   "coma and parenthesis:")
        ui.message("Data to cypher in international morse: {}" "".format(text))
        try:
            out = morse_wabun.cypher(text, morse_wabun.WABUN)
            ui.message("WABUN cyphered data: {}".format(out))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)

        ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
示例#4
0
    def demo(self, ui):
        ui.message("===== Demo Mode =====")
        ui.message("Running a small demo/testing!")
        ui.message("")

        ui.message("--- Cyphering to Morse ---")
        text = "VANCOUVER! VANCOUVER! THIS IS IT!"
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text, morse_wabun.INTER, morse_wabun.STANDARD)
        ui.message("International standard morse: {}".format(out))
        out = morse_wabun.cypher(text, morse_wabun.INTER, morse_wabun.FAST)
        ui.message("International fast morse: {}".format(out))
        ui.message("")

        ui.message("--- Cyphering to Wabun ---")
        text = "LE KATAKANA DU SAMOURAI ETAIT EN FER BLANC."
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text, morse_wabun.WABUN, morse_wabun.STANDARD)
        ui.message("Wabun standard morse: {}".format(out))
        out = morse_wabun.cypher(text, morse_wabun.WABUN, morse_wabun.FAST)
        ui.message("Wabun fast morse: {}".format(out))
        ui.message("")

        ui.message("--- Decyphering ---")
        htext = ".. -.-. .. / ...- .- -. -.-. --- ..- ...- . .-. .-.-.- / " \
                "-.-. --- -- -- . -. - / .- .-.. .-.. . --.. -....- ...- " \
                "--- ..- ... --..-- / .--. .- .-. .. ... ..--.."
        ui.message("Note that most of the time, you can use default options "
                   "here (i.e. automatic detection)!")
        ui.message("First input: {}".format(htext))
        out = morse_wabun.decypher(htext)
        ui.message("The decyphered data is: {}".format(out))
        htext = "T...=.......===.===.=.===.=...=.=.===.===..." \
                "===.===.=.=.===.=.=.......=.===...S......." \
                "S...===.===.=.=.=.=.===.===.=.===...=.===.=.===.===.=.=..." \
                "===.===.=...N...G.......=.===.=.=.=...N.......T...=......." \
                "===.=.===...L...L"
        ui.message("Second input: {}".format(htext))
        out = morse_wabun.decypher(htext)
        ui.message("The decyphered data is: {}".format(out))
        ui.message("")

        ui.message("--- Notes ---")
        ui.message("+ For Wabun cyphering, you can choose the optionnal "
                   "Exhaustive option, to get all possible encodings of each "
                   "words higher than the given threshold of cyphering (or "
                   "the highest possible):")
        text = "THE SHOGUN IS DRINKING GREEN TEA."
        ui.message("Data to cypher: {}".format(text))
        out = morse_wabun.cypher(text, morse_wabun.WABUN, morse_wabun.FAST,
                                 exhaustive=True, min_cypher=0.6)
        out = self._get_exhaustive_txt(out, ' / ', ui,
                                       min_cypher=0.6, act="all")
        ui.message("Cyphered solutions with cypher factor higher than 0.6:")
        ui.message(out)
        ui.message("")

        ui.message("--- Won’t work ---")
        ui.message("+ As usual, only valid letters/codes can be given "
                   "(always uppercase).")
        text = "\"HELLŌ 2012 WØRLD…\""
        ui.message("* International Morse supports numbers, and some special "
                   "chars and accentuated occidental chars")
        ui.message("Data to cypher in international morse: {}"
                   "".format(text))
        try:
            out = morse_wabun.cypher(text, morse_wabun.INTER)
            ui.message("Morse cyphered data: {}".format(out))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("* Wabun Morse supports only ASCII chars, spaces, dots, "
                   "coma and parenthesis:")
        ui.message("Data to cypher in international morse: {}"
                   "".format(text))
        try:
            out = morse_wabun.cypher(text, morse_wabun.WABUN)
            ui.message("WABUN cyphered data: {}".format(out))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)

        ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)