Browse Source

added xml-parser

Paola Fontana Gasio 4 years ago
parent
commit
5515018931
1 changed files with 48 additions and 0 deletions
  1. 48 0
      src/xml-parser

+ 48 - 0
src/xml-parser

@@ -0,0 +1,48 @@
+from bs4 import BeautifulSoup
+from gizmo_funtions import gizmo_stdout
+from gizmo_transforming_functions import duration_to_string, convert_multiple_key_signetures
+
+def how_many_alter_signs(value):
+    alter_sign = ''
+    if value < 0:
+        value = -value
+        for i in range(value):
+            alter_sign += '-'
+    else:
+        for i in range(value):
+            alter_sign += '#'
+    return alter_sign
+
+
+# Reading the data inside the xml 
+# file to a variable under the name  
+# data 
+with open('../tests/motiv.xml', 'r') as f:
+    data = f.read()
+
+# Passing the stored data inside 
+# the beautifulsoup parser, storing  
+# the returned object
+
+BS_data = BeautifulSoup(data, "xml") 
+  
+# Finding all instances of tag  
+# 'notes'
+notes = BS_data.find_all('note')
+# reading notes and parse to 'gizmo notation'
+working_list = []
+new_note = ''
+for note in notes:
+    duration = float(note.find('duration').text) / float(BS_data.find('divisions').text)
+    duration = duration_to_string(duration)
+    pitch = note.find('pitch')
+    if pitch == None: # cut out pauses does not work yet
+        next
+    else:
+        new_note = duration + ',' + note.find('step').text
+        if note.find('alter') != None:
+            new_note += how_many_alter_signs(int(note.find('alter').text))
+        new_note += note.find('octave').text
+        new_note = convert_multiple_key_signetures(new_note)
+    working_list.append(new_note)
+gizmo_stdout(working_list)