- add commandline interface
authorwim
Mon, 25 May 2020 12:34:01 +0200
changeset 2 775a1d0d3be6
parent 1 7fd6cac1a69d
child 3 b89a27b9bd9e
- add commandline interface
.hgignore
xmlChordAna.py
--- a/.hgignore	Mon May 25 10:39:18 2020 +0200
+++ b/.hgignore	Mon May 25 12:34:01 2020 +0200
@@ -0,0 +1,3 @@
+syntax: glob
+*.pyc
+*.xml
--- a/xmlChordAna.py	Mon May 25 10:39:18 2020 +0200
+++ b/xmlChordAna.py	Mon May 25 12:34:01 2020 +0200
@@ -1,5 +1,7 @@
-import fractions as F
-import xml2b40
+#!/usr/bin/env python
+# coding: latin-1
+
+import fractions, os, xml2b40
 import segLibB40 as S
 
 def keySeg (iseg,jseg):
@@ -30,16 +32,25 @@
         regels.append ('---- chord %s, score: %.0f%%' % (chord, percnt))
         for tikken, noten in grpsInSeg:
             noten = ', '.join ([S.b40nm (n) + str(n / 40) for n in noten])
-            x = F.Fraction (tikken, tikkenPerKwart)
-            n, d = x.numerator, x.denominator
-            t = n / d
+            x = fractions.Fraction (tikken, tikkenPerKwart)
+            t = x.numerator / x.denominator
             r = x - t
             if r == 0: r = ''
             regels.append ('%4d %3s %s' % (t, r, noten))
     return regels    
 
 if __name__ == '__main__':
-    fnm = 'mids/bwv539p_arr.xml'
-    noten = xml2b40.xml2b40 (fnm)
-    regels = mkAna (noten, fnm)
-    print '\n'.join (regels)
\ No newline at end of file
+    from optparse import OptionParser
+    parser = OptionParser (usage='%prog [-h] [-g TPQ] [--C5] <file1>')
+    parser.add_option ("-t", action="store", type="int", help="ticks per quarternote", default=384, metavar='TPQ')
+    parser.add_option ("--C5", action="store_true", help="central C is C5", default=False)
+    options, args = parser.parse_args ()
+    if len (args) < 1: parser.error ('file argument needed')
+    fnmext = args [0]
+    fnm, ext = os.path.splitext (fnmext)
+    if ext != '.xml': parser.error ('.xml file needed file needed')
+    if not os.path.exists (fnmext): parser.error ('%s does not exist' % fnmext)
+
+    noten = xml2b40.xml2b40 (fnmext, options.t, options.C5)
+    regels = mkAna (noten, fnmext)
+    print '\n'.join (regels)