Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: experimental/PdfViewer/generate_code.py

Issue 16706007: generate enums for pdf types (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 2
3 class PdfName: 3 class PdfName:
4 def __init__(self, name, abr=''): 4 def __init__(self, name, abr=''):
5 self.fName = name 5 self.fName = name
6 self.fAbr = abr 6 self.fAbr = abr
7 7
8 class PdfInteger: 8 class PdfInteger:
9 def __init__(self, value): 9 def __init__(self, value):
10 self.fValue = value 10 self.fValue = value
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 return self.fParent 81 return self.fParent
82 82
83 class PdfClass: 83 class PdfClass:
84 def __init__(self, name, base): 84 def __init__(self, name, base):
85 self.fFields = [] 85 self.fFields = []
86 self.fIncludes = [] 86 self.fIncludes = []
87 self.fCC = [] 87 self.fCC = []
88 self.fName = name 88 self.fName = name
89 self.fBase = base 89 self.fBase = base
90 90
91 self.fEnumSubclasses = []
92
93 self.fEnum = '!UNDEFINED'
94 self.fEnumEnd = '!UNDEFINED'
95
91 def required(self): 96 def required(self):
92 field = PdfClassField(self, True) 97 field = PdfClassField(self, True)
93 self.fFields.append(field) 98 self.fFields.append(field)
94 return field 99 return field
95 100
96 def optional(self): 101 def optional(self):
97 field = PdfClassField(self, False) 102 field = PdfClassField(self, False)
98 self.fFields.append(field) 103 self.fFields.append(field)
99 return field 104 return field
100 105
101 def include(self, path): 106 def include(self, path):
102 self.fIncludes.append(path) 107 self.fIncludes.append(path)
103 return self 108 return self
104 109
105 def carbonCopy(self, cc): 110 def carbonCopy(self, cc):
106 self.fCC.append(cc) 111 self.fCC.append(cc)
107 return self 112 return self
108 113
109 class PdfClassManager: 114 class PdfClassManager:
110 def __init__(self): 115 def __init__(self):
111 self.fClasses = [] 116 self.fClasses = {}
112 117
113 def addClass(self, name, base=''): 118 def addClass(self, name, base=''):
114 cls = PdfClass(name, base) 119 cls = PdfClass(name, base)
115 self.fClasses.append(cls) 120 self.fClasses[name] = cls
116 return cls 121 return cls
117 122
123 def longName(self, name):
124 ret = ''
125 while name != '':
126 cls = self.fClasses[name]
127 ret = name + ret
128 name = cls.fBase
129
130 return ret
131
132
133 def writeEnum(self, enum, enumToCls):
134 print(' ' + enum + ',')
135 cls = enumToCls[enum]
136 cls.fEnumSubclasses.sort()
137
138 cnt = 0
139 for sub in cls.fEnumSubclasses:
140 self.writeEnum(cls.fEnumSubclasses[cnt], enumToCls)
141 cnt = cnt + 1
142
143 if cnt != 0:
144 print(' ' + cls.fEnumEnd + ',')
145
118 def write(self): 146 def write(self):
147 # generate enum
148 enumsRoot = []
149
150 enumToCls = {}
151
152 for name in self.fClasses:
153 cls = self.fClasses[name]
154 enum = self.longName(name)
155 cls.fEnum = 'k' + enum + '_PdfObjectType'
156 cls.fEnumEnd = 'k' + enum + '__End_PdfObjectType'
157
158 if cls.fBase != '':
159 self.fClasses[cls.fBase].fEnumSubclasses.append(cls.fEnum)
160
161 if cls.fBase == '':
162 enumsRoot.append(cls.fEnum)
163
164 enumToCls[cls.fEnum] = cls
165
166 enumsRoot.sort()
167
168 # write enums
169 print('enum PdfObjectType {')
170 for enum in enumsRoot:
171 self.writeEnum(enum, enumToCls)
172 print('};')
173
174 # generate each class
175 # generate parser
119 return 176 return
120 177
121 def generateCode(): 178 def generateCode():
122 all = PdfClassManager() 179 all = PdfClassManager()
123 180
124 all.addClass('Object') 181 all.addClass('Object')
125 all.addClass('Null') 182 all.addClass('Null')
126 all.addClass('Boolean') 183 all.addClass('Boolean')
127 all.addClass('Integer') 184 all.addClass('Integer')
128 all.addClass('Real') 185 all.addClass('Real')
(...skipping 20 matching lines...) Expand all
149 all.addClass('Form', 'XObject').required().hasField('/Type').must('/XObject'). done()\ 206 all.addClass('Form', 'XObject').required().hasField('/Type').must('/XObject'). done()\
150 .required().hasField('/Subtype').must('/Form'). done() 207 .required().hasField('/Subtype').must('/Form'). done()
151 208
152 209
153 all.write() 210 all.write()
154 211
155 return 1 212 return 1
156 213
157 if '__main__' == __name__: 214 if '__main__' == __name__:
158 sys.exit(generateCode()) 215 sys.exit(generateCode())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698