1*7f2fe78bSCy Schubert''' 2*7f2fe78bSCy Schubert Copyright 2011 by the Massachusetts 3*7f2fe78bSCy Schubert Institute of Technology. All Rights Reserved. 4*7f2fe78bSCy Schubert 5*7f2fe78bSCy Schubert Export of this software from the United States of America may 6*7f2fe78bSCy Schubert require a specific license from the United States Government. 7*7f2fe78bSCy Schubert It is the responsibility of any person or organization contemplating 8*7f2fe78bSCy Schubert export to obtain such a license before exporting. 9*7f2fe78bSCy Schubert 10*7f2fe78bSCy Schubert WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 11*7f2fe78bSCy Schubert distribute this software and its documentation for any purpose and 12*7f2fe78bSCy Schubert without fee is hereby granted, provided that the above copyright 13*7f2fe78bSCy Schubert notice appear in all copies and that both that copyright notice and 14*7f2fe78bSCy Schubert this permission notice appear in supporting documentation, and that 15*7f2fe78bSCy Schubert the name of M.I.T. not be used in advertising or publicity pertaining 16*7f2fe78bSCy Schubert to distribution of the software without specific, written prior 17*7f2fe78bSCy Schubert permission. Furthermore if you modify this software you must label 18*7f2fe78bSCy Schubert your software as modified software and not distribute it in such a 19*7f2fe78bSCy Schubert fashion that it might be confused with the original M.I.T. software. 20*7f2fe78bSCy Schubert M.I.T. makes no representations about the suitability of 21*7f2fe78bSCy Schubert this software for any purpose. It is provided "as is" without express 22*7f2fe78bSCy Schubert or implied warranty. 23*7f2fe78bSCy Schubert''' 24*7f2fe78bSCy Schubertimport re 25*7f2fe78bSCy Schubert 26*7f2fe78bSCy Schubertfrom Cheetah.Template import Template 27*7f2fe78bSCy Schubert 28*7f2fe78bSCy Schubertclass Attribute(object): 29*7f2fe78bSCy Schubert def __init__(self, **argkw): 30*7f2fe78bSCy Schubert self.definition = argkw.get('definition') 31*7f2fe78bSCy Schubert self.name = argkw.get('name') 32*7f2fe78bSCy Schubert self.type = argkw.get('type') 33*7f2fe78bSCy Schubert self.typeId = argkw.get('typeId') 34*7f2fe78bSCy Schubert self.short_description = argkw.get('short_description') 35*7f2fe78bSCy Schubert self.long_description = argkw.get('long_description') 36*7f2fe78bSCy Schubert self.version = argkw.get('version') 37*7f2fe78bSCy Schubert 38*7f2fe78bSCy Schubert def __repr__(self): 39*7f2fe78bSCy Schubert result = list() 40*7f2fe78bSCy Schubert for (attr,value) in self.__dict__.iteritems(): 41*7f2fe78bSCy Schubert result.append('%s=%s' % (attr,value)) 42*7f2fe78bSCy Schubert return 'Attribute: %s' % ','.join(result) 43*7f2fe78bSCy Schubert 44*7f2fe78bSCy Schubert 45*7f2fe78bSCy Schubertclass CompositeType(): 46*7f2fe78bSCy Schubert def __init__(self, **argkw): 47*7f2fe78bSCy Schubert self.category = 'composite' 48*7f2fe78bSCy Schubert self.definition = argkw.get('definition') 49*7f2fe78bSCy Schubert self.name = argkw.get('name') 50*7f2fe78bSCy Schubert self.name_signature = argkw.get('name_signature') 51*7f2fe78bSCy Schubert self.Id = argkw.get('Id') 52*7f2fe78bSCy Schubert self.initializer = argkw.get('initializer') 53*7f2fe78bSCy Schubert self.active = argkw.get('active', False) 54*7f2fe78bSCy Schubert self.version = argkw.get('version') 55*7f2fe78bSCy Schubert self.return_type = argkw.get('return_type') 56*7f2fe78bSCy Schubert self.short_description = argkw.get('short_description') 57*7f2fe78bSCy Schubert self.long_description = argkw.get('long_description') 58*7f2fe78bSCy Schubert self.friends = argkw.get('friends') 59*7f2fe78bSCy Schubert self.type = argkw.get('type') 60*7f2fe78bSCy Schubert self.attributes = self._setAttributes(argkw.get('attributes')) 61*7f2fe78bSCy Schubert 62*7f2fe78bSCy Schubert def __repr__(self): 63*7f2fe78bSCy Schubert result = list() 64*7f2fe78bSCy Schubert for (attr,value) in self.__dict__.iteritems(): 65*7f2fe78bSCy Schubert if attr == 'attributes': 66*7f2fe78bSCy Schubert if value is not None: 67*7f2fe78bSCy Schubert attributes = ['%s' % a for a in value] 68*7f2fe78bSCy Schubert value = '\n %s' % '\n '.join(attributes) 69*7f2fe78bSCy Schubert 70*7f2fe78bSCy Schubert result.append('%s: %s' % (attr,value)) 71*7f2fe78bSCy Schubert result = '\n'.join(result) 72*7f2fe78bSCy Schubert 73*7f2fe78bSCy Schubert return result 74*7f2fe78bSCy Schubert 75*7f2fe78bSCy Schubert def _setAttributes(self, attributes): 76*7f2fe78bSCy Schubert result = None 77*7f2fe78bSCy Schubert if attributes is not None: 78*7f2fe78bSCy Schubert result = list() 79*7f2fe78bSCy Schubert for a in attributes: 80*7f2fe78bSCy Schubert result.append(Attribute(**a)) 81*7f2fe78bSCy Schubert 82*7f2fe78bSCy Schubert return result 83*7f2fe78bSCy Schubert 84*7f2fe78bSCy Schubert def struct_reference(self, name): 85*7f2fe78bSCy Schubert result = re.sub(r'_', '-', name) 86*7f2fe78bSCy Schubert result = '_%s-struct' % result 87*7f2fe78bSCy Schubert 88*7f2fe78bSCy Schubert return result 89*7f2fe78bSCy Schubert 90*7f2fe78bSCy Schubert def macro_reference(self, name): 91*7f2fe78bSCy Schubert result = re.sub(r'_', '-', name) 92*7f2fe78bSCy Schubert result = '_%s-data' % result 93*7f2fe78bSCy Schubert 94*7f2fe78bSCy Schubert return result 95*7f2fe78bSCy Schubert 96*7f2fe78bSCy Schubertclass Parameter(object): 97*7f2fe78bSCy Schubert def __init__(self, **argkw): 98*7f2fe78bSCy Schubert self.seqno = argkw.get('seqno') 99*7f2fe78bSCy Schubert self.name = argkw.get('name') 100*7f2fe78bSCy Schubert self.direction = argkw.get('direction') 101*7f2fe78bSCy Schubert self.type = argkw.get('type') 102*7f2fe78bSCy Schubert self.typeId = argkw.get('typeId') 103*7f2fe78bSCy Schubert self.description = argkw.get('description') 104*7f2fe78bSCy Schubert self.version = argkw.get('version') 105*7f2fe78bSCy Schubert 106*7f2fe78bSCy Schubert def __repr__(self): 107*7f2fe78bSCy Schubert content = (self.name,self.direction,self.seqno,self.type,self.typeId,self.description) 108*7f2fe78bSCy Schubert return 'Parameter: name=%s,direction=%s,seqno=%s,type=%s,typeId=%s,descr=%s' % content 109*7f2fe78bSCy Schubert 110*7f2fe78bSCy Schubertclass Function(object): 111*7f2fe78bSCy Schubert def __init__(self, **argkw): 112*7f2fe78bSCy Schubert self.category = 'function' 113*7f2fe78bSCy Schubert self.name = argkw.get('name') 114*7f2fe78bSCy Schubert self.Id = argkw.get('Id') 115*7f2fe78bSCy Schubert self.active = argkw.get('active', False) 116*7f2fe78bSCy Schubert self.version = argkw.get('version') 117*7f2fe78bSCy Schubert self.parameters = self._setParameters(argkw.get('parameters')) 118*7f2fe78bSCy Schubert self.return_type = argkw.get('return_type') 119*7f2fe78bSCy Schubert self.return_description = argkw.get('return_description') 120*7f2fe78bSCy Schubert self.retval_description = argkw.get('retval_description') 121*7f2fe78bSCy Schubert self.warn_description = argkw.get('warn_description') 122*7f2fe78bSCy Schubert self.sa_description = argkw.get('sa_description') 123*7f2fe78bSCy Schubert self.notes_description = argkw.get('notes_description') 124*7f2fe78bSCy Schubert self.version_num = argkw.get('version_num') 125*7f2fe78bSCy Schubert self.short_description = argkw.get('short_description') 126*7f2fe78bSCy Schubert self.long_description = argkw.get('long_description') 127*7f2fe78bSCy Schubert self.deprecated_description = argkw.get('deprecated_description') 128*7f2fe78bSCy Schubert self.friends = argkw.get('friends') 129*7f2fe78bSCy Schubert 130*7f2fe78bSCy Schubert def _setParameters(self, parameters): 131*7f2fe78bSCy Schubert result = None 132*7f2fe78bSCy Schubert if parameters is not None: 133*7f2fe78bSCy Schubert result = list() 134*7f2fe78bSCy Schubert for p in parameters: 135*7f2fe78bSCy Schubert result.append(Parameter(**p)) 136*7f2fe78bSCy Schubert 137*7f2fe78bSCy Schubert return result 138*7f2fe78bSCy Schubert 139*7f2fe78bSCy Schubert def getObjectRow(self): 140*7f2fe78bSCy Schubert result = [str(self.Id), 141*7f2fe78bSCy Schubert self.name, 142*7f2fe78bSCy Schubert self.category] 143*7f2fe78bSCy Schubert 144*7f2fe78bSCy Schubert return ','.join(result) 145*7f2fe78bSCy Schubert 146*7f2fe78bSCy Schubert def getObjectDescriptionRow(self): 147*7f2fe78bSCy Schubert result = [self.Id, 148*7f2fe78bSCy Schubert self.active, 149*7f2fe78bSCy Schubert self.version, 150*7f2fe78bSCy Schubert self.short_description, 151*7f2fe78bSCy Schubert self.long_description] 152*7f2fe78bSCy Schubert 153*7f2fe78bSCy Schubert return ','.join(result) 154*7f2fe78bSCy Schubert 155*7f2fe78bSCy Schubert def getParameterRows(self): 156*7f2fe78bSCy Schubert result = list() 157*7f2fe78bSCy Schubert for p in self.parameters: 158*7f2fe78bSCy Schubert p_row = [self.Id, 159*7f2fe78bSCy Schubert p.name, 160*7f2fe78bSCy Schubert p.seqno, 161*7f2fe78bSCy Schubert p.type, 162*7f2fe78bSCy Schubert p.typeId, 163*7f2fe78bSCy Schubert p.description, 164*7f2fe78bSCy Schubert p.version] 165*7f2fe78bSCy Schubert result.append(','.join(p_row)) 166*7f2fe78bSCy Schubert 167*7f2fe78bSCy Schubert return '\n'.join(result) 168*7f2fe78bSCy Schubert 169*7f2fe78bSCy Schubert def __repr__(self): 170*7f2fe78bSCy Schubert lines = list() 171*7f2fe78bSCy Schubert lines.append('Category: %s' % self.category) 172*7f2fe78bSCy Schubert lines.append('Function name: %s' % self.name) 173*7f2fe78bSCy Schubert lines.append('Function Id: %s' % self.Id) 174*7f2fe78bSCy Schubert parameters = [' %s' % p for p in self.parameters] 175*7f2fe78bSCy Schubert lines.append('Parameters:\n%s' % '\n'.join(parameters)) 176*7f2fe78bSCy Schubert lines.append('Function return type: %s' % self.return_type) 177*7f2fe78bSCy Schubert lines.append('Function return type description:\n%s' % self.return_description) 178*7f2fe78bSCy Schubert lines.append('Function retval description:\n%s' % self.retval_description) 179*7f2fe78bSCy Schubert lines.append('Function short description:\n%s' % self.short_description) 180*7f2fe78bSCy Schubert lines.append('Function long description:\n%s' % self.long_description) 181*7f2fe78bSCy Schubert lines.append('Warning description:\n%s' % self.warn_description) 182*7f2fe78bSCy Schubert lines.append('See also description:\n%s' % self.sa_description) 183*7f2fe78bSCy Schubert lines.append('NOTE description:\n%s' % self.notes_description) 184*7f2fe78bSCy Schubert lines.append('Version introduced:\n%s' % self.version_num) 185*7f2fe78bSCy Schubert lines.append('Deprecated description:\n%s' % self.deprecated_description) 186*7f2fe78bSCy Schubert result = '\n'.join(lines) 187*7f2fe78bSCy Schubert 188*7f2fe78bSCy Schubert return result 189*7f2fe78bSCy Schubert 190*7f2fe78bSCy Schubert 191*7f2fe78bSCy Schubertclass DocModel(object): 192*7f2fe78bSCy Schubert def __init__(self, **argkw): 193*7f2fe78bSCy Schubert if len(argkw): 194*7f2fe78bSCy Schubert self.name = argkw['name'] 195*7f2fe78bSCy Schubert if argkw['category'] == 'function': 196*7f2fe78bSCy Schubert self.category = 'function' 197*7f2fe78bSCy Schubert self.function = Function(**argkw) 198*7f2fe78bSCy Schubert elif argkw['category'] == 'composite': 199*7f2fe78bSCy Schubert self.category = 'composite' 200*7f2fe78bSCy Schubert self.composite = CompositeType(**argkw) 201*7f2fe78bSCy Schubert 202*7f2fe78bSCy Schubert def __repr__(self): 203*7f2fe78bSCy Schubert obj = getattr(self,self.category) 204*7f2fe78bSCy Schubert return str(obj) 205*7f2fe78bSCy Schubert 206*7f2fe78bSCy Schubert def signature(self): 207*7f2fe78bSCy Schubert param_list = list() 208*7f2fe78bSCy Schubert for p in self.function.parameters: 209*7f2fe78bSCy Schubert if p.type is "... " : 210*7f2fe78bSCy Schubert param_list.append('%s %s' % (p.type,' ')) 211*7f2fe78bSCy Schubert else: 212*7f2fe78bSCy Schubert param_list.append('%s %s' % (p.type, p.name)) 213*7f2fe78bSCy Schubert param_list = ', '.join(param_list) 214*7f2fe78bSCy Schubert result = '%s %s(%s)' % (self.function.return_type, 215*7f2fe78bSCy Schubert self.function.name, param_list) 216*7f2fe78bSCy Schubert 217*7f2fe78bSCy Schubert return result 218*7f2fe78bSCy Schubert 219*7f2fe78bSCy Schubert def save(self, path, template_path): 220*7f2fe78bSCy Schubert f = open(template_path, 'r') 221*7f2fe78bSCy Schubert t = Template(f.read(),self) 222*7f2fe78bSCy Schubert out = open(path, 'w') 223*7f2fe78bSCy Schubert out.write(str(t)) 224*7f2fe78bSCy Schubert out.close() 225*7f2fe78bSCy Schubert f.close() 226*7f2fe78bSCy Schubert 227*7f2fe78bSCy Schubert 228*7f2fe78bSCy Schubertclass DocModelTest(DocModel): 229*7f2fe78bSCy Schubert def __init__(self): 230*7f2fe78bSCy Schubert doc_path = '../docutil/example.yml' 231*7f2fe78bSCy Schubert argkw = yaml.load(open(doc_path,'r')) 232*7f2fe78bSCy Schubert super(DocModelTest,self).__init__(**argkw) 233*7f2fe78bSCy Schubert 234*7f2fe78bSCy Schubert def run_tests(self): 235*7f2fe78bSCy Schubert self.test_save() 236*7f2fe78bSCy Schubert 237*7f2fe78bSCy Schubert def test_print(self): 238*7f2fe78bSCy Schubert print('testing') 239*7f2fe78bSCy Schubert print(self) 240*7f2fe78bSCy Schubert 241*7f2fe78bSCy Schubert 242*7f2fe78bSCy Schubert def test_save(self): 243*7f2fe78bSCy Schubert template_path = '../docutil/function2edit.html' 244*7f2fe78bSCy Schubert 245*7f2fe78bSCy Schubert path = '/var/tsitkova/Sources/v10/trunk/documentation/test_doc.html' 246*7f2fe78bSCy Schubert 247*7f2fe78bSCy Schubert self.save(path, template_path) 248*7f2fe78bSCy Schubert 249*7f2fe78bSCy Schubertif __name__ == '__main__': 250*7f2fe78bSCy Schubert tester = DocModelTest() 251*7f2fe78bSCy Schubert tester.run_tests() 252