open-contracting / ocdskit

A suite of command-line tools for working with OCDS data
https://ocdskit.readthedocs.io
BSD 3-Clause "New" or "Revised" License
17 stars 7 forks source link

Function suggestion - get schema files from actual data #181

Closed odscjames closed 2 years ago

odscjames commented 2 years ago

I have a piece of data - a release package.

I need the schema that that data follows (ie correct OCDS version and with extensions applied), both the package and the release bit.

Is there a function in OCDSKit, or elsewhere, that does this?

(I couldn't see it - sorry if missed! Also COVE will do this work, but I haven't yet looked to see if it's in a form I can easily dig out.)

Thanks,

jpmckinney commented 2 years ago

You can do it with https://ocdsextensionregistry.readthedocs.io/en/latest/api/profile_builder.html

You'd just need to pass it the package's extensions array.

As for the OCDS version, I'm not sure if you want the latest patch, etc. In any case, for most use cases, it doesn't make much sense to use 1.0.x, since 1.1.x is backwards-compatible and generally more correct. If you do need the 1.0.x version, then it's easy: there are no extensions in 1.0, and there will be no further versions in the 1.0.x series, so just use https://standard.open-contracting.org/schema/1__0__3/ if the package has no version key.

jpmckinney commented 2 years ago

OCDS Kit does something similar here:

https://github.com/open-contracting/ocdskit/blob/dd10411c7f7bc0f81e808043d17d6bf800f3bb8e/ocdskit/combine.py#L162-L171

jpmckinney commented 2 years ago

1.0.3 adds a get_ocds_patch_tag function, to simplify the above a bit:

https://github.com/open-contracting/ocdskit/blob/81238891944e9bd53d692b5841d51da48788cb6e/ocdskit/combine.py#L162-L168

In your case it'd probably look like:

from ocdsextensionregistry import ProfileBuilder
from ocdskit.util import get_ocds_patch_tag

tag = get_ocds_patch_tag(package.get("version", "1.0"))
builder = ProfileBuilder(tag, package.get("extensions", []))) 
schema = builder.patched_release_schema() 
odscjames commented 2 years ago

Thank you @jpmckinney