Merge branch 'imag-calendar/init' into master
This commit is contained in:
commit
b7e996ccfe
8 changed files with 767 additions and 0 deletions
|
@ -21,6 +21,7 @@ members = [
|
||||||
"bin/core/imag-tag",
|
"bin/core/imag-tag",
|
||||||
"bin/core/imag-view",
|
"bin/core/imag-view",
|
||||||
"bin/domain/imag-bookmark",
|
"bin/domain/imag-bookmark",
|
||||||
|
"bin/domain/imag-calendar",
|
||||||
"bin/domain/imag-contact",
|
"bin/domain/imag-contact",
|
||||||
"bin/domain/imag-diary",
|
"bin/domain/imag-diary",
|
||||||
"bin/domain/imag-habit",
|
"bin/domain/imag-habit",
|
||||||
|
|
50
bin/domain/imag-calendar/Cargo.toml
Normal file
50
bin/domain/imag-calendar/Cargo.toml
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
[package]
|
||||||
|
name = "imag-calendar"
|
||||||
|
version = "0.10.0"
|
||||||
|
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
description = "Part of the imag core distribution: imag-calendar command"
|
||||||
|
|
||||||
|
keywords = ["imag", "PIM", "personal", "information", "management"]
|
||||||
|
readme = "../../../README.md"
|
||||||
|
license = "LGPL-2.1"
|
||||||
|
|
||||||
|
documentation = "https://imag-pim.org/doc/"
|
||||||
|
repository = "https://github.com/matthiasbeyer/imag"
|
||||||
|
homepage = "http://imag-pim.org"
|
||||||
|
|
||||||
|
[badges]
|
||||||
|
travis-ci = { repository = "matthiasbeyer/imag" }
|
||||||
|
is-it-maintained-issue-resolution = { repository = "matthiasbeyer/imag" }
|
||||||
|
is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
|
||||||
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
log = "0.4"
|
||||||
|
failure = "0.1"
|
||||||
|
walkdir = "2.2.8"
|
||||||
|
vobject = "0.7"
|
||||||
|
handlebars = "2"
|
||||||
|
chrono = "0.4"
|
||||||
|
kairos = "0.3"
|
||||||
|
|
||||||
|
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
||||||
|
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
||||||
|
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
|
||||||
|
libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" }
|
||||||
|
libimagentryref = { version = "0.10.0", path = "../../../lib/entry/libimagentryref" }
|
||||||
|
libimagentryedit = { version = "0.10.0", path = "../../../lib/entry/libimagentryedit" }
|
||||||
|
libimaginteraction = { version = "0.10.0", path = "../../../lib/etc/libimaginteraction" }
|
||||||
|
libimagcalendar = { version = "0.10.0", path = "../../../lib/domain/libimagcalendar" }
|
||||||
|
|
||||||
|
[dependencies.clap]
|
||||||
|
version = "2.33.0"
|
||||||
|
default-features = false
|
||||||
|
features = ["color", "suggestions", "wrap_help"]
|
||||||
|
|
||||||
|
[dependencies.toml-query]
|
||||||
|
version = "0.9.2"
|
||||||
|
default-features = false
|
||||||
|
features = ["typed"]
|
||||||
|
|
77
bin/domain/imag-calendar/src/filters.rs
Normal file
77
bin/domain/imag-calendar/src/filters.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use vobject::icalendar::Event;
|
||||||
|
use libimagerror::trace::MapErrTrace;
|
||||||
|
|
||||||
|
pub fn event_is_before<'a>(event: &Event<'a>, before_spec: &NaiveDateTime) -> bool {
|
||||||
|
let uid = || event.uid()
|
||||||
|
.map(|uid| uid.into_raw())
|
||||||
|
.unwrap_or_else(|| String::from("<No UID>"));
|
||||||
|
|
||||||
|
let dtend_is_before_spec : Result<bool> = event.dtend()
|
||||||
|
.map(|dtend| {
|
||||||
|
let datetime = try_to_parse_datetime(dtend.raw())?;
|
||||||
|
let result = datetime < *before_spec;
|
||||||
|
trace!("{} < {} => {}", datetime, before_spec, result);
|
||||||
|
Ok(result)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| Err({
|
||||||
|
format_err!("Entry with UID {} has no end time, cannot determine whether to list it",
|
||||||
|
uid())
|
||||||
|
}));
|
||||||
|
|
||||||
|
let dtstamp_is_before_spec : Result<bool> = event.dtstamp()
|
||||||
|
.map(|dtstamp| {
|
||||||
|
let datetime = try_to_parse_datetime(dtstamp.raw())?;
|
||||||
|
let result = datetime < *before_spec;
|
||||||
|
trace!("{} < {} => {}", datetime, before_spec, result);
|
||||||
|
Ok(result)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| Err({
|
||||||
|
format_err!("Entry with UID {} has no timestamp, cannot determine whether to list it",
|
||||||
|
uid())
|
||||||
|
}));
|
||||||
|
|
||||||
|
trace!("dtend_is_before_spec = {:?}", dtend_is_before_spec);
|
||||||
|
trace!("dtstamp_is_before_spec = {:?}", dtstamp_is_before_spec);
|
||||||
|
|
||||||
|
match (dtend_is_before_spec, dtstamp_is_before_spec) {
|
||||||
|
(Ok(b), _) => return b,
|
||||||
|
(_, Ok(b)) => return b,
|
||||||
|
(Err(e), _) => return Err(e).map_err_trace_exit_unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn event_is_after<'a>(event: &Event<'a>, after_spec: &NaiveDateTime) -> bool {
|
||||||
|
!event_is_before(event, after_spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_to_parse_datetime(s: &str) -> Result<NaiveDateTime> {
|
||||||
|
const FORMATS : &[&'static str] = &[
|
||||||
|
"%Y%m%dT%H%M%S",
|
||||||
|
"%Y%m%dT%H%M%SZ"
|
||||||
|
];
|
||||||
|
|
||||||
|
::libimagutil::date::try_to_parse_datetime_from_string(s, FORMATS.iter())
|
||||||
|
.ok_or_else(|| format_err!("Cannot parse datetime: {}", s))
|
||||||
|
}
|
||||||
|
|
309
bin/domain/imag-calendar/src/main.rs
Normal file
309
bin/domain/imag-calendar/src/main.rs
Normal file
|
@ -0,0 +1,309 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
|
#![deny(
|
||||||
|
non_camel_case_types,
|
||||||
|
non_snake_case,
|
||||||
|
path_statements,
|
||||||
|
trivial_numeric_casts,
|
||||||
|
unstable_features,
|
||||||
|
unused_allocation,
|
||||||
|
unused_import_braces,
|
||||||
|
unused_imports,
|
||||||
|
unused_must_use,
|
||||||
|
unused_mut,
|
||||||
|
unused_qualifications,
|
||||||
|
while_true,
|
||||||
|
)]
|
||||||
|
|
||||||
|
#[macro_use] extern crate failure;
|
||||||
|
#[macro_use] extern crate log;
|
||||||
|
extern crate clap;
|
||||||
|
extern crate toml_query;
|
||||||
|
extern crate walkdir;
|
||||||
|
extern crate handlebars;
|
||||||
|
extern crate chrono;
|
||||||
|
extern crate kairos;
|
||||||
|
|
||||||
|
#[macro_use] extern crate libimagrt;
|
||||||
|
extern crate libimagcalendar;
|
||||||
|
extern crate libimagerror;
|
||||||
|
extern crate libimagstore;
|
||||||
|
extern crate libimagutil;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::result::Result as RResult;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use failure::Error;
|
||||||
|
use failure::err_msg;
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use toml_query::read::Partial;
|
||||||
|
use toml_query::read::TomlValueReadExt;
|
||||||
|
use walkdir::DirEntry;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
use vobject::icalendar::Event;
|
||||||
|
|
||||||
|
use libimagcalendar::store::EventStore;
|
||||||
|
use libimagerror::io::ToExitCode;
|
||||||
|
use libimagerror::exit::ExitUnwrap;
|
||||||
|
use libimagerror::iter::TraceIterator;
|
||||||
|
use libimagerror::trace::MapErrTrace;
|
||||||
|
use libimagrt::runtime::Runtime;
|
||||||
|
use libimagrt::setup::generate_runtime_setup;
|
||||||
|
|
||||||
|
mod filters;
|
||||||
|
mod ui;
|
||||||
|
mod util;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let version = make_imag_version!();
|
||||||
|
let rt = generate_runtime_setup("imag-calendar",
|
||||||
|
&version,
|
||||||
|
"Calendar management tool",
|
||||||
|
crate::ui::build_ui);
|
||||||
|
|
||||||
|
|
||||||
|
if let Some(name) = rt.cli().subcommand_name() {
|
||||||
|
debug!("Call {}", name);
|
||||||
|
match name {
|
||||||
|
"import" => import(&rt),
|
||||||
|
"list" => list(&rt),
|
||||||
|
"show" => show(&rt),
|
||||||
|
other => {
|
||||||
|
warn!("Right now, only the 'import' command is available");
|
||||||
|
debug!("Unknown command");
|
||||||
|
let _ = rt.handle_unknown_subcommand("imag-calendar", other, rt.cli())
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.code()
|
||||||
|
.map(::std::process::exit);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn import(rt: &Runtime) {
|
||||||
|
let scmd = rt.cli().subcommand_matches("import").unwrap(); // safe by clap
|
||||||
|
let collection_name = rt.cli().value_of("calendar-ref-collection-name").unwrap(); // default by clap
|
||||||
|
let do_fail = scmd.is_present("import-fail");
|
||||||
|
let force_override = scmd.is_present("import-force-override");
|
||||||
|
let ref_config = rt.config()
|
||||||
|
.ok_or_else(|| format_err!("No configuration, cannot continue!"))
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.read_partial::<libimagentryref::reference::Config>()
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
// sanity check
|
||||||
|
debug!("Doing sanity check on config, to see whether the configuration required for importing is there");
|
||||||
|
if ref_config.get(collection_name).is_none() {
|
||||||
|
error!("Configuration missing: {}.{}", libimagentryref::reference::Config::LOCATION, collection_name);
|
||||||
|
::std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("Starting import...");
|
||||||
|
scmd.values_of("filesordirs")
|
||||||
|
.unwrap() // save by clap
|
||||||
|
.into_iter()
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.map(|path| if path.is_dir() { // Find all files
|
||||||
|
Box::new(WalkDir::new(path)
|
||||||
|
.follow_links(false)
|
||||||
|
.into_iter()
|
||||||
|
.filter_entry(is_not_hidden)
|
||||||
|
.filter_map(|r| match r {
|
||||||
|
Err(e) => Some(Err(Error::from(e))),
|
||||||
|
Ok(fe) => {
|
||||||
|
if fe.file_type().is_file() {
|
||||||
|
let path = fe.into_path();
|
||||||
|
trace!("Found file: {}", path.display());
|
||||||
|
Some(Ok(path))
|
||||||
|
} else {
|
||||||
|
None // filter out directories
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})) as Box<dyn Iterator<Item = Result<PathBuf>>>
|
||||||
|
} else { // is file, ensured by clap validator
|
||||||
|
Box::new(std::iter::once(Ok(path)))
|
||||||
|
})
|
||||||
|
.flat_map(|it| it) // From Iter<Iter<Result<PathBuf>>> to Iter<Result<PathBuf>>
|
||||||
|
.trace_unwrap_exit() //... to Iter<PathBuf>
|
||||||
|
.map(|path| {
|
||||||
|
trace!("Importing {}", path.display());
|
||||||
|
let v = rt.store().import_from_path(path, collection_name, &ref_config, force_override)?;
|
||||||
|
Ok(v.into_iter()
|
||||||
|
.filter_map(|result| if do_fail {
|
||||||
|
Some(result.map_err_trace_exit_unwrap())
|
||||||
|
} else {
|
||||||
|
match result {
|
||||||
|
Err(e) => { warn!("Error while importing: {}", e); None }
|
||||||
|
Ok(fle) => Some(fle),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
.trace_unwrap_exit()
|
||||||
|
.flat_map(|it| it)
|
||||||
|
.for_each(|fle| rt.report_touched(fle.get_location()).unwrap_or_exit());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list(rt: &Runtime) {
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe by clap
|
||||||
|
let list_format = get_event_print_format("calendar.list_format", rt, &scmd)
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
let do_filter_past = !scmd.is_present("list-past");
|
||||||
|
let do_filter_before = scmd.value_of("list-before");
|
||||||
|
let do_filter_after = scmd.value_of("list-after");
|
||||||
|
|
||||||
|
let ref_config = rt.config()
|
||||||
|
.ok_or_else(|| format_err!("No configuration, cannot continue!"))
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.read_partial::<libimagentryref::reference::Config>()
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
debug!("List format: {:?}", list_format);
|
||||||
|
debug!("Ref config : {:?}", ref_config);
|
||||||
|
let today = ::chrono::Local::now().naive_local();
|
||||||
|
|
||||||
|
let event_filter = |e: &'_ Event| { // what a crazy hack to make the compiler happy
|
||||||
|
debug!("Filtering event: {:?}", e);
|
||||||
|
|
||||||
|
// generate a function `filter_past` which filters out the past or not
|
||||||
|
let allow_all_past_events = |event| if do_filter_past {
|
||||||
|
filters::event_is_before(event, &today)
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
let do_filter_before = do_filter_before.map(|spec| kairos_parse(spec).map_err_trace_exit_unwrap());
|
||||||
|
|
||||||
|
let allow_events_before_date = |event| do_filter_before.as_ref().map(|spec| {
|
||||||
|
filters::event_is_before(event, spec)
|
||||||
|
}).unwrap_or(true);
|
||||||
|
|
||||||
|
|
||||||
|
let do_filter_after = do_filter_after.map(|spec| kairos_parse(spec).map_err_trace_exit_unwrap());
|
||||||
|
|
||||||
|
let allow_events_after_date = |event| do_filter_after.as_ref().map(|spec| {
|
||||||
|
filters::event_is_after(event, spec)
|
||||||
|
}).unwrap_or(true);
|
||||||
|
|
||||||
|
allow_all_past_events(e) && allow_events_before_date(e) && allow_events_after_date(e)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut listed_events = 0;
|
||||||
|
|
||||||
|
rt.store()
|
||||||
|
.all_events()
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.trace_unwrap_exit()
|
||||||
|
.map(|sid| rt.store().get(sid))
|
||||||
|
.trace_unwrap_exit()
|
||||||
|
.map(|oe| oe.ok_or_else(|| err_msg("Missing entry while calling all_events()")))
|
||||||
|
.trace_unwrap_exit()
|
||||||
|
.map(|ev| ParsedEventFLE::parse(ev, &ref_config))
|
||||||
|
.trace_unwrap_exit()
|
||||||
|
.for_each(|parsed_entry| {
|
||||||
|
parsed_entry
|
||||||
|
.get_data()
|
||||||
|
.events()
|
||||||
|
.filter_map(RResult::ok)
|
||||||
|
.filter(event_filter)
|
||||||
|
.for_each(|event| {
|
||||||
|
listed_events = listed_events + 1;
|
||||||
|
let data = build_data_object_for_handlebars(listed_events, &event);
|
||||||
|
|
||||||
|
let rendered = list_format
|
||||||
|
.render("format", &data)
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
writeln!(rt.stdout(), "{}", rendered).to_exit_code().unwrap_or_exit()
|
||||||
|
});
|
||||||
|
|
||||||
|
rt.report_touched(parsed_entry.get_entry().get_location()).unwrap_or_exit();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show(rt: &Runtime) {
|
||||||
|
let scmd = rt.cli().subcommand_matches("show").unwrap(); // safe by clap
|
||||||
|
let ref_config = rt.config()
|
||||||
|
.ok_or_else(|| format_err!("No configuration, cannot continue!"))
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.read_partial::<libimagentryref::reference::Config>()
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
let list_format = util::get_event_print_format("calendar.show_format", rt, &scmd)
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
let mut shown_events = 0;
|
||||||
|
|
||||||
|
scmd.values_of("show-ids")
|
||||||
|
.unwrap() // safe by clap
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|id| {
|
||||||
|
util::find_event_by_id(rt.store(), id, &ref_config)
|
||||||
|
.map(|entry| { debug!("Found => {:?}", entry); entry })
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.map(|parsed| (parsed, id))
|
||||||
|
})
|
||||||
|
.for_each(|(parsed_entry, id)| {
|
||||||
|
parsed_entry
|
||||||
|
.get_data()
|
||||||
|
.events()
|
||||||
|
.filter_map(RResult::ok)
|
||||||
|
.filter(|pent| {
|
||||||
|
let relevant = pent.uid().map(|uid| uid.raw().starts_with(id)).unwrap_or(false);
|
||||||
|
debug!("Relevant {} => {}", parsed_entry.get_entry().get_location(), relevant);
|
||||||
|
relevant
|
||||||
|
})
|
||||||
|
.for_each(|event| {
|
||||||
|
shown_events = shown_events + 1;
|
||||||
|
let data = util::build_data_object_for_handlebars(shown_events, &event);
|
||||||
|
|
||||||
|
let rendered = list_format
|
||||||
|
.render("format", &data)
|
||||||
|
.map_err(Error::from)
|
||||||
|
.map_err_trace_exit_unwrap();
|
||||||
|
|
||||||
|
writeln!(rt.stdout(), "{}", rendered).to_exit_code().unwrap_or_exit()
|
||||||
|
});
|
||||||
|
|
||||||
|
rt.report_touched(parsed_entry.get_entry().get_location()).unwrap_or_exit();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// helper function to check whether a DirEntry points to something hidden (starting with dot)
|
||||||
|
fn is_not_hidden(entry: &DirEntry) -> bool {
|
||||||
|
!entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
123
bin/domain/imag-calendar/src/ui.rs
Normal file
123
bin/domain/imag-calendar/src/ui.rs
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use clap::{Arg, App, SubCommand};
|
||||||
|
|
||||||
|
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
|
app
|
||||||
|
.arg(Arg::with_name("calendar-ref-collection-name")
|
||||||
|
.long("ref-collection")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.default_value("calendars")
|
||||||
|
.help("Name (Key) of the basepath setting in the configuration file to use"))
|
||||||
|
|
||||||
|
.subcommand(SubCommand::with_name("import")
|
||||||
|
.about("Import directory of calendar files or files directl")
|
||||||
|
.version("0.1")
|
||||||
|
.arg(Arg::with_name("filesordirs")
|
||||||
|
.index(1)
|
||||||
|
.takes_value(true)
|
||||||
|
.required(true)
|
||||||
|
.multiple(true)
|
||||||
|
.value_name("PATH")
|
||||||
|
.validator(import_validator)
|
||||||
|
.help("Import files from this directory (or specify files directly)"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("import-fail")
|
||||||
|
.short("F")
|
||||||
|
.long("fail")
|
||||||
|
.takes_value(false)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("Fail if a file cannot be parsed (if directory is given, all files found must be icalendar files)"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("import-force-override")
|
||||||
|
.long("force")
|
||||||
|
.takes_value(false)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("Override if entry for event already exists"))
|
||||||
|
)
|
||||||
|
|
||||||
|
.subcommand(SubCommand::with_name("list")
|
||||||
|
.about("List calendar entries")
|
||||||
|
.version("0.1")
|
||||||
|
.arg(Arg::with_name("format")
|
||||||
|
.long("format")
|
||||||
|
.short("F")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("Override the format used to list one event"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("list-past")
|
||||||
|
.long("past")
|
||||||
|
.takes_value(false)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("List past events"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("list-before")
|
||||||
|
.long("before")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("List events which are dated before certain date"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("list-after")
|
||||||
|
.long("after")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("List events which are dated after certain date"))
|
||||||
|
)
|
||||||
|
|
||||||
|
.subcommand(SubCommand::with_name("show")
|
||||||
|
.about("Show one or several calendar entries")
|
||||||
|
.version("0.1")
|
||||||
|
.arg(Arg::with_name("show-ids")
|
||||||
|
.index(1)
|
||||||
|
.takes_value(true)
|
||||||
|
.required(true)
|
||||||
|
.multiple(true)
|
||||||
|
.help("UIDs of Calendar entries to show"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("format")
|
||||||
|
.long("format")
|
||||||
|
.short("F")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.multiple(false)
|
||||||
|
.help("Override the format used to show events"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn import_validator<A: AsRef<str>>(s: A) -> Result<(), String> {
|
||||||
|
use libimagutil::cli_validators::*;
|
||||||
|
|
||||||
|
is_existing_path(s.as_ref())?;
|
||||||
|
|
||||||
|
match (is_file(s.as_ref()), is_directory(s.as_ref())) {
|
||||||
|
(Err(_), Err(_)) => Err(format!("Not a file or directory: {}", s.as_ref())),
|
||||||
|
_ => Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
190
bin/domain/imag-calendar/src/util.rs
Normal file
190
bin/domain/imag-calendar/src/util.rs
Normal file
|
@ -0,0 +1,190 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use clap::ArgMatches;
|
||||||
|
use vobject::icalendar::ICalendar;
|
||||||
|
use vobject::icalendar::Event;
|
||||||
|
use handlebars::Handlebars;
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use failure::Error;
|
||||||
|
use failure::err_msg;
|
||||||
|
use toml_query::read::TomlValueReadTypeExt;
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
|
|
||||||
|
use libimagrt::runtime::Runtime;
|
||||||
|
use libimagstore::store::FileLockEntry;
|
||||||
|
use libimagstore::store::Store;
|
||||||
|
use libimagentryref::reference::fassade::RefFassade;
|
||||||
|
use libimagentryref::reference::Ref;
|
||||||
|
use libimagentryref::reference::Config;
|
||||||
|
use libimagentryref::hasher::default::DefaultHasher;
|
||||||
|
use libimagerror::trace::MapErrTrace;
|
||||||
|
use crate::libimagcalendar::store::EventStore;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ParsedEventFLE<'a> {
|
||||||
|
inner: FileLockEntry<'a>,
|
||||||
|
data: ICalendar,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ParsedEventFLE<'a> {
|
||||||
|
|
||||||
|
/// Because libimagcalendar only links to the actual calendar data, we need to read the data and
|
||||||
|
/// parse it.
|
||||||
|
/// With this function, a FileLockEntry can be parsed to a ParsedEventFileLockEntry
|
||||||
|
/// (ParsedEventFLE).
|
||||||
|
pub fn parse(fle: FileLockEntry<'a>, refconfig: &Config) -> Result<Self> {
|
||||||
|
fle.as_ref_with_hasher::<DefaultHasher>()
|
||||||
|
.get_path(refconfig)
|
||||||
|
.and_then(|p| ::std::fs::read_to_string(p).map_err(Error::from))
|
||||||
|
.and_then(|s| ICalendar::build(&s).map_err(Error::from))
|
||||||
|
.map(|cal| ParsedEventFLE {
|
||||||
|
inner: fle,
|
||||||
|
data: cal,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_entry(&self) -> &FileLockEntry<'a> {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_data(&self) -> &ICalendar {
|
||||||
|
&self.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_event_print_format(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches)
|
||||||
|
-> Result<Handlebars>
|
||||||
|
{
|
||||||
|
scmd.value_of("format")
|
||||||
|
.map(String::from)
|
||||||
|
.map(Ok)
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
rt.config()
|
||||||
|
.ok_or_else(|| err_msg("No configuration file"))?
|
||||||
|
.read_string(config_value_path)?
|
||||||
|
.ok_or_else(|| err_msg("Configuration 'contact.list_format' does not exist"))
|
||||||
|
})
|
||||||
|
.and_then(|fmt| {
|
||||||
|
let mut hb = Handlebars::new();
|
||||||
|
hb.register_template_string("format", fmt)?;
|
||||||
|
|
||||||
|
hb.register_escape_fn(::handlebars::no_escape);
|
||||||
|
::libimaginteraction::format::register_all_color_helpers(&mut hb);
|
||||||
|
::libimaginteraction::format::register_all_format_helpers(&mut hb);
|
||||||
|
|
||||||
|
Ok(hb)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_data_object_for_handlebars<'a>(i: usize, event: &Event<'a>)
|
||||||
|
-> BTreeMap<&'static str, String>
|
||||||
|
{
|
||||||
|
macro_rules! process_opt {
|
||||||
|
($t:expr, $text:expr) => {
|
||||||
|
($t).map(|obj| obj.into_raw()).unwrap_or_else(|| String::from($text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut data = BTreeMap::new();
|
||||||
|
|
||||||
|
data.insert("i" , format!("{}", i));
|
||||||
|
data.insert("dtend" , process_opt!(event.dtend() , "<no dtend>"));
|
||||||
|
data.insert("dtstart" , process_opt!(event.dtstart() , "<no dtstart>"));
|
||||||
|
data.insert("dtstamp" , process_opt!(event.dtstamp() , "<no dtstamp>"));
|
||||||
|
data.insert("uid" , process_opt!(event.uid() , "<no uid>"));
|
||||||
|
data.insert("description" , process_opt!(event.description() , "<no description>"));
|
||||||
|
data.insert("summary" , process_opt!(event.summary() , "<no summary>"));
|
||||||
|
data.insert("url" , process_opt!(event.url() , "<no url>"));
|
||||||
|
data.insert("location" , process_opt!(event.location() , "<no location>"));
|
||||||
|
data.insert("class" , process_opt!(event.class() , "<no class>"));
|
||||||
|
data.insert("categories" , process_opt!(event.categories() , "<no categories>"));
|
||||||
|
data.insert("transp" , process_opt!(event.transp() , "<no transp>"));
|
||||||
|
data.insert("rrule" , process_opt!(event.rrule() , "<no rrule>"));
|
||||||
|
|
||||||
|
data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kairos_parse(spec: &str) -> Result<NaiveDateTime> {
|
||||||
|
match ::kairos::parser::parse(spec).map_err_trace_exit_unwrap() {
|
||||||
|
::kairos::parser::Parsed::Iterator(_) => {
|
||||||
|
trace!("before-filter spec resulted in iterator");
|
||||||
|
Err(format_err!("Not a moment in time: {}", spec))
|
||||||
|
}
|
||||||
|
|
||||||
|
::kairos::parser::Parsed::TimeType(tt) => {
|
||||||
|
trace!("before-filter spec resulted in timetype");
|
||||||
|
let tt = tt.calculate()
|
||||||
|
.map_err_trace_exit_unwrap()
|
||||||
|
.get_moment().unwrap_or_else(|| {
|
||||||
|
error!("Not a moment in time: {}", spec);
|
||||||
|
::std::process::exit(1);
|
||||||
|
})
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
trace!("Before filter spec {} => {}", spec, tt);
|
||||||
|
Ok(tt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_event_by_id<'a>(store: &'a Store, id: &str, refconfig: &Config) -> Result<Option<ParsedEventFLE<'a>>> {
|
||||||
|
if let Some(entry) = store.get_event_by_uid(id)? {
|
||||||
|
debug!("Found directly: {} -> {}", id, entry.get_location());
|
||||||
|
return ParsedEventFLE::parse(entry, refconfig).map(Some)
|
||||||
|
}
|
||||||
|
|
||||||
|
for sid in store.all_events()? {
|
||||||
|
let sid = sid?;
|
||||||
|
|
||||||
|
let event = store.get(sid.clone())?.ok_or_else(|| {
|
||||||
|
format_err!("Cannot get {}, which should be there.", sid)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
trace!("Checking whether {} is represented by {}", id, event.get_location());
|
||||||
|
let parsed = ParsedEventFLE::parse(event, refconfig)?;
|
||||||
|
|
||||||
|
if parsed
|
||||||
|
.get_data()
|
||||||
|
.events()
|
||||||
|
.filter_map(|event| if event
|
||||||
|
.as_ref()
|
||||||
|
.map(|e| {
|
||||||
|
trace!("Checking whether {:?} starts with {}", e.uid(), id);
|
||||||
|
e.uid().map(|uid| uid.raw().starts_with(id)).unwrap_or(false)
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
trace!("Seems to be relevant");
|
||||||
|
Some(event)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
return Ok(Some(parsed))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
16
imagrc.toml
16
imagrc.toml
|
@ -334,6 +334,21 @@ Email : {{EMAIL}}
|
||||||
Address : {{ADR}}
|
Address : {{ADR}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
[calendar]
|
||||||
|
list_format = "{{lpad 5 i}} | {{abbrev 5 uid}} | {{summary}} | {{location}}"
|
||||||
|
show_format = """
|
||||||
|
{{i}} - {{uid}}
|
||||||
|
|
||||||
|
Summary : {{summary}}
|
||||||
|
Start : {{dtstart}}
|
||||||
|
End : {{dtend}}
|
||||||
|
Url : {{url}}
|
||||||
|
Location : {{location}}
|
||||||
|
|
||||||
|
{{description}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
[log]
|
[log]
|
||||||
logs = ["default"]
|
logs = ["default"]
|
||||||
default = "default"
|
default = "default"
|
||||||
|
@ -353,6 +368,7 @@ execute_in_store = false
|
||||||
music = "/home/user/music"
|
music = "/home/user/music"
|
||||||
mail = "/home/user/mail"
|
mail = "/home/user/mail"
|
||||||
contacts = "/home/user/contacts"
|
contacts = "/home/user/contacts"
|
||||||
|
calendars = "/home/user/calendars"
|
||||||
|
|
||||||
[mail]
|
[mail]
|
||||||
# The name of the mail reference collection
|
# The name of the mail reference collection
|
||||||
|
|
|
@ -41,6 +41,7 @@ CRATES=(
|
||||||
./lib/domain/libimagwiki
|
./lib/domain/libimagwiki
|
||||||
./bin/domain/imag-habit
|
./bin/domain/imag-habit
|
||||||
./bin/domain/imag-diary
|
./bin/domain/imag-diary
|
||||||
|
./bin/domain/imag-calendar
|
||||||
./bin/domain/imag-contact
|
./bin/domain/imag-contact
|
||||||
./bin/domain/imag-notes
|
./bin/domain/imag-notes
|
||||||
./bin/domain/imag-bookmark
|
./bin/domain/imag-bookmark
|
||||||
|
|
Loading…
Reference in a new issue