Skip to content
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

Closed
wants to merge 20 commits into from
Closed

Reading Netex - First version #92

wants to merge 20 commits into from

Conversation

prhod
Copy link
Contributor

@prhod prhod commented May 21, 2018

Here is a first version of the reading of netex to have some feedbacks.
There is still work to do:

  • calendars are empty in oslo
  • transfers are yet to be read
  • documentation is missing
  • Netex is read from examples, checking with official documentation will be done afterward
    Those points will be done in an other PR.

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/).
Copy link
Member

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<()> {
Copy link
Member

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

Copy link
Contributor Author

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() {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor

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" {
Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

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 = "*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no * dependencies please.

/// # }
/// # fn main() { run().unwrap() }
/// ```
pub fn append(&mut self, other: &mut Vec<T>) -> Result<Idx<T>> {
Copy link
Contributor

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.

let i = other.pop();
match i {
None => break,
Some(item) => idx = self.push(item),
Copy link
Contributor

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.

/// ```
pub fn append(&mut self, other: &mut Vec<T>) -> Result<Idx<T>> {
if other.is_empty() {
bail!("provided Vec is empty")
Copy link
Contributor

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.

Some(item) => idx = self.push(item),
};
}
idx
Copy link
Contributor

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(())
}

Copy link
Contributor Author

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 👍

trip_property_id: None,
geometry_id: None,
stop_times: vec![],
};
Copy link
Contributor

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.

calls: &Element,
) {
let mut stop_sequence = 0;
for call in calls.children() {
Copy link
Contributor

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,
}
Copy link
Contributor

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(),
Copy link
Contributor

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();
}
Copy link
Contributor

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();
Copy link
Contributor

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(())
}
Copy link
Contributor

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?

Copy link
Contributor Author

@prhod prhod May 29, 2018

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info! or debug!

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());
Copy link
Contributor

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.

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless comments?

Copy link
Contributor Author

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 ^^

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

@pbougue
Copy link
Contributor

pbougue commented Jul 30, 2018

This was merged little by little, we can now close it.

@pbougue pbougue closed this Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants