{ "info": { "author": "Kevin L. Mitchell", "author_email": "klmitch@mit.edu", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "================\nSchema Resources\n================\n\n.. image:: https://travis-ci.org/klmitch/schema-resource.svg?branch=master\n :target: https://travis-ci.org/klmitch/schema-resource\n\nThe ``schema-resource`` package is a simple library for loading\n``jsonschema`` schemas using ``pkg_resources``. This means that\nPython packages utilizing the ``schema-resource`` package can bundle\nschemas for validating API or user configuration as separate files\nwith the package source. Further, those schemas may then reference\nother schema files within the package.\n\nSimple Usage\n============\n\nThe simplest way to use ``schema-resource`` begins by understanding\nthe resource URI. A resource URI is a URI with the scheme \"res\". The\n\"network location\"--the part that appears after the \"//\" in a URI--is\nthe package name, as understood by ``pkg_resources``. The path is\nthen interpreted relative to the root directory of the package. For\ninstance, if the package \"spam\" has a schema named \"schema.yaml\", the\nresource URI would be \"res://spam/schema.yaml\". This schema can then\nbe loaded using ``schema_res.load_schema()``, which takes the resource\nURI as its first argument; the result will be an object conforming to\nthe ``jsonschema.IValidator`` interface documented in the\n``jsonschema`` documentation.\n\nThis schema could, of course, be loaded by using a combination of\n``jsonschema`` and ``pkg_resources`` directly; however,\n``schema-resource`` creates the schema with a special\n``jsonschema.RefResolver`` that understands these resource URIs; this\nenhancement allows one schema to refer to another, or part of another,\nresource schema directly.\n\nClass Attributes\n================\n\nOften, a class needs to use a particular schema in order to validate\ninput, often from an API or a configuration file. This can be\nsimplified through the use of ``schema_res.SchemaDescriptor``. This\nclass implements the Python \"descriptor\" protocol, meaning that, when\nassigned to a class attribute, references to the value of the\nattribute will cause a method of ``schema_res.SchemaDescriptor`` to be\ncalled. That method implements an on-demand loading of a schema\nresource, constructing the resource URI if needed from the class's\n``__module__`` attribute. For instance, assume that the ``Spam``\nclass below needs to validate data fed to a class method::\n\n class Spam(object):\n schema = schema_res.SchemaDescriptor(\"spam.yaml\")\n\n @classmethod\n def from_data(cls, data):\n cls.schema.validate(data)\n\n return cls(**data)\n\n ...\n\nThis class first validates the data against the schema loaded from the\n\"spam.yaml\" file bundled with the package sources, loading the schema\nthe first time the method is called. (The\n``jsonschema.IValidator.validate()`` method raises a\n``jsonschema.ValidationError`` exception if the ``data`` doesn't match\nthe requirements of the schema.)\n\nValidating Schemas\n==================\n\nIt is a good idea for the test suite for a package to verify that the\nschemas it bundles are valid. This could be done by simply using the\n``schema_res.load_schema()`` function, calling it for each resource\nURI and passing ``validate=True``, within the package's test suite.\nHowever, there's also a simple helper: ``schema_res.validate()`` takes\none or more resource URIs and calls ``schema_res.load_schema()`` on\neach, passing ``validate=True``. This means that this entire test can\nbe written as a single function call, like so::\n\n class TestSchemas(object):\n def test_valid(self):\n schema_res.validate(\n \"res://spam/schema1.yaml\",\n \"res://spam/schema2.yaml\",\n \"res://spam/schema3.yaml\",\n )\n\nSchema Format\n=============\n\nIn all the examples so far, the schema filenames have had the \".yaml\"\nextension. There is no specific need to use this extension, nor even\nfor the files to be in YAML format: JSON is a subset of YAML, so the\nschema files can be written in regular JSON. However, by using a YAML\nparser to load the schema files, they may be expressed in YAML format,\nwhich this programmer finds easier to write and to read than strict\nJSON syntax.\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/klmitch/schema-resource", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "schema-resource", "package_url": "https://pypi.org/project/schema-resource/", "platform": "", "project_url": "https://pypi.org/project/schema-resource/", "project_urls": { "Homepage": "https://github.com/klmitch/schema-resource" }, "release_url": "https://pypi.org/project/schema-resource/0.0.1/", "requires_dist": null, "requires_python": "", "summary": "Schema Resources", "version": "0.0.1" }, "last_serial": 5692232, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "730c4e4d5e6e0e2fd956483a06e7b7b1", "sha256": "dce7fc2f2819d008ea7152e18126d247eee8c984a2f18b391d234ee35b48e9e0" }, "downloads": -1, "filename": "schema-resource-0.0.1.tar.gz", "has_sig": false, "md5_digest": "730c4e4d5e6e0e2fd956483a06e7b7b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10265, "upload_time": "2019-08-17T16:47:09", "url": "https://files.pythonhosted.org/packages/ff/6d/9ad3179883a145004ec8a7a95d7a651aed28d1484d8360b4d87914d32b24/schema-resource-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "730c4e4d5e6e0e2fd956483a06e7b7b1", "sha256": "dce7fc2f2819d008ea7152e18126d247eee8c984a2f18b391d234ee35b48e9e0" }, "downloads": -1, "filename": "schema-resource-0.0.1.tar.gz", "has_sig": false, "md5_digest": "730c4e4d5e6e0e2fd956483a06e7b7b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10265, "upload_time": "2019-08-17T16:47:09", "url": "https://files.pythonhosted.org/packages/ff/6d/9ad3179883a145004ec8a7a95d7a651aed28d1484d8360b4d87914d32b24/schema-resource-0.0.1.tar.gz" } ] }