Enhancement - export.yaml

- Preliminary export.yaml support.  No unit tests yet, and no decision
  on including this feature.  It may be that libyaml is the right choice,
  as an optional dependency.
This commit is contained in:
Paul Beckingham 2010-07-11 10:22:36 -04:00
parent 8b02d2bdeb
commit 2b48ae8e38
6 changed files with 67 additions and 10 deletions

View file

@ -115,6 +115,7 @@ void Cmd::load ()
commands.push_back ("_merge");
commands.push_back ("export.csv");
commands.push_back ("export.ical");
commands.push_back ("export.yaml");
commands.push_back ("history.monthly");
commands.push_back ("history.annual");
commands.push_back ("ghistory.monthly");
@ -207,6 +208,7 @@ bool Cmd::isReadOnlyCommand ()
command == "_version" ||
command == "export.csv" ||
command == "export.ical" ||
command == "export.yaml" ||
command == "history.monthly" ||
command == "history.annual" ||
command == "ghistory.monthly" ||

View file

@ -233,6 +233,7 @@ int Context::dispatch (std::string &out)
else if (cmd.command == "stop") { rc = handleStop (out); }
else if (cmd.command == "export.csv") { rc = handleExportCSV (out); }
else if (cmd.command == "export.ical") { rc = handleExportiCal (out); }
else if (cmd.command == "export.yaml") { rc = handleExportYAML (out); }
else if (cmd.command == "import") { rc = handleImport (out); }
else if (cmd.command == "duplicate") { rc = handleDuplicate (out); }
else if (cmd.command == "edit") { rc = handleEdit (out); }

View file

@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////////
// task - a command line task list manager.
//
// Copyright 2006 - 2010, Paul Beckingham.
// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
@ -392,6 +392,33 @@ std::string Task::composeCSV () const
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string Task::composeYAML () const
{
std::stringstream out;
// Task header.
out << " task:\n";
// Get all the supported attribute names.
std::vector <std::string> names;
Att::allNames (names);
std::sort (names.begin (), names.end ());
foreach (name, names)
out << " " << *name << ": " << get (*name) << "\n";
// Now the annotations, which are not listed by the Att::allNames call.
std::vector <Att> annotations;
getAnnotations (annotations);
foreach (a, annotations)
out << " annotation:\n"
<< " entry: " << a->name().substr (12) << "\n"
<< " description: " << a->value () << "\n";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
void Task::getAnnotations (std::vector <Att>& annotations) const
{

View file

@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////////
// task - a command line task list manager.
//
// Copyright 2006 - 2010, Paul Beckingham.
// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
@ -44,6 +44,7 @@ public:
void parse (const std::string&);
std::string composeCSV () const;
std::string composeYAML () const;
// Status values.
enum status {pending, completed, deleted, recurring, waiting};

View file

@ -62,8 +62,6 @@ int handleExportCSV (std::string &outs)
<< "'description'"
<< "\n";
int count = 0;
// Get all the tasks.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
@ -77,10 +75,7 @@ int handleExportCSV (std::string &outs)
context.hooks.trigger ("pre-display", *task);
if (task->getStatus () != Task::recurring)
{
out << task->composeCSV ().c_str ();
++count;
}
}
outs = out.str ();
@ -106,8 +101,6 @@ int handleExportiCal (std::string &outs)
<< "VERSION:2.0\n"
<< "PRODID:-//GBF//" << PACKAGE_STRING << "//EN\n";
int count = 0;
// Get all the tasks.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
@ -214,7 +207,6 @@ int handleExportiCal (std::string &outs)
out << "COMMENT:" << anno->value () << "\n";
out << "END:VTODO\n";
++count;
}
}
@ -228,3 +220,36 @@ int handleExportiCal (std::string &outs)
}
////////////////////////////////////////////////////////////////////////////////
int handleExportYAML (std::string &outs)
{
int rc = 0;
if (context.hooks.trigger ("pre-export-command"))
{
// YAML header.
std::stringstream out;
out << "%YAML 1.1\n"
<< "---\n";
// Get all the tasks.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence ();
context.tdb.load (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
foreach (task, tasks)
{
context.hooks.trigger ("pre-display", *task);
out << task->composeYAML ().c_str ();
}
outs = out.str ();
context.hooks.trigger ("post-export-command");
}
return rc;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -133,6 +133,7 @@ int handleImport (std::string&);
// export.cpp
int handleExportCSV (std::string &);
int handleExportiCal (std::string &);
int handleExportYAML (std::string &);
// list template
///////////////////////////////////////////////////////////////////////////////