''
[lilypond.git] / python / lilylib.py.in
blob1da987020c6c54f3ae9d72f7ee4ac599fb6dcb95
1 # lilylib.py -- options and stuff
2 #
3 # source file of the GNU LilyPond music typesetter
5 import os
6 from __main__ import *
7 try:
8 import gettext
9 gettext.bindtextdomain ('lilypond', localedir)
10 gettext.textdomain ('lilypond')
11 _ = gettext.gettext
12 except:
13 def _ (s):
14 return s
16 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
17 program_version = '1.5.17'
19 def identify ():
20 sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
22 def warranty ():
23 identify ()
24 sys.stdout.write ('\n')
25 sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001'))
26 sys.stdout.write ('\n')
27 sys.stdout.write (' Han-Wen Nienhuys')
28 sys.stdout.write (' Jan Nieuwenhuizen')
29 sys.stdout.write ('\n')
30 sys.stdout.write (_ (r'''
31 Distributed under terms of the GNU General Public License. It comes with
32 NO WARRANTY.'''))
33 sys.stdout.write ('\n')
35 def progress (s):
36 errorport.write (s + '\n')
38 def warning (s):
39 progress (_ ("warning: ") + s)
41 def error (s):
44 '''Report the error S. Exit by raising an exception. Please
45 do not abuse by trying to catch this error. If you do not want
46 a stack trace, write to the output directly.
48 RETURN VALUE
50 None
52 '''
54 progress (_ ("error: ") + s)
55 raise _ ("Exiting ... ")
57 def getopt_args (opts):
58 '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
59 short = ''
60 long = []
61 for o in opts:
62 if o[1]:
63 short = short + o[1]
64 if o[0]:
65 short = short + ':'
66 if o[2]:
67 l = o[2]
68 if o[0]:
69 l = l + '='
70 long.append (l)
71 return (short, long)
73 def option_help_str (o):
74 '''Transform one option description (4-tuple ) into neatly formatted string'''
75 sh = ' '
76 if o[1]:
77 sh = '-%s' % o[1]
79 sep = ' '
80 if o[1] and o[2]:
81 sep = ','
83 long = ''
84 if o[2]:
85 long= '--%s' % o[2]
87 arg = ''
88 if o[0]:
89 if o[2]:
90 arg = '='
91 arg = arg + o[0]
92 return ' ' + sh + sep + long + arg
95 def options_help_str (opts):
96 '''Convert a list of options into a neatly formatted string'''
97 w = 0
98 strs =[]
99 helps = []
101 for o in opts:
102 s = option_help_str (o)
103 strs.append ((s, o[3]))
104 if len (s) > w:
105 w = len (s)
107 str = ''
108 for s in strs:
109 str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1])
110 return str
112 def help ():
113 ls = [(_ ("Usage: %s [OPTION]... FILE") % program_name),
114 ('\n\n'),
115 (help_summary),
116 ('\n\n'),
117 (_ ("Options:")),
118 ('\n'),
119 (options_help_str (option_definitions)),
120 ('\n\n'),
121 (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
122 ('\n')]
123 map (sys.stdout.write, ls)
125 def setup_temp ():
127 Create a temporary directory, and return its name.
129 global temp_dir
130 if not keep_temp_dir_p:
131 temp_dir = tempfile.mktemp (program_name)
132 try:
133 os.mkdir (temp_dir, 0777)
134 except OSError:
135 pass
137 return temp_dir
140 def system (cmd, ignore_error = 0):
141 """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
143 RETURN VALUE
145 Exit status of CMD
148 if verbose_p:
149 progress (_ ("Invoking `%s\'") % cmd)
150 st = os.system (cmd)
151 if st:
152 name = re.match ('[ \t]*([^ \t]*)', cmd).group (1)
153 msg = name + ': ' + _ ("command exited with value %d") % st
154 if ignore_error:
155 warning (msg + ' ' + _ ("(ignored)") + ' ')
156 else:
157 error (msg)
159 return st
162 def cleanup_temp ():
163 if not keep_temp_dir_p:
164 if verbose_p:
165 progress (_ ("Cleaning %s...") % temp_dir)
166 shutil.rmtree (temp_dir)
169 def strip_extension (f, ext):
170 (p, e) = os.path.splitext (f)
171 if e == ext:
172 e = ''
173 return p + e
175 # END Library