changeset 1376:d9511f9e04d7 dev/refactor-piper-related

Introduce some POD structs for describing an external server application and the desired libraries to load from it, and disambiguating between empty list request and invalid list request. This allows for overriding PiperVampPluginFactory behaviour for using a PluginScan to populate the list request.
author Lucas Thompson <lucas.thompson@qmul.ac.uk>
date Fri, 10 Feb 2017 11:15:19 +0000
parents da5f4d37988d
children f1c4ca3ccaa3
files plugin/PiperVampPluginFactory.cpp plugin/PiperVampPluginFactory.h
diffstat 2 files changed, 100 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/plugin/PiperVampPluginFactory.cpp	Thu Feb 09 14:32:07 2017 +0000
+++ b/plugin/PiperVampPluginFactory.cpp	Fri Feb 10 11:15:19 2017 +0000
@@ -39,6 +39,7 @@
 #include <QCoreApplication>
 
 #include <iostream>
+#include <algorithm>
 
 #include "base/Profiler.h"
 #include "base/HelperExecPath.h"
@@ -54,14 +55,17 @@
     }
 };
 
-PiperVampPluginFactory::PiperVampPluginFactory(std::initializer_list<QString> servers) :
+PiperVampPluginFactory::PiperVampPluginFactory(std::initializer_list<ServerDescription> servers) :
     m_logger(new Logger)
 {
     HelperExecPath hep(HelperExecPath::AllInstalled);
 
     for (auto server: servers) {
-        for (auto platformHelper: hep.getHelperExecutables(server))
+        for (auto platformHelper: hep.getHelperExecutables(QString::fromStdString(server.name)))
             m_servers.push_back(platformHelper);
+        if (server.hasDesiredExtractors) {
+            setDesiredExtractors(server.name, server.extractors);
+        }
     }
 
     for (auto n: m_servers) {
@@ -72,21 +76,36 @@
     if (m_servers.empty()) {
         SVDEBUG << "NOTE: No Piper Vamp servers found in installation;" 
                 << " found none of the following:" << endl;
-        for (auto serverName: servers)
-            for (auto d: hep.getHelperCandidatePaths(serverName)) {
+        for (auto server: servers)
+            for (auto d: hep.getHelperCandidatePaths(QString::fromStdString(server.name))) {
                 SVDEBUG << "NOTE: " << d << endl;
             }
     }
 }
 
 PiperVampPluginFactory::PiperVampPluginFactory() :
-    PiperVampPluginFactory({"piper-vamp-simple-server"}) {}
+    PiperVampPluginFactory({{"piper-vamp-simple-server"}}) {}
 
 PiperVampPluginFactory::~PiperVampPluginFactory()
 {
     delete m_logger;
 }
 
+void 
+PiperVampPluginFactory::setDesiredExtractors(ServerName name, 
+                                             DesiredExtractors extractors)
+{
+    const bool isValidServerName = std::find_if(
+        m_servers.begin(), 
+        m_servers.end(), 
+        [&name](const HelperExecPath::HelperExec &h) -> bool {
+            return QFileInfo(h.executable).fileName().toStdString() == name;       
+        }) != m_servers.end();
+    if ( isValidServerName ) {
+        m_overrideDesiredExtractors[name] = extractors;
+    }
+}
+
 vector<QString>
 PiperVampPluginFactory::getPluginIdentifiers(QString &errorMessage)
 {
@@ -188,35 +207,55 @@
 {
     QString tag = server.tag;
     string executable = server.executable.toStdString();
+    const string serverName = QFileInfo(server.executable).fileName().toStdString();
+        
+    DesiredSubset from;
+    
+    if (m_overrideDesiredExtractors.find(serverName) != m_overrideDesiredExtractors.end()) {
+        const auto desired = m_overrideDesiredExtractors.at(serverName);
+        if (desired.allAvailable) {
+            if (desired.from.empty()) {
+                from = {};
+            } else {
+                // ambiguous struct
+                return;
+            }
+        } else {
+            if (desired.from.empty()) {
+                // ambigous
+                return;
+            } else {
+                from = desired.from;
+            }
+        }
+    } else {
+        PluginScan *scan = PluginScan::getInstance();
+        auto candidateLibraries =
+            scan->getCandidateLibrariesFor(PluginScan::VampPlugin);
 
-    PluginScan *scan = PluginScan::getInstance();
-    auto candidateLibraries =
-        scan->getCandidateLibrariesFor(PluginScan::VampPlugin);
+        SVDEBUG << "PiperVampPluginFactory: Populating from " << executable << endl;
+        SVDEBUG << "INFO: Have " << candidateLibraries.size()
+                << " candidate Vamp plugin libraries from scanner" << endl;
+        for (const auto &c: candidateLibraries) {
+            if (c.helperTag == tag) {
+                string soname = QFileInfo(c.libraryPath).baseName().toStdString();
+                SVDEBUG << "INFO: For tag \"" << tag << "\" giving library " << soname << endl;
+                from.push_back(soname);
+            }
+        }
 
-    SVDEBUG << "PiperVampPluginFactory: Populating from " << executable << endl;
-    SVDEBUG << "INFO: Have " << candidateLibraries.size()
-            << " candidate Vamp plugin libraries from scanner" << endl;
-        
-    vector<string> from;
-    for (const auto &c: candidateLibraries) {
-        if (c.helperTag == tag) {
-            string soname = QFileInfo(c.libraryPath).baseName().toStdString();
-            SVDEBUG << "INFO: For tag \"" << tag << "\" giving library " << soname << endl;
-            from.push_back(soname);
-        }
-    }
-
-    if (from.empty()) {
-        SVDEBUG << "PiperVampPluginFactory: No candidate libraries for tag \""
-             << tag << "\"";
-        if (scan->scanSucceeded()) {
-            // we have to assume that they all failed to load (i.e. we
-            // exclude them all) rather than sending an empty list
-            // (which would mean no exclusions)
-            SVDEBUG << ", skipping" << endl;
-            return;
-        } else {
-            SVDEBUG << ", but it seems the scan failed, so bumbling on anyway" << endl;
+        if (from.empty()) {
+            SVDEBUG << "PiperVampPluginFactory: No candidate libraries for tag \""
+                 << tag << "\"";
+            if (scan->scanSucceeded()) {
+                // we have to assume that they all failed to load (i.e. we
+                // exclude them all) rather than sending an empty list
+                // (which would mean no exclusions)
+                SVDEBUG << ", skipping" << endl;
+                return;
+            } else {
+                SVDEBUG << ", but it seems the scan failed, so bumbling on anyway" << endl;
+            }
         }
     }
     
--- a/plugin/PiperVampPluginFactory.h	Thu Feb 09 14:32:07 2017 +0000
+++ b/plugin/PiperVampPluginFactory.h	Fri Feb 10 11:15:19 2017 +0000
@@ -34,8 +34,35 @@
 class PiperVampPluginFactory : public FeatureExtractionPluginFactory
 {
 public:
+    using ServerName = std::string;
+    using DesiredSubset = std::vector<std::string>;
+    struct DesiredExtractors 
+    {
+        DesiredExtractors() : allAvailable(false) {} // filtered, but invalid, by default (from should not be empty)
+        
+        DesiredExtractors(DesiredSubset subset) 
+            : allAvailable(subset.empty()), from(subset) {} // if empty assume all available are wanted, can populate struct manually if not
+        
+        bool allAvailable; // used to disambiguate an empty filter list from wanting all available extractors, client code should inspect both to determine validity 
+        DesiredSubset from; // this should only be populated if allAvailable is false
+    };
+    
+    struct ServerDescription 
+    {
+        ServerDescription(ServerName n) 
+            : name(n), hasDesiredExtractors(false) {} // fall back to populating using PluginScan internally
+        ServerDescription(ServerName n, DesiredSubset desired) 
+            : name(n), hasDesiredExtractors(true), extractors(desired) {}
+        ServerName name;
+        bool hasDesiredExtractors; // indicates whether to override the ListRequest made internally
+        DesiredExtractors extractors; // for populating the from field in a ListRequest
+    };
+    
     PiperVampPluginFactory();
-    PiperVampPluginFactory(std::initializer_list<QString> servers);
+    PiperVampPluginFactory(std::initializer_list<ServerDescription> servers);
+    
+    virtual void setDesiredExtractors(ServerName name, DesiredExtractors extractors);
+    
     virtual ~PiperVampPluginFactory();
 
     virtual std::vector<QString> getPluginIdentifiers(QString &errorMessage)
@@ -56,6 +83,7 @@
     std::map<QString, QString> m_origins; // plugin identifier -> server path
     std::map<QString, piper_vamp::PluginStaticData> m_pluginData; // identifier -> data
     std::map<QString, QString> m_taxonomy; // identifier -> category string
+    std::map<ServerName, DesiredExtractors> m_overrideDesiredExtractors;
 
     void populate(QString &errorMessage);
     void populateFrom(const HelperExecPath::HelperExec &, QString &errorMessage);