changeset 43:c69e53b8917c

Read in per-job options from an ini file and add main funciton so you can do.. env PYTHONPATH=$HOME/lib/python python ./musiccutter.py music/Rosy\'s\ Angel\ Lullaby.mid music/Rosy\'s\ Angel\ Lullaby.ini orgues-de-barbarie-27.ini music
author Daniel O'Connor <darius@dons.net.au>
date Tue, 28 Jun 2016 17:58:32 +0930
parents 3925ac56d99e
children 160fcf0a2f83
files musiccutter.py
diffstat 1 files changed, 45 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/musiccutter.py	Tue May 24 19:42:02 2016 +0930
+++ b/musiccutter.py	Tue Jun 28 17:58:32 2016 +0930
@@ -22,6 +22,47 @@
     def __init__(self, ev):
         self.ev = ev
 
+optionlist = { 'leadin' : int, 'timemarks' : bool, 'trytranspose' : bool, 'drawrect' : bool, 'notenames' : bool,
+    'notelines' : bool, 'noteoffset' : int, 'pagesperpdf' : int, 'timescale' : float,
+    'notescale' : float, 'fontname' : str, 'fontsize' : float }
+
+def main():
+    if len(sys.argv) != 5:
+        print('Bad usage')
+        print('	%s music.midi config.ini organconf.ini destdir' % (sys.argv[0]))
+        sys.exit(1)
+
+    filename, config, organconf, destdir = sys.argv[1:]
+    cp = ConfigParser.ConfigParser()
+    cp.read(config)
+    args = {}
+    for opt in optionlist:
+        print opt
+        if not cp.has_option('default', opt):
+            print('Missing option', opt)
+            sys.exit(1)
+        if optionlist[opt] == int:
+            fn = cp.getint
+        elif optionlist[opt] == float:
+            fn = cp.getfloat
+        elif optionlist[opt] == bool:
+            fn = cp.getboolean
+        elif optionlist[opt] == str:
+            fn = cp.get
+        else:
+            print('unknown type')
+            sys.exit(1)
+        try:
+            args[opt] = fn('default', opt)
+        except ValueError:
+            print('Error parsing', opt)
+            sys.exit(1)
+    args['organconf'] = organconf
+    m = Midi2PDF(**args)
+    base, ext = os.path.splitext(filename)
+    base = os.path.basename(base)
+    m.processMidi(filename, destdir + '/' + base + '-%02d.pdf')
+
 def test(filename = None, shift = 0):
     if filename == None:
         filename = 'test.midi'
@@ -36,7 +77,7 @@
     # +---+---+---+ highest note
     #
     m = Midi2PDF(**{
-        'config' : 'orgues-de-barbarie-27.ini',
+        'organconf' : 'orgues-de-barbarie-27.ini',
         'leadin' : 50,
         'timemarks' : False,
         'trytranspose' : True,
@@ -45,7 +86,7 @@
         'notelines' : False,
         'noteoffset' : shift,
         'pagesperpdf' : 1,
-        'timescale' : 30,
+        'timescale' : 35,
         'notescale' : 0.9,
         'fontname' : 'Helvetica',
         'fontsize' : 12,
@@ -55,9 +96,9 @@
     m.processMidi(filename, base + '-%02d.pdf')
 
 class Midi2PDF(object):
-    def __init__(self, config, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize):
+    def __init__(self, organconf, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize):
         cp = ConfigParser.ConfigParser()
-        cp.read(config)
+        cp.read(organconf)
         self.pagewidth = cp.getfloat('default', 'pagewidth')
         self.pageheight = cp.getfloat('default', 'pageheight')
         self.pitch = cp.getfloat('default', 'pitch')