Repository navigation
Centralized Importer Class #237
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
Merged
krzywon
merged 10 commits into
refactor_24_231_io_reorg
from
refactor_24_central_io_classes
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
017d472
Update type hints for importers to allow pathlib.Path objects
krzywon 067a1cb
Remove TODO from ascii
krzywon cf656a3
Guess the file importer based on the file mime type, defaulting to th…
krzywon 2647396
Ensure zip_longest parameter fillvalue has a logical value
krzywon f9cdd8f
Create a basic unit test for the base importer class that imports dat…
krzywon 1bd190d
Fixes to importer class to ensure sasmeasurements and errors are sepa…
krzywon 32eed9b
Create a basic unit test for the generic importer module - failing on…
krzywon 1d7042d
Add a deprecation warning to Importer class
krzywon 1b98a6c
Add file errors to the errors list on import
krzywon 3bf1801
Merge branch 'refactor_24_231_io_reorg' into refactor_24_central_io_c…
krzywon 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 hidden or 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,30 +1,57 @@ | ||
| import mimetypes | ||
| from itertools import zip_longest | ||
| from pathlib import Path | ||
|
|
||
| from sasdata.data import SasMeasurement | ||
| from sasdata.data_io.importers import * | ||
|
|
||
| Path_Type = str | Path | ||
| Value_Type = str | int | float | list[str] | list[int] | list[float] | ||
|
|
||
|
|
||
| class Importer: | ||
| def __init__(self): | ||
| pass | ||
|
|
||
| def import_data(self, files: Path_Type) -> (list[SasMeasurement], list[str]): | ||
| def import_data(self, files: list[Path_Type], configs: list[dict[str, Value_Type]] | None = None) \ | ||
| -> (list[SasMeasurement], list[str]): | ||
| output = [] | ||
| errors = [] | ||
| for file in files: | ||
| if configs is None: | ||
| configs = [] | ||
| fill_value = configs[-1] if len(configs) > 0 else None | ||
| for file, config in zip_longest(files, configs, fillvalue=fill_value): | ||
| file = Path(file) | ||
| if measurements := self._import_from_url(file): | ||
| output.append(measurements) | ||
| else: | ||
| errors.append(f'File "{file}" not found.') | ||
| measurements, errors = self._import_from_url(file, config) | ||
| output.extend(measurements) | ||
| errors.extend(errors) | ||
| return output, errors | ||
|
|
||
| def _import_from_url(self, url: Path_Type) -> list[SasMeasurement]: | ||
| def _import_from_url(self, url: Path_Type, config: dict[str, Value_Type] | None = None) -> (list[SasMeasurement], list[str]): | ||
| file = Path(url) | ||
| if not file.exists(): | ||
| return [] | ||
| with open(url) as f: | ||
| pass | ||
| return [] | ||
| return [], [f'File does not exist: "{file}"'] | ||
| # Deprecation Warning: | ||
| # mimetypes.guess_type() is soft deprecated in favor of mimetypes.guess_file_type() as of python 3.13 | ||
| # Once we move forward from python 3.12, this line should change | ||
| mime_type, encoding = mimetypes.guess_type(url) | ||
| imported = [] | ||
| errors = [] | ||
| # TODO: Need a viable config system that can pass a dictionary of value locations to each importer | ||
| try: | ||
| match mime_type: | ||
| case 'application/xml' | 'text/xml': | ||
| file_dict = import_xml.load_data(file) | ||
| imported.extend(list(file_dict.values())) | ||
| case 'application/vnd.hdfgroup.hdf5': | ||
| file_list = import_hdf5.load_data(file) | ||
| imported.extend(file_list) | ||
| case _: | ||
| file_list = import_ascii.load_data_default_params(file) | ||
| imported.extend(file_list) | ||
| except Exception as e: | ||
| errors.append(f'Error accessing "{file}": {e}') | ||
| for file in imported: | ||
| if hasattr(file, "errors"): | ||
| errors.extend(file.errors) | ||
| return imported, errors | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking longer term, like when #225 is fixed, there may be errors/warnings during file read that could be bundled into this list. |
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
I'll add a comment here so we don't overlook it during the transition to later python versions.