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

Create canvas from IIIF Image #89

Merged
merged 11 commits into from
Nov 11, 2022
Merged

Create canvas from IIIF Image #89

merged 11 commits into from
Nov 11, 2022

Conversation

glenrobson
Copy link
Contributor

Closes #10

@glenrobson
Copy link
Contributor Author

So I'm hitting a brick wall here with a v2 Image API. I can't find a way to get the value of @id or @type:

>>> from iiif_prezi3.skeleton import (Annotation, AnnotationPage, Canvas, Manifest,ResourceItem, ServiceItem, ServiceItem1)
>>> service = ServiceItem1(_id="http://example.com/iiif/1", profile="http://iiif.io/api/image/2/level1.json", _type="ImageService2")
>>> service._type
FieldInfo(default=Ellipsis, alias='@type', alias_priority=2, extra={})

>>> dir(service._type)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__field_constraints__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pretty__', '__reduce__', '__reduce_ex__', '__repr__', '__repr_args__', '__repr_name__', '__repr_str__', '__setattr__', '__sizeof__', '__slotnames__', '__slots__', '__str__', '__subclasshook__', '_validate', 'alias', 'alias_priority', 'allow_mutation', 'const', 'default', 'default_factory', 'description', 'extra', 'ge', 'get_constraints', 'gt', 'le', 'lt', 'max_items', 'max_length', 'min_items', 'min_length', 'multiple_of', 'regex', 'title', 'update_from_config']

>>> str(service._type)
"default=Ellipsis alias='@type' alias_priority=2 extra={}"

It also seems to have lost the id and type:

>>> service.jsonld()
'{"@context": "http://iiif.io/api/presentation/3/context.json", "profile": "http://iiif.io/api/image/2/level1.json"}'

The ServiceItem1 is defined in skeleton as:

class ServiceItem1(Base):
    _id: Id = Field(..., alias='@id')
    _type: str = Field(..., alias='@type')
    profile: Optional[str] = None
    service: Optional[Service] = None

@digitaldogsbody
Copy link
Member

I think it's probably to do with this: https://pydantic-docs.helpmanual.io/usage/models/#automatically-excluded-attributes

Class variables which begin with an underscore and attributes annotated with typing.ClassVar will be automatically excluded from the model.

@digitaldogsbody
Copy link
Member

Found some sort of solution by changing _id and _type to just id and type and then making sure that the by_alias=True argument is passed through to the json() and jsonld() functions.

@glenrobson
Copy link
Contributor Author

It looks like pydantic 1.10 has changed the value of:

copy_on_model_validation

from a boolean to a string of options:

copy_on_model_validation
    string literal to control how models instances are processed during validation, with the following means (see [#4093](https://github.com/pydantic/pydantic/pull/4093) for a full discussion of the changes to this field):

    'none' - models are not copied on validation, they're simply kept "untouched"
    'shallow' - models are shallow copied, this is the default
    'deep' - models are deep copied

pydantic/pydantic#4093

@digitaldogsbody
Copy link
Member

It looks like pydantic 1.10 has changed the value of:

copy_on_model_validation

from a boolean to a string of options:

It was a breaking change (temporarily) but was then reverted (see notes on https://github.com/pydantic/pydantic/releases/tag/v1.9.2) and made so that both the existing False (which we are using) and the new arguments worked.

This was the impetus behind my version pinning of the requirements, as I noticed the deprecation warnings coming from Tox, but when I switched locally all the code broke, because my version of Pydantic was still 1.9.1.

We could change the code to use "none", which would preserve the behavior, as long as we increase the minimum required Pydantic version to >=1.9.2 in setup.py. Otherwise we can wait until it becomes a breaking change in Pydantic 2.0.0 and then bump by a whole version and change our code.

@glenrobson
Copy link
Contributor Author

Reported issue with @id and @type here: koxudaxi/datamodel-code-generator#917

@glenrobson
Copy link
Contributor Author

I think this could be the issue:

https://github.com/koxudaxi/datamodel-code-generator/blob/6926c4fc5f57c0670a62c9c083a24a29117f0099/datamodel_code_generator/reference.py#L168

 name = re.sub(r'[¹²³⁴⁵⁶⁷⁸⁹]|\W', '_', name)

Where:

\W

    Matches any character which is not a word character. This is the opposite of \w. If the 
[ASCII] flag is used this becomes the equivalent of [^a-zA-Z0-9_]. If the [LOCALE] flag 
is used, matches characters which are neither alphanumeric in the current locale nor 
the underscore.

@digitaldogsbody
Copy link
Member

I'd think you're probably right. Might be an easy patch here: https://github.com/koxudaxi/datamodel-code-generator/blob/6926c4fc5f57c0670a62c9c083a24a29117f0099/datamodel_code_generator/reference.py#L157-L158 changing the conditional from == '#' to in ['#', '@']

For now I am happy to make the manual adjustment to skeleton.py and we can revisit if datamodel-codegen recognises/fixes the issue in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Canvas from Image
3 participants