pyxsd.resource_policy

Opt-in resource loading and XML parser hooks, without optional dependencies.

A policy is trusted application code, not a sandbox. Every dependency read uses the same policy. Denials should raise ResourcePolicyError, not OSError (which schema composition treats as an unavailable resource).

Attributes

Exceptions

ResourcePolicyError

A resource/parser policy refused input; never a missing-resource hint.

Classes

ResourceData

Original bytes and their canonical source name, before tree rewriting.

ResourcePolicy

Application-controlled loading and parsing for a schema and its instances.

Functions

validate_purpose(purpose)

Return purpose if it is a known resource purpose, else raise.

new_validation_report(policy)

Use a policy's optional report budget, preserving unlimited core defaults.

source_identifier(source)

Name a path or named stream without substituting the process cwd.

is_root_read_source(source)

Whether source is an explicit path whose first policy read is the root.

policy_compilation(policy, *, root_read)

Enter a policy's optional per-compilation closure scope.

reject_parser_override(resource_policy, xml_policy, ...)

Refuse parser arguments that a trusted ResourcePolicy would swallow.

read_resource(source, policy, *, purpose[, base])

Load paths through the policy; streams still pass through its parser.

record_declarations(root, source, origins)

Record original declaration addresses before composition moves nodes.

Module Contents

pyxsd.resource_policy.ResourcePurpose
pyxsd.resource_policy.validate_purpose(purpose)

Return purpose if it is a known resource purpose, else raise.

Internal callers pass the purpose of a read through several layers; an unknown value is a programming error rather than an authorization decision, so it is rejected before a policy can act on it.

Parameters:

purpose (str)

Return type:

ResourcePurpose

exception pyxsd.resource_policy.ResourcePolicyError

A resource/parser policy refused input; never a missing-resource hint.

class pyxsd.resource_policy.ResourceData

Original bytes and their canonical source name, before tree rewriting.

source_id: str
content: bytes
class pyxsd.resource_policy.ResourcePolicy

Application-controlled loading and parsing for a schema and its instances.

Source IDs are canonical strings: a local path (including a virtual path a policy defines, such as /workspace/...), an absolute remote URI, or <memory> for an anonymous stream. A dependency’s base is its including document, not its directory: a policy resolves a remote base with URI semantics and any other base with its own filesystem semantics. purpose is a ResourcePurpose: "schema" for an explicitly supplied schema, a caller’s namespace mapping, and an ordinary schema dependency; "schema-hint" for a schema an instance document selected through xsi:schemaLocation or xsi:noNamespaceSchemaLocation; and "instance" for an instance document. Parsers must populate the supplied namespace context. Anonymous streams use <memory> and have no implicit filesystem base.

read(source, *, base, purpose)

Authorize and read a named resource, retaining its original bytes.

Parameters:
Return type:

ResourceData

parse(data, context)

Parse admitted bytes and record namespace evidence.

Parameters:
  • data (ResourceData)

  • context (pyxsd.namespaces.NamespaceContext)

Return type:

xml.etree.ElementTree.Element

pyxsd.resource_policy.new_validation_report(policy)

Use a policy’s optional report budget, preserving unlimited core defaults.

Parameters:

policy (ResourcePolicy | None)

Return type:

pyxsd.validation.ValidationReport

pyxsd.resource_policy.source_identifier(source)

Name a path or named stream without substituting the process cwd.

Parameters:

source (object)

Return type:

str | None

pyxsd.resource_policy.is_root_read_source(source)

Whether source is an explicit path whose first policy read is the root.

An anonymous stream has no implicit base, so a policy compilation entered for it must still let its dependencies authorize local-to-remote loading.

Parameters:

source (object)

Return type:

bool

pyxsd.resource_policy.policy_compilation(policy, *, root_read)

Enter a policy’s optional per-compilation closure scope.

RemoteResourcePolicy advertises the hook through the private _pyxsd_compilation_scope capability marker, not through the public compilation() method’s spelling, so a custom policy may define an unrelated compilation attribute without breaking compilation. A policy that wants the same per-compilation state publishes the marker itself. root_read is false for an anonymous stream schema, whose dependencies must still authorize local-to-remote loading.

Parameters:
Return type:

collections.abc.Iterator[None]

pyxsd.resource_policy.reject_parser_override(resource_policy, xml_policy, xml_limits)

Refuse parser arguments that a trusted ResourcePolicy would swallow.

Shared by every schema compiler so a policy/parser combination is rejected identically, before any input is read.

Parameters:
Return type:

None

pyxsd.resource_policy.read_resource(source, policy, *, purpose, base=None)

Load paths through the policy; streams still pass through its parser.

Parameters:
Return type:

ResourceData

pyxsd.resource_policy.record_declarations(root, source, origins)

Record original declaration addresses before composition moves nodes.

Parameters:
Return type:

None