Imported Upstream version 2008.1+svn1648
[opeanno-debian-packaging.git] / development / docstringer.py
blob26bb4e46a0c6fc40aef0c429bda2ae52fcdee763
1 #!/usr/bin/env python
3 # ###################################################
4 # Copyright (C) 2008 The OpenAnno Team
5 # team@openanno.org
6 # This file is part of OpenAnno.
8 # OpenAnno is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 # ###################################################
24 import re
25 is_function = re.compile('^(\\s*)def\\s+([^\\s(]+)\\s*[(](.*)[)]\\s*:\\s*$')
26 is_decorator = re.compile('^(\\s*)@\\s*(.+)\\s*$')
27 func_param = re.compile('\\s*(?:,\\s*)?([^,=\\s]+)(?:\\s*=\\s*([^\\s,]+))?')
28 is_empty = re.compile('^(\\s*)(?:#.*)?$')
29 import sys
30 files = sys.argv[1:]
31 for filename in files:
32 print 'Adding documentation stubs to:', filename
33 file = open(filename, 'r+')
34 funk_reg = None
35 newfile = []
36 for line in file:
37 if is_function.match(line) is not None:
38 funk_reg = is_function.match(line)
39 elif funk_reg is not None and line.strip().startswith('"""'):
40 newfile.append(funk_reg.group())
41 newfile.append(line)
42 funk_reg = None
43 elif funk_reg is not None:
44 params = func_param.findall(funk_reg.group(3))
45 indent = funk_reg.group(1)*2 if funk_reg.group(2) != '__init__' else funk_reg.group(1)
46 docstub = [(indent + '"""\n')]
47 for i in params:
48 if i[0] != 'self' and i[0] != 'cls':
49 docstub.append(("%s@param %s:\n" % (indent, i[0])))
50 docstub.append((indent + '"""\n'))
51 if funk_reg.group(2) == '__init__':
52 newfile.extend(docstub)
53 newfile.append(funk_reg.group())
54 else:
55 newfile.append(funk_reg.group())
56 newfile.extend(docstub)
57 newfile.append(line)
58 funk_reg = None
59 else:
60 newfile.append(line)
61 file.seek(0)
62 file.writelines(newfile)
63 file.close()
64 print 'Done'