Additional work on batch processing
This commit is contained in:
+117
-22
@@ -20,7 +20,7 @@ ScriptEngine::ScriptEngine(QObject *parent) : QObject(parent)
|
||||
_semaphore.release(_pool->maxThreadCount());
|
||||
}
|
||||
|
||||
void ScriptEngine::setParams(const QString &scriptPath, const QStringList &paths, const QString &outputDir)
|
||||
void ScriptEngine::setParams(const QString &scriptPath, const QList<QPair<QString, QString>> &paths, const QString &outputDir)
|
||||
{
|
||||
_scriptPath = scriptPath;
|
||||
_paths = paths;
|
||||
@@ -67,7 +67,23 @@ bool ScriptEngine::isMarked(const File *file) const
|
||||
return _database->isMarked(file->absoluteFilePath());
|
||||
}
|
||||
|
||||
bool ScriptEngine::convert(File *file, QString &outpath, QString format, QVariantMap params)
|
||||
void ScriptEngine::setMaxThread(int maxthread)
|
||||
{
|
||||
int newval = std::max(std::min(QThread::idealThreadCount(), maxthread), 1);
|
||||
int oldval = _pool->maxThreadCount();
|
||||
if(newval > oldval)
|
||||
_semaphore.release(newval - oldval);
|
||||
else if(newval < oldval)
|
||||
_semaphore.acquire(oldval - newval);
|
||||
_pool->setMaxThreadCount(newval);
|
||||
}
|
||||
|
||||
void ScriptEngine::sync()
|
||||
{
|
||||
_pool->waitForDone();
|
||||
}
|
||||
|
||||
bool ScriptEngine::convert(File *file, QString &outpath, const QString &format, const QVariantMap ¶ms, bool async)
|
||||
{
|
||||
QString path;
|
||||
QDir dir(_outputDir);
|
||||
@@ -79,9 +95,17 @@ bool ScriptEngine::convert(File *file, QString &outpath, QString format, QVarian
|
||||
path = dir.absoluteFilePath(outpath);
|
||||
|
||||
info.setFile(path);
|
||||
//qDebug() << info.absolutePath() + "/" + info.completeBaseName() + "." + format.toLower();
|
||||
_semaphore.acquire();
|
||||
_pool->start(new ConvertRunable(file->absoluteFilePath(), info.absolutePath() + "/" + info.completeBaseName() + "." + format.toLower(), format, params, &_semaphore));
|
||||
outpath = info.absolutePath() + "/" + info.completeBaseName() + "." + format.toLower();
|
||||
if(async)
|
||||
{
|
||||
_semaphore.acquire();
|
||||
_pool->start(new ConvertRunable(file->absoluteFilePath(), outpath, format, params, &_semaphore));
|
||||
}
|
||||
else
|
||||
{
|
||||
ConvertRunable crun(file->absoluteFilePath(), outpath, format, params, nullptr);
|
||||
crun.run();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -90,11 +114,16 @@ QJSValue ScriptEngine::newObject()
|
||||
return _jsEngine->newObject();
|
||||
}
|
||||
|
||||
QJSValue ScriptEngine::newArray(uint size)
|
||||
{
|
||||
return _jsEngine->newArray(size);
|
||||
}
|
||||
|
||||
void ScriptEngine::run()
|
||||
{
|
||||
QJSValue jsPaths = _jsEngine->newArray(_paths.size());
|
||||
for(qsizetype i=0; i<_paths.size(); i++)
|
||||
jsPaths.setProperty(i, _jsEngine->newQObject(new File(_paths[i], this)));
|
||||
jsPaths.setProperty(i, _jsEngine->newQObject(new File(_paths[i].first, _paths[i].second, this)));
|
||||
|
||||
_jsEngine->globalObject().setProperty("files", jsPaths);
|
||||
|
||||
@@ -138,9 +167,10 @@ void File::loadFitsKeywords()
|
||||
}
|
||||
else return;
|
||||
|
||||
for(const FITSRecord &record : info.fitsHeader)
|
||||
for(auto &record : info.fitsHeader)
|
||||
{
|
||||
_fitsKeywords[record.key] = record.value.toString();
|
||||
_fitsKeywords.append(record.key);
|
||||
_fitsRecords.insert(record.key, record);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,9 +195,14 @@ bool File::mkpath(const QString &path) const
|
||||
}
|
||||
}
|
||||
|
||||
File::File(const QString &path, Script::ScriptEngine *engine) :
|
||||
File::File(const QString &path, Script::ScriptEngine *engine) : File(path, QString(), engine)
|
||||
{
|
||||
}
|
||||
|
||||
File::File(const QString &path, const QString &root, ScriptEngine *engine) :
|
||||
_engine(engine),
|
||||
_path(path),
|
||||
_root(root),
|
||||
_info(path)
|
||||
{
|
||||
}
|
||||
@@ -187,6 +222,18 @@ QString File::absolutePath() const
|
||||
return _info.absolutePath();
|
||||
}
|
||||
|
||||
QString File::relativeFilePath() const
|
||||
{
|
||||
QDir dir(_root);
|
||||
return dir.relativeFilePath(_info.absoluteFilePath());
|
||||
}
|
||||
|
||||
QString File::relativePath() const
|
||||
{
|
||||
QDir dir(_root);
|
||||
return dir.relativeFilePath(_info.absolutePath());
|
||||
}
|
||||
|
||||
QString File::baseName() const
|
||||
{
|
||||
return _info.baseName();
|
||||
@@ -209,53 +256,101 @@ qint64 File::size() const
|
||||
|
||||
QStringList File::fitsKeywords()
|
||||
{
|
||||
QThread::msleep(500);
|
||||
loadFitsKeywords();
|
||||
return _fitsKeywords.keys();
|
||||
return _fitsKeywords;
|
||||
}
|
||||
|
||||
QString File::fitsValue(const QString &key)
|
||||
{
|
||||
loadFitsKeywords();
|
||||
if(_fitsKeywords.contains(key))
|
||||
return _fitsKeywords[key];
|
||||
if(_fitsRecords.contains(key))
|
||||
return _fitsRecords[key].value.toString();
|
||||
else
|
||||
return QString();
|
||||
}
|
||||
|
||||
QJSValue File::fitsValues(const QString &key)
|
||||
{
|
||||
loadFitsKeywords();
|
||||
if(_fitsRecords.contains(key))
|
||||
{
|
||||
QList<FITSRecord> values = _fitsRecords.values(key);
|
||||
QJSValue array = _engine->newArray(values.size());
|
||||
for(qsizetype i=0; i<values.size(); i++)
|
||||
array.setProperty(i, values[i].value.toString());
|
||||
return array;
|
||||
}
|
||||
else
|
||||
return QString();
|
||||
}
|
||||
|
||||
QJSValue File::fitsRecords()
|
||||
{
|
||||
loadFitsKeywords();
|
||||
|
||||
QJSValue array = _engine->newArray(_fitsRecords.size());
|
||||
uint i = 0;
|
||||
for(auto &record : _fitsRecords)
|
||||
{
|
||||
QJSValue item = _engine->newObject();
|
||||
item.setProperty("key", QString::fromUtf8(record.key));
|
||||
item.setProperty("value", record.value.toString());
|
||||
item.setProperty("comment", QString::fromUtf8(record.comment));
|
||||
item.setProperty("xisf", record.xisf);
|
||||
array.setProperty(i++, item);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
bool File::isMarked() const
|
||||
{
|
||||
return _engine->isMarked(this);
|
||||
}
|
||||
|
||||
bool File::copy(const QString &newpath) const
|
||||
File* File::copy(const QString &newpath) const
|
||||
{
|
||||
if(mkpath(newpath))
|
||||
{
|
||||
if(QFile::copy(_path, _engine->outputDir() + newpath))
|
||||
return true;
|
||||
return new File(_engine->outputDir() + newpath, _engine);
|
||||
_engine->logError("Failed copy to " + newpath);
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool File::move(const QString &newpath) const
|
||||
bool File::move(const QString &newpath)
|
||||
{
|
||||
if(mkpath(newpath))
|
||||
{
|
||||
if(QFile::rename(_path, _engine->outputDir() + newpath))
|
||||
{
|
||||
_path = _engine->outputDir() + newpath;
|
||||
return true;
|
||||
}
|
||||
_engine->logError("Failed move to " + newpath);
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool File::convertTo(const QString &format)
|
||||
File* File::convert(const QString &outpath, const QString &format, const QVariantMap ¶ms)
|
||||
{
|
||||
_engine->reportError("Not implemented");
|
||||
return false;
|
||||
QString path = outpath;
|
||||
if(_engine->convert(this, path, format, params, false))
|
||||
return new File(path, _engine);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
File* File::convertAsync(const QString &outpath, const QString &format, const QVariantMap ¶ms)
|
||||
{
|
||||
QString path = outpath;
|
||||
if(_engine->convert(this, path, format, params, true))
|
||||
return new File(path, _engine);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QJSValue File::stats()
|
||||
@@ -298,7 +393,7 @@ ScriptEngineThread::~ScriptEngineThread()
|
||||
if(_engine)_engine->interrupt();
|
||||
}
|
||||
|
||||
void ScriptEngineThread::setParams(const QString &scriptPath, const QStringList &paths, const QString &outputDir)
|
||||
void ScriptEngineThread::setParams(const QString &scriptPath, const QList<QPair<QString, QString>> &paths, const QString &outputDir)
|
||||
{
|
||||
_engine->setParams(scriptPath, paths, outputDir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user