Enhancement - import command

- Implemented import command
- Implemented Context::clearMessages to remove all accumulated
  messages.  This is needed because parts of the import process are
  recursive and we don't want Context to dump messages for every
  import record on completion.
This commit is contained in:
Paul Beckingham 2009-06-17 00:59:31 -04:00
parent 07cf8d6ee8
commit 61cedc3ad1
4 changed files with 99 additions and 84 deletions

View file

@ -572,3 +572,11 @@ void Context::footnote (const std::string& input)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Context::clearMessages ()
{
headers.clear ();
messages.clear ();
footnotes.clear ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -55,10 +55,12 @@ public:
void header (const std::string&); // Header sink void header (const std::string&); // Header sink
void message (const std::string&); // Message sink void message (const std::string&); // Message sink
void footnote (const std::string&); // Footnote sink void footnote (const std::string&); // Footnote sink
void clearMessages ();
void parse ();
private: private:
void loadCorrectConfigFile (); void loadCorrectConfigFile ();
void parse ();
void autoFilter (); void autoFilter ();
public: public:

View file

@ -181,9 +181,10 @@ static void decorateTask (Task& task)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importTask_1_4_3 (const std::vector <std::string>& lines) static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
context.tdb.lock (context.config.get ("locking", true));
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
{ {
@ -229,7 +230,7 @@ static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
throw "unrecoverable"; throw "unrecoverable";
// Build up this task ready for insertion. // Build up this task ready for insertion.
T task; Task task;
// Handle the 12 fields. // Handle the 12 fields.
for (unsigned int f = 0; f < fields.size (); ++f) for (unsigned int f = 0; f < fields.size (); ++f)
@ -237,14 +238,14 @@ static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
switch (f) switch (f)
{ {
case 0: // 'uuid' case 0: // 'uuid'
task.setUUID (fields[f].substr (1, 36)); task.set ("uuid", fields[f].substr (1, 36));
break; break;
case 1: // 'status' case 1: // 'status'
if (fields[f] == "'pending'") task.setStatus (T::pending); if (fields[f] == "'pending'") task.setStatus (Task::pending);
else if (fields[f] == "'recurring'") task.setStatus (T::recurring); else if (fields[f] == "'recurring'") task.setStatus (Task::recurring);
else if (fields[f] == "'deleted'") task.setStatus (T::deleted); else if (fields[f] == "'deleted'") task.setStatus (Task::deleted);
else if (fields[f] == "'completed'") task.setStatus (T::completed); else if (fields[f] == "'completed'") task.setStatus (Task::completed);
break; break;
case 2: // 'tags' case 2: // 'tags'
@ -299,13 +300,12 @@ static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
case 11: // 'description' case 11: // 'description'
if (fields[f].length () > 2) if (fields[f].length () > 2)
task.setDescription (fields[f].substr (1, fields[f].length () - 2)); task.set ("description", fields[f].substr (1, fields[f].length () - 2));
break; break;
} }
} }
if (! tdb.addT (task)) context.tdb.add (task);
failed.push_back (*it);
} }
catch (...) catch (...)
@ -313,10 +313,11 @@ static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
failed.push_back (*it); failed.push_back (*it);
} }
} }
*/
context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size () - 1) << (lines.size () - failed.size () - 1)
<< " tasks successfully, with " << " tasks successfully, with "
@ -330,16 +331,17 @@ static std::string importTask_1_4_3 (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importTask_1_5_0 (const std::vector <std::string>& lines) static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
context.tdb.lock (context.config.get ("locking", true));
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
{ {
@ -385,7 +387,7 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
throw "unrecoverable"; throw "unrecoverable";
// Build up this task ready for insertion. // Build up this task ready for insertion.
T task; Task task;
// Handle the 13 fields. // Handle the 13 fields.
for (unsigned int f = 0; f < fields.size (); ++f) for (unsigned int f = 0; f < fields.size (); ++f)
@ -393,14 +395,14 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
switch (f) switch (f)
{ {
case 0: // 'uuid' case 0: // 'uuid'
task.setUUID (fields[f].substr (1, 36)); task.set ("uuid", fields[f].substr (1, 36));
break; break;
case 1: // 'status' case 1: // 'status'
if (fields[f] == "'pending'") task.setStatus (T::pending); if (fields[f] == "'pending'") task.setStatus (Task::pending);
else if (fields[f] == "'recurring'") task.setStatus (T::recurring); else if (fields[f] == "'recurring'") task.setStatus (Task::recurring);
else if (fields[f] == "'deleted'") task.setStatus (T::deleted); else if (fields[f] == "'deleted'") task.setStatus (Task::deleted);
else if (fields[f] == "'completed'") task.setStatus (T::completed); else if (fields[f] == "'completed'") task.setStatus (Task::completed);
break; break;
case 2: // 'tags' case 2: // 'tags'
@ -460,13 +462,12 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
case 12: // 'description' case 12: // 'description'
if (fields[f].length () > 2) if (fields[f].length () > 2)
task.setDescription (fields[f].substr (1, fields[f].length () - 2)); task.set ("description", fields[f].substr (1, fields[f].length () - 2));
break; break;
} }
} }
if (! tdb.addT (task)) context.tdb.add (task);
failed.push_back (*it);
} }
catch (...) catch (...)
@ -475,9 +476,10 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
} }
} }
*/ context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size () - 1) << (lines.size () - failed.size () - 1)
<< " tasks successfully, with " << " tasks successfully, with "
@ -491,7 +493,6 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
@ -499,9 +500,10 @@ static std::string importTask_1_5_0 (const std::vector <std::string>& lines)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importTask_1_6_0 (const std::vector <std::string>& lines) static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
context.tdb.lock (context.config.get ("locking", true));
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
{ {
@ -547,7 +549,7 @@ static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
throw "unrecoverable"; throw "unrecoverable";
// Build up this task ready for insertion. // Build up this task ready for insertion.
T task; Task task;
// Handle the 13 fields. // Handle the 13 fields.
for (unsigned int f = 0; f < fields.size (); ++f) for (unsigned int f = 0; f < fields.size (); ++f)
@ -555,14 +557,14 @@ static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
switch (f) switch (f)
{ {
case 0: // 'uuid' case 0: // 'uuid'
task.setUUID (fields[f].substr (1, 36)); task.set ("uuid", fields[f].substr (1, 36));
break; break;
case 1: // 'status' case 1: // 'status'
if (fields[f] == "'pending'") task.setStatus (T::pending); if (fields[f] == "'pending'") task.setStatus (Task::pending);
else if (fields[f] == "'recurring'") task.setStatus (T::recurring); else if (fields[f] == "'recurring'") task.setStatus (Task::recurring);
else if (fields[f] == "'deleted'") task.setStatus (T::deleted); else if (fields[f] == "'deleted'") task.setStatus (Task::deleted);
else if (fields[f] == "'completed'") task.setStatus (T::completed); else if (fields[f] == "'completed'") task.setStatus (Task::completed);
break; break;
case 2: // 'tags' case 2: // 'tags'
@ -622,13 +624,12 @@ static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
case 12: // 'description' case 12: // 'description'
if (fields[f].length () > 2) if (fields[f].length () > 2)
task.setDescription (fields[f].substr (1, fields[f].length () - 2)); task.set ("description", fields[f].substr (1, fields[f].length () - 2));
break; break;
} }
} }
if (! tdb.addT (task)) context.tdb.add (task);
failed.push_back (*it);
} }
catch (...) catch (...)
@ -636,10 +637,11 @@ static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
failed.push_back (*it); failed.push_back (*it);
} }
} }
*/
context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size () - 1) << (lines.size () - failed.size () - 1)
<< " tasks successfully, with " << " tasks successfully, with "
@ -653,14 +655,13 @@ static std::string importTask_1_6_0 (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importTaskCmdLine (const std::vector <std::string>& lines) static std::string importTaskCmdLine (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
@ -673,21 +674,21 @@ static std::string importTaskCmdLine (const std::vector <std::string>& lines)
std::vector <std::string> args; std::vector <std::string> args;
split (args, std::string ("add ") + line, ' '); split (args, std::string ("add ") + line, ' ');
T task; context.task.clear ();
std::string command; context.cmd.command = "";
parse (args, command, task); context.parse ();
handleAdd (); handleAdd ();
context.clearMessages ();
} }
catch (...) catch (...)
{ {
context.clearMessages ();
failed.push_back (line); failed.push_back (line);
} }
} }
*/
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size ()) << (lines.size () - failed.size ())
<< " tasks successfully, with " << " tasks successfully, with "
@ -701,7 +702,6 @@ static std::string importTaskCmdLine (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
@ -709,9 +709,10 @@ static std::string importTaskCmdLine (const std::vector <std::string>& lines)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importTodoSh_2_0 (const std::vector <std::string>& lines) static std::string importTodoSh_2_0 (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
context.tdb.lock (context.config.get ("locking", true));
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
{ {
@ -780,37 +781,39 @@ static std::string importTodoSh_2_0 (const std::vector <std::string>& lines)
} }
} }
T task; context.task.clear ();
std::string command; context.cmd.command = "";
parse (args, command, task); context.parse ();
decorateTask (task); decorateTask (context.task);
if (isPending) if (isPending)
{ {
task.setStatus (T::pending); context.task.setStatus (Task::pending);
} }
else else
{ {
task.setStatus (T::completed); context.task.setStatus (Task::completed);
char end[16]; char end[16];
sprintf (end, "%u", (unsigned int) endDate.toEpoch ()); sprintf (end, "%u", (unsigned int) endDate.toEpoch ());
task.set ("end", end); context.task.set ("end", end);
} }
if (! tdb.addT (task)) context.tdb.add (context.task);
failed.push_back (*it); context.clearMessages ();
} }
catch (...) catch (...)
{ {
context.clearMessages ();
failed.push_back (*it); failed.push_back (*it);
} }
} }
*/
context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size ()) << (lines.size () - failed.size ())
<< " tasks successfully, with " << " tasks successfully, with "
@ -824,17 +827,17 @@ static std::string importTodoSh_2_0 (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importText (const std::vector <std::string>& lines) static std::string importText (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
int count = 0; int count = 0;
context.tdb.lock (context.config.get ("locking", true));
std::vector <std::string>::const_iterator it; std::vector <std::string>::const_iterator it;
for (it = lines.begin (); it != lines.end (); ++it) for (it = lines.begin (); it != lines.end (); ++it)
{ {
@ -854,25 +857,27 @@ static std::string importText (const std::vector <std::string>& lines)
std::vector <std::string> args; std::vector <std::string> args;
split (args, std::string ("add ") + line, ' '); split (args, std::string ("add ") + line, ' ');
T task; context.task.clear ();
std::string command; context.cmd.command = "";
parse (args, command, task); context.parse ();
decorateTask (task); decorateTask (context.task);
if (! tdb.addT (task)) context.tdb.add (context.task);
failed.push_back (*it); context.clearMessages ();
} }
catch (...) catch (...)
{ {
context.clearMessages ();
failed.push_back (line); failed.push_back (line);
} }
} }
} }
*/
context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< count << count
<< " tasks successfully, with " << " tasks successfully, with "
@ -886,7 +891,6 @@ static std::string importText (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }
@ -894,9 +898,10 @@ static std::string importText (const std::vector <std::string>& lines)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static std::string importCSV (const std::vector <std::string>& lines) static std::string importCSV (const std::vector <std::string>& lines)
{ {
/*
std::vector <std::string> failed; std::vector <std::string> failed;
context.tdb.lock (context.config.get ("locking", true));
// Set up mappings. Assume no fields match. // Set up mappings. Assume no fields match.
std::map <std::string, int> mapping; std::map <std::string, int> mapping;
mapping ["id"] = -1; mapping ["id"] = -1;
@ -1045,20 +1050,20 @@ static std::string importCSV (const std::vector <std::string>& lines)
std::vector <std::string> fields; std::vector <std::string> fields;
split (fields, *it, ','); split (fields, *it, ',');
T task; Task task;
int f; int f;
if ((f = mapping["uuid"]) != -1) if ((f = mapping["uuid"]) != -1)
task.setUUID (lowerCase (unquoteText (trim (fields[f])))); task.set ("uuid", lowerCase (unquoteText (trim (fields[f]))));
if ((f = mapping["status"]) != -1) if ((f = mapping["status"]) != -1)
{ {
std::string value = lowerCase (unquoteText (trim (fields[f]))); std::string value = lowerCase (unquoteText (trim (fields[f])));
if (value == "recurring") task.setStatus (T::recurring); if (value == "recurring") task.setStatus (Task::recurring);
else if (value == "deleted") task.setStatus (T::deleted); else if (value == "deleted") task.setStatus (Task::deleted);
else if (value == "completed") task.setStatus (T::completed); else if (value == "completed") task.setStatus (Task::completed);
else task.setStatus (T::pending); else task.setStatus (Task::pending);
} }
if ((f = mapping["tags"]) != -1) if ((f = mapping["tags"]) != -1)
@ -1102,10 +1107,9 @@ static std::string importCSV (const std::vector <std::string>& lines)
task.set ("bg", lowerCase (unquoteText (trim (fields[f])))); task.set ("bg", lowerCase (unquoteText (trim (fields[f]))));
if ((f = mapping["description"]) != -1) if ((f = mapping["description"]) != -1)
task.setDescription (unquoteText (trim (fields[f]))); task.set ("description", unquoteText (trim (fields[f])));
if (! tdb.addT (task)) context.tdb.add (task);
failed.push_back (*it);
} }
catch (...) catch (...)
@ -1113,10 +1117,11 @@ static std::string importCSV (const std::vector <std::string>& lines)
failed.push_back (*it); failed.push_back (*it);
} }
} }
*/
context.tdb.commit ();
context.tdb.unlock ();
std::stringstream out; std::stringstream out;
/*
out << "Imported " out << "Imported "
<< (lines.size () - failed.size () - 1) << (lines.size () - failed.size () - 1)
<< " tasks successfully, with " << " tasks successfully, with "
@ -1130,7 +1135,6 @@ static std::string importCSV (const std::vector <std::string>& lines)
join (bad, "\n", failed); join (bad, "\n", failed);
return out.str () + "\nCould not import:\n\n" + bad; return out.str () + "\nCould not import:\n\n" + bad;
} }
*/
return out.str (); return out.str ();
} }

View file

@ -7,7 +7,8 @@ OBJECTS = ../TDB.o ../Task.o ../valid.o ../text.o ../Date.o ../Table.o \
../Duration.o ../util.o ../Config.o ../Sequence.o ../Att.o ../Cmd.o \ ../Duration.o ../util.o ../Config.o ../Sequence.o ../Att.o ../Cmd.o \
../Record.o ../StringTable.o ../Subst.o ../Nibbler.o ../Location.o \ ../Record.o ../StringTable.o ../Subst.o ../Nibbler.o ../Location.o \
../Filter.o ../Context.o ../Keymap.o ../command.o ../interactive.o \ ../Filter.o ../Context.o ../Keymap.o ../command.o ../interactive.o \
../report.o ../Grid.o ../color.o ../rules.o ../recur.o ../custom.o ../report.o ../Grid.o ../color.o ../rules.o ../recur.o ../custom.o \
../import.o ../edit.o
all: $(PROJECT) all: $(PROJECT)