allow to specify 'fassung' of laws
All checks were successful
CI/CD Pipeline / test (push) Successful in 1m18s

This commit is contained in:
philipp 2024-08-27 13:48:31 +02:00
parent 0154d00c58
commit d7f3f96a5e
7 changed files with 39 additions and 67 deletions

View File

@ -1,46 +0,0 @@
#!/bin/bash
# Check if at least one argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <output_directory>"
exit 1
fi
# Assign the first argument as the output directory
OUTPUT_DIR="$1"
# Ensure the output directory exists or try to create it
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
if [ $? -ne 0 ]; then
echo "Failed to create output directory: $OUTPUT_DIR"
exit 2
fi
fi
# Directory containing config files
CONFIG_DIR="data/configs/"
# Check if the config directory exists
if [ ! -d "$CONFIG_DIR" ]; then
echo "Configuration directory does not exist: $CONFIG_DIR"
exit 3
fi
# Iterate over each .toml file in the config directory
for config_file in "$CONFIG_DIR"*.toml; do
# Check if config files are present
if [ ! -f "$config_file" ]; then
echo "No .toml config files found in $CONFIG_DIR"
exit 4
fi
# Extract the filename without the extension
base_name=$(basename -- "$config_file" .toml)
# Execute the cargo command with the current config file and redirect the output
echo "Processing $config_file..."
cargo r -r -- -c "$config_file" > "$OUTPUT_DIR/$base_name.md"
done
echo "All configurations processed successfully."

View File

@ -91,13 +91,13 @@ impl Config {
/// use risp::Config; /// use risp::Config;
/// use std::path::Path; /// use std::path::Path;
/// ///
/// let (_, law_id, builder, parser) = Config::load(Path::new("data/configs/abgb.toml")).unwrap(); /// let (_, law_id, _,builder, parser) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
/// ///
/// assert_eq!(law_id, 10001622); /// assert_eq!(law_id, 10001622);
/// ``` /// ```
pub fn load<P: AsRef<Path> + std::fmt::Debug>( pub fn load<P: AsRef<Path> + std::fmt::Debug>(
path: P, path: P,
) -> Result<(Typ, usize, law::Builder, Parser), Error> { ) -> Result<(Typ, usize, String, law::Builder, Parser), Error> {
info!("Using cache dir: {}", misc::get_cache_dir().unwrap()); info!("Using cache dir: {}", misc::get_cache_dir().unwrap());
let config_str = fs::read_to_string(path)?; let config_str = fs::read_to_string(path)?;
@ -153,7 +153,13 @@ impl Config {
); );
parser.move_para_headers_into_content(); parser.move_para_headers_into_content();
} }
Ok((config.law.typ, config.law.id, builder, parser)) Ok((
config.law.typ,
config.law.id,
config.law.fassung,
builder,
parser,
))
} }
} }
@ -163,10 +169,16 @@ struct Law {
name: String, name: String,
#[serde(default = "default_type")] #[serde(default = "default_type")]
typ: Typ, typ: Typ,
#[serde(default = "today")]
fassung: String,
par_sign: Option<String>, par_sign: Option<String>,
classifiers: Option<Vec<Classifier>>, classifiers: Option<Vec<Classifier>>,
} }
fn today() -> String {
String::from("today")
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Classifier { struct Classifier {
name: String, name: String,

View File

@ -76,8 +76,8 @@ impl Law {
pub fn from_config<P: AsRef<Path> + tracing::Value + std::fmt::Debug>( pub fn from_config<P: AsRef<Path> + tracing::Value + std::fmt::Debug>(
path: P, path: P,
) -> Result<Law, Error> { ) -> Result<Law, Error> {
let (typ, law_id, mut builder, parser) = Config::load(path)?; let (typ, law_id, fassung, mut builder, parser) = Config::load(path)?;
let pars = parse(&typ, law_id)?; let pars = parse(&typ, law_id, &fassung)?;
for par in pars { for par in pars {
let cont = parser.parse(&par, &mut builder)?; let cont = parser.parse(&par, &mut builder)?;
@ -308,7 +308,7 @@ impl Builder {
/// use risp::{Config, law::{Law, Heading}}; /// use risp::{Config, law::{Law, Heading}};
/// use std::path::Path; /// use std::path::Path;
/// ///
/// let (_, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap(); /// let (_, _, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
/// ///
/// builder.new_header("1. Theil"); /// builder.new_header("1. Theil");
/// ///
@ -401,7 +401,7 @@ impl Builder {
/// use risp::{Config, law::{Law, Heading}}; /// use risp::{Config, law::{Law, Heading}};
/// use std::path::Path; /// use std::path::Path;
/// ///
/// let (_, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap(); /// let (_, _, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
/// ///
/// builder.new_header("1. Theil"); /// builder.new_header("1. Theil");
/// builder.new_desc("Description of the first header"); /// builder.new_desc("Description of the first header");
@ -460,7 +460,7 @@ impl Builder {
/// use risp::{Config, law::{Law, Heading, Content, HeadingContent, Section}}; /// use risp::{Config, law::{Law, Heading, Content, HeadingContent, Section}};
/// use std::path::Path; /// use std::path::Path;
/// ///
/// let (_, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap(); /// let (_, _, _, mut builder, _) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
/// builder.new_header("1. Theil"); /// builder.new_header("1. Theil");
/// ///
/// let par = "§ 1".to_string(); /// let par = "§ 1".to_string();
@ -736,7 +736,7 @@ mod tests {
for config in configs { for config in configs {
let path = format!("{}", config.unwrap().path().display()); let path = format!("{}", config.unwrap().path().display());
if path.contains("abgb") { if path.contains("abgb") {
let (_, _law_id, mut builder, _) = Config::load(&path).unwrap(); let (_, _law_id, _, mut builder, _) = Config::load(&path).unwrap();
builder.new_header("Nullter Theil. Einleitung"); builder.new_header("Nullter Theil. Einleitung");
builder.new_par("§ 1.".into(), Content::Text("My test law text.".into())); builder.new_par("§ 1.".into(), Content::Text("My test law text.".into()));
builder.new_header("Erster Hauptstück. Einleitung"); builder.new_header("Erster Hauptstück. Einleitung");

View File

@ -58,7 +58,7 @@ fn main() {
} else { } else {
let config = &args.config.unwrap(); // ok, checked with clap let config = &args.config.unwrap(); // ok, checked with clap
if let Some(par_url) = &args.par_url { if let Some(par_url) = &args.par_url {
let (_, _, mut builder, parser) = Config::load(config).unwrap(); let (_, _, _, mut builder, parser) = Config::load(config).unwrap();
builder.add_classifier(Classifier::new("always-true", Arc::new(always_true))); builder.add_classifier(Classifier::new("always-true", Arc::new(always_true)));
// builder.new_header("initial"); // builder.new_header("initial");
parser.parse(par_url, &mut builder).unwrap(); parser.parse(par_url, &mut builder).unwrap();

View File

@ -62,18 +62,18 @@ use ris_structure::OgdSearchResult;
/// ``` /// ```
/// use risp::{Typ, overview::parse}; /// use risp::{Typ, overview::parse};
/// ///
/// let list_with_xml_links_to_paragraphs = parse(&Typ::Bund, 10001905).unwrap(); /// let list_with_xml_links_to_paragraphs = parse(&Typ::Bund, 10001905, "today").unwrap();
/// assert_eq!(list_with_xml_links_to_paragraphs.len(), 31); // TEG has 31 paragraphs /// assert_eq!(list_with_xml_links_to_paragraphs.len(), 31); // TEG has 31 paragraphs
/// assert_eq!(list_with_xml_links_to_paragraphs[0], "https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12025190/NOR12025190.xml"); // Link to first paragraph /// assert_eq!(list_with_xml_links_to_paragraphs[0], "https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12025190/NOR12025190.xml"); // Link to first paragraph
/// ``` /// ```
pub fn parse(typ: &Typ, law_id: usize) -> Result<Vec<String>, Error> { pub fn parse(typ: &Typ, law_id: usize, fassung: &str) -> Result<Vec<String>, Error> {
let mut page = 1; let mut page = 1;
let mut skip = true; let mut skip = true;
let mut ret = Vec::new(); let mut ret = Vec::new();
loop { loop {
//info!("=== Fetching overview page #{page} ==="); //info!("=== Fetching overview page #{page} ===");
event!(Level::INFO, "Fetching over page #{page}"); event!(Level::INFO, "Fetching over page #{page}");
let json = fetch_page(typ, law_id, page)?; let json = fetch_page(typ, law_id, fassung, page)?;
let (cont, nodes) = parse_from_str(&json, skip)?; let (cont, nodes) = parse_from_str(&json, skip)?;
for n in nodes { for n in nodes {
ret.push(n.clone()); ret.push(n.clone());
@ -114,7 +114,7 @@ fn parse_from_str(content: &str, skip_first: bool) -> Result<(bool, Vec<String>)
Ok((true, ret)) Ok((true, ret))
} }
fn fetch_page(typ: &Typ, overview_id: usize, page: usize) -> Result<String, Error> { fn fetch_page(typ: &Typ, overview_id: usize, fassung: &str, page: usize) -> Result<String, Error> {
use std::fs; use std::fs;
let expected_filename = format!("{}law-{overview_id}-{page}", get_cache_dir()?); let expected_filename = format!("{}law-{overview_id}-{page}", get_cache_dir()?);
@ -143,13 +143,18 @@ fn fetch_page(typ: &Typ, overview_id: usize, page: usize) -> Result<String, Erro
let overview_id = format!("{overview_id}"); let overview_id = format!("{overview_id}");
let page = format!("{page}"); let page = format!("{page}");
let current_date = current_date();
let fassung = if fassung == "today" {
current_date()
} else {
fassung.into()
};
let mut form_params = vec![ let mut form_params = vec![
("Applikation", application), ("Applikation", application),
("Gesetzesnummer", &overview_id), ("Gesetzesnummer", &overview_id),
("DokumenteProSeite", "OneHundred"), ("DokumenteProSeite", "OneHundred"),
("Seitennummer", &page), ("Seitennummer", &page),
("Fassung.FassungVom", &current_date), ("Fassung.FassungVom", &fassung),
]; ];
if let Some(additional) = additional_params { if let Some(additional) = additional_params {
@ -183,9 +188,9 @@ mod tests {
for config in configs { for config in configs {
let path = format!("{}", config.unwrap().path().display()); let path = format!("{}", config.unwrap().path().display());
let (typ, law_id, _, _) = Config::load(&path).unwrap(); let (typ, law_id, fassung, _, _) = Config::load(&path).unwrap();
let actual = parse(&typ, law_id).unwrap(); let actual = parse(&typ, law_id, &fassung).unwrap();
let expected_path = format!("./data/expected/overview/{law_id}"); let expected_path = format!("./data/expected/overview/{law_id}");
match fs::read_to_string(&expected_path) { match fs::read_to_string(&expected_path) {
Ok(expected) => { Ok(expected) => {

View File

@ -105,7 +105,7 @@ impl Parser {
/// use risp::{Config, law::{Law, Heading, Content, Section, HeadingContent}}; /// use risp::{Config, law::{Law, Heading, Content, Section, HeadingContent}};
/// use std::path::Path; /// use std::path::Path;
/// ///
/// let (_, _, mut builder, parser) = Config::load(Path::new("data/configs/abgb.toml")).unwrap(); /// let (_, _, _, mut builder, parser) = Config::load(Path::new("data/configs/abgb.toml")).unwrap();
/// let result = parser.parse("https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12017691/NOR12017691.xml", &mut builder).unwrap(); /// let result = parser.parse("https://www.ris.bka.gv.at/Dokumente/Bundesnormen/NOR12017691/NOR12017691.xml", &mut builder).unwrap();
/// ///
/// let law: Law = builder.into(); /// let law: Law = builder.into();
@ -223,7 +223,7 @@ mod tests {
let path = format!("{}", config.unwrap().path().display()); let path = format!("{}", config.unwrap().path().display());
println!("Testing {path}"); println!("Testing {path}");
let (_, law_id, mut builder, parser) = Config::load(&path).unwrap(); let (_, law_id, _, mut builder, parser) = Config::load(&path).unwrap();
let paragraph_path = format!("./data/expected/overview/{law_id}"); let paragraph_path = format!("./data/expected/overview/{law_id}");
let expected_path = format!("./data/expected/par/{law_id}"); let expected_path = format!("./data/expected/par/{law_id}");

View File

@ -3,4 +3,5 @@
cargo r -- --clear-cache cargo r -- --clear-cache
rm ./data/expected/overview/* rm ./data/expected/overview/*
rm ./data/expected/par/* rm ./data/expected/par/*
cargo t cargo t overview
cargo t all_configs_produce_expected_output