eines vorab: Ich kenne mich mit Python nicht aus.
Ich habe mir gestern ein Base64-Script (b64.py -- GitHub) heruntergeladen und so modifiziert, dass es die Zeilen nach 64 bzw. 76 Zeichen umbricht. Auch meine urlsafe-Variante funktioniert. Allerdings habe ich jetzt sechs Skripte. Ich würde die gerne zusammenfassen, indem ich das ursprüngliche Skript um die Optionen "urlsafe" und "line length" erweitere. Das Skript muss unter Python 2.7 funktionieren, weil ich die Python-Version, die im Adobe Font Development Kit for OpenType enthalten ist, anspreche.
Wahrscheinlich bekäme ich das irgendwann auch selbst hin. Aber es würde Tage dauern. Vielleicht kann mir jemand dabei helfen.
Edit: Die Zeilenumbrüche sind wegen der UNIX-Kodierung in Notepad übrigens nicht sichtbar, in UltraEdit aber schon. Was ist denn die Windows- bzw. DOS-Entsprechung für "/n"?
Das ursprüngliche Skript:
Code: Alles auswählen
import sys
import argparse
import base64
parser = argparse.ArgumentParser(description='Encode and Decode base64.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encode', action='store_const', const=base64.standard_b64encode, dest='func')
group.add_argument('-d', '--decode', action='store_const', const=base64.standard_b64decode, dest='func')
parser.add_argument('-i', '--infile', dest='infile', metavar='INFILE', help='Defaults to stdin')
parser.add_argument('-o', '--outfile', dest='outfile', metavar='OUTFILE', help='Defaults to stdout')
parser.add_argument('args', nargs='*')
opts = parser.parse_args()
if (opts.infile == None):
instr = ' '.join(opts.args).encode()
else:
with open(opts.infile, 'rb') as ifh:
instr = ifh.read()
outstr = opts.func(instr)
if (opts.outfile == None):
print(outstr)
else:
with open(opts.outfile, 'wb') as ofh:
ofh.write(outstr)
Code: Alles auswählen
import sys
import argparse
import base64
parser = argparse.ArgumentParser(description='Encode and Decode base64.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encode', action='store_const', const=base64.urlsafe_b64encode, dest='func')
group.add_argument('-d', '--decode', action='store_const', const=base64.urlsafe_b64decode, dest='func')
parser.add_argument('-i', '--infile', dest='infile', metavar='INFILE', help='Defaults to stdin')
parser.add_argument('-o', '--outfile', dest='outfile', metavar='OUTFILE', help='Defaults to stdout')
parser.add_argument('args', nargs='*')
opts = parser.parse_args()
if (opts.infile == None):
instr = ' '.join(opts.args).encode()
else:
with open(opts.infile, 'rb') as ifh:
instr = ifh.read()
outstr = opts.func(instr)
if (opts.outfile == None):
print(outstr)
else:
with open(opts.outfile, 'wb') as ofh:
ofh.write(outstr)
Code: Alles auswählen
import sys
import argparse
import base64
parser = argparse.ArgumentParser(description='Encode and Decode base64.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encode', action='store_const', const=base64.standard_b64encode, dest='func')
group.add_argument('-d', '--decode', action='store_const', const=base64.standard_b64decode, dest='func')
parser.add_argument('-i', '--infile', dest='infile', metavar='INFILE', help='Defaults to stdin')
parser.add_argument('-o', '--outfile', dest='outfile', metavar='OUTFILE', help='Defaults to stdout')
parser.add_argument('args', nargs='*')
opts = parser.parse_args()
def insert_newlines(string, every=64):
return '\n'.join(string[i:i+every] for i in xrange(0, len(string), every))
if (opts.infile == None):
instr = ' '.join(opts.args).encode()
else:
with open(opts.infile, 'rb') as ifh:
instr = ifh.read()
outstr = opts.func(instr)
if (opts.outfile == None):
print(insert_newlines(outstr))
else:
with open(opts.outfile, 'wb') as ofh:
ofh.write(insert_newlines(outstr))