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

Updates to latest sphinx-jsonschema #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion sphinx-pydantic/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.1"
__version__ = "0.1.2"
29 changes: 12 additions & 17 deletions sphinx-pydantic/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@
# Depend on JsonSchema
JsonSchema = __import__('sphinx-jsonschema').JsonSchema


class PyDanticSchema(JsonSchema):
"""Wrapper around sphinx-jsonschemas native Directive.
"""
has_content = True

def __init__(self, directive, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
def __init__(self, name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):

# Get content source.
if len(content) > 0:
json_content = self.from_content(arguments[0], content)
json_content = self.from_content(arguments[0])
else:
json_content = self.from_import(arguments[0])

# Pass the new content to sphinx-jsonschema
super(PyDanticSchema, self).__init__(directive='jsonschema', arguments=[], options=options,
content=json_content, lineno=lineno, content_offset=content_offset, block_text=block_text,
super(PyDanticSchema, self).__init__(name='jsonschema', arguments=[], options=options,
content=json_content, lineno=lineno, content_offset=content_offset, block_text=block_text,
state=state, state_machine= state_machine)

def obj_to_content(self, obj):
# Verify type.
if not issubclass(obj, pydantic.BaseModel):
raise TypeError(f"{arguments[0]} is not a subclass of pydantic.BaseModel.")
raise TypeError(f"{obj} is not a subclass of pydantic.BaseModel.")

# Generate a json schema from pydantic. If available use user defined function schema_rst.
if callable(getattr(obj, "schema_rst", None)):
Expand Down Expand Up @@ -55,22 +57,15 @@ def from_import(self, loc):

return self.obj_to_content(obj)

def from_content(self, _):
data = '\n'.join(self.content)
return data, ''

def from_content(self, name, content):
# Content to code.
code = '\n'.join(content)

# Execute code.
exec(code, globals())

# Get object
obj = globals()[name]

return self.obj_to_content(obj)

def setup(app):
# Add pydantic directive to sphinx.
app.add_directive("pydantic", PyDanticSchema)
app.add_config_value('jsonschema_options', {}, 'env')

return {
'version': __version__,
Expand Down