actually implement choosing adapters

This commit is contained in:
phiresky 2019-06-08 00:04:48 +02:00
parent d8fcf7b015
commit a4c82de43d
10 changed files with 72 additions and 10 deletions

View File

@ -14,6 +14,7 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "ffmpeg".to_owned(),
version: 1,
description: "Uses ffmpeg to extract video metadata and subtitles".to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -7,11 +7,12 @@ pub mod tar;
pub mod zip;
use crate::preproc::PreprocConfig;
use failure::*;
use log::*;
use regex::{Regex, RegexSet};
use std::collections::HashMap;
use std::io::prelude::*;
use std::path::Path;
use std::rc::Rc;
//pub use ffmpeg::FffmpegAdapter;
pub enum Matcher {
@ -24,9 +25,11 @@ pub enum Matcher {
}
pub struct AdapterMeta {
/// unique short name of this adapter (a-z0-9 only)
pub name: String,
// version identifier. used to key cache entries, change if your output format changes
/// version identifier. used to key cache entries, change if your output format changes
pub version: i32,
pub description: String,
pub matchers: Vec<Matcher>,
}
@ -76,8 +79,55 @@ pub fn get_adapters() -> Vec<Rc<dyn FileAdapter>> {
adapters
}
pub fn adapter_matcher() -> Result<impl Fn(FileMeta) -> Option<Rc<dyn FileAdapter>>, regex::Error> {
let adapters = get_adapters();
pub fn get_adapters_filtered(adapter_names: &Vec<String>) -> Fallible<Vec<Rc<dyn FileAdapter>>> {
let all_adapters = get_adapters();
let adapters = if !adapter_names.is_empty() {
let adapters_map: HashMap<_, _> = all_adapters
.iter()
.map(|e| (e.metadata().name.clone(), e.clone()))
.collect();
let mut adapters = vec![];
let mut subtractive = false;
for (i, name) in adapter_names.iter().enumerate() {
let mut name = &name[..];
if i == 0 && name.starts_with("-") {
subtractive = true;
name = &name[1..];
adapters = all_adapters.clone();
}
if subtractive {
let inx = adapters
.iter()
.position(|a| &a.metadata().name == name)
.ok_or_else(|| format_err!("Could not remove {}: Not in list", name))?;
adapters.remove(inx);
} else {
adapters.push(
adapters_map
.get(name)
.ok_or_else(|| format_err!("Unknown adapter: \"{}\"", name))?
.clone(),
);
}
}
adapters
} else {
all_adapters
};
debug!(
"Chosen adapters: {}",
adapters
.iter()
.map(|a| a.metadata().name.clone())
.collect::<Vec<String>>()
.join(",")
);
Ok(adapters)
}
pub fn adapter_matcher(
adapter_names: &Vec<String>,
) -> Fallible<impl Fn(FileMeta) -> Option<Rc<dyn FileAdapter>>> {
let adapters = get_adapters_filtered(adapter_names)?;
let mut fname_regexes = vec![];
//let mut mime_regexes = vec![];
for adapter in adapters.into_iter() {

View File

@ -45,6 +45,7 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "pandoc".to_owned(),
version: 1,
description: "Uses pandoc to convert binary/unreadable text documents to plain text markdown-like text".to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -10,6 +10,8 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "poppler".to_owned(),
version: 1,
description: "Uses pdftotext (from poppler-utils) to extract plain text from PDF files"
.to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -11,6 +11,9 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "sqlite".to_owned(),
version: 1,
description:
"Uses sqlite bindings to convert sqlite databases into a simple plain text format"
.to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -12,6 +12,7 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "tar".to_owned(),
version: 1,
description: "Reads a tar file as a stream and recurses down into its contents".to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -13,6 +13,7 @@ lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "zip".to_owned(),
version: 1,
description: "Reads a zip file as a stream and recurses down into its contents".to_owned(),
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -103,7 +103,7 @@ where
{
match std::env::var(RGA_CONFIG) {
Ok(val) => {
error!("Loading args from env {}={}", RGA_CONFIG, val);
debug!("Loading args from env {}={}", RGA_CONFIG, val);
Ok(serde_json::from_str(&val)?)
}
Err(_) => {

View File

@ -38,7 +38,7 @@ fn split_args() -> Fallible<(RgaArgs, Vec<OsString>)> {
for opt in app.p.opts() {
// only parse --x=... for now
if let Some(l) = opt.s.long {
if arg.starts_with(&format!("--{}=", l)) {
if arg.starts_with(&format!("--{}", l)) {
return true;
}
}
@ -62,10 +62,10 @@ fn main() -> Fallible<()> {
env_logger::init();
let (args, passthrough_args) = split_args()?;
let adapters = get_adapters();
let adapters = get_adapters_filtered(&args.rga_adapters)?;
if args.rga_list_adapters {
println!("Adapters:");
println!("Adapters:\n");
for adapter in adapters {
let meta = adapter.metadata();
let matchers = meta
@ -76,7 +76,10 @@ fn main() -> Fallible<()> {
})
.collect::<Vec<_>>()
.join(", ");
print!("{} v{}: {}", meta.name, meta.version, matchers);
print!(
" - {}\n {}\n Extensions: {}\n",
meta.name, meta.description, matchers
);
println!("");
}
return Ok(());

View File

@ -20,7 +20,7 @@ pub struct PreprocConfig<'a> {
*
*/
pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
let adapters = adapter_matcher()?;
let adapters = adapter_matcher(&ai.config.args.rga_adapters)?;
let AdaptInfo {
filepath_hint,
is_real_file,