Browse Source

check gizmo notation before use a motiv

Paola Fontana Gasio 4 years ago
parent
commit
062574218e
2 changed files with 44 additions and 1 deletions
  1. 40 0
      src/gizmo_funtions.py
  2. 4 1
      src/song-gen

+ 40 - 0
src/gizmo_funtions.py

@@ -73,3 +73,43 @@ def diff_char(text):
         else:
         else:
             char_list.append(char)
             char_list.append(char)
     return char_list
     return char_list
+
+# This function checks if the given tempi is in correct notation and
+# returns a boolean value
+def check_tempo_gizmo_notation(tempo):
+    if len(tempo) != 4:
+        return False
+    elif tempo[1:2] != '.':
+        return False
+    else:
+        tempo = tempo[:1] + tempo[2:]
+        if not tempo.isdigit():
+            return False
+        else:
+             return True
+
+# this function deletes whitespaces from gizmo notation and return a boolean value if 
+# it is okay or not.
+def check_gizmo_notation(name):
+    with open(name, "r") as file_opener:
+        content = file_opener.read()
+    content = content.replace(" ", "")
+    content = content.strip()
+    content_list = content.split('\n')
+    # remove all empty notes
+    working_list = content_list[:]
+    for i in content_list:
+        if i == '':
+            working_list.remove('')
+    content_list = working_list
+    gizmo_write2file(content_list, name)
+    try:
+        for line in content_list:
+            parts = line.split(',')
+            if not check_pitch_gizmo_notation(parts[1]):
+                return False
+            if not check_tempo_gizmo_notation(parts[0]):
+                return False
+        return True
+    except(IndexError):
+        return False

+ 4 - 1
src/song-gen

@@ -25,6 +25,8 @@ except (IndexError, ValueError):
 # get commandline argument motiv's which already are determined
 # get commandline argument motiv's which already are determined
 try:
 try:
     already_given = str(sys.argv[2])
     already_given = str(sys.argv[2])
+    # strip whitespaces
+    already_given = already_given.strip()
     # raise exception if not letter only
     # raise exception if not letter only
     if not already_given.isalpha():
     if not already_given.isalpha():
         raise ValueError("Does not contain only letters")
         raise ValueError("Does not contain only letters")
@@ -42,6 +44,8 @@ for i in already_given:
     name = 'motiv-' + i
     name = 'motiv-' + i
     if not path.exists(name):
     if not path.exists(name):
         working_list.remove(i)
         working_list.remove(i)
+    elif not check_gizmo_notation(name):
+        working_list.remove(i)
 already_given = working_list
 already_given = working_list
 
 
 # format the remaining (motiv) argument in a list;
 # format the remaining (motiv) argument in a list;
@@ -101,7 +105,6 @@ def motiv_gen(motiv_commandline_argument, name):
 
 
 """this program should produce a little song without accompaniment yet"""
 """this program should produce a little song without accompaniment yet"""
 
 
-
 # compute missing motives
 # compute missing motives
 for i in range(len(diff_motiv_list)):
 for i in range(len(diff_motiv_list)):
     name = 'motiv-' + diff_motiv_list[i]
     name = 'motiv-' + diff_motiv_list[i]