-
Notifications
You must be signed in to change notification settings - Fork 5
Add ability to load non-resource files #235
Conversation
By moving the functions to utilities, it becomes possible to load non-resource files
iati/resources.py
Outdated
@@ -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')) |
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.
Nested function call
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.
iati/resources.py
Outdated
@@ -9,7 +9,7 @@ | |||
Example: |
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.
Module docstring about load_as_x
not moved
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.
iati/tests/utilities.py
Outdated
@@ -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'))) |
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.
load_as_tree
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.
iati/tests/utilities.py
Outdated
"""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'))) |
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.
load_as_tree
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.
iati/tests/utilities.py
Outdated
"""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'))) |
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.
load_as_tree
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.
@@ -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')) |
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.
Nested function call
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.
Datasets can now directly handle the result of load_as_tree - this seems like a reasonable thing to expect
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.
@@ -78,7 +78,7 @@ def xml_str(self): | |||
|
|||
@xml_str.setter | |||
def xml_str(self, value): |
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.
docstring?
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 tell if you should be ducking because there's no docstring. I assume not though.
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.
Because it's a property, the getter is documented but the setter isn'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.
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): |
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.
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?
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.
Yes - they are different.
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.
(one returns a string, the other a Dataset)
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.
Yeah, I think I was half-asleep when I wrote this one... Not saying your code is dull or anything... 👀
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.
🍰
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 am such a professional ⭐️
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.
🥇
iati/utilities.py
Outdated
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. |
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.
Important to be done before going to production. Would suggest doing it now tbh.
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.
TODO should be removed since the error is a documented part of the API.
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.
iati/utilities.py
Outdated
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. |
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.
As above.
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.
TODO should be removed since the error is a documented part of the API.
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.
iati/utilities.py
Outdated
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. |
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.
Again on the probs should do sooner rather than later front.
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.
Pending adding version-independent tests.
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.
doc = etree.parse(path) | ||
return doc | ||
except OSError: | ||
raise |
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.
Maybe more error info?
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.
see TODO on the function 05bc5b7
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.
Looks good to me... probably... I'm very tired.
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).