]> git.donarmstrong.com Git - lilypond.git/blob - python/musicxml.py
MusicXML: Fixes of my previous commit
[lilypond.git] / python / musicxml.py
1 # -*- coding: utf-8 -*-
2 import new
3 import string
4 from rational import *
5 import re
6 import sys
7 import copy
8
9 def escape_ly_output_string (input_string):
10     return_string = input_string
11     needs_quotes = not re.match (u"^[a-zA-ZäöüÜÄÖßñ]*$", return_string);
12     if needs_quotes:
13         return_string = "\"" + string.replace (return_string, "\"", "\\\"") + "\""
14     return return_string
15
16
17 class Xml_node:
18     def __init__ (self):
19         self._children = []
20         self._data = None
21         self._original = None
22         self._name = 'xml_node'
23         self._parent = None
24         self._attribute_dict = {}
25         
26     def get_parent (self):
27         return self._parent
28     
29     def is_first (self):
30         return self._parent.get_typed_children (self.__class__)[0] == self
31
32     def original (self):
33         return self._original 
34     def get_name (self):
35         return self._name
36
37     def get_text (self):
38         if self._data:
39             return self._data
40
41         if not self._children:
42             return ''
43
44         return ''.join ([c.get_text () for c in self._children])
45
46     def message (self, msg):
47         sys.stderr.write (msg+'\n')
48
49         p = self
50         while p:
51             sys.stderr.write ('  In: <%s %s>\n' % (p._name, ' '.join (['%s=%s' % item for item in p._attribute_dict.items()])))
52             p = p.get_parent ()
53         
54     def get_typed_children (self, klass):
55         if not klass:
56             return []
57         else:
58             return [c for c in self._children if isinstance(c, klass)]
59
60     def get_named_children (self, nm):
61         return self.get_typed_children (get_class (nm))
62
63     def get_named_child (self, nm):
64         return self.get_maybe_exist_named_child (nm)
65
66     def get_children (self, predicate):
67         return [c for c in self._children if predicate(c)]
68
69     def get_all_children (self):
70         return self._children
71
72     def get_maybe_exist_named_child (self, name):
73         return self.get_maybe_exist_typed_child (get_class (name))
74
75     def get_maybe_exist_typed_child (self, klass):
76         cn = self.get_typed_children (klass)
77         if len (cn)==0:
78             return None
79         elif len (cn) == 1:
80             return cn[0]
81         else:
82             raise "More than 1 child", klass
83
84     def get_unique_typed_child (self, klass):
85         cn = self.get_typed_children(klass)
86         if len (cn) <> 1:
87             sys.stderr.write (self.__dict__ + '\n')
88             raise 'Child is not unique for', (klass, 'found', cn)
89
90         return cn[0]
91
92 class Music_xml_node (Xml_node):
93     def __init__ (self):
94         Xml_node.__init__ (self)
95         self.duration = Rational (0)
96         self.start = Rational (0)
97
98 class Work (Xml_node):
99     def get_work_information (self, tag):
100         wt = self.get_maybe_exist_named_child (tag)
101         if wt:
102             return wt.get_text ()
103         else:
104             return ''
105       
106     def get_work_title (self):
107         return self.get_work_information ('work-title')
108     def get_work_number (self):
109         return self.get_work_information ('work-number')
110     def get_opus (self):
111         return self.get_work_information ('opus')
112
113 class Identification (Xml_node):
114     def get_rights (self):
115         rights = self.get_maybe_exist_named_child ('rights')
116         if rights:
117             return rights.get_text ()
118         else:
119             return ''
120
121     def get_creator (self, type):
122         creators = self.get_named_children ('creator')
123         # return the first creator tag that has the particular type
124         for i in creators:
125             if hasattr (i, 'type') and i.type == type:
126                 return i.get_text ()
127         return None
128
129     def get_composer (self):
130         c = self.get_creator ('composer')
131         if c:
132             return c
133         creators = self.get_named_children ('creator')
134         # return the first creator tag that has no type at all
135         for i in creators:
136             if not hasattr (i, 'type'):
137                 return i.get_text ()
138         return None
139     def get_arranger (self):
140         return self.get_creator ('arranger')
141     def get_editor (self):
142         return self.get_creator ('editor')
143     def get_poet (self):
144         v = self.get_creator ('lyricist')
145         if v:
146             return v
147         v = self.get_creator ('poet')
148         return v
149     
150     def get_encoding_information (self, type):
151         enc = self.get_named_children ('encoding')
152         if enc:
153             children = enc[0].get_named_children (type)
154             if children:
155                 return children[0].get_text ()
156         else:
157             return None
158       
159     def get_encoding_software (self):
160         return self.get_encoding_information ('software')
161     def get_encoding_date (self):
162         return self.get_encoding_information ('encoding-date')
163     def get_encoding_person (self):
164         return self.get_encoding_information ('encoder')
165     def get_encoding_description (self):
166         return self.get_encoding_information ('encoding-description')
167
168
169 class Duration (Music_xml_node):
170     def get_length (self):
171         dur = int (self.get_text ()) * Rational (1,4)
172         return dur
173
174 class Hash_comment (Music_xml_node):
175     pass
176 class Hash_text (Music_xml_node):
177     pass
178
179 class Pitch (Music_xml_node):
180     def get_step (self):
181         ch = self.get_unique_typed_child (get_class (u'step'))
182         step = ch.get_text ().strip ()
183         return step
184     def get_octave (self):
185         ch = self.get_unique_typed_child (get_class (u'octave'))
186
187         step = ch.get_text ().strip ()
188         return int (step)
189
190     def get_alteration (self):
191         ch = self.get_maybe_exist_typed_child (get_class (u'alter'))
192         alter = 0
193         if ch:
194             alter = int (ch.get_text ().strip ())
195         return alter
196
197 class Measure_element (Music_xml_node):
198     def get_voice_id (self):
199         voice_id = self.get_maybe_exist_named_child ('voice')
200         if voice_id:
201             return voice_id.get_text ()
202         else:
203             return None
204
205     def is_first (self):
206         cn = self._parent.get_typed_children (self.__class__)
207         cn = [c for c in cn if c.get_voice_id () == self.get_voice_id ()]
208         return cn[0] == self
209
210 class Attributes (Measure_element):
211     def __init__ (self):
212         Measure_element.__init__ (self)
213         self._dict = {}
214
215     def set_attributes_from_previous (self, dict):
216         self._dict.update (dict)
217         
218     def read_self (self):
219         for c in self.get_all_children ():
220             self._dict[c.get_name()] = c
221
222     def get_named_attribute (self, name):
223         return self._dict.get (name)
224
225     def get_measure_length (self):
226         (n,d) = self.get_time_signature ()
227         return Rational (n,d)
228         
229     def get_time_signature (self):
230         "return time sig as a (beat, beat-type) tuple"
231
232         try:
233             mxl = self.get_named_attribute ('time')
234             if mxl:
235                 beats = mxl.get_maybe_exist_named_child ('beats')
236                 type = mxl.get_maybe_exist_named_child ('beat-type')
237                 return (int (beats.get_text ()),
238                         int (type.get_text ()))
239             else:
240                 return (4, 4)
241         except KeyError:
242             sys.stderr.write ('error: requested time signature, but time sig unknown\n')
243             return (4, 4)
244
245     # returns clef information in the form ("cleftype", position, octave-shift)
246     def get_clef_information (self):
247         clefinfo = ['G', 2, 0]
248         mxl = self.get_named_attribute ('clef')
249         if not mxl:
250             return clefinfo
251         sign = mxl.get_maybe_exist_named_child ('sign')
252         if sign:
253             clefinfo[0] = sign.get_text()
254         line = mxl.get_maybe_exist_named_child ('line')
255         if line:
256             clefinfo[1] = string.atoi (line.get_text ())
257         octave = mxl.get_maybe_exist_named_child ('clef-octave-change')
258         if octave:
259             clefinfo[2] = string.atoi (octave.get_text ())
260         return clefinfo
261
262     def get_key_signature (self):
263         "return (fifths, mode) tuple"
264
265         key = self.get_named_attribute ('key')
266         mode_node = key.get_maybe_exist_named_child ('mode')
267         mode = 'major'
268         if mode_node:
269             mode = mode_node.get_text ()
270
271         fifths = int (key.get_maybe_exist_named_child ('fifths').get_text ())
272         return (fifths, mode)
273                 
274
275 class Note (Measure_element):
276     def __init__ (self):
277         Measure_element.__init__ (self)
278         self.instrument_name = ''
279         
280     def get_duration_log (self):
281         ch = self.get_maybe_exist_typed_child (get_class (u'type'))
282
283         if ch:
284             log = ch.get_text ().strip()
285             return {'256th': 8,
286                     '128th': 7,
287                     '64th': 6,
288                     '32nd': 5,
289                     '16th': 4,
290                     'eighth': 3,
291                     'quarter': 2,
292                     'half': 1,
293                     'whole': 0,
294                     'breve': -1,
295                     'long': -2}.get (log, 0)
296         else:
297             sys.stderr.write ("Encountered note without duration (no <type> element): %s\n" % self)
298             return 0
299
300     def get_factor (self):
301         return 1
302
303     def get_pitches (self):
304         return self.get_typed_children (get_class (u'pitch'))
305
306 class Part_list (Music_xml_node):
307     def __init__ (self):
308         Music_xml_node.__init__ (self)
309         self._id_instrument_name_dict = {}
310         
311     def generate_id_instrument_dict (self):
312
313         ## not empty to make sure this happens only once.
314         mapping = {1: 1}
315         for score_part in self.get_named_children ('score-part'):
316             for instr in score_part.get_named_children ('score-instrument'):
317                 id = instr.id
318                 name = instr.get_named_child ("instrument-name")
319                 mapping[id] = name.get_text ()
320
321         self._id_instrument_name_dict = mapping
322
323     def get_instrument (self, id):
324         if not self._id_instrument_name_dict:
325             self.generate_id_instrument_dict()
326
327         instrument_name = self._id_instrument_name_dict.get (id)
328         if instrument_name:
329             return instrument_name
330         else:
331             sys.stderr.write ("Opps, couldn't find instrument for ID=%s\n" % id)
332             return "Grand Piano"
333
334 class Part_group (Music_xml_node):
335     pass
336 class Score_part (Music_xml_node):
337     pass
338         
339 class Measure (Music_xml_node):
340     def get_notes (self):
341         return self.get_typed_children (get_class (u'note'))
342
343 class Syllabic (Music_xml_node):
344     def continued (self):
345         text = self.get_text()
346         return (text == "begin") or (text == "middle")
347 class Text (Music_xml_node):
348     pass
349
350 class Lyric (Music_xml_node):
351     def get_number (self):
352         if hasattr (self, 'number'):
353             return self.number
354         else:
355             return -1
356
357     def lyric_to_text (self):
358         continued = False
359         syllabic = self.get_maybe_exist_typed_child (Syllabic)
360         if syllabic:
361             continued = syllabic.continued ()
362         text = self.get_maybe_exist_typed_child (Text)
363         
364         if text:
365             text = text.get_text()
366             # We need to convert soft hyphens to -, otherwise the ascii codec as well
367             # as lilypond will barf on that character
368             text = string.replace( text, u'\xad', '-' )
369         
370         if text == "-" and continued:
371             return "--"
372         elif text == "_" and continued:
373             return "__"
374         elif continued and text:
375             return escape_ly_output_string (text) + " --"
376         elif continued:
377             return "--"
378         elif text:
379             return escape_ly_output_string (text)
380         else:
381             return ""
382
383 class Musicxml_voice:
384     def __init__ (self):
385         self._elements = []
386         self._staves = {}
387         self._start_staff = None
388         self._lyrics = []
389         self._has_lyrics = False
390
391     def add_element (self, e):
392         self._elements.append (e)
393         if (isinstance (e, Note)
394             and e.get_maybe_exist_typed_child (Staff)):
395             name = e.get_maybe_exist_typed_child (Staff).get_text ()
396
397             if not self._start_staff and not e.get_maybe_exist_typed_child (Grace):
398                 self._start_staff = name
399             self._staves[name] = True
400
401         lyrics = e.get_typed_children (Lyric)
402         if not self._has_lyrics:
403           self.has_lyrics = len (lyrics) > 0
404
405         for l in lyrics:
406             nr = l.get_number()
407             if (nr > 0) and not (nr in self._lyrics):
408                 self._lyrics.append (nr)
409
410     def insert (self, idx, e):
411         self._elements.insert (idx, e)
412
413     def get_lyrics_numbers (self):
414         if (len (self._lyrics) == 0) and self._has_lyrics:
415             #only happens if none of the <lyric> tags has a number attribute
416             return [1]
417         else:
418             return self._lyrics
419
420
421
422 class Part (Music_xml_node):
423     def __init__ (self):
424         Music_xml_node.__init__ (self)
425         self._voices = []
426
427     def get_part_list (self):
428         n = self
429         while n and n.get_name() != 'score-partwise':
430             n = n._parent
431
432         return n.get_named_child ('part-list')
433         
434     def interpret (self):
435         """Set durations and starting points."""
436         
437         part_list = self.get_part_list ()
438         
439         now = Rational (0)
440         factor = Rational (1)
441         attributes_dict = {}
442         attributes_object = None
443         measures = self.get_typed_children (Measure)
444         last_moment = Rational (-1)
445         last_measure_position = Rational (-1)
446         for m in measures:
447             measure_start_moment = now
448             measure_position = Rational (0)
449             for n in m.get_all_children ():
450                 if isinstance (n, Hash_text):
451                     continue
452                 dur = Rational (0)
453
454                 if n.__class__ == Attributes:
455                     n.set_attributes_from_previous (attributes_dict)
456                     n.read_self ()
457                     attributes_dict = n._dict.copy ()
458                     attributes_object = n
459                     
460                     factor = Rational (1,
461                                        int (attributes_dict.get ('divisions').get_text ()))
462
463                 
464                 if (n.get_maybe_exist_typed_child (Duration)):
465                     mxl_dur = n.get_maybe_exist_typed_child (Duration)
466                     dur = mxl_dur.get_length () * factor
467                     
468                     if n.get_name() == 'backup':
469                         dur = - dur
470                     if n.get_maybe_exist_typed_child (Grace):
471                         dur = Rational (0)
472
473                     rest = n.get_maybe_exist_typed_child (Rest)
474                     if (rest
475                         and attributes_object
476                         and attributes_object.get_measure_length () == dur):
477
478                         rest._is_whole_measure = True
479
480                 if (dur > Rational (0) 
481                     and n.get_maybe_exist_typed_child (Chord)):
482                     now = last_moment
483                     measure_position = last_measure_position
484
485                 n._when = now
486                 n._measure_position = measure_position
487                 n._duration = dur
488                 if dur > Rational (0):
489                     last_moment = now
490                     last_measure_position = measure_position
491                     now += dur
492                     measure_position += dur
493                 elif dur < Rational (0):
494                     # backup element, reset measure position
495                     now += dur
496                     measure_position += dur
497                     if measure_position < 0:
498                         # backup went beyond the measure start => reset to 0
499                         now -= measure_position
500                         measure_position = 0
501                     last_moment = now
502                     last_measure_position = measure_position
503                 if n._name == 'note':
504                     instrument = n.get_maybe_exist_named_child ('instrument')
505                     if instrument:
506                         n.instrument_name = part_list.get_instrument (instrument.id)
507
508             if attributes_object:
509                 length = attributes_object.get_measure_length ()
510                 new_now = measure_start_moment + length
511                 
512                 if now <> new_now:
513                     problem = 'incomplete'
514                     if now > new_now:
515                         problem = 'overfull'
516
517                     ## only for verbose operation.
518                     if problem <> 'incomplete':
519                         m.message ('%s measure? Expected: %s, Difference: %s' % (problem, now, new_now - now))
520
521                 now = new_now
522
523     # modify attributes so that only those applying to the given staff remain
524     def extract_attributes_for_staff (part, attr, staff):
525         attributes = copy.copy (attr)
526         attributes._children = copy.copy (attr._children)
527         attributes._dict = attr._dict.copy ()
528         for c in attributes._children:
529             if hasattr (c, 'number') and c.number != staff:
530                 attributes._children.remove (c)
531         return attributes
532
533     def extract_voices (part):
534         voices = {}
535         measures = part.get_typed_children (Measure)
536         elements = []
537         for m in measures:
538             elements.extend (m.get_all_children ())
539         # make sure we know all voices already so that dynamics, clefs, etc.
540         # can be assigned to the correct voices
541         voice_to_staff_dict = {}
542         for n in elements:
543             voice_id = n.get_maybe_exist_named_child (u'voice')
544             vid = None
545             if voice_id:
546                 vid = voice_id.get_text ()
547
548             staff_id = n.get_maybe_exist_named_child (u'staff')
549             sid = None
550             if staff_id:
551                 sid = staff_id.get_text ()
552             else:
553                 sid = "None"
554             if vid and not voices.has_key (vid):
555                 voices[vid] = Musicxml_voice()
556             if vid and sid and not n.get_maybe_exist_typed_child (Grace):
557                 if not voice_to_staff_dict.has_key (vid):
558                     voice_to_staff_dict[vid] = sid
559         # invert the voice_to_staff_dict into a staff_to_voice_dict (since we
560         # need to assign staff-assigned objects like clefs, times, etc. to
561         # all the correct voices. This will never work entirely correct due
562         # to staff-switches, but that's the best we can do!
563         staff_to_voice_dict = {}
564         for (v,s) in voice_to_staff_dict.items ():
565             if not staff_to_voice_dict.has_key (s):
566                 staff_to_voice_dict[s] = [v]
567             else:
568                 staff_to_voice_dict[s].append (v)
569
570
571         start_attr = None
572         for n in elements:
573             voice_id = n.get_maybe_exist_typed_child (get_class ('voice'))
574
575             # TODO: If the first element of a voice is a dynamics entry,
576             #       then voice_id is not yet set! Thus it will currently be ignored
577             if not (voice_id or isinstance (n, Attributes) or isinstance (n, Direction) ):
578                 continue
579
580             if isinstance (n, Attributes) and not start_attr:
581                 start_attr = n
582                 continue
583
584             if isinstance (n, Attributes):
585                 # assign these only to the voices they really belongs to!
586                 for (s, vids) in staff_to_voice_dict.items ():
587                     staff_attributes = part.extract_attributes_for_staff (n, s)
588                     for v in vids:
589                         voices[v].add_element (staff_attributes)
590                 continue
591
592             if isinstance (n, Direction):
593                 staff_id = n.get_maybe_exist_named_child (u'staff')
594                 if staff_id:
595                     staff_id = staff_id.get_text ()
596                 if staff_id:
597                     dir_voices = staff_to_voice_dict.get (staff_id, voices.keys ())
598                 else:
599                     dir_voices = voices.keys ()
600                 for v in dir_voices:
601                     voices[v].add_element (n)
602                 continue
603
604             id = voice_id.get_text ()
605             voices[id].add_element (n)
606
607         if start_attr:
608             for (s, vids) in staff_to_voice_dict.items ():
609                 staff_attributes = part.extract_attributes_for_staff (start_attr, s)
610                 staff_attributes.read_self ()
611                 for v in vids:
612                     voices[v].insert (0, staff_attributes)
613                     voices[v]._elements[0].read_self()
614
615         part._voices = voices
616
617     def get_voices (self):
618         return self._voices
619
620 class Notations (Music_xml_node):
621     def get_tie (self):
622         ts = self.get_named_children ('tied')
623         starts = [t for t in ts if t.type == 'start']
624         if starts:
625             return starts[0]
626         else:
627             return None
628
629     def get_tuplet (self):
630         return self.get_maybe_exist_typed_child (Tuplet)
631
632 class Time_modification(Music_xml_node):
633     def get_fraction (self):
634         b = self.get_maybe_exist_named_child ('actual-notes')
635         a = self.get_maybe_exist_named_child ('normal-notes')
636         return (int(a.get_text ()), int (b.get_text ()))
637
638 class Accidental (Music_xml_node):
639     def __init__ (self):
640         Music_xml_node.__init__ (self)
641         self.editorial = False
642         self.cautionary = False
643
644 class Music_xml_spanner (Music_xml_node):
645     def get_type (self):
646         if hasattr (self, 'type'):
647             return self.type
648         else:
649             return 0
650     def get_size (self):
651         if hasattr (self, 'size'):
652             return string.atoi (self.size)
653         else:
654             return 0
655
656 class Wedge (Music_xml_spanner):
657     pass
658
659 class Tuplet (Music_xml_spanner):
660     pass
661
662 class Slur (Music_xml_spanner):
663     def get_type (self):
664         return self.type
665
666 class Beam (Music_xml_spanner):
667     def get_type (self):
668         return self.get_text ()
669     def is_primary (self):
670         return self.number == "1"
671
672 class Wavy_line (Music_xml_spanner):
673     pass
674     
675 class Pedal (Music_xml_spanner):
676     pass
677
678 class Glissando (Music_xml_spanner):
679     pass
680
681 class Octave_shift (Music_xml_spanner):
682     # default is 8 for the octave-shift!
683     def get_size (self):
684         if hasattr (self, 'size'):
685             return string.atoi (self.size)
686         else:
687             return 8
688
689 class Chord (Music_xml_node):
690     pass
691
692 class Dot (Music_xml_node):
693     pass
694
695 # Rests in MusicXML are <note> blocks with a <rest> inside. This class is only
696 # for the inner <rest> element, not the whole rest block.
697 class Rest (Music_xml_node):
698     def __init__ (self):
699         Music_xml_node.__init__ (self)
700         self._is_whole_measure = False
701     def is_whole_measure (self):
702         return self._is_whole_measure
703     def get_step (self):
704         ch = self.get_maybe_exist_typed_child (get_class (u'display-step'))
705         if ch:
706             step = ch.get_text ().strip ()
707             return step
708         else:
709             return None
710     def get_octave (self):
711         ch = self.get_maybe_exist_typed_child (get_class (u'display-octave'))
712         if ch:
713             step = ch.get_text ().strip ()
714             return int (step)
715         else:
716             return None
717
718 class Type (Music_xml_node):
719     pass
720 class Grace (Music_xml_node):
721     pass
722 class Staff (Music_xml_node):
723     pass
724
725 class Direction (Music_xml_node):
726     pass
727 class DirType (Music_xml_node):
728     pass
729
730 class Bend (Music_xml_node):
731     def bend_alter (self):
732         alter = self.get_maybe_exist_named_child ('bend-alter')
733         if alter:
734             return alter.get_text()
735         else:
736             return 0
737
738
739
740 ## need this, not all classes are instantiated
741 ## for every input file. Only add those classes, that are either directly
742 ## used by class name or extend Music_xml_node in some way!
743 class_dict = {
744         '#comment': Hash_comment,
745         '#text': Hash_text,
746         'accidental': Accidental,
747         'attributes': Attributes,
748         'beam' : Beam,
749         'bend' : Bend,
750         'chord': Chord,
751         'dot': Dot,
752         'direction': Direction,
753         'direction-type': DirType,
754         'duration': Duration,
755         'glissando': Glissando,
756         'grace': Grace,
757         'identification': Identification,
758         'lyric': Lyric,
759         'measure': Measure,
760         'notations': Notations,
761         'note': Note,
762         'octave-shift': Octave_shift,
763         'part': Part,
764     'part-group': Part_group,
765         'part-list': Part_list,
766         'pedal': Pedal,
767         'pitch': Pitch,
768         'rest': Rest,
769     'score-part': Score_part,
770         'slur': Slur,
771         'staff': Staff,
772         'syllabic': Syllabic,
773         'text': Text,
774         'time-modification': Time_modification,
775         'tuplet': Tuplet,
776         'type': Type,
777         'wavy-line': Wavy_line,
778         'wedge': Wedge,
779         'work': Work,
780 }
781
782 def name2class_name (name):
783     name = name.replace ('-', '_')
784     name = name.replace ('#', 'hash_')
785     name = name[0].upper() + name[1:].lower()
786
787     return str (name)
788
789 def get_class (name):
790     classname = class_dict.get (name)
791     if classname:
792         return classname
793     else:
794         class_name = name2class_name (name)
795         klass = new.classobj (class_name, (Music_xml_node,) , {})
796         class_dict[name] = klass
797         return klass
798         
799 def lxml_demarshal_node (node):
800     name = node.tag
801
802     if name is None:
803         return None
804     klass = get_class (name)
805     py_node = klass()
806     
807     py_node._original = node
808     py_node._name = name
809     py_node._data = node.text
810     py_node._children = [lxml_demarshal_node (cn) for cn in node.getchildren()]
811     py_node._children = filter (lambda x: x, py_node._children)
812     
813     for c in py_node._children:
814         c._parent = py_node
815
816     for (k,v) in node.items ():
817         py_node.__dict__[k] = v
818         py_node._attribute_dict[k] = v
819
820     return py_node
821
822 def minidom_demarshal_node (node):
823     name = node.nodeName
824
825     klass = get_class (name)
826     py_node = klass()
827     py_node._name = name
828     py_node._children = [minidom_demarshal_node (cn) for cn in node.childNodes]
829     for c in py_node._children:
830         c._parent = py_node
831
832     if node.attributes:
833         for (nm, value) in node.attributes.items():
834             py_node.__dict__[nm] = value
835             py_node._attribute_dict[nm] = value
836             
837     py_node._data = None
838     if node.nodeType == node.TEXT_NODE and node.data:
839         py_node._data = node.data 
840
841     py_node._original = node
842     return py_node
843
844
845 if __name__  == '__main__':
846         import lxml.etree
847         
848         tree = lxml.etree.parse ('beethoven.xml')
849         mxl_tree = lxml_demarshal_node (tree.getroot ())
850         ks = class_dict.keys()
851         ks.sort()
852         print '\n'.join (ks)