Support format strings when dumping imported items
In commit 3eae7ce (Add --list-imported command line option, 2016-01-12), we added an option to print the hashes of imported items to stdout. Extend this command line option such that it dumps the items using the specified formatting strings. With the new behavior it is, for example, easier to check items for import errors. Also, rename the option from --list-imported to --dump-imported (it is not part of any official release yet so we do not need to care about backwards compatibility). Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
fe0621bafd
commit
07954626c6
@ -45,7 +45,11 @@ def calcurse_wipe():
|
|||||||
|
|
||||||
|
|
||||||
def calcurse_import(icaldata):
|
def calcurse_import(icaldata):
|
||||||
p = subprocess.Popen([calcurse, '-i', '-', '--list-imported', '-q'],
|
p = subprocess.Popen([calcurse, '-i', '-', '--dump-imported', '-q',
|
||||||
|
'--format-recur-apt=%(hash)\\n',
|
||||||
|
'--format-event=%(hash)\\n',
|
||||||
|
'--format-recur-event=%(hash)\\n',
|
||||||
|
'--format-todo=%(hash)\\n'],
|
||||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
return p.communicate(icaldata.encode('utf-8'))[0].decode('utf-8').rstrip()
|
return p.communicate(icaldata.encode('utf-8'))[0].decode('utf-8').rstrip()
|
||||||
|
|
||||||
|
@ -135,8 +135,10 @@ appointments can be specified using the *-c* flag.
|
|||||||
*-l* <num>, *--limit* <num>::
|
*-l* <num>, *--limit* <num>::
|
||||||
Limit the number of results printed to 'num'.
|
Limit the number of results printed to 'num'.
|
||||||
|
|
||||||
*--list-imported*::
|
*--dump-imported*::
|
||||||
When importing items, print the hash of each newly created object to stdout.
|
When importing items, print each newly created object to stdout. Format
|
||||||
|
strings can be used to specify which details are printed. See also:
|
||||||
|
'Formatting Options'.
|
||||||
|
|
||||||
*-n*, *--next*::
|
*-n*, *--next*::
|
||||||
Print the next appointment within upcoming 24 hours and exit. The indicated
|
Print the next appointment within upcoming 24 hours and exit. The indicated
|
||||||
|
@ -328,8 +328,10 @@ can be specified using the `-c` flag.
|
|||||||
`-l <num>, --limit <num>`::
|
`-l <num>, --limit <num>`::
|
||||||
Limit the number of results printed to 'num'.
|
Limit the number of results printed to 'num'.
|
||||||
|
|
||||||
`--list-imported`::
|
`--dump-imported`::
|
||||||
When importing items, print the hash of each newly created object to stdout.
|
When importing items, print each newly created object to stdout. Format
|
||||||
|
strings can be used to specify which details are printed. See also:
|
||||||
|
<<basics_format_strings,Format strings>>.
|
||||||
|
|
||||||
`-n, --next`::
|
`-n, --next`::
|
||||||
Print the next appointment within upcoming 24 hours and exit. The indicated
|
Print the next appointment within upcoming 24 hours and exit. The indicated
|
||||||
|
29
src/args.c
29
src/args.c
@ -70,7 +70,7 @@ enum {
|
|||||||
OPT_FMT_EV,
|
OPT_FMT_EV,
|
||||||
OPT_FMT_REV,
|
OPT_FMT_REV,
|
||||||
OPT_FMT_TODO,
|
OPT_FMT_TODO,
|
||||||
OPT_LIST_IMPORTED,
|
OPT_DUMP_IMPORTED,
|
||||||
OPT_EXPORT_UID,
|
OPT_EXPORT_UID,
|
||||||
OPT_READ_ONLY,
|
OPT_READ_ONLY,
|
||||||
OPT_STATUS,
|
OPT_STATUS,
|
||||||
@ -402,7 +402,7 @@ int parse_args(int argc, char **argv)
|
|||||||
const char *fmt_todo = NULL;
|
const char *fmt_todo = NULL;
|
||||||
/* Import and export parameters */
|
/* Import and export parameters */
|
||||||
int xfmt = IO_EXPORT_ICAL;
|
int xfmt = IO_EXPORT_ICAL;
|
||||||
int list_imported = 0, export_uid = 0;
|
int dump_imported = 0, export_uid = 0;
|
||||||
/* Data file locations */
|
/* Data file locations */
|
||||||
const char *cfile = NULL, *datadir = NULL, *ifile = NULL;
|
const char *cfile = NULL, *datadir = NULL, *ifile = NULL;
|
||||||
|
|
||||||
@ -459,7 +459,7 @@ int parse_args(int argc, char **argv)
|
|||||||
{"format-recur-event", required_argument, NULL, OPT_FMT_REV},
|
{"format-recur-event", required_argument, NULL, OPT_FMT_REV},
|
||||||
{"format-todo", required_argument, NULL, OPT_FMT_TODO},
|
{"format-todo", required_argument, NULL, OPT_FMT_TODO},
|
||||||
{"export-uid", no_argument, NULL, OPT_EXPORT_UID},
|
{"export-uid", no_argument, NULL, OPT_EXPORT_UID},
|
||||||
{"list-imported", no_argument, NULL, OPT_LIST_IMPORTED},
|
{"dump-imported", no_argument, NULL, OPT_DUMP_IMPORTED},
|
||||||
{"read-only", no_argument, NULL, OPT_READ_ONLY},
|
{"read-only", no_argument, NULL, OPT_READ_ONLY},
|
||||||
{"status", no_argument, NULL, OPT_STATUS},
|
{"status", no_argument, NULL, OPT_STATUS},
|
||||||
{"daemon", no_argument, NULL, OPT_DAEMON},
|
{"daemon", no_argument, NULL, OPT_DAEMON},
|
||||||
@ -669,8 +669,8 @@ int parse_args(int argc, char **argv)
|
|||||||
case OPT_FMT_TODO:
|
case OPT_FMT_TODO:
|
||||||
fmt_todo = optarg;
|
fmt_todo = optarg;
|
||||||
break;
|
break;
|
||||||
case OPT_LIST_IMPORTED:
|
case OPT_DUMP_IMPORTED:
|
||||||
list_imported = 1;
|
dump_imported = 1;
|
||||||
break;
|
break;
|
||||||
case OPT_EXPORT_UID:
|
case OPT_EXPORT_UID:
|
||||||
export_uid = 1;
|
export_uid = 1;
|
||||||
@ -776,7 +776,24 @@ int parse_args(int argc, char **argv)
|
|||||||
/* Get default pager in case we need to show a log file. */
|
/* Get default pager in case we need to show a log file. */
|
||||||
vars_init();
|
vars_init();
|
||||||
io_load_data(NULL);
|
io_load_data(NULL);
|
||||||
io_import_data(IO_IMPORT_ICAL, ifile, list_imported);
|
if (dump_imported) {
|
||||||
|
/*
|
||||||
|
* Use default values for non-specified format strings.
|
||||||
|
*/
|
||||||
|
fmt_apt = fmt_apt ? fmt_apt : "%(raw)";
|
||||||
|
fmt_rapt = fmt_rapt ? fmt_rapt : "%(raw)";
|
||||||
|
fmt_ev = fmt_ev ? fmt_ev : "%(raw)";
|
||||||
|
fmt_rev = fmt_rev ? fmt_rev : "%(raw)";
|
||||||
|
fmt_todo = fmt_todo ? fmt_todo : "%(raw)";
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* Do not dump items, unset format strings explicitly.
|
||||||
|
*/
|
||||||
|
fmt_apt = fmt_rapt = fmt_ev = fmt_rev = NULL;
|
||||||
|
fmt_todo = NULL;
|
||||||
|
}
|
||||||
|
io_import_data(IO_IMPORT_ICAL, ifile, fmt_ev, fmt_rev, fmt_apt,
|
||||||
|
fmt_rapt, fmt_todo);
|
||||||
io_save_apts(path_apts);
|
io_save_apts(path_apts);
|
||||||
io_save_todo(path_todo);
|
io_save_todo(path_todo);
|
||||||
} else if (export) {
|
} else if (export) {
|
||||||
|
@ -276,7 +276,7 @@ static inline void key_generic_reload(void)
|
|||||||
static inline void key_generic_import(void)
|
static inline void key_generic_import(void)
|
||||||
{
|
{
|
||||||
wins_erase_status_bar();
|
wins_erase_status_bar();
|
||||||
io_import_data(IO_IMPORT_ICAL, NULL, 0);
|
io_import_data(IO_IMPORT_ICAL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||||
ui_calendar_monthly_view_cache_set_invalid();
|
ui_calendar_monthly_view_cache_set_invalid();
|
||||||
do_storage(0);
|
do_storage(0);
|
||||||
wins_update(FLAG_ALL);
|
wins_update(FLAG_ALL);
|
||||||
|
@ -809,8 +809,9 @@ int display_help(const char *);
|
|||||||
int run_hook(const char *);
|
int run_hook(const char *);
|
||||||
|
|
||||||
/* ical.c */
|
/* ical.c */
|
||||||
void ical_import_data(FILE *, FILE *, int, unsigned *, unsigned *, unsigned *,
|
void ical_import_data(FILE *, FILE *, unsigned *, unsigned *, unsigned *,
|
||||||
unsigned *, unsigned *);
|
unsigned *, unsigned *, const char *, const char *,
|
||||||
|
const char *, const char *, const char *);
|
||||||
void ical_export_data(FILE *, int);
|
void ical_export_data(FILE *, int);
|
||||||
|
|
||||||
/* io.c */
|
/* io.c */
|
||||||
@ -837,7 +838,8 @@ int io_check_file(const char *);
|
|||||||
int io_check_data_files(void);
|
int io_check_data_files(void);
|
||||||
void io_startup_screen(int);
|
void io_startup_screen(int);
|
||||||
void io_export_data(enum export_type, int);
|
void io_export_data(enum export_type, int);
|
||||||
void io_import_data(enum import_type, const char *, int);
|
void io_import_data(enum import_type, const char *, const char *, const char *,
|
||||||
|
const char *, const char *, const char *);
|
||||||
struct io_file *io_log_init(void);
|
struct io_file *io_log_init(void);
|
||||||
void io_log_print(struct io_file *, int, const char *);
|
void io_log_print(struct io_file *, int, const char *);
|
||||||
void io_log_display(struct io_file *, const char *, const char *);
|
void io_log_display(struct io_file *, const char *, const char *);
|
||||||
|
83
src/ical.c
83
src/ical.c
@ -325,21 +325,19 @@ static void ical_log(FILE * log, ical_types_e type, unsigned lineno,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void ical_store_todo(int priority, int completed, char *mesg,
|
static void ical_store_todo(int priority, int completed, char *mesg,
|
||||||
char *note, int list)
|
char *note, const char *fmt_todo)
|
||||||
{
|
{
|
||||||
struct todo *todo = todo_add(mesg, priority, completed, note);
|
struct todo *todo = todo_add(mesg, priority, completed, note);
|
||||||
if (list) {
|
if (fmt_todo)
|
||||||
char *hash = todo_hash(todo);
|
print_todo(fmt_todo, todo);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
mem_free(mesg);
|
mem_free(mesg);
|
||||||
erase_note(¬e);
|
erase_note(¬e);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ical_store_event(char *mesg, char *note, long day, long end,
|
ical_store_event(char *mesg, char *note, long day, long end,
|
||||||
ical_rpt_t * rpt, llist_t * exc, int list)
|
ical_rpt_t * rpt, llist_t * exc, const char *fmt_ev,
|
||||||
|
const char *fmt_rev)
|
||||||
{
|
{
|
||||||
const int EVENTID = 1;
|
const int EVENTID = 1;
|
||||||
struct event *ev;
|
struct event *ev;
|
||||||
@ -349,21 +347,15 @@ ical_store_event(char *mesg, char *note, long day, long end,
|
|||||||
rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
|
rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
|
||||||
rpt->freq, rpt->until, exc);
|
rpt->freq, rpt->until, exc);
|
||||||
mem_free(rpt);
|
mem_free(rpt);
|
||||||
if (list) {
|
if (fmt_rev)
|
||||||
char *hash = recur_event_hash(rev);
|
print_recur_event(fmt_rev, day, rev);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end == 0 || end - day <= DAYINSEC) {
|
if (end == 0 || end - day <= DAYINSEC) {
|
||||||
ev = event_new(mesg, note, day, EVENTID);
|
ev = event_new(mesg, note, day, EVENTID);
|
||||||
if (list) {
|
if (fmt_ev)
|
||||||
char *hash = event_hash(ev);
|
print_event(fmt_ev, day, ev);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,11 +375,8 @@ ical_store_event(char *mesg, char *note, long day, long end,
|
|||||||
rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
|
rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
|
||||||
rpt->freq, rpt->until, exc);
|
rpt->freq, rpt->until, exc);
|
||||||
mem_free(rpt);
|
mem_free(rpt);
|
||||||
if (list) {
|
if (fmt_rev)
|
||||||
char *hash = recur_event_hash(rev);
|
print_recur_event(fmt_rev, day, rev);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
mem_free(mesg);
|
mem_free(mesg);
|
||||||
@ -396,7 +385,8 @@ cleanup:
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
ical_store_apoint(char *mesg, char *note, long start, long dur,
|
ical_store_apoint(char *mesg, char *note, long start, long dur,
|
||||||
ical_rpt_t * rpt, llist_t * exc, int has_alarm, int list)
|
ical_rpt_t * rpt, llist_t * exc, int has_alarm,
|
||||||
|
const char *fmt_apt, const char *fmt_rapt)
|
||||||
{
|
{
|
||||||
char state = 0L;
|
char state = 0L;
|
||||||
struct apoint *apt;
|
struct apoint *apt;
|
||||||
@ -408,18 +398,12 @@ ical_store_apoint(char *mesg, char *note, long start, long dur,
|
|||||||
rapt = recur_apoint_new(mesg, note, start, dur, state,
|
rapt = recur_apoint_new(mesg, note, start, dur, state,
|
||||||
rpt->type, rpt->freq, rpt->until, exc);
|
rpt->type, rpt->freq, rpt->until, exc);
|
||||||
mem_free(rpt);
|
mem_free(rpt);
|
||||||
if (list) {
|
if (fmt_rapt)
|
||||||
char *hash = recur_apoint_hash(rapt);
|
print_recur_apoint(fmt_rapt, start, rapt->start, rapt);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
apt = apoint_new(mesg, note, start, dur, state);
|
apt = apoint_new(mesg, note, start, dur, state);
|
||||||
if (list) {
|
if (fmt_apt)
|
||||||
char *hash = apoint_hash(apt);
|
print_apoint(fmt_apt, start, apt);
|
||||||
printf("%s\n", hash);
|
|
||||||
mem_free(hash);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mem_free(mesg);
|
mem_free(mesg);
|
||||||
erase_note(¬e);
|
erase_note(¬e);
|
||||||
@ -897,9 +881,10 @@ static char *ical_read_summary(char *line)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ical_read_event(FILE * fdi, FILE * log, int list, unsigned *noevents,
|
ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
|
||||||
unsigned *noapoints, unsigned *noskipped, char *buf,
|
unsigned *noapoints, unsigned *noskipped, char *buf,
|
||||||
char *lstore, unsigned *lineno)
|
char *lstore, unsigned *lineno, const char *fmt_ev,
|
||||||
|
const char *fmt_rev, const char *fmt_apt, const char *fmt_rapt)
|
||||||
{
|
{
|
||||||
const int ITEMLINE = *lineno;
|
const int ITEMLINE = *lineno;
|
||||||
ical_vevent_e vevent_type;
|
ical_vevent_e vevent_type;
|
||||||
@ -963,13 +948,15 @@ ical_read_event(FILE * fdi, FILE * log, int list, unsigned *noevents,
|
|||||||
ical_store_apoint(vevent.mesg, vevent.note,
|
ical_store_apoint(vevent.mesg, vevent.note,
|
||||||
vevent.start, vevent.dur,
|
vevent.start, vevent.dur,
|
||||||
vevent.rpt, &vevent.exc,
|
vevent.rpt, &vevent.exc,
|
||||||
vevent.has_alarm, list);
|
vevent.has_alarm, fmt_apt,
|
||||||
|
fmt_rapt);
|
||||||
(*noapoints)++;
|
(*noapoints)++;
|
||||||
break;
|
break;
|
||||||
case EVENT:
|
case EVENT:
|
||||||
ical_store_event(vevent.mesg, vevent.note,
|
ical_store_event(vevent.mesg, vevent.note,
|
||||||
vevent.start, vevent.end,
|
vevent.start, vevent.end,
|
||||||
vevent.rpt, &vevent.exc, list);
|
vevent.rpt, &vevent.exc,
|
||||||
|
fmt_ev, fmt_rev);
|
||||||
(*noevents)++;
|
(*noevents)++;
|
||||||
break;
|
break;
|
||||||
case UNDEFINED:
|
case UNDEFINED:
|
||||||
@ -1050,9 +1037,8 @@ cleanup:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ical_read_todo(FILE * fdi, FILE * log, int list, unsigned *notodos,
|
ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos, unsigned *noskipped,
|
||||||
unsigned *noskipped, char *buf, char *lstore,
|
char *buf, char *lstore, unsigned *lineno, const char *fmt_todo)
|
||||||
unsigned *lineno)
|
|
||||||
{
|
{
|
||||||
const int ITEMLINE = *lineno;
|
const int ITEMLINE = *lineno;
|
||||||
struct {
|
struct {
|
||||||
@ -1083,7 +1069,7 @@ ical_read_todo(FILE * fdi, FILE * log, int list, unsigned *notodos,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ical_store_todo(vtodo.priority, vtodo.completed,
|
ical_store_todo(vtodo.priority, vtodo.completed,
|
||||||
vtodo.mesg, vtodo.note, list);
|
vtodo.mesg, vtodo.note, fmt_todo);
|
||||||
(*notodos)++;
|
(*notodos)++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1121,9 +1107,11 @@ cleanup:
|
|||||||
|
|
||||||
/* Import calcurse data. */
|
/* Import calcurse data. */
|
||||||
void
|
void
|
||||||
ical_import_data(FILE * stream, FILE * log, int list, unsigned *events,
|
ical_import_data(FILE * stream, FILE * log, unsigned *events,
|
||||||
unsigned *apoints, unsigned *todos, unsigned *lines,
|
unsigned *apoints, unsigned *todos, unsigned *lines,
|
||||||
unsigned *skipped)
|
unsigned *skipped, const char *fmt_ev, const char *fmt_rev,
|
||||||
|
const char *fmt_apt, const char *fmt_rapt,
|
||||||
|
const char *fmt_todo)
|
||||||
{
|
{
|
||||||
char buf[BUFSIZ], lstore[BUFSIZ];
|
char buf[BUFSIZ], lstore[BUFSIZ];
|
||||||
int major, minor;
|
int major, minor;
|
||||||
@ -1139,11 +1127,12 @@ ical_import_data(FILE * stream, FILE * log, int list, unsigned *events,
|
|||||||
while (ical_readline(stream, buf, lstore, lines)) {
|
while (ical_readline(stream, buf, lstore, lines)) {
|
||||||
(*lines)++;
|
(*lines)++;
|
||||||
if (starts_with_ci(buf, "BEGIN:VEVENT")) {
|
if (starts_with_ci(buf, "BEGIN:VEVENT")) {
|
||||||
ical_read_event(stream, log, list, events, apoints,
|
ical_read_event(stream, log, events, apoints,
|
||||||
skipped, buf, lstore, lines);
|
skipped, buf, lstore, lines, fmt_ev,
|
||||||
|
fmt_rev, fmt_apt, fmt_rapt);
|
||||||
} else if (starts_with_ci(buf, "BEGIN:VTODO")) {
|
} else if (starts_with_ci(buf, "BEGIN:VTODO")) {
|
||||||
ical_read_todo(stream, log, list, todos, skipped, buf,
|
ical_read_todo(stream, log, todos, skipped, buf,
|
||||||
lstore, lines);
|
lstore, lines, fmt_todo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
src/io.c
10
src/io.c
@ -1222,7 +1222,10 @@ static FILE *get_import_stream(enum import_type type)
|
|||||||
* A temporary log file is created in /tmp to store the import process report,
|
* A temporary log file is created in /tmp to store the import process report,
|
||||||
* and is cleared at the end.
|
* and is cleared at the end.
|
||||||
*/
|
*/
|
||||||
void io_import_data(enum import_type type, const char *stream_name, int list)
|
void io_import_data(enum import_type type, const char *stream_name,
|
||||||
|
const char *fmt_ev, const char *fmt_rev,
|
||||||
|
const char *fmt_apt, const char *fmt_rapt,
|
||||||
|
const char *fmt_todo)
|
||||||
{
|
{
|
||||||
const char *proc_report =
|
const char *proc_report =
|
||||||
_("Import process report: %04d lines read");
|
_("Import process report: %04d lines read");
|
||||||
@ -1266,9 +1269,10 @@ void io_import_data(enum import_type type, const char *stream_name, int list)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (type == IO_IMPORT_ICAL)
|
if (type == IO_IMPORT_ICAL)
|
||||||
ical_import_data(stream, log->fd, list, &stats.events,
|
ical_import_data(stream, log->fd, &stats.events,
|
||||||
&stats.apoints, &stats.todos,
|
&stats.apoints, &stats.todos,
|
||||||
&stats.lines, &stats.skipped);
|
&stats.lines, &stats.skipped, fmt_ev, fmt_rev,
|
||||||
|
fmt_apt, fmt_rapt, fmt_todo);
|
||||||
|
|
||||||
if (stream != stdin)
|
if (stream != stdin)
|
||||||
file_close(stream, __FILE_POS__);
|
file_close(stream, __FILE_POS__);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user