From 9e929a9e042bc32daabfae8b8ce78dd2ec0aa612 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Fri, 3 May 2019 15:28:24 +0200 Subject: [PATCH] Small .ini cleanup and tackling patter issues --- mapping/GrandMA2.py | 35 ++++++++++++++++++++++++++++++++--- patch/default.ini | 6 +++--- test.py | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/mapping/GrandMA2.py b/mapping/GrandMA2.py index a0c557e..96b23ae 100644 --- a/mapping/GrandMA2.py +++ b/mapping/GrandMA2.py @@ -1,6 +1,6 @@ from mapping import BasicMessageParse,BasicAction import mido -import re +import re,configparser commandlist={1:"go",2:"stop",3:"resume",4:"timed_go",6:"set",7:"fire",11:"go_off"} #A MessageParse object MUST be included in the file, the rest is implementation Specific @@ -26,14 +26,43 @@ class MessageParse(BasicMessageParse): else: raise MatchError +#An Action object MUST be included in the file. +#Called first with a config file, will be callable to launch an action with the matched regex as an argument class Action(BasicAction): """The list of action to perform when a message is read""" def __init__(self,config): """Parse the config and ready all return actions""" - self.config=config - # Load config + # Load config, in a near future, the config will be preloaded beforehand, but for right now, practicallity will prevail + self.config=configparser.ConfigParser() + self.config.read(config) + self.sections={} # Parse config & check sanity + if "interface" not in self.config: + raise IOError #You must define an interface to connect to + # Create all the call setups + for elt in self.config.sections(): + if elt !="interface": + if "/" in elt: + selt=elt.split("/") + if selt[0] not in self.sections: + self.sections[selt[0]]={} + self.sections[selt[0]].update({selt[1]:self.config[elt]}) + else: + if elt in self.sections: + if "*" not in self.sections[elt]: + self.sections[elt]["*"]=self.config[elt] + # This means you will override any "/x" config + else: + self.sections[elt]={"*":self.config[elt]} + + + def format(self,command,number,page): + "Format a match to call the correct action" + try: + return self.sections[command][page] + except: + return self.sections[command]["*"] def __call__(self,match): """Launch the action depending on the config, the config format is supposed to be as canon as possible diff --git a/patch/default.ini b/patch/default.ini index 1277840..5a3e672 100644 --- a/patch/default.ini +++ b/patch/default.ini @@ -8,13 +8,13 @@ mapping = GrandMA2 #Patching faders -[patch:1] #Patches layer 1 patch:2 would patch layer 2 and so on +[set/1] #Patches layer 1 patch:2 would patch layer 2 and so on 1 = cc/10 #Send (midi) control change (10) with value mapped to the fader (1) value (fader 1 => cc 10) 5-50 = cc/11 #Map the faders 5-50 to the control change 11-45 (Straight patch) 56 = note/152 #Map fader 56 to note 152 -56/val = lambda f: x>50 #Function choosing the value to send instead of a simple mapping +57 = val/lambda f: x>50 #Function choosing the value to send instead of a simple mapping -[executor] +[fire] #No layer specified 13 = note/10 15 = cc/44/127 #Send control change with value 127 17-27 = cc/45/127 # Send control change 45-55 with value 127 diff --git a/test.py b/test.py index 8248a9d..ffe8e05 100644 --- a/test.py +++ b/test.py @@ -73,6 +73,30 @@ class mappingTest(minitest.simpleTestUnit): else: self.addFailure("Incorrect fields: "+inc[:-1]) + self.currentTest("Testing Config") + try: + self.act=GrandMA2.Action("patch/default.ini") + self.addSuccess() + except IOError: + self.addFailure("Can't load config file") + + self.currentTest("Testing the formatter") + try: + setsect=self.act.format("set","1","1") + try: + self.act.format("set","2","3") + self.addFailure("Not supposed to load page 3") + except: + try: + self.act.format("fire","2","11") + self.addSuccess() + except: + self.addFailure("Can't acess the fire section") + except: + self.addFailure("Can't get (set 1 on page 1)") + + + def testParse(self,message): try: parsed=self.parser(message)