]> git.donarmstrong.com Git - lilypond.git/blob - bin/genheader.py
57f0e9fb8f93176193f3433af8c4ef2fc17b552c
[lilypond.git] / bin / genheader.py
1 #!@PYTHON@
2
3
4 # genheader.py -- do headers (like these) 
5
6 # source file of the GNU LilyPond music typesetter
7
8 # (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9
10
11
12
13 import posix
14 import pwd
15 import regex
16 import regsub
17 import string
18 import sys
19 import os
20 import getopt
21
22 class My_options:
23     def __init__(self):
24         self.commentify = None
25         self.add_hdr_def = 0
26         self.classname = ''
27
28 my_options = My_options()
29
30 def mail():
31     return os.environ['MAILADDRESS']
32
33 def name():
34     return pwd.getpwuid(posix.getuid())[4]
35
36 def c_commentify(str):
37     return  '/* ' + regsub.gsub('^','  ', str) + '\n */';
38
39 def sh_commentify(str):
40     return regsub.gsub('^', '# ', str)
41
42 def tex_commentify(str):
43     return regsub.gsub('^', '% ', str)
44
45 def project_str():
46     cwd = posix.getcwd()
47     if regex.search('flower', cwd) <> -1:
48         PROJECT = "the Flower Library"
49     elif regex.search('mf$', cwd) <> -1:
50         PROJECT = "the Feta (defintively not an abbreviation for Font-En-Tja) music font"
51     else:
52         PROJECT = "the GNU LilyPond music typesetter"
53     return PROJECT
54
55 def head_str(filename):
56     if my_options.add_hdr_def:
57         what = "declare " 
58     else:
59         what=" implement "
60
61
62     headstr = '\n%s -- %s\n\nsource file of %s\n\n(c) 1997 %s <%s>\n' \
63               %(filename, what, project_str(), name(), mail())
64     return headstr
65
66
67 def c_include(filename):
68     startdef= filename;
69     trans = string.maketrans( string.lowercase + '-.', string.uppercase + '__')
70     startdef = string.translate(filename, trans)
71
72    
73     headstr = "\n\n#ifndef %s\n#define %s\n" % (startdef, startdef)
74     terminatestr = "#endif /* %s */\n"  % (startdef);
75
76     return headstr+ '\n\n'+ terminatestr;
77
78
79
80
81
82 (options, files) = getopt.getopt(sys.argv[1:], 'tcsh', ['class']) 
83
84 for opt in options:
85     o = opt[0]
86     a = opt[1]
87     if o == '-c':
88         my_options.commentify = c_commentify
89     elif o == '-t':
90         my_options.commentify = tex_commentify
91     elif o == '-s':
92         my_options.commentify = sh_commentify
93     elif o == '-h':
94         my_options.add_hdr_def = 1
95     elif o == '--class':
96         my_options.classname = a
97
98
99
100 #
101 # FIXME:  should create xxx.cc and include/xxx.hh, with implement/declare Xxx
102 # in  one run
103 if my_options.classname:
104     pass
105         
106 def do_file(nm):
107     s = my_options.commentify(head_str(nm)) 
108     if my_options.add_hdr_def:
109         s = s + c_include(nm)
110     return s
111
112
113 def extension(ext,nm):
114     ext = '\\.' + ext
115     return regex.search(ext, nm) <> -1
116
117 def c_extension(nm):
118     return extension('hh',nm) or extension('cc',nm) \
119            or extension('icc', nm) or extension('tcc',nm)
120 def select_commentification(nm):
121     if c_extension (nm):
122         return c_commentify
123     elif extension('py',nm) or extension('pl',nm) or extension('sh',nm):
124         return  sh_commentify
125     elif extension('mf',nm) or extension('tex',nm) or extension('ly',nm):
126         return tex_commentify
127     else: 
128         raise 'help'
129
130 for nm in files:
131     if extension('hh', nm) or extension('icc', nm) or  extension('tcc', nm): 
132         my_options.add_hdr_def = 1
133     if my_options.commentify == None:
134         my_options.commentify = select_commentification(nm)
135     print do_file(nm)