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