def _get_exhaustive_txt(out, w_sep, ui, min_cypher, act=None): ui.message("Exaustive found {} solutions for a minimum cyphering of " "{}, among which {} solutions with the highest possible " "cyphering ({})." "".format(out["n_solutions"], min_cypher, out["best_n_solutions"], out["best_cypher"])) if act not in {"all", "best", "rand", "rand_best"}: options = [("all", "*all solutions", ""), ("best", "all $best solutions", ""), ("rand", "*one random solution", ""), ("rand_best", "or one *random best solution", "")] act = ui.get_choice("Do you want to get", options, oneline=True) if act == "all": lines = utils.format_multiwords(out["solutions"], sep="") return "\n {}".format("\n ".join(lines)) elif act == "best": lines = utils.format_multiwords(out["best_solutions"], sep="") return "\n {}".format("\n ".join(lines)) elif act == "rand": return "".join((random.choice(w) for w in out["solutions"])) else: return "".join((random.choice(w) for w in out["best_solutions"]))
def _get_exhaustive_txt(out, ui, cypher_goal, act=None): ui.message("Exaustive found {} solutions cyphering of {} ± 0.05, " "and {} solutions with the highest possible cyphering ({})." "".format(out["n_solutions"], cypher_goal, out["best_n_solutions"], out["best_cypher"])) if act not in {"goal", "best", "rand", "rand_best"}: options = [("goal", "*goal solutions", ""), ("best", "all $best solutions", ""), ("rand", "*one random goal solution", ""), ("rand_best", "or one *random best solution", "")] act = ui.get_choice("Do you want to get", options, oneline=True) if act == "goal": lines = utils.format_multiwords(out["solutions"], sep=" ") return "\n {}".format("\n ".join(lines)) elif act == "best": lines = utils.format_multiwords(out["best_solutions"], sep=" ") return "\n {}".format("\n ".join(lines)) elif act == "rand": return " ".join((random.choice(w) for w in out["solutions"])) else: return " ".join((random.choice(w) for w in out["best_solutions"]))
def decypher(self, ui): """Interactive version of decypher().""" txt = "" ui.message("===== Decypher Mode =====") while 1: txt = ui.text_input("Please choose some atomic digits text", sub_type=ui.UPPER) try: txt = atomicdigits.decypher(txt) txt = "\n " + "\n ".join(utils.format_multiwords(txt)) ui.text_output("Text successfully decyphered", txt, "The decyphered text is") except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) options = [("redo", "*decypher another data", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ == "quit": return
def _get_largonji_txt(out, ui, act=None): ui.message("Largonji found {} solutions." "".format(out["n_solutions"])) if act not in {"all", "rand"}: options = [("all", "*all solutions", ""), ("rand", "or *one random solution", "")] act = ui.get_choice("Do you want to get", options, oneline=True) if act == "all": lines = utils.format_multiwords(out["solutions"], sep=" ") return "\n {}".format("\n ".join(lines)) else: return " ".join((random.choice(w) for w in out["solutions"]))
def decypher(self, ui): """Interactive version of decypher().""" txt = "" ui.message("===== Decypher Mode =====") while 1: txt = ui.text_input("Please choose some atomic digits text") syllable = None method = argots.GENERIC # Get obfuscating method. options = [(argots.JAVANAIS, "*javanais decyphering", ""), (argots.FEU, "langue de *feu", ""), (argots.GENERIC, "$generic", ""), (argots.LARGONJI, "or *largonji des loucherbèmes one", "")] method = ui.get_choice("Do you want to use", options, oneline=True) if method == argots.LARGONJI: # Get sets of suffix syllables. v_sylb = argots.LARGONJI_SYLLABLES_V s = ui.get_data("Suffix vowel-compliant syllables, comma " "separated (or nothing to use default " "'{}' ones): " "".format("', '".join(v_sylb)), allow_void=True) if s: v_sylb = [s.strip() for s in s.split(",")] c_sylb = argots.LARGONJI_SYLLABLES_C s = ui.get_data("Suffix consonant-compliant syllables, " "comma separated (or nothing to use " "default '{}' ones): " "".format("', '".join(c_sylb)), allow_void=True) if s: c_sylb = [s.strip() for s in s.split(",")] else: # Get obfuscating syllable. s = ui.get_data("Obfuscating syllable (or nothing to search " "for the most common one): ", allow_void=True, validate=self._validate, validate_kwargs={'method': method}) if s: syllable = s try: if method == argots.LARGONJI: txt = argots.decypher_largonji(txt, v_sylb, c_sylb) txt = "\n " + \ "\n ".join(utils.format_multiwords(txt)) else: txt = argots.decypher(txt, method, syllable) if len(txt) > 1: txt = "\n " + \ "\n".join(["Using '{}':\n {}" "".format(t[0], t[1]) for t in txt]) elif txt: txt = txt[0][1] else: txt = "" ui.text_output("Text successfully decyphered", txt, "The decyphered text is") except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) options = [("redo", "*decypher another data", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ == "quit": return
def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") ui.message("--- Cyphering ---") ui.message("+ Argot Javanais & co.") text = "Les « Blousons Noirs » vous parlent…" ui.message("Data to cypher: {}".format(text)) out = argots.cypher(text, argots.JAVANAIS, 'av') ui.message("Argot Javanais with 'av': {}".format(out)) ui.message("") ui.message("+ Largonji des Loucherbèmes.") text = "Les bouchers de la Villette osent un tango langoureux !" ui.message("Data to cypher: {}".format(text)) out = argots.cypher_largonji(text, argots.LARGONJI_SYLLABLES_V, argots.LARGONJI_SYLLABLES_C) out = self._get_largonji_txt(out, ui, act="rand") ui.message("Largonji des Loucherbèmes: {}".format(out)) ui.message("") ui.message("--- Decyphering ---") ui.message("+ Argot Javanais & co.") htext = "LEfes EfApEfachEfes sEfont sEfur lEfe sEfentEfier dEfes " \ "HEfallEfes." ui.message("Note that most of the time, you can use default options " "here (i.e. Generic, and give no obfuscating syllable), " "as that kind of “cyphering” is really easy to break!") ui.message("Langue de Feu text used as input: {}".format(htext)) out = argots.decypher(htext, argots.FEU) ui.message("The decyphered data is:\n With '{}': {}" "".format(out[0][0], out[0][1])) ui.message("") ui.message("+ Largonji des Loucherbèmes.") htext = "La mie du loucherbouche lricottefesse lavecouche le " \ "loulangerbesse !" ui.message("Largonji de Loucherbems text used as input: {}" "".format(htext)) out = argots.decypher_largonji(htext) ui.message("The decyphered data is:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Notes ---") ui.message("+ With Argot Javanais & co, you can choose the optional " "Exhaustive option, to get all possible encodings of each " "words higher than the given goal of cyphering (or the " "highest possible):") text = "Do you know Ménilmuche and Belleville ?" ui.message("Data to cypher: {}".format(text)) out = argots.cypher(text, argots.GENERIC, 'uz', exhaustive=True, cypher_goal=0.2) out = self._get_exhaustive_txt(out, ui, cypher_goal=0.2, act="goal") ui.message("Generic cyphered solutions with cypher factor higher " "than 0.2:") ui.message(out) ui.message("") ui.message("+ Here is what you’ll get if you try to decypher some " "largonji without giving any suffix syllables (note how " "some words aren’t decyphered well at all):") htext = "La mie du loucherbouche lricottefesse lavecouche le " \ "loulangerbesse !" ui.message("Largonji de Loucherbems text used as input: {}" "".format(htext)) out = argots.decypher_largonji(htext, (), ()) ui.message("The decyphered data is:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Won’t work ---") ui.message("+ With Argot Javanais & co, the obfuscating syllable " "must comply to the chosen method!") text = "Hello WORLD !" ui.message("Data to cypher, using Javanais and 'eh': {}\n" "".format(text)) try: out = argots.cypher(text, argots.JAVANAIS, 'eh') ui.message("Javanais cyphered data: {}".format(out)) except Exception as e: ui.message(str(e), level=ui.ERROR) ui.message("") ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
def decypher(self, ui): """Interactive version of decypher().""" txt = "" ui.message("===== Decypher Mode =====") while 1: txt = ui.text_input("Please choose some atomic digits text") syllable = None method = argots.GENERIC # Get obfuscating method. options = [(argots.JAVANAIS, "*javanais decyphering", ""), (argots.FEU, "langue de *feu", ""), (argots.GENERIC, "$generic", ""), (argots.LARGONJI, "or *largonji des loucherbèmes one", "")] method = ui.get_choice("Do you want to use", options, oneline=True) if method == argots.LARGONJI: # Get sets of suffix syllables. v_sylb = argots.LARGONJI_SYLLABLES_V s = ui.get_data("Suffix vowel-compliant syllables, comma " "separated (or nothing to use default " "'{}' ones): " "".format("', '".join(v_sylb)), allow_void=True) if s: v_sylb = [s.strip() for s in s.split(",")] c_sylb = argots.LARGONJI_SYLLABLES_C s = ui.get_data("Suffix consonant-compliant syllables, " "comma separated (or nothing to use " "default '{}' ones): " "".format("', '".join(c_sylb)), allow_void=True) if s: c_sylb = [s.strip() for s in s.split(",")] else: # Get obfuscating syllable. s = ui.get_data( "Obfuscating syllable (or nothing to search " "for the most common one): ", allow_void=True, validate=self._validate, validate_kwargs={'method': method}) if s: syllable = s try: if method == argots.LARGONJI: txt = argots.decypher_largonji(txt, v_sylb, c_sylb) txt = "\n " + \ "\n ".join(utils.format_multiwords(txt)) else: txt = argots.decypher(txt, method, syllable) if len(txt) > 1: txt = "\n " + \ "\n".join(["Using '{}':\n {}" "".format(t[0], t[1]) for t in txt]) elif txt: txt = txt[0][1] else: txt = "" ui.text_output("Text successfully decyphered", txt, "The decyphered text is") except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) options = [("redo", "*decypher another data", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ == "quit": return
def main(): # Treating direct script call with args # Args retrieval import argparse parser = argparse.ArgumentParser(description="" "Cypher/decypher a text according to " "“Argot Javanais”, “Langue de Feu”, " "or a generic version of those, i.e. by " "adding an obfuscating syllable between " "consonants and vowels.\n" "Example: 'Test' => 'Tavest'.\n") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") sparsers = parser.add_subparsers(dest="command") cparser = sparsers.add_parser('cypher', help="Cypher text.") cparser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to cypher.") cparser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the cyphered text.") cparser.add_argument('-d', '--data', help="The text to cypher.") cparser.add_argument('-s', '--syllable', help="Obfuscating syllable to insert in text.") cparser.add_argument('-j', '--javanais', action="store_true", help="Restrict allowed obfuscating syllables to ja, " "av and va (and their case variants).") cparser.add_argument('-f', '--feu', action="store_true", help="Restrict allowed obfuscating syllables to a f " "and a vowel (and their case variants).") cparser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. WARNING: with long words, it will " "take a *very* long time to compute (seconds " "with 20 chars word, and increasing at a high " "rate)!") cparser.add_argument('--cypher_goal', type=float, default=0.2, help="Minimum level of cyphering, if possible. Only " "relevant with --exhaustive! Note typical good " "cypher level is 0.3 (nbr of syllables added/" "nbr of chars, for each word), defaults to 0.2.") cparser.add_argument('-l', '--largonji', action="store_true", help="Use Largonji des Loucherbems cyphering.") cparser.add_argument('--vowel_syllables', nargs='*', default=LARGONJI_SYLLABLES_V, help="Largonji: syllables to add after a vowel, at " "the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_V))) cparser.add_argument('--consonant_syllables', nargs='*', default=LARGONJI_SYLLABLES_C, help="Largonji: syllables to add after a consonant, " "at the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_C))) dparser = sparsers.add_parser('decypher', help="Decypher text.") dparser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to decypher.") dparser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered text.") dparser.add_argument('-d', '--data', help="The text to decypher.") dparser.add_argument('-s', '--syllable', help="Obfuscating syllable to remove from text (if " "none given, the most common one compatible " "with choosen method will be used).") dparser.add_argument('-j', '--javanais', action="store_true", help="Restrict allowed obfuscating syllables to ja, " "av and va (and their case variants).") dparser.add_argument('-f', '--feu', action="store_true", help="Restrict allowed obfuscating syllables to a f " "and a vowel (and their case variants).") dparser.add_argument('-l', '--largonji', action="store_true", help="Use Largonji des Loucherbems decyphering.") dparser.add_argument('--vowel_syllables', nargs='*', default=LARGONJI_SYLLABLES_V, help="Largonji: syllables to search for vowels, at " "the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_V))) dparser.add_argument('--consonant_syllables', nargs='*', default=LARGONJI_SYLLABLES_C, help="Largonji: syllables to search for consonants, " "at the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_C))) sparsers.add_parser('about', help="About Argots…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() method = GENERIC syllable = args.syllable if args.javanais: method = JAVANAIS if not syllable: print("WARNING: No obfuscating syllable given, using " "'av' default one.") syllable = "av" if args.feu: method = FEU if not syllable: print("WARNING: No obfuscating syllable given, using " "'fe' default one.") syllable = "fe" if args.largonji: out = cypher_largonji(data, args.vowel_syllables, args.consonant_syllables) print("Largonji found {} solutions." "".format(out["n_solutions"])) print(out["solutions"]) text = "\n".join( utils.format_multiwords(out["solutions"], sep=" ")) btext = "" else: if args.syllable in data: print("WARNING: The chosen obfuscating syllable is " "already present in the text, decyphering will " "likely give wrong results…") out = cypher(data, method, args.syllable, args.exhaustive, args.cypher_goal) if args.exhaustive: print("Exhaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.cypher_goal, out["best_n_solutions"], out["best_cypher"])) text = "\n".join( utils.format_multiwords(out["solutions"], sep=" ")) b_text = "\n".join( utils.format_multiwords(out["best_solutions"], sep=" ")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if args.exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() method = GENERIC if args.javanais: method = JAVANAIS if args.feu: method = FEU if args.largonji: out = decypher_largonji(data, args.vowel_syllables, args.consonant_syllables) text = "\n".join(utils.format_multiwords(out)) else: out = decypher(data, method, args.syllable) text = "\n\n".join( ["Using '{}':\n {}" "".format(o[0], o[1]) for o in out]) if args.ofile: args.ofile.write(text) else: print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "about": print(__about__) return
def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") ui.message("--- Cyphering ---") ui.message("Data to cypher: {}".format("HOW ARE YOU NICEDAYISNTIT")) out = atomicdigits.cypher("HOW ARE YOU NICEDAYISNTIT") ui.message("Atomic digits cyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out, sep=" ")))) ui.message("") htext = "90 53 16 53 16 A Q 92 53 52 16 53 M 15 L E 52 16 T" ui.message("--- Decyphering ---") ui.message("Atomic digits text used as input: {}".format(htext)) out = atomicdigits.decypher(htext) ui.message("The decyphered data is:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Notes ---") ui.message("+ 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):") ui.message("Data to cypher: {}".format("HOW ARE YOU NICEDAYISNTIT")) out = atomicdigits.cypher("HOW ARE YOU NICEDAYISNTIT", exhaustive=True, min_cypher=0.8) out = self._get_exhaustive_txt(out, ui, min_cypher=0.8, act="all") ui.message(out) ui.message("") htext = "1874 A75 39892 75358DA39535081T" ui.message("+ You can try to decypher a text with atomic numbers " "merged (i.e. no more spaces between them – nasty!):") ui.message("Data to decypher: {}".format(htext)) out = atomicdigits.decypher(htext) ui.message("Atomic digits decyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Won’t work ---") ui.message("+ The input text to cypher must be ASCII uppercase " "chars only:") ui.message("Data to cypher: {}\n".format("Hello WORLD !")) try: out = atomicdigits.cypher("Hello WORLD !") ui.message("Atomic digits cyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) except Exception as e: ui.message(str(e), ui.ERROR) ui.message("") ui.message("+ The input text to decypher must be valid atomic digits:") htext = "90 53 016 53 16 A Q 922 53 52 16 53 M 15 L E 52 16 T" ui.message("Atomic digits text used as input: {}".format(htext)) try: out = atomicdigits.decypher(htext) ui.message("Atomic digits decyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) except Exception as e: ui.message(str(e), ui.ERROR) ui.message("") ui.get_choice("", [("", "Go back to *menu", "")], oneline=True)
def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") ui.message("--- Cyphering ---") ui.message("Data to cypher: {}".format("HOW ARE YOU NICEDAYISNTIT")) out = atomicdigits.cypher("HOW ARE YOU NICEDAYISNTIT") ui.message("Atomic digits cyphered data:\n {}" "".format("\n ".join( utils.format_multiwords(out, sep=" ")))) ui.message("") htext = "90 53 16 53 16 A Q 92 53 52 16 53 M 15 L E 52 16 T" ui.message("--- Decyphering ---") ui.message("Atomic digits text used as input: {}".format(htext)) out = atomicdigits.decypher(htext) ui.message("The decyphered data is:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Notes ---") ui.message("+ 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):") ui.message("Data to cypher: {}".format("HOW ARE YOU NICEDAYISNTIT")) out = atomicdigits.cypher("HOW ARE YOU NICEDAYISNTIT", exhaustive=True, min_cypher=0.8) out = self._get_exhaustive_txt(out, ui, min_cypher=0.8, act="all") ui.message(out) ui.message("") htext = "1874 A75 39892 75358DA39535081T" ui.message("+ You can try to decypher a text with atomic numbers " "merged (i.e. no more spaces between them – nasty!):") ui.message("Data to decypher: {}".format(htext)) out = atomicdigits.decypher(htext) ui.message("Atomic digits decyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) ui.message("") ui.message("--- Won’t work ---") ui.message("+ The input text to cypher must be ASCII uppercase " "chars only:") ui.message("Data to cypher: {}\n".format("Hello WORLD !")) try: out = atomicdigits.cypher("Hello WORLD !") ui.message("Atomic digits cyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) except Exception as e: ui.message(str(e), level=ui.ERROR) ui.message("") ui.message("+ The input text to decypher must be valid atomic digits:") htext = "90 53 016 53 16 A Q 922 53 52 16 53 M 15 L E 52 16 T" ui.message("Atomic digits text used as input: {}".format(htext)) try: out = atomicdigits.decypher(htext) ui.message("Atomic digits decyphered data:\n {}" "".format("\n ".join(utils.format_multiwords(out)))) except Exception as e: ui.message(str(e), level=ui.ERROR) ui.message("") ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
def main(): # The argparse is much nicer than directly using sys.argv... # Try 'program.py -h' to see! ;) import argparse parser = argparse.ArgumentParser(description="Cypher/decypher some text " "in morse or Wabun code.") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") sparsers = parser.add_subparsers(dest="command") cypher_parser = sparsers.add_parser('cypher', help="Cypher data.", description="Cypher some text in " "morse or Wabun code.") cypher_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to cypher.") cypher_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the cyphered " "text.") cypher_parser.add_argument('-d', '--data', help="The text to cypher.") cypher_parser.add_argument('-w', '--wabun', action="store_const", const=WABUN, default=INTER, dest="method", help="Use Wabun instead of international code.") cypher_parser.add_argument('-f', '--fast', action="store_const", const=FAST, default=STANDARD, dest="variant", help="Use fast morse/Wabun variant, instead of " " standard one.") cypher_parser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. Only relevant with --wabun. " "WARNING: with long words, it " "will take a *very* long time to compute " "(seconds with 20 chars word, and " "increasing at a high rate)!") cypher_parser.add_argument('--min_cypher', type=float, default=0.7, help="Minimum level of cyphering, if possible. " "Only relevant with --exhaustive!" "Note typical good cypher level is 0.7 " "(nbr of chars/nbr of cyphered chars, " "for each word), defaults to 0.7.") decypher_parser = sparsers.add_parser('decypher', help="Text to Decypher.", description="Decypher some text " "from morse or Wabun " "code (auto-detected " "by default).") decypher_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to " "decypher.") decypher_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered " "text.") decypher_parser.add_argument('-d', '--data', help="The text to decypher.") decypher_parser.add_argument('-n', '--international', action="store_const", const=INTER, default=None, help="Force international decyphering " "(default: auto detection).") decypher_parser.add_argument('-w', '--wabun', action="store_const", const=WABUN, default=None, dest="method", help="Force Wabun decyphering (default: " "auto detection).") sparsers.add_parser('about', help="About Morse|Wabun…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() exhaustive = args.exhaustive and args.method == WABUN out = cypher(data, args.method, args.variant, exhaustive, args.min_cypher) if exhaustive: print("Exaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.min_cypher, out["best_n_solutions"], out["best_cypher"])) text = "\n".join(utils.format_multiwords(out["solutions"], sep="")) b_text = \ "\n".join(utils.format_multiwords(out["best_solutions"], sep="")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() out = decypher(data, args.method) if args.ofile: args.ofile.write(out) else: print(out) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "about": print(__about__) return
def main(): # The argparse is much nicer than directly using sys.argv... # Try 'program.py -h' to see! ;) import argparse parser = argparse.ArgumentParser(description="" "Encrypt/decypher some text in " "atomic digits code.") sparsers = parser.add_subparsers(dest="command") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") hide_parser = sparsers.add_parser('cypher', help="Encryptcode text in " "atomic digits.") hide_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to convert to " "atomic digits.") hide_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the atomic digits " "text.") hide_parser.add_argument('-d', '--data', help="The text to cypher in atomic digits.") hide_parser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. WARNING: with long words, it " "will take a *very* long time to compute " "(seconds with 20 chars word, and " "increasing at a high rate)!") hide_parser.add_argument('--min_cypher', type=float, default=0.8, help="Minimum level of cyphering, if possible. " "Only relevant with --exhaustive, defaults " "to 0.8!") unhide_parser = sparsers.add_parser('decypher', help="Decypher atomic digits to text.") unhide_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to convert " "from atomic digits.") unhide_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered " "text.") unhide_parser.add_argument('-d', '--data', help="The text to decypher.") sparsers.add_parser('about', help="About AtomicDigits…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() out = cypher(data, exhaustive=args.exhaustive, min_cypher=args.min_cypher) if args.exhaustive: print("Exaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.min_cypher, out["best_n_solutions"], out["best_cypher"])) text = "\n".join(utils.format_multiwords(out["solutions"], sep=" ")) b_text = \ "\n".join(utils.format_multiwords(out["best_solutions"], sep=" ")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if args.exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return 0 elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() out = decypher(data) text = "\n".join(utils.format_multiwords(out)) if args.ofile: args.ofile.write(text) else: print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return 0 elif args.command == "about": print(__about__) return
def main(): # The argparse is much nicer than directly using sys.argv... # Try 'program.py -h' to see! ;) import argparse parser = argparse.ArgumentParser(description="" "Encrypt/decypher some text in " "atomic digits code.") sparsers = parser.add_subparsers(dest="command") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") hide_parser = sparsers.add_parser('cypher', help="Encryptcode text in " "atomic digits.") hide_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to convert to " "atomic digits.") hide_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the atomic digits " "text.") hide_parser.add_argument('-d', '--data', help="The text to cypher in atomic digits.") hide_parser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. WARNING: with long words, it " "will take a *very* long time to compute " "(seconds with 20 chars word, and " "increasing at a high rate)!") hide_parser.add_argument('--min_cypher', type=float, default=0.8, help="Minimum level of cyphering, if possible. " "Only relevant with --exhaustive, defaults " "to 0.8!") unhide_parser = sparsers.add_parser('decypher', help="Decypher atomic digits to text.") unhide_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to convert " "from atomic digits.") unhide_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered " "text.") unhide_parser.add_argument('-d', '--data', help="The text to decypher.") sparsers.add_parser('about', help="About AtomicDigits…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() out = cypher(data, exhaustive=args.exhaustive, min_cypher=args.min_cypher) if args.exhaustive: print("Exaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.min_cypher, out["best_n_solutions"], out["best_cypher"])) text = "\n".join( utils.format_multiwords(out["solutions"], sep=" ")) b_text = \ "\n".join(utils.format_multiwords(out["best_solutions"], sep=" ")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if args.exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return 0 elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() out = decypher(data) text = "\n".join(utils.format_multiwords(out)) if args.ofile: args.ofile.write(text) else: print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return 0 elif args.command == "about": print(__about__) return
def main(): # The argparse is much nicer than directly using sys.argv... # Try 'program.py -h' to see! ;) import argparse parser = argparse.ArgumentParser(description="Cypher/decypher some text " "in morse or Wabun code.") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") sparsers = parser.add_subparsers(dest="command") cypher_parser = sparsers.add_parser('cypher', help="Cypher data.", description="Cypher some text in " "morse or Wabun code.") cypher_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to cypher.") cypher_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the cyphered " "text.") cypher_parser.add_argument('-d', '--data', help="The text to cypher.") cypher_parser.add_argument('-w', '--wabun', action="store_const", const=WABUN, default=INTER, dest="method", help="Use Wabun instead of international code.") cypher_parser.add_argument('-f', '--fast', action="store_const", const=FAST, default=STANDARD, dest="variant", help="Use fast morse/Wabun variant, instead of " " standard one.") cypher_parser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. Only relevant with --wabun. " "WARNING: with long words, it " "will take a *very* long time to compute " "(seconds with 20 chars word, and " "increasing at a high rate)!") cypher_parser.add_argument('--min_cypher', type=float, default=0.7, help="Minimum level of cyphering, if possible. " "Only relevant with --exhaustive!" "Note typical good cypher level is 0.7 " "(nbr of chars/nbr of cyphered chars, " "for each word), defaults to 0.7.") decypher_parser = sparsers.add_parser('decypher', help="Text to Decypher.", description="Decypher some text " "from morse or Wabun " "code (auto-detected " "by default).") decypher_parser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to " "decypher.") decypher_parser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered " "text.") decypher_parser.add_argument('-d', '--data', help="The text to decypher.") decypher_parser.add_argument('-n', '--international', action="store_const", const=INTER, default=None, help="Force international decyphering " "(default: auto detection).") decypher_parser.add_argument('-w', '--wabun', action="store_const", const=WABUN, default=None, dest="method", help="Force Wabun decyphering (default: " "auto detection).") sparsers.add_parser('about', help="About Morse|Wabun…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() exhaustive = args.exhaustive and args.method == WABUN out = cypher(data, args.method, args.variant, exhaustive, args.min_cypher) if exhaustive: print("Exaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.min_cypher, out["best_n_solutions"], out["best_cypher"])) text = "\n".join( utils.format_multiwords(out["solutions"], sep="")) b_text = \ "\n".join(utils.format_multiwords(out["best_solutions"], sep="")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() out = decypher(data, args.method) if args.ofile: args.ofile.write(out) else: print(out) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "about": print(__about__) return
def main(): # Treating direct script call with args # Args retrieval import argparse parser = argparse.ArgumentParser(description="" "Cypher/decypher a text according to " "“Argot Javanais”, “Langue de Feu”, " "or a generic version of those, i.e. by " "adding an obfuscating syllable between " "consonants and vowels.\n" "Example: 'Test' => 'Tavest'.\n") parser.add_argument('--debug', action="store_true", default=False, help="Enable debug mode.") sparsers = parser.add_subparsers(dest="command") cparser = sparsers.add_parser('cypher', help="Cypher text.") cparser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to cypher.") cparser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the cyphered text.") cparser.add_argument('-d', '--data', help="The text to cypher.") cparser.add_argument('-s', '--syllable', help="Obfuscating syllable to insert in text.") cparser.add_argument('-j', '--javanais', action="store_true", help="Restrict allowed obfuscating syllables to ja, " "av and va (and their case variants).") cparser.add_argument('-f', '--feu', action="store_true", help="Restrict allowed obfuscating syllables to a f " "and a vowel (and their case variants).") cparser.add_argument('--exhaustive', action="store_true", help="Use a complete search of all possible " "cypherings. WARNING: with long words, it will " "take a *very* long time to compute (seconds " "with 20 chars word, and increasing at a high " "rate)!") cparser.add_argument('--cypher_goal', type=float, default=0.2, help="Minimum level of cyphering, if possible. Only " "relevant with --exhaustive! Note typical good " "cypher level is 0.3 (nbr of syllables added/" "nbr of chars, for each word), defaults to 0.2.") cparser.add_argument('-l', '--largonji', action="store_true", help="Use Largonji des Loucherbems cyphering.") cparser.add_argument('--vowel_syllables', nargs='*', default=LARGONJI_SYLLABLES_V, help="Largonji: syllables to add after a vowel, at " "the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_V))) cparser.add_argument('--consonant_syllables', nargs='*', default=LARGONJI_SYLLABLES_C, help="Largonji: syllables to add after a consonant, " "at the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_C))) dparser = sparsers.add_parser('decypher', help="Decypher text.") dparser.add_argument('-i', '--ifile', type=argparse.FileType('r'), help="A file containing the text to decypher.") dparser.add_argument('-o', '--ofile', type=argparse.FileType('w'), help="A file into which write the decyphered text.") dparser.add_argument('-d', '--data', help="The text to decypher.") dparser.add_argument('-s', '--syllable', help="Obfuscating syllable to remove from text (if " "none given, the most common one compatible " "with choosen method will be used).") dparser.add_argument('-j', '--javanais', action="store_true", help="Restrict allowed obfuscating syllables to ja, " "av and va (and their case variants).") dparser.add_argument('-f', '--feu', action="store_true", help="Restrict allowed obfuscating syllables to a f " "and a vowel (and their case variants).") dparser.add_argument('-l', '--largonji', action="store_true", help="Use Largonji des Loucherbems decyphering.") dparser.add_argument('--vowel_syllables', nargs='*', default=LARGONJI_SYLLABLES_V, help="Largonji: syllables to search for vowels, at " "the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_V))) dparser.add_argument('--consonant_syllables', nargs='*', default=LARGONJI_SYLLABLES_C, help="Largonji: syllables to search for consonants, " "at the end of words (defaults to '{}')." "".format("', '".join(LARGONJI_SYLLABLES_C))) sparsers.add_parser('about', help="About Argots…") args = parser.parse_args() utils.DEBUG = args.debug if args.command == "cypher": try: data = args.data if args.ifile: data = args.ifile.read() method = GENERIC syllable = args.syllable if args.javanais: method = JAVANAIS if not syllable: print("WARNING: No obfuscating syllable given, using " "'av' default one.") syllable = "av" if args.feu: method = FEU if not syllable: print("WARNING: No obfuscating syllable given, using " "'fe' default one.") syllable = "fe" if args.largonji: out = cypher_largonji(data, args.vowel_syllables, args.consonant_syllables) print("Largonji found {} solutions." "".format(out["n_solutions"])) print(out["solutions"]) text = "\n".join(utils.format_multiwords(out["solutions"], sep=" ")) btext = "" else: if args.syllable in data: print("WARNING: The chosen obfuscating syllable is " "already present in the text, decyphering will " "likely give wrong results…") out = cypher(data, method, args.syllable, args.exhaustive, args.cypher_goal) if args.exhaustive: print("Exhaustive found {} solutions for a minimum " "cyphering of {}, among which {} solutions with the " "highest possible cyphering ({}):" "".format(out["n_solutions"], args.cypher_goal, out["best_n_solutions"], out["best_cypher"])) text = "\n".join(utils.format_multiwords(out["solutions"], sep=" ")) b_text = "\n".join(utils.format_multiwords( out["best_solutions"], sep=" ")) else: text = out b_text = "" if args.ofile: args.ofile.write(text) if b_text: args.ofile.write("\n\n") args.ofile.write(b_text) else: if args.exhaustive: print("Best solutions:") print(b_text) print("\nAll solutions:") print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "decypher": try: data = args.data if args.ifile: data = args.ifile.read() method = GENERIC if args.javanais: method = JAVANAIS if args.feu: method = FEU if args.largonji: out = decypher_largonji(data, args.vowel_syllables, args.consonant_syllables) text = "\n".join(utils.format_multiwords(out)) else: out = decypher(data, method, args.syllable) text = "\n\n".join(["Using '{}':\n {}" "".format(o[0], o[1]) for o in out]) if args.ofile: args.ofile.write(text) else: print(text) except Exception as e: if utils.DEBUG: raise e print(e, "\n\n") finally: if args.ifile: args.ifile.close() if args.ofile: args.ofile.close() return elif args.command == "about": print(__about__) return