-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading Netex - First version #92
Conversation
README.md
Outdated
navitia_model is a rust crate to manage transit data. Its model corresponds to the one used in [navitia](https://github.com/CanalTP/navitia). This repository also provides (incomplete) [GTFS](http://gtfs.org/) to [NTFS](https://github.com/CanalTP/navitia/blob/dev/documentation/ntfs/ntfs_fr.md) and (soon) NTFS to GTFS conversion. | ||
navitia_model is a rust crate to manage transit data. Its model corresponds to the one used in [navitia](https://github.com/CanalTP/navitia). This repository also provides : | ||
- (incomplete) [GTFS](http://gtfs.org/) to [NTFS](https://github.com/CanalTP/navitia/blob/dev/documentation/ntfs/ntfs_fr.md) and (soon) NTFS to GTFS conversion. | ||
- (incomplete) [Netex](http://netex-cen.eu/) to [NTFS](https://github.com/CanalTP/navitia/blob/dev/documentation/ntfs/ntfs_fr.md) and (soon) Netex to GTFS conversion. Conversion fixtues comes from official [Github repository](https://github.com/NeTEx-CEN/NeTEx/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixtures
src/netex/mod.rs
Outdated
extern crate tempdir; | ||
extern crate zip; | ||
|
||
fn add_prefix(prefix: String, collections: &mut Collections) -> Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is duplicated (from gtfs/mod.rs)
We should move it to another place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've created a read_utils
mod for those functions. I needed to change the result of the fn read_config. Tell me if you're not comfortable with it.
src/netex/mod.rs
Outdated
println!("Reading Netex data from {:?}", path); | ||
let mut collections = Collections::default(); | ||
if path.is_file() { | ||
match path.extension().unwrap().to_str().unwrap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have 2 unwrap
. are you sure this never returns None?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use will be on .xml files or .zip files, so the extension() will always return something. the to_str() will always be successful if the string is UTF-8, and I assume it will always be OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if there is a file without extension?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match path.extension()
.ok_or_else(|| format_err!("input file {:?} has no extention", path))?
.to_str()
.ok_or_else(|| format_err!("input file {:?} has unknown extention", path))?
{
Or maybe
match path.extension().and_then(|ext| ext.to_str()) {
and match on the Option.
src/netex/mod.rs
Outdated
let mut zip = zip::ZipArchive::new(zip_file)?; | ||
for i in 0..zip.len() { | ||
let mut file = zip.by_index(i)?; | ||
if file.sanitized_name().extension().unwrap() == "xml" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here file.sanitized_name().extension()
could be None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've added a check
src/ntfs/mod.rs
Outdated
@@ -170,6 +172,18 @@ pub fn write<P: AsRef<path::Path>>(model: &Model, path: P) -> Result<()> { | |||
Ok(()) | |||
} | |||
|
|||
/// Exports a `Model` to a | |||
/// [NTFS](https://github.com/CanalTP/navitia/blob/dev/documentation/ntfs/ntfs_fr.md) | |||
/// ZIP archiveat the given full path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
archive at
Cargo.toml
Outdated
@@ -14,6 +14,11 @@ serde_derive = "1" | |||
structopt = "0.2" | |||
failure = "0.1.1" | |||
serde_json = "1" | |||
quick-xml = "0.12.1" | |||
minidom = "*" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no * dependencies please.
src/collection.rs
Outdated
/// # } | ||
/// # fn main() { run().unwrap() } | ||
/// ``` | ||
pub fn append(&mut self, other: &mut Vec<T>) -> Result<Idx<T>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idx is strange. Better to just return Result<()>
to be consistent with the append of Collection.
src/collection.rs
Outdated
let i = other.pop(); | ||
match i { | ||
None => break, | ||
Some(item) => idx = self.push(item), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a failure, and then a success, you'll quiet the error. Please add a test of this case.
src/collection.rs
Outdated
/// ``` | ||
pub fn append(&mut self, other: &mut Vec<T>) -> Result<Idx<T>> { | ||
if other.is_empty() { | ||
bail!("provided Vec is empty") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't care of that, it must be legal.
src/collection.rs
Outdated
Some(item) => idx = self.push(item), | ||
}; | ||
} | ||
idx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn append(&mut self, other: &mut Vec<T>) -> Result<()> {
for o in v.drain(..) {
let _ = self.push(item)?
}
Ok(())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the code, it's much better 👍
src/netex/read.rs
Outdated
trip_property_id: None, | ||
geometry_id: None, | ||
stop_times: vec![], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The VehicleJourney { ..Default::default() }
should be great in this case.
src/netex/read.rs
Outdated
calls: &Element, | ||
) { | ||
let mut stop_sequence = 0; | ||
for call in calls.children() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (stop_sequence, call) in calls.children().enumerate() {
phone: None, | ||
address: None, | ||
sort_order: None, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..Default::default()
"tram" => "Tramway".to_string(), | ||
"water" => "Boat".to_string(), | ||
"cableWay" => "BusRapidTransit".to_string(), | ||
_ => "Bus".to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really?
destination_id: None, | ||
}; | ||
collections.routes.push(route).unwrap(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..Default::default()
src/ntfs/mod.rs
Outdated
pub fn write_to_zip<P: AsRef<path::Path>>(model: &Model, path: P) -> Result<()> { | ||
let path = path.as_ref(); | ||
info!("Writing NTFS to ZIP File {:?}", path); | ||
let input_tmp_dir = TempDir::new("read_netex_work").unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no unwrap, simply ?
src/ntfs/mod.rs
Outdated
write(model, input_tmp_dir.path())?; | ||
zip_to(input_tmp_dir.path(), path)?; | ||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you do that without temp dir, directly in the zip file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this, but trying to achieve this would implies a heavy refacto and/or a lot of time to do it. The main purpose of this PR is to enable a Netex reading. It could be an enhancement on a further PR (issue #93).
src/utils.rs
Outdated
let mut zip = zip::ZipWriter::new(file); | ||
let options = zip::write::FileOptions::default() | ||
.compression_method(zip::CompressionMethod::Bzip2) | ||
.unix_permissions(0o755); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default permissions should be OK, and 0o644 are better permissions for a zip file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, i'm removing permissions
src/utils.rs
Outdated
|
||
let mut zip = zip::ZipWriter::new(file); | ||
let options = zip::write::FileOptions::default() | ||
.compression_method(zip::CompressionMethod::Bzip2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it work out of the box on Windows? Better to use a standard compression algorithm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not confident with compression algorithm. I totally agree with you. Which one is the most suited ?
src/utils.rs
Outdated
.to_string(); | ||
|
||
if path.is_file() { | ||
println!("adding {:?} as {:?} ...", path, name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
info! or debug!
tests/read_netex.rs
Outdated
file1.read_to_end(&mut file1_content)?; | ||
file2.read_to_end(&mut file2_content)?; | ||
let compare = file1_content == file2_content; //to avoid having file content in std output | ||
assert!(compare, "content of file {} is different", file1.name()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can assert!(a == b, "msg")
it will not output file content.
tests/read_netex.rs
Outdated
let tmp_dir = TempDir::new("netex_computed_result").unwrap(); | ||
let file_path = tmp_dir.path().join("netex_computed_result_ratp.zip"); | ||
// let tmp_dir = Path::new("fixtures/netex/computed_result/"); | ||
// let file_path = tmp_dir.join("netex_computed_result_ratp.zip"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useless comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
until stabilized, I use those lines to generate the expected result in a local dir instead of the tmp_dir. I'm removing them ^^
tests/read_netex.rs
Outdated
let tmp_dir = TempDir::new("netex_computed_result").unwrap(); | ||
let file_path = tmp_dir.path().join("netex_computed_result_oslo.zip"); | ||
// let tmp_dir = Path::new("fixtures/netex/computed_result/"); | ||
// let file_path = tmp_dir.join("netex_computed_result_oslo.zip"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same.
This was merged little by little, we can now close it. |
Here is a first version of the reading of netex to have some feedbacks.
There is still work to do:
Those points will be done in an other PR.