diff runner/JAMSFeatureWriter.cpp @ 152:db83ea0e102d jams

Make the JSON well-formed (though still empty), except in the case where we are asked to write more than one file's features to the same output file or stdout
author Chris Cannam
date Tue, 14 Oct 2014 12:35:19 +0100
parents 3921e0c1f4dd
children ad96fd5f9cd7
line wrap: on
line diff
--- a/runner/JAMSFeatureWriter.cpp	Tue Oct 14 11:27:43 2014 +0100
+++ b/runner/JAMSFeatureWriter.cpp	Tue Oct 14 12:35:19 2014 +0100
@@ -25,6 +25,7 @@
 JAMSFeatureWriter::JAMSFeatureWriter() :
     FileFeatureWriter(SupportOneFilePerTrackTransform |
                       SupportOneFilePerTrack |
+                      SupportOneFileTotal |
 		      SupportStdOut,
                       "json"),
     m_network(false),
@@ -73,10 +74,11 @@
 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
 {
     QString json
-	("'file_metadata':"
-	 "  { 'artist': \"%1\","
-	 "    'title': \"%2\" }");
+	(" \"file_metadata\":\n"
+	 "  { \"artist\": \"%1\",\n"
+	 "    \"title\": \"%2\" },\n");
     m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
+    cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
 }
 
 void
@@ -95,30 +97,66 @@
 
     QTextStream &stream = *sptr;
 
-    if (m_startedTransforms.find(transformId) == m_startedTransforms.end()) {
+    TrackTransformPair tt(trackId, transformId);
+    TrackTransformPair targetKey = getFilenameKey(trackId, transformId);
+
+    if (m_startedTargets.find(targetKey) == m_startedTargets.end()) {
+        // Need to write track-level preamble
+        stream << "{" << m_metadata[trackId] << endl;
+        m_startedTargets.insert(targetKey);
+    }
+
+    if (m_data.find(tt) == m_data.end()) {
 
 	identifyTask(transform);
 
-	if (m_manyFiles ||
-	    (m_startedTracks.find(trackId) == m_startedTracks.end())) {
-
-	    // track-level preamble
-	    stream << "{" << m_metadata[trackId] << endl;
-	}
-
-	stream << "'" << getTaskKey(m_tasks[transformId]) << "':" << endl;
-	stream << "  [ ";
+        QString json("\"%1\": [ ");
+        m_data[tt] = json.arg(getTaskKey(m_tasks[transformId]));
     }
 
-    m_startedTracks.insert(trackId);
-    m_startedTransforms.insert(transformId);
-
     for (int i = 0; i < int(features.size()); ++i) {
 	
     }	
 }
 
 void
+JAMSFeatureWriter::finish()
+{
+    cerr << "Finish called on " << this << endl;
+
+    set<QTextStream *> startedStreams;
+
+    for (DataMap::const_iterator i = m_data.begin();
+         i != m_data.end(); ++i) {
+
+        TrackTransformPair tt = i->first;
+        QString data = i->second;
+
+        QTextStream *sptr = getOutputStream(tt.first, tt.second);
+        if (!sptr) {
+            throw FailedToOpenOutputStream(tt.first, tt.second);
+        }
+
+        if (startedStreams.find(sptr) != startedStreams.end()) {
+            *sptr << "," << endl;
+        }
+        startedStreams.insert(sptr);
+        
+        *sptr << data << "]";
+    }
+        
+    for (FileStreamMap::const_iterator i = m_streams.begin();
+	 i != m_streams.end(); ++i) {
+	*(i->second) << endl << "}" << endl;
+    }
+
+    m_data.clear();
+    m_startedTargets.clear();
+
+    FileFeatureWriter::finish();
+}
+
+void
 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
 {
     QString pluginId = transform.getPluginIdentifier();
@@ -223,15 +261,3 @@
     }
     return "unknown";
 }
-
-void
-JAMSFeatureWriter::finish()
-{
-    for (FileStreamMap::const_iterator i = m_streams.begin();
-	 i != m_streams.end(); ++i) {
-	*(i->second) << "}" << endl;
-    }
-
-    FileFeatureWriter::finish();
-}
-