1 # -*- coding: utf-8 -*-
13 ly.stderr_write ((_ ("error: %s") % str) + "\n")
16 def escape_ly_output_string (input_string):
17 return_string = input_string
18 needs_quotes = not re.match (u"^[a-zA-ZäöüÜÄÖßñ]*$", return_string);
20 return_string = "\"" + string.replace (return_string, "\"", "\\\"") + "\""
24 def musicxml_duration_to_log (dur):
36 'long': -2}.get (dur, 0)
45 self._name = 'xml_node'
47 self._attribute_dict = {}
49 def get_parent (self):
53 return self._parent.get_typed_children (self.__class__)[0] == self
64 if not self._children:
67 return ''.join ([c.get_text () for c in self._children])
69 def message (self, msg):
70 ly.stderr_write (msg+'\n')
74 sys.stderr.write (' In: <%s %s>\n' % (p._name, ' '.join (['%s=%s' % item for item in p._attribute_dict.items ()])))
77 def get_typed_children (self, klass):
81 return [c for c in self._children if isinstance(c, klass)]
83 def get_named_children (self, nm):
84 return self.get_typed_children (get_class (nm))
86 def get_named_child (self, nm):
87 return self.get_maybe_exist_named_child (nm)
89 def get_children (self, predicate):
90 return [c for c in self._children if predicate(c)]
92 def get_all_children (self):
95 def get_maybe_exist_named_child (self, name):
96 return self.get_maybe_exist_typed_child (get_class (name))
98 def get_maybe_exist_typed_child (self, klass):
99 cn = self.get_typed_children (klass)
105 raise "More than 1 child", klass
107 def get_unique_typed_child (self, klass):
108 cn = self.get_typed_children(klass)
110 sys.stderr.write (self.__dict__ + '\n')
111 raise 'Child is not unique for', (klass, 'found', cn)
115 def get_named_child_value_number (self, name, default):
116 n = self.get_maybe_exist_named_child (name)
118 return string.atoi (n.get_text())
123 class Music_xml_node (Xml_node):
125 Xml_node.__init__ (self)
126 self.duration = Rational (0)
127 self.start = Rational (0)
129 class Work (Xml_node):
130 def get_work_information (self, tag):
131 wt = self.get_maybe_exist_named_child (tag)
133 return wt.get_text ()
137 def get_work_title (self):
138 return self.get_work_information ('work-title')
139 def get_work_number (self):
140 return self.get_work_information ('work-number')
142 return self.get_work_information ('opus')
144 class Identification (Xml_node):
145 def get_rights (self):
146 rights = self.get_maybe_exist_named_child ('rights')
148 return rights.get_text ()
152 def get_creator (self, type):
153 creators = self.get_named_children ('creator')
154 # return the first creator tag that has the particular type
156 if hasattr (i, 'type') and i.type == type:
160 def get_composer (self):
161 c = self.get_creator ('composer')
164 creators = self.get_named_children ('creator')
165 # return the first creator tag that has no type at all
167 if not hasattr (i, 'type'):
170 def get_arranger (self):
171 return self.get_creator ('arranger')
172 def get_editor (self):
173 return self.get_creator ('editor')
175 v = self.get_creator ('lyricist')
178 v = self.get_creator ('poet')
181 def get_encoding_information (self, type):
182 enc = self.get_named_children ('encoding')
184 children = enc[0].get_named_children (type)
186 return children[0].get_text ()
190 def get_encoding_software (self):
191 return self.get_encoding_information ('software')
192 def get_encoding_date (self):
193 return self.get_encoding_information ('encoding-date')
194 def get_encoding_person (self):
195 return self.get_encoding_information ('encoder')
196 def get_encoding_description (self):
197 return self.get_encoding_information ('encoding-description')
199 def get_encoding_software_list (self):
200 enc = self.get_named_children ('encoding')
203 softwares = e.get_named_children ('software')
205 software.append (s.get_text ())
210 class Duration (Music_xml_node):
211 def get_length (self):
212 dur = int (self.get_text ()) * Rational (1,4)
215 class Hash_comment (Music_xml_node):
217 class Hash_text (Music_xml_node):
220 class Pitch (Music_xml_node):
222 ch = self.get_unique_typed_child (get_class (u'step'))
223 step = ch.get_text ().strip ()
225 def get_octave (self):
226 ch = self.get_unique_typed_child (get_class (u'octave'))
228 step = ch.get_text ().strip ()
231 def get_alteration (self):
232 ch = self.get_maybe_exist_typed_child (get_class (u'alter'))
235 alter = int (ch.get_text ().strip ())
238 class Unpitched (Music_xml_node):
240 ch = self.get_unique_typed_child (get_class (u'display-step'))
241 step = ch.get_text ().strip ()
244 def get_octave (self):
245 ch = self.get_unique_typed_child (get_class (u'display-octave'))
248 octave = ch.get_text ().strip ()
253 class Measure_element (Music_xml_node):
254 def get_voice_id (self):
255 voice_id = self.get_maybe_exist_named_child ('voice')
257 return voice_id.get_text ()
262 # Look at all measure elements (previously we had self.__class__, which
263 # only looked at objects of the same type!
264 cn = self._parent.get_typed_children (Measure_element)
265 # But only look at the correct voice; But include Attributes, too, which
266 # are not tied to any particular voice
267 cn = [c for c in cn if (c.get_voice_id () == self.get_voice_id ()) or isinstance (c, Attributes)]
270 class Attributes (Measure_element):
272 Measure_element.__init__ (self)
274 self._original_tag = None
277 cn = self._parent.get_typed_children (self.__class__)
278 if self._original_tag:
279 return cn[0] == self._original_tag
283 def set_attributes_from_previous (self, dict):
284 self._dict.update (dict)
286 def read_self (self):
287 for c in self.get_all_children ():
288 self._dict[c.get_name()] = c
290 def get_named_attribute (self, name):
291 return self._dict.get (name)
293 def get_measure_length (self):
294 (n,d) = self.get_time_signature ()
295 return Rational (n,d)
297 def get_time_signature (self):
298 "return time sig as a (beat, beat-type) tuple"
301 mxl = self.get_named_attribute ('time')
303 beats = mxl.get_maybe_exist_named_child ('beats')
304 type = mxl.get_maybe_exist_named_child ('beat-type')
305 return (int (beats.get_text ()),
306 int (type.get_text ()))
310 error (_ ("requested time signature, but time sig is unknown"))
313 # returns clef information in the form ("cleftype", position, octave-shift)
314 def get_clef_information (self):
315 clefinfo = ['G', 2, 0]
316 mxl = self.get_named_attribute ('clef')
319 sign = mxl.get_maybe_exist_named_child ('sign')
321 clefinfo[0] = sign.get_text()
322 line = mxl.get_maybe_exist_named_child ('line')
324 clefinfo[1] = string.atoi (line.get_text ())
325 octave = mxl.get_maybe_exist_named_child ('clef-octave-change')
327 clefinfo[2] = string.atoi (octave.get_text ())
330 def get_key_signature (self):
331 "return (fifths, mode) tuple"
333 key = self.get_named_attribute ('key')
334 mode_node = key.get_maybe_exist_named_child ('mode')
337 mode = mode_node.get_text ()
339 fifths = int (key.get_maybe_exist_named_child ('fifths').get_text ())
340 return (fifths, mode)
342 class Barline (Measure_element):
344 class BarStyle (Music_xml_node):
346 class Partial (Measure_element):
347 def __init__ (self, partial):
348 Measure_element.__init__ (self)
349 self.partial = partial
351 class Note (Measure_element):
353 Measure_element.__init__ (self)
354 self.instrument_name = ''
355 self._after_grace = False
357 return self.get_maybe_exist_named_child (u'grace')
358 def is_after_grace (self):
359 if not self.is_grace():
361 gr = self.get_maybe_exist_typed_child (Grace)
362 return self._after_grace or hasattr (gr, 'steal-time-previous');
364 def get_duration_log (self):
365 ch = self.get_maybe_exist_named_child (u'type')
368 log = ch.get_text ().strip()
369 return musicxml_duration_to_log (log)
370 elif self.get_maybe_exist_named_child (u'grace'):
371 # FIXME: is it ok to default to eight note for grace notes?
374 self.message (_ ("Encountered note at %s with %s duration (no <type> element):") % (self.start, self.duration) )
377 def get_factor (self):
380 def get_pitches (self):
381 return self.get_typed_children (get_class (u'pitch'))
383 class Part_list (Music_xml_node):
385 Music_xml_node.__init__ (self)
386 self._id_instrument_name_dict = {}
388 def generate_id_instrument_dict (self):
390 ## not empty to make sure this happens only once.
392 for score_part in self.get_named_children ('score-part'):
393 for instr in score_part.get_named_children ('score-instrument'):
395 name = instr.get_named_child ("instrument-name")
396 mapping[id] = name.get_text ()
398 self._id_instrument_name_dict = mapping
400 def get_instrument (self, id):
401 if not self._id_instrument_name_dict:
402 self.generate_id_instrument_dict()
404 instrument_name = self._id_instrument_name_dict.get (id)
406 return instrument_name
408 ly.stderr_write (_ ("Unable to find instrument for ID=%s\n") % id)
411 class Part_group (Music_xml_node):
413 class Score_part (Music_xml_node):
416 class Measure (Music_xml_node):
418 Music_xml_node.__init__ (self)
420 def is_implicit (self):
421 return hasattr (self, 'implicit') and self.implicit == 'yes'
422 def get_notes (self):
423 return self.get_typed_children (get_class (u'note'))
425 class Syllabic (Music_xml_node):
426 def continued (self):
427 text = self.get_text()
428 return (text == "begin") or (text == "middle")
429 class Text (Music_xml_node):
432 class Lyric (Music_xml_node):
433 def get_number (self):
434 if hasattr (self, 'number'):
439 def lyric_to_text (self):
441 syllabic = self.get_maybe_exist_typed_child (Syllabic)
443 continued = syllabic.continued ()
444 text = self.get_maybe_exist_typed_child (Text)
447 text = text.get_text()
448 # We need to convert soft hyphens to -, otherwise the ascii codec as well
449 # as lilypond will barf on that character
450 text = string.replace( text, u'\xad', '-' )
452 if text == "-" and continued:
454 elif text == "_" and continued:
456 elif continued and text:
457 return escape_ly_output_string (text) + " --"
461 return escape_ly_output_string (text)
465 class Musicxml_voice:
469 self._start_staff = None
471 self._has_lyrics = False
473 def add_element (self, e):
474 self._elements.append (e)
475 if (isinstance (e, Note)
476 and e.get_maybe_exist_typed_child (Staff)):
477 name = e.get_maybe_exist_typed_child (Staff).get_text ()
479 if not self._start_staff and not e.get_maybe_exist_typed_child (Grace):
480 self._start_staff = name
481 self._staves[name] = True
483 lyrics = e.get_typed_children (Lyric)
484 if not self._has_lyrics:
485 self.has_lyrics = len (lyrics) > 0
489 if (nr > 0) and not (nr in self._lyrics):
490 self._lyrics.append (nr)
492 def insert (self, idx, e):
493 self._elements.insert (idx, e)
495 def get_lyrics_numbers (self):
496 if (len (self._lyrics) == 0) and self._has_lyrics:
497 #only happens if none of the <lyric> tags has a number attribute
503 class Part (Music_xml_node):
505 Music_xml_node.__init__ (self)
507 self._staff_attributes_dict = {}
509 def get_part_list (self):
511 while n and n.get_name() != 'score-partwise':
514 return n.get_named_child ('part-list')
516 def interpret (self):
517 """Set durations and starting points."""
518 """The starting point of the very first note is 0!"""
520 part_list = self.get_part_list ()
523 factor = Rational (1)
525 attributes_object = None
526 measures = self.get_typed_children (Measure)
527 last_moment = Rational (-1)
528 last_measure_position = Rational (-1)
529 measure_position = Rational (0)
530 measure_start_moment = now
531 is_first_measure = True
532 previous_measure = None
533 # Graces at the end of a measure need to have their position set to the
537 # implicit measures are used for artificial measures, e.g. when
538 # a repeat bar line splits a bar into two halves. In this case,
539 # don't reset the measure position to 0. They are also used for
540 # upbeats (initial value of 0 fits these, too).
541 # Also, don't reset the measure position at the end of the loop,
542 # but rather when starting the next measure (since only then do we
543 # know if the next measure is implicit and continues that measure)
544 if not m.is_implicit ():
545 # Warn about possibly overfull measures and reset the position
546 if attributes_object and previous_measure and previous_measure.partial == 0:
547 length = attributes_object.get_measure_length ()
548 new_now = measure_start_moment + length
550 problem = 'incomplete'
553 ## only for verbose operation.
554 if problem <> 'incomplete' and previous_measure:
555 previous_measure.message ('%s measure? Expected: %s, Difference: %s' % (problem, now, new_now - now))
557 measure_start_moment = now
558 measure_position = Rational (0)
560 for n in m.get_all_children ():
561 # figured bass has a duration, but applies to the next note
562 # and should not change the current measure position!
563 if isinstance (n, FiguredBass):
564 n._divisions = factor.denominator ()
566 n._measure_position = measure_position
569 if isinstance (n, Hash_text):
573 if n.__class__ == Attributes:
574 n.set_attributes_from_previous (attributes_dict)
576 attributes_dict = n._dict.copy ()
577 attributes_object = n
579 factor = Rational (1,
580 int (attributes_dict.get ('divisions').get_text ()))
583 if (n.get_maybe_exist_typed_child (Duration)):
584 mxl_dur = n.get_maybe_exist_typed_child (Duration)
585 dur = mxl_dur.get_length () * factor
587 if n.get_name() == 'backup':
589 # reset all graces before the backup to after-graces:
590 for n in pending_graces:
591 n._when = n._prev_when
592 n._measure_position = n._prev_measure_position
593 n._after_grace = True
595 if n.get_maybe_exist_typed_child (Grace):
598 rest = n.get_maybe_exist_typed_child (Rest)
600 and attributes_object
601 and attributes_object.get_measure_length () == dur):
603 rest._is_whole_measure = True
605 if (dur > Rational (0)
606 and n.get_maybe_exist_typed_child (Chord)):
608 measure_position = last_measure_position
611 n._measure_position = measure_position
613 # For all grace notes, store the previous note, in case need
614 # to turn the grace note into an after-grace later on!
615 if isinstance(n, Note) and n.is_grace ():
616 n._prev_when = last_moment
617 n._prev_measure_position = last_measure_position
618 # After-graces are placed at the same position as the previous note
619 if isinstance(n, Note) and n.is_after_grace ():
620 # TODO: We should do the same for grace notes at the end of
621 # a measure with no following note!!!
622 n._when = last_moment
623 n._measure_position = last_measure_position
624 elif isinstance(n, Note) and n.is_grace ():
625 pending_graces.append (n)
626 elif (dur > Rational (0)):
630 if dur > Rational (0):
632 last_measure_position = measure_position
634 measure_position += dur
635 elif dur < Rational (0):
636 # backup element, reset measure position
638 measure_position += dur
639 if measure_position < 0:
640 # backup went beyond the measure start => reset to 0
641 now -= measure_position
644 last_measure_position = measure_position
645 if n._name == 'note':
646 instrument = n.get_maybe_exist_named_child ('instrument')
648 n.instrument_name = part_list.get_instrument (instrument.id)
650 # reset all graces at the end of the measure to after-graces:
651 for n in pending_graces:
652 n._when = n._prev_when
653 n._measure_position = n._prev_measure_position
654 n._after_grace = True
656 # Incomplete first measures are not padded, but registered as partial
658 is_first_measure = False
659 # upbeats are marked as implicit measures
660 if attributes_object and m.is_implicit ():
661 length = attributes_object.get_measure_length ()
662 measure_end = measure_start_moment + length
663 if measure_end <> now:
667 # modify attributes so that only those applying to the given staff remain
668 def extract_attributes_for_staff (part, attr, staff):
669 attributes = copy.copy (attr)
670 attributes._children = [];
671 attributes._dict = attr._dict.copy ()
672 attributes._original_tag = attr
673 # copy only the relevant children over for the given staff
674 for c in attr._children:
675 if (not (hasattr (c, 'number') and (c.number != staff)) and
676 not (isinstance (c, Hash_text))):
677 attributes._children.append (c)
678 if not attributes._children:
683 def extract_voices (part):
685 measures = part.get_typed_children (Measure)
689 elements.append (Partial (m.partial))
690 elements.extend (m.get_all_children ())
691 # make sure we know all voices already so that dynamics, clefs, etc.
692 # can be assigned to the correct voices
693 voice_to_staff_dict = {}
695 voice_id = n.get_maybe_exist_named_child (u'voice')
698 vid = voice_id.get_text ()
700 staff_id = n.get_maybe_exist_named_child (u'staff')
703 sid = staff_id.get_text ()
706 if vid and not voices.has_key (vid):
707 voices[vid] = Musicxml_voice()
708 if vid and sid and not n.get_maybe_exist_typed_child (Grace):
709 if not voice_to_staff_dict.has_key (vid):
710 voice_to_staff_dict[vid] = sid
711 # invert the voice_to_staff_dict into a staff_to_voice_dict (since we
712 # need to assign staff-assigned objects like clefs, times, etc. to
713 # all the correct voices. This will never work entirely correct due
714 # to staff-switches, but that's the best we can do!
715 staff_to_voice_dict = {}
716 for (v,s) in voice_to_staff_dict.items ():
717 if not staff_to_voice_dict.has_key (s):
718 staff_to_voice_dict[s] = [v]
720 staff_to_voice_dict[s].append (v)
724 assign_to_next_note = []
727 voice_id = n.get_maybe_exist_typed_child (get_class ('voice'))
729 if not (voice_id or isinstance (n, Attributes) or
730 isinstance (n, Direction) or isinstance (n, Partial) or
731 isinstance (n, Barline) or isinstance (n, Harmony) or
732 isinstance (n, FiguredBass) ):
735 if isinstance (n, Attributes) and not start_attr:
739 if isinstance (n, Attributes):
740 # assign these only to the voices they really belongs to!
741 for (s, vids) in staff_to_voice_dict.items ():
742 staff_attributes = part.extract_attributes_for_staff (n, s)
745 voices[v].add_element (staff_attributes)
748 if isinstance (n, Partial) or isinstance (n, Barline):
749 for v in voices.keys ():
750 voices[v].add_element (n)
753 if isinstance (n, Direction):
754 staff_id = n.get_maybe_exist_named_child (u'staff')
756 staff_id = staff_id.get_text ()
758 dir_voices = staff_to_voice_dict.get (staff_id, voices.keys ())
760 dir_voices = voices.keys ()
762 voices[v].add_element (n)
765 if isinstance (n, Harmony) or isinstance (n, FiguredBass):
766 # store the harmony or figured bass element until we encounter
767 # the next note and assign it only to that one voice.
768 assign_to_next_note.append (n)
771 id = voice_id.get_text ()
772 if hasattr (n, 'print-object') and getattr (n, 'print-object') == "no":
776 for i in assign_to_next_note:
777 voices[id].add_element (i)
778 assign_to_next_note = []
779 voices[id].add_element (n)
781 # Assign all remaining elements from assign_to_next_note to the voice
782 # of the previous note:
783 for i in assign_to_next_note:
784 voices[id].add_element (i)
785 assign_to_next_note = []
788 for (s, vids) in staff_to_voice_dict.items ():
789 staff_attributes = part.extract_attributes_for_staff (start_attr, s)
790 staff_attributes.read_self ()
791 part._staff_attributes_dict[s] = staff_attributes
793 voices[v].insert (0, staff_attributes)
794 voices[v]._elements[0].read_self()
796 part._voices = voices
798 def get_voices (self):
800 def get_staff_attributes (self):
801 return self._staff_attributes_dict
803 class Notations (Music_xml_node):
805 ts = self.get_named_children ('tied')
806 starts = [t for t in ts if t.type == 'start']
812 def get_tuplets (self):
813 return self.get_typed_children (Tuplet)
815 class Time_modification(Music_xml_node):
816 def get_fraction (self):
817 b = self.get_maybe_exist_named_child ('actual-notes')
818 a = self.get_maybe_exist_named_child ('normal-notes')
819 return (int(a.get_text ()), int (b.get_text ()))
821 class Accidental (Music_xml_node):
823 Music_xml_node.__init__ (self)
824 self.editorial = False
825 self.cautionary = False
827 class Music_xml_spanner (Music_xml_node):
829 if hasattr (self, 'type'):
834 if hasattr (self, 'size'):
835 return string.atoi (self.size)
839 class Wedge (Music_xml_spanner):
842 class Tuplet (Music_xml_spanner):
845 class Bracket (Music_xml_spanner):
848 class Dashes (Music_xml_spanner):
851 class Slur (Music_xml_spanner):
855 class Beam (Music_xml_spanner):
857 return self.get_text ()
858 def is_primary (self):
859 return self.number == "1"
861 class Wavy_line (Music_xml_spanner):
864 class Pedal (Music_xml_spanner):
867 class Glissando (Music_xml_spanner):
870 class Slide (Music_xml_spanner):
873 class Octave_shift (Music_xml_spanner):
874 # default is 8 for the octave-shift!
876 if hasattr (self, 'size'):
877 return string.atoi (self.size)
881 class Chord (Music_xml_node):
884 class Dot (Music_xml_node):
887 # Rests in MusicXML are <note> blocks with a <rest> inside. This class is only
888 # for the inner <rest> element, not the whole rest block.
889 class Rest (Music_xml_node):
891 Music_xml_node.__init__ (self)
892 self._is_whole_measure = False
893 def is_whole_measure (self):
894 return self._is_whole_measure
896 ch = self.get_maybe_exist_typed_child (get_class (u'display-step'))
898 step = ch.get_text ().strip ()
902 def get_octave (self):
903 ch = self.get_maybe_exist_typed_child (get_class (u'display-octave'))
905 step = ch.get_text ().strip ()
910 class Type (Music_xml_node):
912 class Grace (Music_xml_node):
914 class Staff (Music_xml_node):
917 class Direction (Music_xml_node):
919 class DirType (Music_xml_node):
922 class Bend (Music_xml_node):
923 def bend_alter (self):
924 alter = self.get_maybe_exist_named_child ('bend-alter')
926 return alter.get_text()
930 class Words (Music_xml_node):
933 class Harmony (Music_xml_node):
936 class ChordPitch (Music_xml_node):
937 def step_class_name (self):
939 def alter_class_name (self):
942 ch = self.get_unique_typed_child (get_class (self.step_class_name ()))
943 return ch.get_text ().strip ()
944 def get_alteration (self):
945 ch = self.get_maybe_exist_typed_child (get_class (self.alter_class_name ()))
948 alter = int (ch.get_text ().strip ())
951 class Root (ChordPitch):
954 class Bass (ChordPitch):
955 def step_class_name (self):
957 def alter_class_name (self):
960 class ChordModification (Music_xml_node):
962 ch = self.get_maybe_exist_typed_child (get_class (u'degree-type'))
963 return {'add': 1, 'alter': 1, 'subtract': -1}.get (ch.get_text ().strip (), 0)
964 def get_value (self):
965 ch = self.get_maybe_exist_typed_child (get_class (u'degree-value'))
968 value = int (ch.get_text ().strip ())
970 def get_alter (self):
971 ch = self.get_maybe_exist_typed_child (get_class (u'degree-alter'))
974 value = int (ch.get_text ().strip ())
978 class Frame (Music_xml_node):
979 def get_frets (self):
980 return self.get_named_child_value_number ('frame-frets', 4)
981 def get_strings (self):
982 return self.get_named_child_value_number ('frame-strings', 6)
983 def get_first_fret (self):
984 return self.get_named_child_value_number ('first-fret', 1)
986 class Frame_Note (Music_xml_node):
987 def get_string (self):
988 return self.get_named_child_value_number ('string', 1)
990 return self.get_named_child_value_number ('fret', 0)
991 def get_fingering (self):
992 return self.get_named_child_value_number ('fingering', -1)
993 def get_barre (self):
994 n = self.get_maybe_exist_named_child ('barre')
996 return getattr (n, 'type', '')
1000 class FiguredBass (Music_xml_node):
1003 class BeatUnit (Music_xml_node):
1006 class BeatUnitDot (Music_xml_node):
1009 class PerMinute (Music_xml_node):
1014 ## need this, not all classes are instantiated
1015 ## for every input file. Only add those classes, that are either directly
1016 ## used by class name or extend Music_xml_node in some way!
1018 '#comment': Hash_comment,
1020 'accidental': Accidental,
1021 'attributes': Attributes,
1023 'bar-style': BarStyle,
1026 'beat-unit': BeatUnit,
1027 'beat-unit-dot': BeatUnitDot,
1029 'bracket' : Bracket,
1032 'degree' : ChordModification,
1034 'direction': Direction,
1035 'direction-type': DirType,
1036 'duration': Duration,
1038 'frame-note': Frame_Note,
1039 'figured-bass': FiguredBass,
1040 'glissando': Glissando,
1043 'identification': Identification,
1046 'notations': Notations,
1048 'octave-shift': Octave_shift,
1050 'part-group': Part_group,
1051 'part-list': Part_list,
1053 'per-minute': PerMinute,
1057 'score-part': Score_part,
1061 'syllabic': Syllabic,
1063 'time-modification': Time_modification,
1066 'unpitched': Unpitched,
1067 'wavy-line': Wavy_line,
1073 def name2class_name (name):
1074 name = name.replace ('-', '_')
1075 name = name.replace ('#', 'hash_')
1076 name = name[0].upper() + name[1:].lower()
1080 def get_class (name):
1081 classname = class_dict.get (name)
1085 class_name = name2class_name (name)
1086 klass = new.classobj (class_name, (Music_xml_node,) , {})
1087 class_dict[name] = klass
1090 def lxml_demarshal_node (node):
1093 # Ignore comment nodes, which are also returned by the etree parser!
1094 if name is None or node.__class__.__name__ == "_Comment":
1096 klass = get_class (name)
1099 py_node._original = node
1100 py_node._name = name
1101 py_node._data = node.text
1102 py_node._children = [lxml_demarshal_node (cn) for cn in node.getchildren()]
1103 py_node._children = filter (lambda x: x, py_node._children)
1105 for c in py_node._children:
1108 for (k, v) in node.items ():
1109 py_node.__dict__[k] = v
1110 py_node._attribute_dict[k] = v
1114 def minidom_demarshal_node (node):
1115 name = node.nodeName
1117 klass = get_class (name)
1119 py_node._name = name
1120 py_node._children = [minidom_demarshal_node (cn) for cn in node.childNodes]
1121 for c in py_node._children:
1125 for (nm, value) in node.attributes.items ():
1126 py_node.__dict__[nm] = value
1127 py_node._attribute_dict[nm] = value
1129 py_node._data = None
1130 if node.nodeType == node.TEXT_NODE and node.data:
1131 py_node._data = node.data
1133 py_node._original = node
1137 if __name__ == '__main__':
1140 tree = lxml.etree.parse ('beethoven.xml')
1141 mxl_tree = lxml_demarshal_node (tree.getroot ())
1142 ks = class_dict.keys ()
1144 print '\n'.join (ks)