@@ -41,31 +41,43 @@ def main():
4141 "\n This tool allows to encode/decode input strings/files with an extended set of codecs.\n \n " \
4242 .format (__version__ , __author__ , __email__ , __copyright__ , __license__ , __source__ )
4343 examples = "usage examples:\n - " + "\n - " .join ([
44- "codext --search bitcoin" ,
45- "codext -d base32 -i file.b32" ,
46- "codext morse < to_be_encoded.txt" ,
47- "echo \" test\" | codext base100" ,
48- "echo -en \" test\" | codext braille -o test.braille" ,
49- "codext base64 < to_be_encoded.txt > text.b64" ,
50- "echo -en \" test\" | codext base64 | codext base32" ,
51- "echo -en \" mrdvm6teie6t2cq=\" | codext upper | codext -d base32 | codext -d base64" ,
52- "echo -en \" test\" | codext upper reverse base32 | codext -d base32 reverse lower" ,
53- "echo -en \" test\" | codext upper reverse base32 base64 morse" ,
44+ "codext search bitcoin" ,
45+ "codext decode base32 -i file.b32" ,
46+ "codext encode morse < to_be_encoded.txt" ,
47+ "echo \" test\" | codext encode base100" ,
48+ "echo -en \" test\" | codext encode braille -o test.braille" ,
49+ "codext encode base64 < to_be_encoded.txt > text.b64" ,
50+ "echo -en \" test\" | codext encode base64 | codext encode base32" ,
51+ "echo -en \" mrdvm6teie6t2cq=\" | codext encode upper | codext decode base32 | codext decode base64" ,
52+ "echo -en \" test\" | codext encode upper reverse base32 | codext decode base32 reverse lower" ,
53+ "echo -en \" test\" | codext encode upper reverse base32 base64 morse" ,
54+ "echo -en \" test\" | codext encode base64 gzip | codext guess" ,
55+ "echo -en \" test\" | codext encode base64 gzip | codext guess gzip" ,
5456 ])
5557 parser = argparse .ArgumentParser (description = descr , epilog = examples , formatter_class = argparse .RawTextHelpFormatter )
56- parser .add_argument ("encoding" , nargs = "+" , help = "list of encodings to apply" )
57- parser .add_argument ("-d" , "--decode" , action = "store_true" , help = "set decode mode" )
58- parser .add_argument ("-e" , "--errors" , default = "strict" , choices = ["ignore" , "leave" , "replace" , "strict" ],
59- help = "error handling" )
58+ sparsers = parser .add_subparsers (dest = "command" , help = "command to be executed" )
6059 parser .add_argument ("-i" , "--input-file" , dest = "infile" , help = "input file (if none, take stdin as input)" )
6160 parser .add_argument ("-o" , "--output-file" , dest = "outfile" , help = "output file (if none, display result to stdout)" )
6261 parser .add_argument ("-s" , "--strip-newlines" , action = "store_true" , dest = "strip" , help = "strip newlines from input" )
63- parser .add_argument ("--search" , action = "store_true" , help = "search for encoding names" )
62+ encode = sparsers .add_parser ("encode" , help = "encode input using the specified codecs" )
63+ encode .add_argument ("encoding" , nargs = "+" , help = "list of encodings to apply" )
64+ encode .add_argument ("-e" , "--errors" , default = "strict" , choices = ["ignore" , "leave" , "replace" , "strict" ],
65+ help = "error handling" )
66+ decode = sparsers .add_parser ("decode" , help = "decode input using the specified codecs" )
67+ decode .add_argument ("encoding" , nargs = "+" , help = "list of encodings to apply" )
68+ decode .add_argument ("-e" , "--errors" , default = "strict" , choices = ["ignore" , "leave" , "replace" , "strict" ],
69+ help = "error handling" )
70+ guess = sparsers .add_parser ("guess" , help = "try guessing the decoding codecs" )
71+ guess .add_argument ("encoding" , nargs = "*" , help = "list of known encodings to apply" )
72+ guess .add_argument ("-c" , "--category" , choices = list_categories (), nargs = "*" , help = "codec categories to search in" )
73+ guess .add_argument ("-d" , "--depth" , default = 3 , type = int , help = "maximum codec search depth" )
74+ search = sparsers .add_parser ("search" , help = "search for codecs" )
75+ search .add_argument ("pattern" , nargs = "+" , help = "encoding pattern to search" )
6476 args = parser .parse_args ()
6577 # if a search pattern is given, only handle it
66- if args .search :
78+ if args .command == " search" :
6779 results = []
68- for enc in args .encoding :
80+ for enc in args .pattern :
6981 results .extend (codecs .search (enc ))
7082 print (", " .join (results ) or "No encoding found" )
7183 return
@@ -79,18 +91,18 @@ def main():
7991 c += line
8092 if args .strip :
8193 c = re .sub (r"\r?\n" , "" , c )
82- # encode or decode
83- for encoding in args .encoding :
84- c = getattr (codecs , ["encode" , "decode" ][args .decode ])(c , encoding , args .errors )
94+ if args .command in ["decode" , "encode" ]:
95+ # encode or decode
96+ for encoding in args .encoding :
97+ c = getattr (codecs , ["encode" , "decode" ][args .command == "decode" ])(c , encoding , args .errors )
98+ elif args .command == "guess" :
99+ c , e = codecs .guess (c , max_depth = args .depth , codec_categories = args .category , found = args .encoding )
100+ if e :
101+ print ("Encodings: %s" % ", " .join (e ))
85102 # handle output file or stdout
86103 if args .outfile :
87104 with open (args .outfile , 'wb' ) as f :
88105 f .write (c )
89106 else :
90- if PY3 :
91- try :
92- c = c .decode ("utf-8" )
93- except :
94- c = c .decode ("latin-1" )
95- print (c , end = "" )
107+ print (ensure_str (c ), end = "" )
96108
0 commit comments