This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
forked from savaga/mcm-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy2mcm.py
82 lines (72 loc) · 2.14 KB
/
copy2mcm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pprint import pprint, pformat
import pdb
import sys, getopt
import contextlib
@contextlib.contextmanager
def smart_open(filename=None):
if filename and filename != '-':
fh = open(filename, 'w')
else:
fh = sys.stdout
try:
yield fh
finally:
if fh is not sys.stdout:
fh.close()
def print_help(name):
print '%s -i <inputfile.mcm> -o <outputfile.mcm> -f <from pos> -t <to pos> -c <count>' % name
def read_font(filename):
data = []
with open(filename, "r") as ih:
lines = [line.rstrip('\r\n') for line in ih]
if lines[0] == "MAX7456":
char_array = []
char_lines = 0
for line in lines[1:]:
if line == "":
break
char_array.append(line)
char_lines += 1
if (char_lines == 64):
data.append(list(char_array))
char_array = []
char_lines = 0
print "Read %s chars from %s" % (len(data), filename)
return list(data)
inputfile = None
outputfile = None
copy_from = 0
copy_to = 0
chars_num = 0
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:o:f:t:c:",["ifile=","ofile=","from=","to=","count="])
except getopt.GetoptError:
print_help(sys.argv[0])
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_help(sys.argv[0])
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-f", "--from"):
copy_from = int(arg, 0)
elif opt in ("-t", "--to"):
copy_to = int(arg, 0)
elif opt in ("-c", "--count"):
chars_num = int(arg, 0)
if inputfile is None:
print_help(sys.argv[0])
sys.exit(1)
current_char = 0
idata = read_font(inputfile)
odata = read_font(outputfile)
for i in xrange(chars_num):
odata[copy_to + i] = idata[copy_from + i]
with smart_open(outputfile) as fh:
print >>fh, "MAX7456"
for char in odata:
for line in char:
print >>fh, line