diff options
| author | Michal Idziorek <m.i@gmx.at> | 2014-12-17 12:55:50 +0100 |
|---|---|---|
| committer | Michal Idziorek <m.i@gmx.at> | 2014-12-17 12:55:50 +0100 |
| commit | 0a9f1b1fb19ea7e0c54c884b7ae8c709ea738d1f (patch) | |
| tree | d0a1938b1d2d0c70683769d4eb7fa53644062e36 | |
| parent | f4518cf996c137e18cd70dd2d23ba9d8cfcfb526 (diff) | |
playing with python3
| -rw-r--r-- | font/Makefile | 2 | ||||
| -rw-r--r-- | font/binarize.py | 99 |
2 files changed, 74 insertions, 27 deletions
diff --git a/font/Makefile b/font/Makefile index f758bfd..2eeaf37 100644 --- a/font/Makefile +++ b/font/Makefile @@ -1,5 +1,5 @@ ############ fool-font ############ binfont.bin: binfont.src - python binarize.py $< $@ + python3.2 binarize.py $< $@ clean: -rm binfont.bin diff --git a/font/binarize.py b/font/binarize.py index 6dcbdef..1abb9d6 100644 --- a/font/binarize.py +++ b/font/binarize.py @@ -1,40 +1,87 @@ -# this is a simple script to convert the ascii files into binaries -# everything but 0 an 1 , or alternatively _ and X is ignored in -# source file. - -import binascii import sys +def binarize(file_in, file_out): + + """ Create a binary file from an ASCII file. Everything + but '0' and '1' (alternatively '_' and 'X') is ignored + from the input file. """ + + print ("binarizing " + file_in + " to "+ file_out + ".") + + try: + + f=open(file_in,'r') + + try: + + o=open(file_out,'wb') + + try: + + while True: + + b=readnext(f) + if b==-1: + break + + o.write(bytes([b])) + + except IOError: + print("IOError happened during processing."); + print("I am closing the input and ouput files and aborting."); + print("Warning: The output file is very likely incomplete."); + + o.close() + + except IOError: + print("IOError opening output file: " + file_out); + + f.close() + + except IOError: + print("IOError opening input file: " + file_in); + + +def readnext(f): + + """ Read next 'byte' from the given ASCII file (BufferedWriter) + everything but '_','X','0','1' is ignored. + Returns the byte as integer or -1 on EOF """ + + l=0 + b="" + + while l<8: + + c=f.read(1) + + if not c: + return -1 -f=open(sys.argv[1],'r') -o=open(sys.argv[2],'wb') + if c=="0" or c=='_': + l+=1 + b+="0" -print "binarizing " + sys.argv[1] + " to "+sys.argv[2] + "." + if c=="1" or c=='X': + l+=1 + b+="1" -b="" -l=0 + return int(b,2) -while True: +def usage(): - c=f.read(1) + """ Prints usage information to sdtout """ - if not c: - break + print ("python3.x binarize.py [file_in] [file_out]") - if c=="0" or c=='_': - l+=1 - b+="0" - if c=="1" or c=='X': - l+=1 - b+="1" - if l==8: -# print b - l=0 - o.write(chr(int(b,2))) - b="" +""" direct use """ -o.close() +if __name__ == "__main__": + if(len(sys.argv)==3): + binarize(sys.argv[1],sys.argv[2]) + else: + usage() |
