altair-viz / schemapi Goto Github PK
View Code? Open in Web Editor NEWAuto-generate Python APIs from JSON schema specifications
License: BSD 3-Clause "New" or "Revised" License
Auto-generate Python APIs from JSON schema specifications
License: BSD 3-Clause "New" or "Revised" License
Please create a release 0.4 so the latest released code matches the documentation in readme.md.
Eg when I pip install schemapi now, the documentation does not reflect what gets installed.
There's one issue here that I'm not sure how to address: as this package is currently set up, all JSONschema property names must be valid Python variable names. Currently schemapi
contains a hack that removes offending characters when constructing the classes: https://github.com/altair-viz/schemapi/blob/master/schemapi/utils/core.py#L38
This is clearly not ideal, as the resulting dictionaries will not match the schema.
One way to make this work would be to create the traitlets classes in a non-standard way in order to handle arbitrary trait names: e.g.
Foo = T.MetaHasTraits('Foo', (T.HasTraits,), {'$schema': T.Unicode()})
print(Foo.class_traits())
{'$schema': <traitlets.traitlets.Unicode at 0x1069dd668>}
I don't think this is ideal.
Another way forward would be to not allow any non-standard names, with the exception of things like $schema
, $ref
, $id
, and others that have special meaning within the JSON schema specification. These would have to somehow be mapped to and from internal representations in JSONSchema
when it's converted to and from a dictionary.
This came up in Altair here: vega/altair#370
The code below reproduces the problem:
def gen_debugger_protocol():
import os.path
json_file = os.path.join(os.path.dirname(__file__), 'debugProtocol.json')
if not os.path.exists(json_file):
import requests
req = requests.get('https://raw.githubusercontent.com/Microsoft/vscode-debugadapter-node/master/debugProtocol.json')
assert req.status_code == 200
with open(json_file, 'wb') as stream:
stream.write(req.content)
import json
import schemapi
with open(json_file, 'rb') as json_contents:
contents = json.loads(json_contents.read())
api = schemapi.JSONSchema(contents)
api.write_module('debug_protocol_schema')
if __name__ == '__main__':
gen_debugger_protocol()
The error I have is:
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\jsonschema.py", line 466, in write_module
return utils.save_module(spec=self.source_tree(),
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\jsonschema.py", line 413, in source_tree
schema_content = template.render(cls=self, classes=classes)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 12, in top-level template code
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\jsonschema.py", line 344, in object_code
return trait_extractor.object_code()
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\trait_extractors.py", line 191, in object_code
for ref in self.schema['allOf']]
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\trait_extractors.py", line 191, in <listcomp>
for ref in self.schema['allOf']]
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\jsonschema.py", line 333, in trait_code
return trait_extractor.trait_code(**kwargs)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\trait_extractors.py", line 482, in trait_code
in self.schema.wrapped_properties().items()}
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\trait_extractors.py", line 481, in <dictcomp>
trait_codes = {name: Variable(prop.trait_code) for (name, prop)
File "C:\tools\Miniconda\envs\tests_py36\lib\site-packages\schemapi\jsonschema.py", line 336, in trait_code
"keys {0}".format(tuple(self.schema.keys())))
ValueError: No recognized trait code for schema with keys ('type', 'description')
Only have JSONSchema and JSONSchemaPlugin available. Therefore, after putting "import schemapi" and then api = schemapi.SchemaModuleGenerator(schemaFilename, root_name='MachineData'), when executing it gives me the error module 'schemapi' has no attribute 'SchemaModuleGenerator'.
Never had an issue like this from python libraries I have added in the past and could use some guidance on how to resolve this. Was looking forward to investigating using this api to help generate json payloads from data that I have acquired and mapped to python dictionaries using a schema.
Please advise.
Thanks,
Bob Sloma
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.