Skip to content
Snippets Groups Projects
Commit 9372e2bf authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: redirect progress report to Python

parent 769deb6e
No related branches found
No related tags found
No related merge requests found
......@@ -205,6 +205,7 @@ public:
#if SWIGPYTHON
%include "otbPythonLogOutput.i"
#endif
......@@ -895,6 +896,22 @@ class ApplicationProxy(object):
#endif /* OTB_SWIGNUMPY */
class ProgressReporterManager: public itkObject
{
public:
/** Default constructor */
static ProgressReporterManager_Pointer New();
virtual void Delete();
void SetLogOutputCallback(otb::LogOutputCallback* callback);
itkCommand* GetAddProcessCommand();
protected:
PythonLogOutput();
};
DECLARE_REF_COUNT_CLASS( ProgressReporterManager )
class Registry : public itkObject
{
public:
......
......@@ -28,6 +28,7 @@
#include "otbPythonLogOutput.h"
#include "otbLogger.h"
#include "otbProgressReporterManager.h"
typedef otb::Wrapper::Application Application;
typedef otb::Wrapper::Application::Pointer Application_Pointer;
......@@ -47,5 +48,7 @@ typedef otb::Logger::Pointer Logger_Pointer;
typedef otb::LogOutputCallback LogOutputCallback;
typedef otb::PythonLogOutput PythonLogOutput;
typedef otb::PythonLogOutput::Pointer PythonLogOutput_Pointer;
typedef otb::ProgressReporterManager ProgressReporterManager;
typedef otb::ProgressReporterManager::Pointer ProgressReporterManager_Pointer;
#endif
......@@ -19,29 +19,59 @@
*/
#include "otbCallbackProgressReporter.h"
#include "otbStopwatch.h"
namespace otb
{
CallbackProgressReporter
::CallbackProgressReporter()
{
}
CallbackProgressReporter
::CallbackProgressReporter(itk::ProcessObject* process,
const char *comment)
: FilterWatcherBase(process, comment)
{
}
CallbackProgressReporter
::CallbackProgressReporter(itk::ProcessObject* process,
const std::string& comment)
: FilterWatcherBase(process, comment.c_str())
{
}
void
CallbackProgressReporter
::ShowProgress()
{
m_Callback->Call("ShowProgress");
if (m_Process)
{
int progressPercent = static_cast<int>(m_Process->GetProgress() * 100);
if (progressPercent > 100)
{
progressPercent = 100;
}
m_Callback->Call("\r"+std::to_string(progressPercent)+"%");
m_Callback->Flush();
}
}
void
CallbackProgressReporter
::StartFilter()
{
m_Callback->Call("StartFilter");
m_Stopwatch.Start();
}
void
CallbackProgressReporter
::EndFilter()
{
m_Callback->Call("EndFilter");
m_Stopwatch.Stop();
}
}
......@@ -29,9 +29,17 @@ namespace otb
class CallbackProgressReporter : public FilterWatcherBase
{
public:
/** Constructor. Takes a ProcessObject to monitor and an optional
* comment string that is prepended to each event message. */
CallbackProgressReporter(itk::ProcessObject* process,
const char *comment = "");
CallbackProgressReporter(itk::ProcessObject* process,
const std::string& comment = "");
/** Default constructor */
CallbackProgressReporter() = default;
CallbackProgressReporter();
/** Destructor. */
virtual ~CallbackProgressReporter() = default;
......
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "otbProgressReporterManager.h"
#include "otbWrapperAddProcessToWatchEvent.h"
namespace otb
{
ProgressReporterManager
::ProgressReporterManager()
{
// Add the callback to be added when a AddProcessToWatch event is invoked
m_AddProcessCommand = AddProcessCommandType::New();
m_AddProcessCommand->SetCallbackFunction(this, &ProgressReporterManager::LinkWatchers);
}
void ProgressReporterManager::LinkWatchers(itk::Object * itkNotUsed(caller), const itk::EventObject & event)
{
if (typeid(otb::Wrapper::AddProcessToWatchEvent) == typeid( event ))
{
const Wrapper::AddProcessToWatchEvent* eventToWatch = dynamic_cast<const Wrapper::AddProcessToWatchEvent*> (&event);
CallbackProgressReporter * watch = new CallbackProgressReporter(eventToWatch->GetProcess(),
eventToWatch->GetProcessDescription());
watch->SetCallback(m_Callback);
m_WatcherList.push_back(watch);
}
}
}
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbProgressReporterManager_h
#define otbProgressReporterManager_h
#include "itkCommand.h"
#include "otbCallbackProgressReporter.h"
#include "otbWrapperApplication.h"
#include "otbLogOutputCallback.h"
namespace otb
{
class ProgressReporterManager : public itk::Object
{
public:
/** Standard class typedefs. */
typedef ProgressReporterManager Self;
typedef itk::Object Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
itkTypeMacro(ProgressReporterManager, itk::Object);
itkNewMacro(ProgressReporterManager);
typedef LogOutputCallback CallbackType;
/** Command Member */
typedef itk::MemberCommand< Self > AddProcessCommandType;
/** Filter watcher list type */
typedef std::vector<CallbackProgressReporter *> WatcherListType;
/** Set the logger callback function */
void SetLogOutputCallback(CallbackType * callback)
{
m_Callback = callback;
this->Modified();
}
AddProcessCommandType* GetAddProcessCommand()
{
return m_AddProcessCommand.GetPointer();
}
protected:
/** Default constructor */
ProgressReporterManager();
/** Destructor. */
virtual ~ProgressReporterManager() = default;
/** Load the watchers for internal progress and writing progress report. */
void LinkWatchers(itk::Object * caller, const itk::EventObject & event);
private:
CallbackType * m_Callback;
AddProcessCommandType::Pointer m_AddProcessCommand;
WatcherListType m_WatcherList;
};
}
#endif //otbProgressReporterManager_h
......@@ -35,12 +35,14 @@ set(SWIG_MODULE_otbApplication_EXTRA_DEPS
otbLogOutputCallback.h
otbPythonLogOutput.h
otbCallbackProgressReporter.h
otbProgressReporterManager.h
OTBApplicationEngine)
SWIG_add_module( otbApplicationPy3 python ../otbApplication.i
otbApplicationPYTHON_wrap.cxx
../python/itkPyCommand.cxx
../python/otbPythonLogOutput.cxx
../python/otbCallbackProgressReporter.cxx)
../python/otbCallbackProgressReporter.cxx
../python/otbProgressReporterManager.cxx)
SWIG_link_libraries( otbApplicationPy3 ${PYTHON3_LIBRARIES} OTBApplicationEngine )
set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR})
set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_NAME _otbApplication)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment