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