Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Add ability to load non-resource files #235

Merged
merged 10 commits into from
Nov 14, 2017
Merged

Add ability to load non-resource files #235

merged 10 commits into from
Nov 14, 2017

Conversation

hayfield
Copy link
Contributor

@hayfield hayfield commented Nov 14, 2017

Fixes #234
Fixes #228

Allows custom Schema objects to be created (see: #234).

Also has a wider benefit in providing a consistent method for loading from files. For example, the load_as_dataset() utility function, and handling of encoding (see: #228).

By moving the functions to utilities, it becomes possible to load non-resource files
@hayfield hayfield added incomplete A PR that is in a state that is not ready for review. resources Relating to handling of resource (not code) files. labels Nov 14, 2017
@@ -9,7 +9,7 @@
Example:
To load a test XML file located in `my_test_file` and use it to create a `Dataset`::

dataset = iati.resources.load_as_dataset(iati.tests.resources.get_test_data_path('my_test_file'))
dataset = iati.utilities.load_as_dataset(iati.tests.resources.get_test_data_path('my_test_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.

Nested function call

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -9,7 +9,7 @@
Example:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Module docstring about load_as_x not moved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -34,11 +34,11 @@
SCHEMA_NAME_VALID = 'iati-activities-schema'
"""A string containing a valid Schema name."""

XML_TREE_VALID = etree.fromstring(iati.tests.resources.load_as_string('valid_not_iati'))
XML_TREE_VALID = etree.fromstring(iati.utilities.load_as_string(iati.tests.resources.get_test_data_path('valid_not_iati')))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

load_as_tree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"""An etree that is valid XML but not IATI XML."""
XML_TREE_VALID_IATI = etree.fromstring(iati.tests.resources.load_as_string('valid_iati'))
XML_TREE_VALID_IATI = etree.fromstring(iati.utilities.load_as_string(iati.tests.resources.get_test_data_path('valid_iati')))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

load_as_tree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"""A valid IATI etree."""
XML_TREE_VALID_IATI_INVALID_CODE = etree.fromstring(iati.tests.resources.load_as_string('valid_iati_invalid_code'))
XML_TREE_VALID_IATI_INVALID_CODE = etree.fromstring(iati.utilities.load_as_string(iati.tests.resources.get_test_data_path('valid_iati_invalid_code')))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

load_as_tree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -699,7 +699,7 @@ def get_error_codes():
Raise an error when there is a problem with non-base_exception-related errors.

"""
err_codes_str = iati.resources.load_as_string(iati.resources.get_lib_data_path('validation_err_codes.yaml'))
err_codes_str = iati.utilities.load_as_string(iati.resources.get_lib_data_path('validation_err_codes.yaml'))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nested function call

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no clean way to undertake parameterisation of fixtures with fixtures.
A stackoverflow answer is used as a base:
  https://stackoverflow.com/a/29407931

Fixtures need parametrizing like this due to use of the tmpdir fixture.
There are now cleaner ways to do things. This change updates the README to reflect this.
@hayfield hayfield added complete A PR that is in a state that is ready for review. datasets Relating to IATI Datasets. enhancement Some sort of new functionality (rather than fixing or tweaking something that already existed). and removed incomplete A PR that is in a state that is not ready for review. labels Nov 14, 2017
@hayfield hayfield requested a review from a team November 14, 2017 14:16
@@ -78,7 +78,7 @@ def xml_str(self):

@xml_str.setter
def xml_str(self, value):
Copy link
Contributor

Choose a reason for hiding this comment

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

docstring?

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 tell if you should be ducking because there's no docstring. I assume not though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's a property, the getter is documented but the setter isn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool

@@ -101,11 +103,11 @@ def get_test_ruleset_path(name, version=None):
return os.path.join(PATH_TEST_DATA, iati.resources.get_folder_name_for_version(version), 'rulesets/{0}'.format(name) + iati.resources.FILE_RULESET_EXTENSION)


def load_as_dataset(file_path):
def load_as_dataset(relative_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this and load_as_str be 2 functions? They're very similar. Is it possible to do a try catch where the fail attempts to local the file in the second way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - they are different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(one returns a string, the other a Dataset)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think I was half-asleep when I wrote this one... Not saying your code is dull or anything... 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🍰

Copy link
Contributor

Choose a reason for hiding this comment

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

I am such a professional ⭐️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🥇

Todo:
Ensure all reasonably possible `OSError`s are documented here and in functions that call this.

Add error handling for when the specified file does not exist.
Copy link
Contributor

Choose a reason for hiding this comment

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

Important to be done before going to production. Would suggest doing it now tbh.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO should be removed since the error is a documented part of the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Todo:
Ensure all reasonably possible OSErrors are documented here and in functions that call this.

Add error handling for when the specified file does not exist.
Copy link
Contributor

Choose a reason for hiding this comment

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

As above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO should be removed since the error is a documented part of the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does not fully hide the lxml internal workings. This includes making reference to a private lxml type.

Todo:
Handle when the specified file can be accessed without issue, but it does not contain valid XML.
Copy link
Contributor

Choose a reason for hiding this comment

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

Again on the probs should do sooner rather than later front.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pending adding version-independent tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doc = etree.parse(path)
return doc
except OSError:
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe more error info?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see TODO on the function 05bc5b7

Copy link
Contributor

@allthatilk allthatilk left a comment

Choose a reason for hiding this comment

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

Looks good to me... probably... I'm very tired.

@hayfield hayfield merged commit 42588bd into dev Nov 14, 2017
@hayfield hayfield deleted the custom-schema-loading branch November 14, 2017 15:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
complete A PR that is in a state that is ready for review. datasets Relating to IATI Datasets. enhancement Some sort of new functionality (rather than fixing or tweaking something that already existed). resources Relating to handling of resource (not code) files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants