TW-1440: Implement "task import" from STDIN

- Read tasks from STDIN when no files are specified, or only "-"
  is specified.
This commit is contained in:
Wilhelm Schuermann 2015-05-25 14:25:43 +02:00
parent 3e043291f0
commit a78c9a6eb8
14 changed files with 124 additions and 42 deletions

View file

@ -1,5 +1,6 @@
2.4.5 () -
- TW-1440 "task import" from STDIN (thanks to Renato Alves).
- TW-1572 Better urgency inheritance (thanks to Jens Erat).
------ current release ---------------------------

View file

@ -345,13 +345,15 @@ In general, this is not the recommended method of modifying tasks, but is
provided for exceptional circumstances. Use carefully.
.TP
.B task import <file> [<file> ...]
.B task import [<file> ...]
Imports tasks in the JSON format. The standard task release comes with a few
example scripts, such as:
import-todo.sh.pl
import-yaml.pl
If no file or "-" is specified, import tasks from STDIN.
.TP
.B task log <mods>
Adds a new task that is already completed, to the task list.

View file

@ -41,7 +41,7 @@ extern Context context;
CmdImport::CmdImport ()
{
_keyword = "import";
_usage = "task import <file> [<file> ...]";
_usage = "task import [<file> ...]";
_description = STRING_CMD_IMPORT_USAGE;
_read_only = false;
_displays_id = false;
@ -53,11 +53,25 @@ int CmdImport::execute (std::string& output)
int rc = 0;
int count = 0;
// Use the description as a file name.
// Get filenames from command line arguments.
std::vector <std::string> words = context.cli.getWords ();
if (! words.size ())
throw std::string (STRING_CMD_IMPORT_NOFILE);
if (! words.size () || (words.size () == 1 && words[0] == "-"))
{
// No files or only "-" specified, import tasks from STDIN.
std::vector <std::string> lines;
std::string line;
std::cout << format (STRING_CMD_IMPORT_FILE, "STDIN") << "\n";
while (std::getline (std::cin, line))
lines.push_back (line);
if (lines.size () > 0)
count = import (lines);
}
else
{
// Import tasks from all specified files.
for (auto& word : words)
{
File incoming (word);
@ -70,6 +84,18 @@ int CmdImport::execute (std::string& output)
std::vector <std::string> lines;
incoming.read (lines);
count += import (lines);
}
}
context.footnote (format (STRING_CMD_IMPORT_SUMMARY, count));
return rc;
}
////////////////////////////////////////////////////////////////////////////////
int CmdImport::import (std::vector <std::string>& lines)
{
int count = 0;
for (auto& line : lines)
{
std::string object = trimLeft (
@ -93,10 +119,8 @@ int CmdImport::execute (std::string& output)
<< task.get ("description")
<< "\n";
}
}
context.footnote (format (STRING_CMD_IMPORT_SUMMARY, count));
return rc;
return count;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -35,6 +35,7 @@ class CmdImport : public Command
public:
CmdImport ();
int execute (std::string&);
int import (std::vector <std::string>& lines);
};
/*

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importiert eine JSON-Datei"
#define STRING_CMD_IMPORT_SUMMARY "{1} Aufgabe importiert."
#define STRING_CMD_IMPORT_NOFILE "Sie müssen eine zu importierende Datei angeben."
#define STRING_CMD_IMPORT_FILE "Importiere '{1}'"
#define STRING_CMD_IMPORT_MISSING "Datei '{1}' nicht gefunden."
#define STRING_TASK_NO_DESC "Kommentar fehlt Beschreibung: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Imports JSON files"
#define STRING_CMD_IMPORT_SUMMARY "Imported {1} tasks."
#define STRING_CMD_IMPORT_NOFILE "You must specify a file to import."
#define STRING_CMD_IMPORT_FILE "Importing '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importas JSON-dosierojn"
#define STRING_CMD_IMPORT_SUMMARY "Importis {1} taskojn."
#define STRING_CMD_IMPORT_NOFILE "Vi devas specifi kiu dosieron import."
#define STRING_CMD_IMPORT_FILE "Importanta '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Komento havas mankon de priskribo: {1}"

View file

@ -457,7 +457,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa archivos JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importadas {1} tareas."
#define STRING_CMD_IMPORT_NOFILE "Debe especificar un archivo a importar."
#define STRING_CMD_IMPORT_FILE "Importando '{1}'"
#define STRING_CMD_IMPORT_MISSING "Archivo '{1}' no encontrado."
#define STRING_TASK_NO_DESC "La anotación carece de descripción: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Imports JSON files"
#define STRING_CMD_IMPORT_SUMMARY "Imported {1} tasks."
#define STRING_CMD_IMPORT_NOFILE "You must specify a file to import."
#define STRING_CMD_IMPORT_FILE "Importing '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -448,7 +448,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa file JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importati {1} task."
#define STRING_CMD_IMPORT_NOFILE "Specificare il file da importare."
#define STRING_CMD_IMPORT_FILE "Importazione di '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Annotazione senza descrizione: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "JSON ファイルをインポート"
#define STRING_CMD_IMPORT_SUMMARY "{1} task をインポートしました。"
#define STRING_CMD_IMPORT_NOFILE "インポートするファイルを指定してください。"
#define STRING_CMD_IMPORT_FILE "'{1}' をインポート中"
#define STRING_CMD_IMPORT_MISSING "ファイル '{1}' が見つかりません。"
#define STRING_TASK_NO_DESC "Annotation is missing a description: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importuje pliki JSON"
#define STRING_CMD_IMPORT_SUMMARY "Zaimportowano {1} zadań."
#define STRING_CMD_IMPORT_NOFILE "Musisz podać plik do zaimportowania."
#define STRING_CMD_IMPORT_FILE "Importowanie '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Komentarz nie posiada treści: {1}"

View file

@ -449,7 +449,6 @@
#define STRING_CMD_IMPORT_USAGE "Importa ficheiros JSON"
#define STRING_CMD_IMPORT_SUMMARY "Importadas {1} tarefas."
#define STRING_CMD_IMPORT_NOFILE "Necessita especificar o ficheiro a importar."
#define STRING_CMD_IMPORT_FILE "A importar '{1}'"
#define STRING_CMD_IMPORT_MISSING "File '{1}' not found."
#define STRING_TASK_NO_DESC "Descrição da anotação em falta: {1}"

63
test/import_stdin.t Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
###############################################################################
#
# Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# http://www.opensource.org/licenses/mit-license.php
#
###############################################################################
import sys
import os
import unittest
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from basetest import Task, TestCase
class TestImportSTDIN(TestCase):
def setUp(self):
self.t = Task()
self.t.config("report.foo.description", "foo")
self.t.config("report.foo.columns", "id,description,tags")
self.t.config("report.foo.sort", "id+")
def test_import_stdin(self):
"""When no files specified, import from STDIN"""
json_data = """
{"uuid":"00000000-0000-0000-0000-000000000000","description":"zero","project":"A","status":"pending","entry":"1234567889"}
{"uuid":"11111111-1111-1111-1111-111111111111","description":"one","project":"B","status":"pending","entry":"1234567889"}
{"uuid":"22222222-2222-2222-2222-222222222222","description":"two","status":"completed","entry":"1234524689","end":"1234524690"}""" # borrowed from Perl import test
code, out, err = self.t("import", input=json_data)
self.assertRegexpMatches(err, "Imported 3 tasks.", "import all tasks")
code, out, err = self.t("foo")
self.assertRegexpMatches(out, "1\s+zero", "first task present")
self.assertRegexpMatches(out, "2\s+one", "second task present")
self.assertRegexpMatches(out, "-\s+two", "third task present")
if __name__ == "__main__":
from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4