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 cn
= self
._parent
.get_typed_children (self
.__class
__)
263 cn
= [c
for c
in cn
if c
.get_voice_id () == self
.get_voice_id ()]
266 class Attributes (Measure_element
):
268 Measure_element
.__init
__ (self
)
271 def set_attributes_from_previous (self
, dict):
272 self
._dict
.update (dict)
274 def read_self (self
):
275 for c
in self
.get_all_children ():
276 self
._dict
[c
.get_name()] = c
278 def get_named_attribute (self
, name
):
279 return self
._dict
.get (name
)
281 def get_measure_length (self
):
282 (n
,d
) = self
.get_time_signature ()
283 return Rational (n
,d
)
285 def get_time_signature (self
):
286 "return time sig as a (beat, beat-type) tuple"
289 mxl
= self
.get_named_attribute ('time')
291 beats
= mxl
.get_maybe_exist_named_child ('beats')
292 type = mxl
.get_maybe_exist_named_child ('beat-type')
293 return (int (beats
.get_text ()),
294 int (type.get_text ()))
298 error (_ ("requested time signature, but time sig is unknown"))
301 # returns clef information in the form ("cleftype", position, octave-shift)
302 def get_clef_information (self
):
303 clefinfo
= ['G', 2, 0]
304 mxl
= self
.get_named_attribute ('clef')
307 sign
= mxl
.get_maybe_exist_named_child ('sign')
309 clefinfo
[0] = sign
.get_text()
310 line
= mxl
.get_maybe_exist_named_child ('line')
312 clefinfo
[1] = string
.atoi (line
.get_text ())
313 octave
= mxl
.get_maybe_exist_named_child ('clef-octave-change')
315 clefinfo
[2] = string
.atoi (octave
.get_text ())
318 def get_key_signature (self
):
319 "return (fifths, mode) tuple"
321 key
= self
.get_named_attribute ('key')
322 mode_node
= key
.get_maybe_exist_named_child ('mode')
325 mode
= mode_node
.get_text ()
327 fifths
= int (key
.get_maybe_exist_named_child ('fifths').get_text ())
328 return (fifths
, mode
)
330 class Barline (Measure_element
):
332 class BarStyle (Music_xml_node
):
334 class Partial (Measure_element
):
335 def __init__ (self
, partial
):
336 Measure_element
.__init
__ (self
)
337 self
.partial
= partial
339 class Note (Measure_element
):
341 Measure_element
.__init
__ (self
)
342 self
.instrument_name
= ''
344 def get_duration_log (self
):
345 ch
= self
.get_maybe_exist_named_child (u
'type')
348 log
= ch
.get_text ().strip()
349 return musicxml_duration_to_log (log
)
350 elif self
.get_maybe_exist_named_child (u
'grace'):
351 # FIXME: is it ok to default to eight note for grace notes?
354 self
.message (_ ("Encountered note at %s with %s duration (no <type> element):") % (self
.start
, self
.duration
) )
357 def get_factor (self
):
360 def get_pitches (self
):
361 return self
.get_typed_children (get_class (u
'pitch'))
363 class Part_list (Music_xml_node
):
365 Music_xml_node
.__init
__ (self
)
366 self
._id
_instrument
_name
_dict
= {}
368 def generate_id_instrument_dict (self
):
370 ## not empty to make sure this happens only once.
372 for score_part
in self
.get_named_children ('score-part'):
373 for instr
in score_part
.get_named_children ('score-instrument'):
375 name
= instr
.get_named_child ("instrument-name")
376 mapping
[id] = name
.get_text ()
378 self
._id
_instrument
_name
_dict
= mapping
380 def get_instrument (self
, id):
381 if not self
._id
_instrument
_name
_dict
:
382 self
.generate_id_instrument_dict()
384 instrument_name
= self
._id
_instrument
_name
_dict
.get (id)
386 return instrument_name
388 ly
.stderr_write (_ ("Unable to find instrument for ID=%s\n") % id)
391 class Part_group (Music_xml_node
):
393 class Score_part (Music_xml_node
):
396 class Measure (Music_xml_node
):
398 Music_xml_node
.__init
__ (self
)
400 def is_implicit (self
):
401 return hasattr (self
, 'implicit') and self
.implicit
== 'yes'
402 def get_notes (self
):
403 return self
.get_typed_children (get_class (u
'note'))
405 class Syllabic (Music_xml_node
):
406 def continued (self
):
407 text
= self
.get_text()
408 return (text
== "begin") or (text
== "middle")
409 class Text (Music_xml_node
):
412 class Lyric (Music_xml_node
):
413 def get_number (self
):
414 if hasattr (self
, 'number'):
419 def lyric_to_text (self
):
421 syllabic
= self
.get_maybe_exist_typed_child (Syllabic
)
423 continued
= syllabic
.continued ()
424 text
= self
.get_maybe_exist_typed_child (Text
)
427 text
= text
.get_text()
428 # We need to convert soft hyphens to -, otherwise the ascii codec as well
429 # as lilypond will barf on that character
430 text
= string
.replace( text
, u
'\xad', '-' )
432 if text
== "-" and continued
:
434 elif text
== "_" and continued
:
436 elif continued
and text
:
437 return escape_ly_output_string (text
) + " --"
441 return escape_ly_output_string (text
)
445 class Musicxml_voice
:
449 self
._start
_staff
= None
451 self
._has
_lyrics
= False
453 def add_element (self
, e
):
454 self
._elements
.append (e
)
455 if (isinstance (e
, Note
)
456 and e
.get_maybe_exist_typed_child (Staff
)):
457 name
= e
.get_maybe_exist_typed_child (Staff
).get_text ()
459 if not self
._start
_staff
and not e
.get_maybe_exist_typed_child (Grace
):
460 self
._start
_staff
= name
461 self
._staves
[name
] = True
463 lyrics
= e
.get_typed_children (Lyric
)
464 if not self
._has
_lyrics
:
465 self
.has_lyrics
= len (lyrics
) > 0
469 if (nr
> 0) and not (nr
in self
._lyrics
):
470 self
._lyrics
.append (nr
)
472 def insert (self
, idx
, e
):
473 self
._elements
.insert (idx
, e
)
475 def get_lyrics_numbers (self
):
476 if (len (self
._lyrics
) == 0) and self
._has
_lyrics
:
477 #only happens if none of the <lyric> tags has a number attribute
483 class Part (Music_xml_node
):
485 Music_xml_node
.__init
__ (self
)
487 self
._staff
_attributes
_dict
= {}
489 def get_part_list (self
):
491 while n
and n
.get_name() != 'score-partwise':
494 return n
.get_named_child ('part-list')
496 def interpret (self
):
497 """Set durations and starting points."""
498 """The starting point of the very first note is 0!"""
500 part_list
= self
.get_part_list ()
503 factor
= Rational (1)
505 attributes_object
= None
506 measures
= self
.get_typed_children (Measure
)
507 last_moment
= Rational (-1)
508 last_measure_position
= Rational (-1)
509 measure_position
= Rational (0)
510 measure_start_moment
= now
511 is_first_measure
= True
512 previous_measure
= None
514 # implicit measures are used for artificial measures, e.g. when
515 # a repeat bar line splits a bar into two halves. In this case,
516 # don't reset the measure position to 0. They are also used for
517 # upbeats (initial value of 0 fits these, too).
518 # Also, don't reset the measure position at the end of the loop,
519 # but rather when starting the next measure (since only then do we
520 # know if the next measure is implicit and continues that measure)
521 if not m
.is_implicit ():
522 # Warn about possibly overfull measures and reset the position
523 if attributes_object
and previous_measure
and previous_measure
.partial
== 0:
524 length
= attributes_object
.get_measure_length ()
525 new_now
= measure_start_moment
+ length
527 problem
= 'incomplete'
530 ## only for verbose operation.
531 if problem
<> 'incomplete' and previous_measure
:
532 previous_measure
.message ('%s measure? Expected: %s, Difference: %s' % (problem
, now
, new_now
- now
))
534 measure_start_moment
= now
535 measure_position
= Rational (0)
537 for n
in m
.get_all_children ():
538 # figured bass has a duration, but applies to the next note
539 # and should not change the current measure position!
540 if isinstance (n
, FiguredBass
):
541 n
._divisions
= factor
.denominator ()
544 if isinstance (n
, Hash_text
):
548 if n
.__class
__ == Attributes
:
549 n
.set_attributes_from_previous (attributes_dict
)
551 attributes_dict
= n
._dict
.copy ()
552 attributes_object
= n
554 factor
= Rational (1,
555 int (attributes_dict
.get ('divisions').get_text ()))
558 if (n
.get_maybe_exist_typed_child (Duration
)):
559 mxl_dur
= n
.get_maybe_exist_typed_child (Duration
)
560 dur
= mxl_dur
.get_length () * factor
562 if n
.get_name() == 'backup':
564 if n
.get_maybe_exist_typed_child (Grace
):
567 rest
= n
.get_maybe_exist_typed_child (Rest
)
569 and attributes_object
570 and attributes_object
.get_measure_length () == dur
):
572 rest
._is
_whole
_measure
= True
574 if (dur
> Rational (0)
575 and n
.get_maybe_exist_typed_child (Chord
)):
577 measure_position
= last_measure_position
580 n
._measure
_position
= measure_position
582 if dur
> Rational (0):
584 last_measure_position
= measure_position
586 measure_position
+= dur
587 elif dur
< Rational (0):
588 # backup element, reset measure position
590 measure_position
+= dur
591 if measure_position
< 0:
592 # backup went beyond the measure start => reset to 0
593 now
-= measure_position
596 last_measure_position
= measure_position
597 if n
._name
== 'note':
598 instrument
= n
.get_maybe_exist_named_child ('instrument')
600 n
.instrument_name
= part_list
.get_instrument (instrument
.id)
602 # Incomplete first measures are not padded, but registered as partial
604 is_first_measure
= False
605 # upbeats are marked as implicit measures
606 if attributes_object
and m
.is_implicit ():
607 length
= attributes_object
.get_measure_length ()
608 measure_end
= measure_start_moment
+ length
609 if measure_end
<> now
:
613 # modify attributes so that only those applying to the given staff remain
614 def extract_attributes_for_staff (part
, attr
, staff
):
615 attributes
= copy
.copy (attr
)
616 attributes
._children
= copy
.copy (attr
._children
)
617 attributes
._dict
= attr
._dict
.copy ()
618 for c
in attributes
._children
:
619 if hasattr (c
, 'number') and c
.number
!= staff
:
620 attributes
._children
.remove (c
)
623 def extract_voices (part
):
625 measures
= part
.get_typed_children (Measure
)
629 elements
.append (Partial (m
.partial
))
630 elements
.extend (m
.get_all_children ())
631 # make sure we know all voices already so that dynamics, clefs, etc.
632 # can be assigned to the correct voices
633 voice_to_staff_dict
= {}
635 voice_id
= n
.get_maybe_exist_named_child (u
'voice')
638 vid
= voice_id
.get_text ()
640 staff_id
= n
.get_maybe_exist_named_child (u
'staff')
643 sid
= staff_id
.get_text ()
646 if vid
and not voices
.has_key (vid
):
647 voices
[vid
] = Musicxml_voice()
648 if vid
and sid
and not n
.get_maybe_exist_typed_child (Grace
):
649 if not voice_to_staff_dict
.has_key (vid
):
650 voice_to_staff_dict
[vid
] = sid
651 # invert the voice_to_staff_dict into a staff_to_voice_dict (since we
652 # need to assign staff-assigned objects like clefs, times, etc. to
653 # all the correct voices. This will never work entirely correct due
654 # to staff-switches, but that's the best we can do!
655 staff_to_voice_dict
= {}
656 for (v
,s
) in voice_to_staff_dict
.items ():
657 if not staff_to_voice_dict
.has_key (s
):
658 staff_to_voice_dict
[s
] = [v
]
660 staff_to_voice_dict
[s
].append (v
)
664 assign_to_next_note
= []
667 voice_id
= n
.get_maybe_exist_typed_child (get_class ('voice'))
669 if not (voice_id
or isinstance (n
, Attributes
) or
670 isinstance (n
, Direction
) or isinstance (n
, Partial
) or
671 isinstance (n
, Barline
) or isinstance (n
, Harmony
) or
672 isinstance (n
, FiguredBass
) ):
675 if isinstance (n
, Attributes
) and not start_attr
:
679 if isinstance (n
, Attributes
):
680 # assign these only to the voices they really belongs to!
681 for (s
, vids
) in staff_to_voice_dict
.items ():
682 staff_attributes
= part
.extract_attributes_for_staff (n
, s
)
684 voices
[v
].add_element (staff_attributes
)
687 if isinstance (n
, Partial
) or isinstance (n
, Barline
):
688 for v
in voices
.keys ():
689 voices
[v
].add_element (n
)
692 if isinstance (n
, Direction
):
693 staff_id
= n
.get_maybe_exist_named_child (u
'staff')
695 staff_id
= staff_id
.get_text ()
697 dir_voices
= staff_to_voice_dict
.get (staff_id
, voices
.keys ())
699 dir_voices
= voices
.keys ()
701 voices
[v
].add_element (n
)
704 if isinstance (n
, Harmony
) or isinstance (n
, FiguredBass
):
705 # store the harmony or figured bass element until we encounter
706 # the next note and assign it only to that one voice.
707 assign_to_next_note
.append (n
)
710 id = voice_id
.get_text ()
711 if hasattr (n
, 'print-object') and getattr (n
, 'print-object') == "no":
715 for i
in assign_to_next_note
:
716 voices
[id].add_element (i
)
717 assign_to_next_note
= []
718 voices
[id].add_element (n
)
720 # Assign all remaining elements from assign_to_next_note to the voice
721 # of the previous note:
722 for i
in assign_to_next_note
:
723 voices
[id].add_element (i
)
724 assign_to_next_note
= []
727 for (s
, vids
) in staff_to_voice_dict
.items ():
728 staff_attributes
= part
.extract_attributes_for_staff (start_attr
, s
)
729 staff_attributes
.read_self ()
730 part
._staff
_attributes
_dict
[s
] = staff_attributes
732 voices
[v
].insert (0, staff_attributes
)
733 voices
[v
]._elements
[0].read_self()
735 part
._voices
= voices
737 def get_voices (self
):
739 def get_staff_attributes (self
):
740 return self
._staff
_attributes
_dict
742 class Notations (Music_xml_node
):
744 ts
= self
.get_named_children ('tied')
745 starts
= [t
for t
in ts
if t
.type == 'start']
751 def get_tuplets (self
):
752 return self
.get_typed_children (Tuplet
)
754 class Time_modification(Music_xml_node
):
755 def get_fraction (self
):
756 b
= self
.get_maybe_exist_named_child ('actual-notes')
757 a
= self
.get_maybe_exist_named_child ('normal-notes')
758 return (int(a
.get_text ()), int (b
.get_text ()))
760 class Accidental (Music_xml_node
):
762 Music_xml_node
.__init
__ (self
)
763 self
.editorial
= False
764 self
.cautionary
= False
766 class Music_xml_spanner (Music_xml_node
):
768 if hasattr (self
, 'type'):
773 if hasattr (self
, 'size'):
774 return string
.atoi (self
.size
)
778 class Wedge (Music_xml_spanner
):
781 class Tuplet (Music_xml_spanner
):
784 class Bracket (Music_xml_spanner
):
787 class Dashes (Music_xml_spanner
):
790 class Slur (Music_xml_spanner
):
794 class Beam (Music_xml_spanner
):
796 return self
.get_text ()
797 def is_primary (self
):
798 return self
.number
== "1"
800 class Wavy_line (Music_xml_spanner
):
803 class Pedal (Music_xml_spanner
):
806 class Glissando (Music_xml_spanner
):
809 class Slide (Music_xml_spanner
):
812 class Octave_shift (Music_xml_spanner
):
813 # default is 8 for the octave-shift!
815 if hasattr (self
, 'size'):
816 return string
.atoi (self
.size
)
820 class Chord (Music_xml_node
):
823 class Dot (Music_xml_node
):
826 # Rests in MusicXML are <note> blocks with a <rest> inside. This class is only
827 # for the inner <rest> element, not the whole rest block.
828 class Rest (Music_xml_node
):
830 Music_xml_node
.__init
__ (self
)
831 self
._is
_whole
_measure
= False
832 def is_whole_measure (self
):
833 return self
._is
_whole
_measure
835 ch
= self
.get_maybe_exist_typed_child (get_class (u
'display-step'))
837 step
= ch
.get_text ().strip ()
841 def get_octave (self
):
842 ch
= self
.get_maybe_exist_typed_child (get_class (u
'display-octave'))
844 step
= ch
.get_text ().strip ()
849 class Type (Music_xml_node
):
851 class Grace (Music_xml_node
):
853 class Staff (Music_xml_node
):
856 class Direction (Music_xml_node
):
858 class DirType (Music_xml_node
):
861 class Bend (Music_xml_node
):
862 def bend_alter (self
):
863 alter
= self
.get_maybe_exist_named_child ('bend-alter')
865 return alter
.get_text()
869 class Words (Music_xml_node
):
872 class Harmony (Music_xml_node
):
875 class ChordPitch (Music_xml_node
):
876 def step_class_name (self
):
878 def alter_class_name (self
):
881 ch
= self
.get_unique_typed_child (get_class (self
.step_class_name ()))
882 return ch
.get_text ().strip ()
883 def get_alteration (self
):
884 ch
= self
.get_maybe_exist_typed_child (get_class (self
.alter_class_name ()))
887 alter
= int (ch
.get_text ().strip ())
890 class Root (ChordPitch
):
893 class Bass (ChordPitch
):
894 def step_class_name (self
):
896 def alter_class_name (self
):
899 class ChordModification (Music_xml_node
):
901 ch
= self
.get_maybe_exist_typed_child (get_class (u
'degree-type'))
902 return {'add': 1, 'alter': 1, 'subtract': -1}.get (ch
.get_text ().strip (), 0)
903 def get_value (self
):
904 ch
= self
.get_maybe_exist_typed_child (get_class (u
'degree-value'))
907 value
= int (ch
.get_text ().strip ())
909 def get_alter (self
):
910 ch
= self
.get_maybe_exist_typed_child (get_class (u
'degree-alter'))
913 value
= int (ch
.get_text ().strip ())
917 class Frame (Music_xml_node
):
918 def get_frets (self
):
919 return self
.get_named_child_value_number ('frame-frets', 4)
920 def get_strings (self
):
921 return self
.get_named_child_value_number ('frame-strings', 6)
922 def get_first_fret (self
):
923 return self
.get_named_child_value_number ('first-fret', 1)
925 class Frame_Note (Music_xml_node
):
926 def get_string (self
):
927 return self
.get_named_child_value_number ('string', 1)
929 return self
.get_named_child_value_number ('fret', 0)
930 def get_fingering (self
):
931 return self
.get_named_child_value_number ('fingering', -1)
932 def get_barre (self
):
933 n
= self
.get_maybe_exist_named_child ('barre')
935 return getattr (n
, 'type', '')
939 class FiguredBass (Music_xml_node
):
942 class BeatUnit (Music_xml_node
):
945 class BeatUnitDot (Music_xml_node
):
948 class PerMinute (Music_xml_node
):
953 ## need this, not all classes are instantiated
954 ## for every input file. Only add those classes, that are either directly
955 ## used by class name or extend Music_xml_node in some way!
957 '#comment': Hash_comment
,
959 'accidental': Accidental
,
960 'attributes': Attributes
,
962 'bar-style': BarStyle
,
965 'beat-unit': BeatUnit
,
966 'beat-unit-dot': BeatUnitDot
,
971 'degree' : ChordModification
,
973 'direction': Direction
,
974 'direction-type': DirType
,
975 'duration': Duration
,
977 'frame-note': Frame_Note
,
978 'figured-bass': FiguredBass
,
979 'glissando': Glissando
,
982 'identification': Identification
,
985 'notations': Notations
,
987 'octave-shift': Octave_shift
,
989 'part-group': Part_group
,
990 'part-list': Part_list
,
992 'per-minute': PerMinute
,
996 'score-part': Score_part
,
1000 'syllabic': Syllabic
,
1002 'time-modification': Time_modification
,
1005 'unpitched': Unpitched
,
1006 'wavy-line': Wavy_line
,
1012 def name2class_name (name
):
1013 name
= name
.replace ('-', '_')
1014 name
= name
.replace ('#', 'hash_')
1015 name
= name
[0].upper() + name
[1:].lower()
1019 def get_class (name
):
1020 classname
= class_dict
.get (name
)
1024 class_name
= name2class_name (name
)
1025 klass
= new
.classobj (class_name
, (Music_xml_node
,) , {})
1026 class_dict
[name
] = klass
1029 def lxml_demarshal_node (node
):
1032 # Ignore comment nodes, which are also returned by the etree parser!
1033 if name
is None or node
.__class
__.__name
__ == "_Comment":
1035 klass
= get_class (name
)
1038 py_node
._original
= node
1039 py_node
._name
= name
1040 py_node
._data
= node
.text
1041 py_node
._children
= [lxml_demarshal_node (cn
) for cn
in node
.getchildren()]
1042 py_node
._children
= filter (lambda x
: x
, py_node
._children
)
1044 for c
in py_node
._children
:
1047 for (k
,v
) in node
.items ():
1048 py_node
.__dict
__[k
] = v
1049 py_node
._attribute
_dict
[k
] = v
1053 def minidom_demarshal_node (node
):
1054 name
= node
.nodeName
1056 klass
= get_class (name
)
1058 py_node
._name
= name
1059 py_node
._children
= [minidom_demarshal_node (cn
) for cn
in node
.childNodes
]
1060 for c
in py_node
._children
:
1064 for (nm
, value
) in node
.attributes
.items():
1065 py_node
.__dict
__[nm
] = value
1066 py_node
._attribute
_dict
[nm
] = value
1068 py_node
._data
= None
1069 if node
.nodeType
== node
.TEXT_NODE
and node
.data
:
1070 py_node
._data
= node
.data
1072 py_node
._original
= node
1076 if __name__
== '__main__':
1079 tree
= lxml
.etree
.parse ('beethoven.xml')
1080 mxl_tree
= lxml_demarshal_node (tree
.getroot ())
1081 ks
= class_dict
.keys()
1083 print '\n'.join (ks
)