- Added a run-time enable/disable control, for nested add/modify calls.
This commit is contained in:
Paul Beckingham 2014-09-09 23:36:45 -04:00
parent 1fc388886f
commit 7e35508bb4
2 changed files with 61 additions and 47 deletions

View file

@ -44,6 +44,7 @@ extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Hooks::Hooks () Hooks::Hooks ()
: _enabled (true)
{ {
} }
@ -55,8 +56,6 @@ Hooks::~Hooks ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Hooks::initialize () void Hooks::initialize ()
{ {
if (context.config.getBoolean ("hooks"))
{
// Scan <rc.data.location>/hooks // Scan <rc.data.location>/hooks
Directory d (context.config.get ("data.location")); Directory d (context.config.get ("data.location"));
d += "hooks"; d += "hooks";
@ -66,28 +65,38 @@ void Hooks::initialize ()
_scripts = d.list (); _scripts = d.list ();
std::sort (_scripts.begin (), _scripts.end ()); std::sort (_scripts.begin (), _scripts.end ());
} }
}
_enabled = context.config.getBoolean ("hooks");
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// The on-launch event is triggered once, after initialization, before an bool Hooks::enable (bool value)
// processing occurs {
bool old_value = _enabled;
_enabled = value;
return old_value;
}
////////////////////////////////////////////////////////////////////////////////
// The on-launch event is triggered once, after initialization, before any
// processing occurs, i.e first
// //
// No input // Input:
// - none
// //
// Output: // Output:
// - all emitted JSON lines must be fully-formed tasks // - all emitted JSON lines are added/modified as tasks, if the exit code is
// - all emitted non-JSON lines are considered feedback messages // zero, otherwise ignored.
// // - minimal new task: {"description":"Buy milk"}
// Exit: // - to modify a task include complete JSON
// 0 Means: - all emitted JSON lines are added/modified // - all emitted non-JSON lines are considered feedback messages if the exit
// - all emitted non-JSON lines become footnote entries // code is zero, otherwise they are considered errors.
// non-0 Means: - all emitted JSON lines are ignored
// - all emitted non-JSON lines become error entries
// //
void Hooks::onLaunch () void Hooks::onLaunch ()
{ {
context.timer_hooks.start (); context.timer_hooks.start ();
if (! _enabled)
return;
std::vector <std::string> matchingScripts = scripts ("on-launch"); std::vector <std::string> matchingScripts = scripts ("on-launch");
std::vector <std::string>::iterator i; std::vector <std::string>::iterator i;
@ -129,20 +138,22 @@ void Hooks::onLaunch ()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// The on-exit event is triggered once, after all processing is complete, i.e.
// last
//
// Input: // Input:
// - A read-only line of JSON for each task added/modified // - read-only line of JSON for each task added/modified
// //
// Output: // Output:
// - all emitted non-JSON lines are considered feedback messages // - any emitted JSON is ignored
// - The onExit event occurs too late to allow any changes, so the input is not // - all emitted non-JSON lines are considered feedback messages if the exit
// to be modified // code is zero, otherwise they are considered errors.
// //
// Exit:
// 0 Means: - all emitted non-JSON lines become footnote entries
// non-0 Means: - all emitted non-JSON lines become error entries
void Hooks::onExit () void Hooks::onExit ()
{ {
context.timer_hooks.start (); context.timer_hooks.start ();
if (! _enabled)
return;
std::vector <std::string> matchingScripts = scripts ("on-exit"); std::vector <std::string> matchingScripts = scripts ("on-exit");
std::vector <std::string>::iterator i; std::vector <std::string>::iterator i;
@ -176,21 +187,21 @@ void Hooks::onExit ()
// The on-add event is triggered separately for each task added // The on-add event is triggered separately for each task added
// //
// Input: // Input:
// - A line of JSON for the task added // - line of JSON for the task added
// //
// Output: // Output:
// - all emitted JSON lines must be fully-formed tasks // - all emitted JSON lines are added/modified as tasks, if the exit code is
// - all emitted non-JSON lines are considered feedback messages // zero, otherwise ignored.
// // - minimal new task: {"description":"Buy milk"}
// Exit: // - to modify a task include complete JSON
// 0 Means: - all emitted JSON lines are added/modified // - all emitted non-JSON lines are considered feedback messages if the exit
// - all emitted non-JSON lines become footnote entries // code is zero, otherwise they are considered errors.
// non-0 Means: - all emitted JSON lines are ignored
// - all emitted non-JSON lines become error entries
// //
void Hooks::onAdd (Task& after) void Hooks::onAdd (Task& after)
{ {
context.timer_hooks.start (); context.timer_hooks.start ();
if (! _enabled)
return;
std::vector <std::string> matchingScripts = scripts ("on-add"); std::vector <std::string> matchingScripts = scripts ("on-add");
std::vector <std::string>::iterator i; std::vector <std::string>::iterator i;
@ -244,21 +255,22 @@ void Hooks::onAdd (Task& after)
// The on-modify event is triggered separately for each task added or modified // The on-modify event is triggered separately for each task added or modified
// //
// Input: // Input:
// - A line of JSON for the original task // - line of JSON for the original task
// - A line of JSON for the modified task // - line of JSON for the modified task, the diff being the modification
// //
// Output: // Output:
// - all emitted JSON lines must be fully-formed tasks // - all emitted JSON lines are added/modified as tasks, if the exit code is
// - all emitted non-JSON lines are considered feedback messages // zero, otherwise ignored.
// - minimal new task: {"description":"Buy milk"}
// - to modify a task include complete JSON
// - all emitted non-JSON lines are considered feedback messages if the exit
// code is zero, otherwise they are considered errors.
// //
// Exit:
// 0 Means: - all emitted JSON lines are added/modified
// - all emitted non-JSON lines become footnote entries
// non-0 Means: - all emitted JSON lines are ignored
// - all emitted non-JSON lines become error entries
void Hooks::onModify (const Task& before, Task& after) void Hooks::onModify (const Task& before, Task& after)
{ {
context.timer_hooks.start (); context.timer_hooks.start ();
if (! _enabled)
return;
std::vector <std::string> matchingScripts = scripts ("on-modify"); std::vector <std::string> matchingScripts = scripts ("on-modify");
std::vector <std::string>::iterator i; std::vector <std::string>::iterator i;
@ -293,7 +305,7 @@ void Hooks::onModify (const Task& before, Task& after)
first = false; first = false;
} }
else else
context.tdb2.add (newTask); context.tdb2.modify (newTask);
} }
else else
context.footnote (*line); context.footnote (*line);

View file

@ -39,6 +39,7 @@ public:
Hooks& operator= (const Hooks&); // Deliberately unimplemented Hooks& operator= (const Hooks&); // Deliberately unimplemented
void initialize (); void initialize ();
bool enable (bool);
void onLaunch (); void onLaunch ();
void onExit (); void onExit ();
@ -51,6 +52,7 @@ private:
std::vector <std::string> scripts (const std::string&); std::vector <std::string> scripts (const std::string&);
private: private:
bool _enabled;
std::vector <std::string> _scripts; std::vector <std::string> _scripts;
}; };