|
20 | 20 | import sys
|
21 | 21 |
|
22 | 22 | class File(object):
|
23 |
| - def __init__(self,directory,subdir,fileName,mode): |
24 |
| - self.directory = directory |
25 |
| - self.subdir = subdir |
26 |
| - self.fileName = fileName |
27 |
| - filePath = join(directory,subdir,fileName) |
28 |
| - self.fileSize = stat(filePath).st_size |
29 |
| - self.bareName = fileName.replace('.','_') |
30 |
| - self.inputFile = open(filePath,mode) |
| 23 | + def __init__(self,directory,subdir,fileName,mode): |
| 24 | + self.directory = directory |
| 25 | + self.subdir = subdir |
| 26 | + self.fileName = fileName |
| 27 | + filePath = join(directory,subdir,fileName) |
| 28 | + self.fileSize = stat(filePath).st_size |
| 29 | + self.bareName = fileName.replace('.','_') |
| 30 | + self.inputFile = open(filePath,mode) |
31 | 31 |
|
32 |
| - def formatByte(self,byte): |
33 |
| - if isinstance(byte,int): |
34 |
| - return "%02x" % byte |
35 |
| - else: |
36 |
| - return format(ord(byte),'02x') |
| 32 | + def formatByte(self,byte): |
| 33 | + if isinstance(byte,int): |
| 34 | + return "%02x" % byte |
| 35 | + else: |
| 36 | + return format(ord(byte),'02x') |
37 | 37 |
|
38 |
| - def writeBytes(self,data,outputFile): |
39 |
| - bytes_per_line=16 |
40 |
| - print("static const unsigned char %s_data[] = " % self.bareName,file=outputFile) |
41 |
| - print("{",file=outputFile) |
42 |
| - lines = [data[x:x+bytes_per_line] for x in range(0,len(data),bytes_per_line)] |
43 |
| - linesAsString = ',\n '.join([', '.join(['0x'+self.formatByte(byte) for byte in line]) for line in lines]) |
44 |
| - print(' %s' % linesAsString,file=outputFile) |
45 |
| - print("};",file=outputFile) |
46 |
| - print("const int %s_len = %d;\n" % (self.bareName,len(data)),file=outputFile) |
| 38 | + def writeBytes(self,data,outputFile): |
| 39 | + bytes_per_line=16 |
| 40 | + print("static const unsigned char %s_data[] = " % self.bareName,file=outputFile) |
| 41 | + print("{",file=outputFile) |
| 42 | + lines = [data[x:x+bytes_per_line] for x in range(0,len(data),bytes_per_line)] |
| 43 | + linesAsString = ',\n '.join([', '.join(['0x'+self.formatByte(byte) for byte in line]) for line in lines]) |
| 44 | + print(' %s' % linesAsString,file=outputFile) |
| 45 | + print("};",file=outputFile) |
| 46 | + print("const int %s_len = %d;\n" % (self.bareName,len(data)),file=outputFile) |
47 | 47 |
|
48 |
| - def convertToBytes(self,outputFile): |
49 |
| - lines = [x for x in self.inputFile.readlines() if not x.startswith('#')] |
50 |
| - w,h = (int(x) for x in lines[0].split()) |
51 |
| - data = "".join(map(chr,[int(w>>8),int(w&0xFF),int(h>>8),int(h&0xFF)]+ |
52 |
| - [int(x) for line in lines[1:] for x in line.split()])) |
53 |
| - self.writeBytes(data,outputFile) |
| 48 | + def convertToBytes(self,outputFile): |
| 49 | + lines = [x for x in self.inputFile.readlines() if not x.startswith('#')] |
| 50 | + w,h = (int(x) for x in lines[0].split()) |
| 51 | + data = "".join(map(chr,[int(w>>8),int(w&0xFF),int(h>>8),int(h&0xFF)]+ |
| 52 | + [int(x) for line in lines[1:] for x in line.split()])) |
| 53 | + self.writeBytes(data,outputFile) |
54 | 54 |
|
55 |
| - @staticmethod |
56 |
| - def factory(directory,subdir,fname): |
57 |
| - ext = splitext(fname)[1] |
58 |
| - if ext=='.lum': return LumFile(directory,subdir,fname) |
59 |
| - if ext=='.luma': return LumaFile(directory,subdir,fname) |
60 |
| - if ext=='.css': return CSSFile(directory,subdir,fname) |
61 |
| - if ext=='.svg': return SVGFile(directory,subdir,fname) |
62 |
| - return VerbatimFile(directory,subdir,fname) |
| 55 | + @staticmethod |
| 56 | + def factory(directory,subdir,fname): |
| 57 | + ext = splitext(fname)[1] |
| 58 | + if ext=='.lum': return LumFile(directory,subdir,fname) |
| 59 | + if ext=='.luma': return LumaFile(directory,subdir,fname) |
| 60 | + if ext=='.css': return CSSFile(directory,subdir,fname) |
| 61 | + if ext=='.svg': return SVGFile(directory,subdir,fname) |
| 62 | + return VerbatimFile(directory,subdir,fname) |
63 | 63 |
|
64 | 64 | class VerbatimFile(File):
|
65 |
| - def __init__(self,directory,subdir,fileName): |
66 |
| - File.__init__(self,directory,subdir,fileName,"rb") |
67 |
| - def writeContents(self,outputFile): |
68 |
| - self.writeBytes(self.inputFile.read(),outputFile) |
69 |
| - def writeDirEntry(self,outputFile): |
70 |
| - print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Verbatim }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
| 65 | + def __init__(self,directory,subdir,fileName): |
| 66 | + File.__init__(self,directory,subdir,fileName,"rb") |
| 67 | + def writeContents(self,outputFile): |
| 68 | + self.writeBytes(self.inputFile.read(),outputFile) |
| 69 | + def writeDirEntry(self,outputFile): |
| 70 | + print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Verbatim }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
71 | 71 |
|
72 | 72 | class CSSFile(File):
|
73 |
| - def __init__(self,directory,subdir,fileName): |
74 |
| - File.__init__(self,directory,subdir,fileName,"r") |
75 |
| - def writeContents(self,outputFile): |
76 |
| - self.writeBytes(self.inputFile.read(),outputFile) |
77 |
| - def writeDirEntry(self,outputFile): |
78 |
| - print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::CSS }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
| 73 | + def __init__(self,directory,subdir,fileName): |
| 74 | + File.__init__(self,directory,subdir,fileName,"r") |
| 75 | + def writeContents(self,outputFile): |
| 76 | + self.writeBytes(self.inputFile.read(),outputFile) |
| 77 | + def writeDirEntry(self,outputFile): |
| 78 | + print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::CSS }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
79 | 79 |
|
80 | 80 | class SVGFile(File):
|
81 |
| - def __init__(self,directory,subdir,fileName): |
82 |
| - File.__init__(self,directory,subdir,fileName,"r") |
83 |
| - def writeContents(self,outputFile): |
84 |
| - self.writeBytes(self.inputFile.read(),outputFile) |
85 |
| - def writeDirEntry(self,outputFile): |
86 |
| - print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::SVG }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
| 81 | + def __init__(self,directory,subdir,fileName): |
| 82 | + File.__init__(self,directory,subdir,fileName,"r") |
| 83 | + def writeContents(self,outputFile): |
| 84 | + self.writeBytes(self.inputFile.read(),outputFile) |
| 85 | + def writeDirEntry(self,outputFile): |
| 86 | + print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::SVG }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
87 | 87 |
|
88 | 88 | class LumFile(File):
|
89 |
| - def __init__(self,directory,subdir,fileName): |
90 |
| - File.__init__(self,directory,subdir,fileName,"r") |
91 |
| - def writeContents(self,outputFile): |
92 |
| - self.convertToBytes(outputFile) |
93 |
| - def writeDirEntry(self,outputFile): |
94 |
| - print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Luminance }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
| 89 | + def __init__(self,directory,subdir,fileName): |
| 90 | + File.__init__(self,directory,subdir,fileName,"r") |
| 91 | + def writeContents(self,outputFile): |
| 92 | + self.convertToBytes(outputFile) |
| 93 | + def writeDirEntry(self,outputFile): |
| 94 | + print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Luminance }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
95 | 95 |
|
96 | 96 | class LumaFile(File):
|
97 |
| - def __init__(self,directory,subdir,fileName): |
98 |
| - File.__init__(self,directory,subdir,fileName,"r") |
99 |
| - def writeContents(self,outputFile): |
100 |
| - self.convertToBytes(outputFile) |
101 |
| - def writeDirEntry(self,outputFile): |
102 |
| - print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::LumAlpha }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
| 97 | + def __init__(self,directory,subdir,fileName): |
| 98 | + File.__init__(self,directory,subdir,fileName,"r") |
| 99 | + def writeContents(self,outputFile): |
| 100 | + self.convertToBytes(outputFile) |
| 101 | + def writeDirEntry(self,outputFile): |
| 102 | + print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::LumAlpha }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile) |
103 | 103 |
|
104 | 104 | def main():
|
105 |
| - if len(sys.argv)<3: |
106 |
| - sys.exit('Usage: %s directory output_file.cpp' % sys.argv[0]) |
107 |
| - directory = sys.argv[1] |
108 |
| - files = [] |
109 |
| - for dirName, subdirList, fileList in walk(directory): |
110 |
| - for fname in fileList: |
111 |
| - subdir = dirName[len(directory)+1:] if dirName.startswith(directory) else dirName |
112 |
| - if subdir: |
113 |
| - files.append(File.factory(directory,subdir,fname)) |
114 |
| - files.sort(key=lambda f: f.subdir + "/" + f.fileName) |
115 |
| - outputFile = open(sys.argv[2],"w") |
116 |
| - print("#include \"resourcemgr.h\"\n",file=outputFile) |
117 |
| - for f in files: |
118 |
| - f.writeContents(outputFile) |
119 |
| - print("void initResources() { ResourceMgr::instance().registerResources({",file=outputFile) |
120 |
| - for f in files: |
121 |
| - f.writeDirEntry(outputFile) |
122 |
| - print("});}",file=outputFile) |
| 105 | + if len(sys.argv)<3: |
| 106 | + sys.exit('Usage: %s directory output_file.cpp' % sys.argv[0]) |
| 107 | + directory = sys.argv[1] |
| 108 | + files = [] |
| 109 | + for dirName, subdirList, fileList in walk(directory): |
| 110 | + for fname in fileList: |
| 111 | + subdir = dirName[len(directory)+1:] if dirName.startswith(directory) else dirName |
| 112 | + if subdir: |
| 113 | + files.append(File.factory(directory,subdir,fname)) |
| 114 | + files.sort(key=lambda f: f.subdir + "/" + f.fileName) |
| 115 | + outputFile = open(sys.argv[2],"w") |
| 116 | + print("#include \"resourcemgr.h\"\n",file=outputFile) |
| 117 | + for f in files: |
| 118 | + f.writeContents(outputFile) |
| 119 | + print("void initResources() { ResourceMgr::instance().registerResources({",file=outputFile) |
| 120 | + for f in files: |
| 121 | + f.writeDirEntry(outputFile) |
| 122 | + print("});}",file=outputFile) |
123 | 123 |
|
124 | 124 | if __name__ == '__main__':
|
125 |
| - main() |
| 125 | + main() |
0 commit comments