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

Raise error when loading schema with bad prefix #789

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -64,6 +67,9 @@ jobs:
run: |
HED_GITHUB_TOKEN=${{ secrets.HED_GITHUB_TOKEN }} coverage run -m unittest

- name: Run spec_test coverage
run: HED_GITHUB_TOKEN=${{ secrets.HED_GITHUB_TOKEN }} coverage run --append -m unittest spec_tests/test_errors.py

- name: Archive code coverage results
if: ${{matrix.python-version == '3.9'}}
uses: actions/upload-artifact@v3
Expand Down
2 changes: 2 additions & 0 deletions hed/errors/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class HedExceptions:
ROOTED_TAG_HAS_PARENT = "SCHEMA_LIBRARY_INVALID"
ROOTED_TAG_DOES_NOT_EXIST = "SCHEMA_LIBRARY_INVALID"
IN_LIBRARY_IN_UNMERGED = "SCHEMA_LIBRARY_INVALID"
INVALID_LIBRARY_PREFIX = "SCHEMA_LIBRARY_INVALID"


SCHEMA_VERSION_INVALID = 'SCHEMA_VERSION_INVALID'
SCHEMA_SECTION_MISSING = 'SCHEMA_SECTION_MISSING'
Expand Down
8 changes: 8 additions & 0 deletions hed/schema/hed_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from hed.errors import ErrorHandler
from hed.errors.error_types import ValidationErrors
from hed.schema.hed_schema_base import HedSchemaBase
from hed.errors.exceptions import HedFileError, HedExceptions


class HedSchema(HedSchemaBase):
Expand Down Expand Up @@ -265,10 +266,17 @@ def set_schema_prefix(self, schema_namespace):
Parameters:
schema_namespace (str): Should be empty, or end with a colon.(Colon will be automated added if missing).

:raises HedFileError:
- The prefix is invalid
"""
if schema_namespace and schema_namespace[-1] != ":":
schema_namespace += ":"

if schema_namespace and not schema_namespace[:-1].isalpha():
raise HedFileError(HedExceptions.INVALID_LIBRARY_PREFIX,
"Schema namespace must contain only alpha characters",
self.filename)

self._namespace = schema_namespace

def __eq__(self, other):
Expand Down
8 changes: 6 additions & 2 deletions hed/schema/hed_schema_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def _load_schema_version(xml_version=None, xml_folder=None):
""" Return specified version or latest if not specified.

Parameters:
xml_folder (str): Path to a folder containing schema.
xml_version (str): HED version format string. Expected format: '[schema_namespace:][library_name_]X.Y.Z'.
xml_folder (str): Path to a folder containing schema.

Returns:
HedSchema or HedSchemaGroup: The requested HedSchema object.
Expand All @@ -121,6 +121,7 @@ def _load_schema_version(xml_version=None, xml_folder=None):
- The xml_version is not valid.
- The specified version cannot be found or loaded
- Other fatal errors loading the schema (These are unlikely if you are not editing them locally)
- The prefix is invalid
"""
schema_namespace = ""
library_name = None
Expand Down Expand Up @@ -164,14 +165,17 @@ def load_schema_version(xml_version=None, xml_folder=None):
An empty string returns the latest version
A json str format is also supported,
based on the output of HedSchema.get_formatted_version
Basic format: '[schema_namespace:][library_name_]X.Y.Z'.
xml_folder (str): Path to a folder containing schema.

Returns:
HedSchema or HedSchemaGroup: The schema or schema group extracted.

:raises HedFileError:
- The xml_version is not valid.
- A fatal error was encountered in parsing
- The specified version cannot be found or loaded
- Other fatal errors loading the schema (These are unlikely if you are not editing them locally)
- The prefix is invalid
"""
# Check if we start and end with a square bracket, or double quote. This might be valid json
if xml_version and isinstance(xml_version, str) and \
Expand Down
Loading