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 { static ref METADATA: AdapterMeta = AdapterMeta {
name: "ffmpeg".to_owned(), name: "ffmpeg".to_owned(),
version: 1, version: 1,
description: "Uses ffmpeg to extract video metadata and subtitles".to_owned(),
matchers: EXTENSIONS matchers: EXTENSIONS
.iter() .iter()
.map(|s| Matcher::FileExtension(s.to_string())) .map(|s| Matcher::FileExtension(s.to_string()))

View File

@ -7,11 +7,12 @@ pub mod tar;
pub mod zip; pub mod zip;
use crate::preproc::PreprocConfig; use crate::preproc::PreprocConfig;
use failure::*; use failure::*;
use log::*;
use regex::{Regex, RegexSet}; use regex::{Regex, RegexSet};
use std::collections::HashMap;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
//pub use ffmpeg::FffmpegAdapter; //pub use ffmpeg::FffmpegAdapter;
pub enum Matcher { pub enum Matcher {
@ -24,9 +25,11 @@ pub enum Matcher {
} }
pub struct AdapterMeta { pub struct AdapterMeta {
/// unique short name of this adapter (a-z0-9 only)
pub name: String, 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 version: i32,
pub description: String,
pub matchers: Vec<Matcher>, pub matchers: Vec<Matcher>,
} }
@ -76,8 +79,55 @@ pub fn get_adapters() -> Vec<Rc<dyn FileAdapter>> {
adapters adapters
} }
pub fn adapter_matcher() -> Result<impl Fn(FileMeta) -> Option<Rc<dyn FileAdapter>>, regex::Error> { pub fn get_adapters_filtered(adapter_names: &Vec<String>) -> Fallible<Vec<Rc<dyn FileAdapter>>> {
let adapters = get_adapters(); 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 fname_regexes = vec![];
//let mut mime_regexes = vec![]; //let mut mime_regexes = vec![];
for adapter in adapters.into_iter() { for adapter in adapters.into_iter() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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