-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add Read_SDMX method #159
Closed
Closed
Add Read_SDMX method #159
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3664415
Draft code for read_sdmx.
javihern98 bdf8145
Merge branch 'develop' into 156-add-read_sdmx-convenience-method
javihern98 2d87af7
Refactored code on Message class to move submission and ActionType to…
javihern98 617aa4c
Linting and mypy changes
javihern98 2c2cab3
Refactored code to ensure we use the Short URN as keys of the message…
javihern98 ffb2a8e
Added tests for read_sdmx with csv files.
javihern98 01f5489
Linting and mypy changes.
javihern98 ab63f01
Adapted structures tests to reach max code coverage
javihern98 8b960d2
Removed unique_id method and adapted code to use Reference.
javihern98 41e7a8d
Added utils methods and classes to all.
javihern98 b140de0
Adapted tests for input processor to max code coverage.
javihern98 0c03933
Adapted tests for read_sdmx to max code coverage.
javihern98 f567c50
Added tag to concept and itemReference to avoid serialization issues.
javihern98 85076af
Merge remote-tracking branch 'refs/remotes/origin/develop' into 156-a…
javihern98 fe1fbb4
Fixed message imports to prevent circular imports. Ruff automatic fix…
javihern98 6b6886f
Adapted ReadSDMX to infer the format automatically.
javihern98 b6e6200
Linting changes.
javihern98 03dd8ad
Renamed ReadFormat to SDMXFormat. Added Submission and Error formats …
javihern98 bd27711
Ignored mypy error.
javihern98 5818c40
Added to_schema method to DataStructureDefinition. Draft code for get…
javihern98 6a4bac7
Replaced whole URN to Short URN in Dataset. Added tests to match cove…
javihern98 d38ed7f
Added get_datasets to io
javihern98 e2b2946
Added URL parsing support on read_sdmx. Made httpx a mandatory depend…
javihern98 4eca815
Merge branch 'develop' into 156-add-read_sdmx-convenience-method
javihern98 183fe4d
Updated poetry lock.
javihern98 d8fa929
Merge remote-tracking branch 'origin/156-add-read_sdmx-convenience-me…
javihern98 3b48f9e
Updated poetry lock and used correct version of httpx. Updated tests …
javihern98 ed6303a
Added validate flag to get_datasets.
javihern98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
"""IO module for SDMX data.""" | ||
|
||
from pysdmx.io.reader import read_sdmx | ||
|
||
__all__ = ["read_sdmx"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
"""SDMX All formats reader module.""" | ||
|
||
from enum import Enum | ||
from io import BytesIO | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from pysdmx.errors import Invalid | ||
from pysdmx.io.input_processor import process_string_to_read | ||
from pysdmx.model.dataset import Dataset | ||
from pysdmx.model.message import Message | ||
|
||
|
||
class ReadFormat(Enum): | ||
"""Enumeration of supported SDMX read formats.""" | ||
|
||
SDMX_ML_2_1 = "SDMX-ML 2.1" | ||
# SDMX_JSON_2 = "SDMX-JSON 2.0.0" | ||
# FUSION_JSON = "FusionJSON" | ||
SDMX_CSV_1_0 = "SDMX-CSV 1.0" | ||
SDMX_CSV_2_0 = "SDMX-CSV 2.0" | ||
|
||
def check_extension(self, extension: str) -> bool: | ||
"""Check if the extension is valid for the format. | ||
|
||
Args: | ||
extension: The file extension. | ||
|
||
Returns: | ||
bool: True if the extension is valid, False otherwise | ||
""" | ||
if self == ReadFormat.SDMX_ML_2_1 and extension == "xml": | ||
return True | ||
# if self == ReadFormat.SDMX_JSON_2 and extension == "json": | ||
# return True | ||
# if self == ReadFormat.FUSION_JSON and extension == "json": | ||
# return True | ||
if self == ReadFormat.SDMX_CSV_1_0 and extension == "csv": | ||
return True | ||
return bool(self == ReadFormat.SDMX_CSV_2_0 and extension == "csv") | ||
|
||
def __str__(self) -> str: | ||
"""Return the string representation of the format.""" | ||
return self.value | ||
|
||
|
||
def read_sdmx( | ||
infile: Union[str, Path, BytesIO], | ||
format: ReadFormat, | ||
validate: bool = True, | ||
use_dataset_id: bool = False, | ||
) -> Message: | ||
"""Reads any sdmx file or buffer and returns a dictionary. | ||
|
||
Supported metadata formats are: | ||
- SDMX-ML 2.1 | ||
- SDMX JSON 2.0.0 | ||
- FusionJSON | ||
|
||
Supported data formats are: | ||
- SDMX-ML 2.1 | ||
- SDMX-CSV 1.0 | ||
- SDMX-CSV 2.0 | ||
|
||
Args: | ||
infile: Path to file (pathlib.Path), URL, or string. | ||
format: Enumerated format of the SDMX file. | ||
use_dataset_id: Whether to use the dataset ID as | ||
the key in the resulting dictionary (only for SDMX-ML). | ||
validate: Validate the input file (only for SDMX-ML). | ||
|
||
Returns: | ||
A dictionary containing the parsed SDMX data or metadata. | ||
|
||
Raises: | ||
Invalid: If the file is empty or the format is not supported. | ||
""" | ||
input_str, ext = process_string_to_read(infile) | ||
if not format.check_extension(ext): | ||
raise Invalid(f"Invalid format {format} for extension {ext}.") | ||
|
||
elif format == ReadFormat.SDMX_ML_2_1: | ||
# SDMX-ML 2.1 | ||
from pysdmx.io.xml.sdmx21.reader import read_xml | ||
|
||
result = read_xml( | ||
input_str, validate=validate, use_dataset_id=use_dataset_id | ||
) | ||
elif format == ReadFormat.SDMX_CSV_1_0: | ||
# SDMX-CSV 1.0 | ||
from pysdmx.io.csv.sdmx10.reader import read | ||
|
||
result = read(input_str) | ||
else: | ||
# SDMX-CSV 2.0 | ||
from pysdmx.io.csv.sdmx20.reader import read | ||
|
||
result = read(input_str) | ||
|
||
if len(result) == 0: | ||
raise Invalid("Empty SDMX Message") | ||
|
||
# TODO: Add here the Schema download for Datasets, based on structure | ||
|
||
# Returning a Message class | ||
if format in (ReadFormat.SDMX_CSV_1_0, ReadFormat.SDMX_CSV_2_0): | ||
return Message(data=result) | ||
|
||
first_value = next(iter(result.values())) | ||
if isinstance(first_value, Dataset): | ||
return Message(data=result) | ||
return Message(structures=result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@sosna This is a placeholder to include as well methods to read the JSON formats, still I have not found the way to implement them. Any ideas?