Add start/endUnremovableSection commands
[orchestrallily.git] / generate_oly_score.py
blob979c0687c1ead31751500e8f19229c684f1ddf4a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import sys
5 import os
6 import os.path
7 import getopt
8 import re
9 import filecmp
10 import string
11 import subprocess
12 import codecs
13 import fnmatch
14 from jinja2 import Environment, Template, FileSystemLoader
15 import jinja2
17 import pprint
18 pp = pprint.PrettyPrinter(indent=4)
20 program_name = 'generate_oly_score';
21 settings_file = 'oly_structure.def';
22 script_path = os.path.dirname(__file__);
24 ######################################################################
25 # Options handling
26 ######################################################################
28 help_text = r"""Usage: %(program_name)s [OPTIONS]... [DEF-FILE]
29 Create a complete file structure for a score using OrchestralLily.
30 If no definitions file it given, the file oly_structure.def is used
32 Options:
33 -h, --help print this help
34 -o, --output=DIR write output files to DIR (default: read from settings file)
35 """
37 def help (text):
38 sys.stdout.write (text)
39 sys.exit (0)
43 ######################################################################
44 # Settings
45 ######################################################################
47 class Settings:
48 options = {};
49 arguments = [];
50 globals={};
51 raw_data={};
52 out_dir = "";
53 template_env = None;
55 def __init__ (self):
56 settings_file = self.load_options ();
57 self.load (settings_file);
58 self.init_template_env ();
59 self.init_arrays ();
61 def output_dir (self):
62 return self.out_dir
63 def defaults (self):
64 return self.raw_data.get ("defaults", {});
65 def score_names (self):
66 return self.raw_data.get ("scores", ["Score"]);
68 def get_score_settings (self, id):
69 settings = self.globals.copy();
70 settings.update (self.defaults ());
71 settings.update ({"name": id})
72 settings.update (self.raw_data.get (id, {}));
73 self.normalize_part_definitions (settings);
74 return settings;
76 def normalize_part_definitions (self, score_settings):
77 scorename = score_settings.get ("name", "Score");
78 parts = score_settings.get ("parts", [scorename]);
79 result = [];
80 for this_part in parts:
81 if isinstance (this_part, basestring):
82 this_part = {"id": this_part, "filename": this_part }
83 elif not isinstance (this_part, dict):
84 warning ("Invalid part list for score %a: %a" % (scorename, p));
85 return [];
86 if "id" not in this_part:
87 this_part["id"] = scorename;
88 if "filename" not in this_part:
89 this_part["filename"] = this_part["id"];
90 if "piece" not in this_part:
91 this_part["piece"] = this_part["id"];
92 this_part["score"] = scorename;
93 this_part.update (score_settings);
94 result.append (this_part);
95 score_settings["parts"] = result;
97 def has_tex (self):
98 return "latex" in self.raw_data;
99 def get_tex_settings (self):
100 settings = self.globals.copy();
101 settings.update (self.defaults ());
102 settings.update (self.raw_data.get ("latex", {}));
103 return settings;
105 def get_score_parts (self, score_settings):
106 scorename = score_settings.get ("name", "Score");
107 parts = score_settings.get ("parts", [scorename]);
108 result = [];
109 for p in parts:
110 this_part = p;
111 if isinstance (this_part, basestring):
112 this_part = {"id": this_part, "filename": this_part }
113 elif not isinstance (this_part, dict):
114 warning ("Invalid part list for score %a: %a" % (scorename, p));
115 return [];
116 if "id" not in this_part:
117 this_part["id"] = scorename;
118 if "filename" not in this_part:
119 this_part["filename"] = this_part["id"];
120 if "piece" not in this_part:
121 this_part["piece"] = this_part["id"];
122 this_part["score"] = scorename;
123 this_part.update (score_settings);
124 result.append (this_part);
125 return result;
127 def load_options (self):
128 (self.options, self.arguments) = getopt.getopt (sys.argv[1:], 'ho:', ['help', 'output='])
129 for opt in self.options:
130 o = opt[0]
131 a = opt[1]
132 if o == '-h' or o == '--help':
133 help (help_text % globals ())
134 elif o == '-o' or o == '--output':
135 self.out_dir = a
136 else:
137 raise Exception ('unknown option: ' + o)
138 if self.arguments:
139 return self.arguments[0]
140 else:
141 return "oly_structure.def";
143 def load (self, filename):
144 try:
145 in_f = codecs.open (filename, "r", "utf-8")
146 s = in_f.read()
147 in_f.close()
148 self.raw_data = eval(s);
149 except IOError:
150 print ("Unable to load settings file '%s'. Exiting..." % file_name)
151 exit (-1);
152 except SyntaxError as ex:
153 print ex;
154 print ("Unable to interpret settings file '%s', it's syntax is invalid. Exiting..." % filename);
155 exit (-1);
156 if not self.out_dir:
157 self.out_dir = self.raw_data.get ("output_dir", self.out_dir) + "/";
159 def get_template_name (self):
160 return self.raw_data.get ("template", "Full");
161 def get_template_names (self, pattern):
162 allfiles = os.listdir (self.templatepath);
163 files = [];
164 for f in allfiles:
165 if fnmatch.fnmatch(f, pattern):
166 files.append (f);
167 return files;
170 def init_template_env (self):
171 global program_name;
172 global script_path;
173 templatename = self.get_template_name ();
174 self.templatepath = script_path + '/Templates/' + templatename;
175 self.template_env = Environment (
176 loader = FileSystemLoader(self.templatepath),
177 block_start_string = '<$', block_end_string = '$>',
178 variable_start_string = '<<', variable_end_string = '>>',
179 comment_start_string = '<#', comment_end_string = '#>',
180 #line_statement_prefix = '#'
182 def get_template (self, template):
183 return self.template_env.get_template (template);
186 def init_arrays (self):
187 # Globals
188 self.globals = self.raw_data.copy ();
189 del self.globals["defaults"];
190 if "latex" in self.globals:
191 del self.globals["latex"];
192 for i in self.globals.get ("scores", []):
193 if i in self.globals:
194 del self.globals[i];
196 def assemble_filename (self, base, id, name, ext):
197 parts = [base, id, name];
198 if "" in parts:
199 parts.remove ("")
200 return "_".join (parts) + "." + ext;
201 def assemble_movement_filename (self, basename, mvmnt):
202 return self.assemble_filename( basename, "Music", mvmnt, "ily");
203 def assemble_instrument_filename (self, basename, instr):
204 return self.assemble_filename( basename, "Instrument", instr, "ly");
205 def assemble_score_filename (self, basename, instr):
206 return self.assemble_filename( basename, "Score", instr, "ly");
207 def assemble_settings_filename (self, basename, s):
208 return self.assemble_filename( basename, "Settings", s, "ily");
209 def assemble_itex_filename (self, basename, filename):
210 return self.assemble_filename( "TeX", basename, filename, "itex");
211 def assemble_texscore_filename (self, basename, score):
212 return self.assemble_filename( "TeX", basename, "Score_" + score, "tex");
214 ######################################################################
215 # File Writing
216 ######################################################################
218 def write_file (path, fname, contents):
219 file_name = path + fname;
220 fn = file_name
221 if os.path.exists (file_name):
222 fn += ".new"
224 dir = os.path.dirname (fn);
225 if not os.path.exists (dir):
226 os.mkdir (dir)
228 try:
229 out_f = codecs.open (fn, "w", "utf-8")
230 s = out_f.write(contents)
231 out_f.write ("\n")
232 out_f.close()
233 except IOError:
234 print ("Unable to write to output file '%s'. Exiting..." % fn)
235 exit (-1);
237 # If the file already existed, check if the new file is identical.
238 if (fn != file_name):
239 patchfile = os.path.join (dir, "patches", os.path.basename(file_name) + ".patch")
240 if os.path.exists (patchfile):
241 try:
242 retcode = subprocess.call("patch -Ns \""+ fn+ "\" \"" + patchfile + "\"", shell=True)
243 if retcode < 0:
244 print >>sys.stderr, "Unable to apply patch to file \"", fn, "\"."
245 except OSError, e:
246 print >>sys.stderr, "Execution failed:", e
248 if filecmp.cmp (fn, file_name):
249 os.unlink (fn);
250 else:
251 print ("A file %s already existed, created new file %s." % (os.path.basename(file_name), os.path.basename(fn)))
255 ######################################################################
256 # Creating movement files
257 ######################################################################
260 def generate_movement_files (score_name, score_settings, settings):
261 parts_files = [];
262 for part_settings in settings.get_score_parts (score_settings):
263 template = settings.get_template ("Lily_Music_Movement.ily");
264 basename = part_settings.get ("basename", score_name);
265 filename = part_settings.get ("filename", score_name + "Part");
266 filename = settings.assemble_movement_filename (basename, filename);
267 write_file (settings.out_dir, filename, template.render (part_settings));
268 parts_files.append (filename);
270 return parts_files;
274 ######################################################################
275 # Creating instrumental score files
276 ######################################################################
278 def generate_instrument_files (score_name, score_settings, settings):
279 instrument_files = [];
280 settings_files = set ();
281 instrument_settings = score_settings.copy ();
282 instrument_settings["parts"] = settings.get_score_parts (score_settings);
284 template = settings.get_template ("Lily_Instrument.ly");
285 basename = score_settings.get ("basename", score_name );
287 noscore_instruments = score_settings.get ("noscore_instruments", [])
288 for i in score_settings.get ("instruments", []):
289 if i in noscore_instruments:
290 continue;
291 instrument_settings["instrument"] = i;
292 if i in score_settings.get ("vocalvoices"):
293 instrument_settings["settings"] = "VocalVoice";
294 else:
295 instrument_settings["settings"] = "Instrument";
296 settings_files.add (instrument_settings["settings"]);
297 filename = settings.assemble_instrument_filename (basename, i);
298 write_file (settings.out_dir, filename, template.render (instrument_settings));
299 instrument_files.append (filename);
301 return (instrument_files, settings_files);
305 ######################################################################
306 # Creating score files
307 ######################################################################
310 score_name_map = {
311 "Particell": "Particell",
313 settings_map = {
314 "OrganScore": "VocalScore",
315 "ChoralScore": "ChoralScore",
316 "LongScore": "FullScore",
317 "OriginalScore": "FullScore",
318 "Particell": "FullScore"
320 scores_cues = ["ChoralScore", "VocalScore"];
322 def generate_score_files (score_name, score_settings, settings):
323 score_files = [];
324 settings_files = set ();
325 s_settings = score_settings.copy ();
326 s_settings["parts"] = settings.get_score_parts (score_settings);
327 s_settings["fullscore"] = True;
329 template = settings.get_template ("Lily_Score.ly");
330 basename = score_settings.get ("basename", score_name );
332 for s in score_settings.get ("scores", []):
333 fullsn = score_name_map.get (s, (s + "Score"));
334 s_settings["score"] = fullsn;
335 s_settings["nocues"] = fullsn not in scores_cues;
336 s_settings["settings"] = settings_map.get (fullsn,fullsn)
337 settings_files.add (s_settings["settings"]);
338 filename = settings.assemble_score_filename (basename, s);
339 write_file (settings.out_dir, filename, template.render (s_settings));
340 score_files.append (filename);
342 return (score_files, settings_files);
346 ######################################################################
347 # Creating settings files
348 ######################################################################
350 def write_settings_file_if_exists (settings, score_settings, template, filename):
351 try:
352 template = settings.get_template (template);
353 basename = score_settings.get ("basename", "");
354 filename = settings.assemble_settings_filename (basename, filename);
355 write_file (settings.out_dir, filename, template.render (score_settings));
356 return filename;
357 except jinja2.exceptions.TemplateNotFound:
358 return None;
360 def generate_settings_files (score_name, score_settings, settings, settings_files):
361 out_files = [];
362 out_files.append (write_settings_file_if_exists (settings, score_settings, "Lily_Settings_Global.ily", "Global" ));
363 out_files.append (write_settings_file_if_exists (settings, score_settings, "Lily_Settings.ily", "" ));
365 for s in settings_files:
366 score_settings["settings"] = s.lower ();
367 out_files.append (write_settings_file_if_exists (settings, score_settings, "Lily_Settings_Generic.ily", s));
369 return out_files;
373 ######################################################################
374 # Generation of Lilypond Files
375 ######################################################################
377 def generate_scores (settings):
378 files = {}
379 for s in settings.score_names ():
380 score_settings = settings.get_score_settings (s);
381 parts_files = generate_movement_files (s, score_settings, settings);
382 (instrument_files, isettings_files) = generate_instrument_files (s, score_settings, settings);
383 (score_files, ssettings_files) = generate_score_files (s, score_settings, settings);
384 included_settings_files = ssettings_files | isettings_files;
385 score_settings["parts_files"] = parts_files;
386 settings_files = generate_settings_files (s, score_settings, settings, included_settings_files );
387 files[s] = {"settings": settings_files,
388 "scores": score_files,
389 "instruments": instrument_files,
390 "parts": parts_files };
391 return files
395 ######################################################################
396 # Creating LaTeX files
397 ######################################################################
399 def write_itex_file (settings, tex_settings, template, filename):
400 template = settings.get_template (template);
401 basename = tex_settings.get ("basename", "");
402 filename = settings.assemble_itex_filename (basename, filename);
403 write_file (settings.out_dir, filename, template.render (tex_settings));
404 return filename;
406 def write_texscore_file (settings, tex_settings, template, score):
407 template = settings.get_template (template);
408 basename = tex_settings.get ("basename", "");
409 filename = settings.assemble_texscore_filename (basename, score);
410 write_file (settings.out_dir, filename, template.render (tex_settings));
411 return filename;
413 no_criticalcomments_scores = ["Vocal", "Choral", "Organ"];
415 def generate_tex_files (settings, lily_files):
416 tex_files = [];
417 tex_includes = [];
418 tex_settings = settings.get_tex_settings ();
419 tex_settings["lily_files"] = lily_files;
421 score_map = {};
422 instruments_scores = [];
423 for s in settings.score_names ():
424 score_settings = settings.get_score_settings (s);
425 instruments_scores.append (score_settings);
426 for p in score_settings.get ("scores", []):
427 score_map[p] = score_map.get (p, []) + [score_settings];
428 tex_settings["works"] = instruments_scores;
430 tex_include_templates = settings.get_template_names("TeX_*.itex");
431 for t in tex_include_templates:
432 base = re.sub( r'TeX_(.*)\.itex', r'\1', t);
433 tex_includes.append (write_itex_file (settings, tex_settings, t, base));
435 for (score, parts) in score_map.items ():
436 this_settings = tex_settings.copy ();
437 this_settings["scoretype"] = score;
438 this_settings["scores"] = parts;
439 if "createCriticalComments" not in tex_settings:
440 this_settings["createCriticalComments"] = score not in no_criticalcomments_scores;
441 tex_files.append (write_texscore_file (settings, this_settings, "TeX_Score.tex", score))
443 this_settings = tex_settings.copy ();
444 this_settings["scoretype"] = "InstrumentalParts";
445 if "createCriticalComments" not in tex_settings:
446 this_settings["createCriticalComments"] = score not in no_criticalcomments_scores;
447 tex_files.append (write_texscore_file (settings, this_settings, "TeX_Instruments.tex", "Instruments"))
449 return [tex_files, tex_includes];
453 ######################################################################
454 # Creating Makefile
455 ######################################################################
457 def generate_make_files (settings, lily_files, tex_files):
458 make_settings = settings.raw_data.copy ();
460 tex_settings = settings.get_tex_settings ();
461 tex_settings["includes"] = tex_files[1];
462 tex_settings["files"] = tex_files[0];
463 make_settings["latex"] = tex_settings;
465 score_map = {};
466 instruments_scores = [];
467 nr = 0;
468 for s in settings.score_names ():
469 nr += 1;
470 score_settings = settings.get_score_settings (s);
471 if nr > 1:
472 score_settings["nr"] = nr;
473 else:
474 score_settings["nr"] = "";
475 score_settings["srcfiles"] = lily_files[s];
476 instruments_scores.append (score_settings);
477 for p in score_settings.get ("scores", []):
478 score_map[p] = score_map.get (p, []) + [score_settings];
479 make_settings["works"] = instruments_scores;
481 template = settings.get_template ("Makefile");
482 basename = tex_settings.get ("basename", "");
483 file = write_file (settings.out_dir, "Makefile", template.render (make_settings));
484 return file;
488 # movements = settings.get ("parts", {});
490 # replacements["instruments"] = string.join (settings.get ("instruments", []));
491 # replacements["scores"] = string.join (settings.get ("scores", []))## + " Instruments";
492 # replacements["srcfiles"] = string.join (src_files);
494 # write_file (output_dir + "Makefile", templates["Makefile"] % replacements);
496 # del replacements["instruments"]
497 # del replacements["scores"]
498 # del replacements["srcfiles"]
499 # return;
502 ######################################################################
503 # Link the orchestrallily package
504 ######################################################################
506 def generate_oly_link (settings):
507 global script_path;
508 try:
509 os.symlink ("../"+script_path, settings.out_dir+"orchestrallily")
510 except OSError:
511 pass
513 ######################################################################
514 # Main function
515 ######################################################################
517 def main ():
518 settings = Settings ();
519 print ("Creating OrchestralLily template in \"%s\", using template \"%s\"." %
520 (settings.out_dir, settings.get_template_name () ));
522 print ("Creating Lilypond files")
523 lily_files = generate_scores (settings);
525 if settings.has_tex ():
526 print ("Creating LaTeX files")
527 tex_files = generate_tex_files (settings, lily_files);
528 print ("Creating OrchestralLily package link")
529 generate_oly_link (settings);
530 print ("Creating Makefile")
531 generate_make_files (settings, lily_files, tex_files);
533 main ();