def help(self, out=None, initial_heading_level=0): """Return full reStructuredText help for this class""" builder = help.SchemaHelpBuilder(self.schema, self.url, out=out, initial_heading_level=initial_heading_level) builder.build() builder.begin_subheading("Info") builder.begin_list() builder.define("Application", self.application.url + ';help') builder.define("Schema URL", self.schema_url) builder.define("JSON URL", self.url) builder.define("Primary Key", self.primary_key) builder.end_list() builder.end_subheading() if self.exposed_methods: builder.begin_subheading("Methods") for name, method in self.exposed_methods.items(): new_builder = help.SchemaHelpBuilder(method_schema(self, method), initial_heading_level=builder._heading_level) new_builder.build() builder.line(new_builder.rst) builder.end_subheading() if self.document_class.exposed_methods: builder.begin_subheading("Document Instance Methods") for name, method in self.document_class.exposed_methods.items(): new_builder = help.SchemaHelpBuilder(method_schema(None, method), initial_heading_level=builder._heading_level) new_builder.build() builder.line(new_builder.rst) builder.end_subheading() return builder.rst
def help(self, out=None, initial_heading_level=0): """Return full reStructuredText help for this class""" builder = help.SchemaHelpBuilder( self.schema, self.url, out=out, initial_heading_level=initial_heading_level) builder.begin_subheading(self.name) builder.begin_list() builder.define("Collection", self.collection.url + ';help') builder.define("Schema URL", self.schema_url) builder.define("JSON URL", self.url) builder.end_list() builder.end_subheading() builder.build() if self.exposed_methods: builder.begin_subheading("Methods") for name, method in self.exposed_methods.items(): new_builder = help.SchemaHelpBuilder( method_schema(self, method), initial_heading_level=builder._heading_level) new_builder.build() builder.line(new_builder.rst) return builder.rst
def schema(self): if self.is_application(): return self.get_application().schema elif self.is_collection(): return self.get_collection().schema elif self.is_document(): return self.get_document().collection.schema elif self.is_subdocument(): return self.get_document().collection.schema elif self.is_application_method_call(): return method_schema(self.get_application(), self.get_application_method()) elif self.is_collection_method_call(): return method_schema(self.get_collection(), self.get_collection_method()) elif self.is_document_method_call(): return method_schema(self.get_document(), self.get_document_method()) else: raise EndpointError("Endpoint {0} cannot be dereferenced. This is likely a bug.".format(self.url))
def __call__(self, reference, result, **kwargs): if 'indent' in kwargs: kwargs['indent'] = int(kwargs['indent']) if 'method' in reference.kind: # ordered_schema = natural_order(method_schema(*reference.value)) return 'application/json', json.dumps(method_schema(*reference.value), **kwargs) else: # ordered_schema = natural_order(reference.value.schema) return 'application/json', json.dumps(reference.value.schema, **kwargs)
def __init__(cls, name, bases, nmspc): super(CollectionMetaclass, cls).__init__(name, bases, nmspc) cls.exposed_methods = {} cls.title = cls.title or cls.__name__ cls.name = utils.convert_camelcase(cls.__name__) for base in bases: if hasattr(base, 'exposed_methods'): cls.exposed_methods.update(base.exposed_methods) for name, method in (n for n in nmspc.items() if hasattr(n[1], 'exposed')): cls.exposed_methods[name] = method if cls.document_class and (cls.document_class is not Document): cls.abstract = False cls.schema = deepcopy(cls.document_class.schema) if cls.primary_key == 'id': cls.schema['properties']['id'] = {"type": "string", "description": "The primary key.", "title": "ID"} cls.document_class.schema['properties']['id'] = {"type": "string", "description": "The primary key.", "title": "ID"} cls.schema["methods"] = {} for m in cls.exposed_methods.values(): cls.schema['methods'][m.slug] = method_schema(False, m) cls.schema['documentMethods'] = {} for m in cls.document_class.exposed_methods.values(): cls.schema["documentMethods"][m.slug] = method_schema(None, m) cls.schema['primary_key'] = 'id' if not cls.primary_key else cls.primary_key if not 'description' in cls.schema: cls.schema['description'] = cls.document_class.__doc__ or 'No Description Provided.' if not 'title' in cls.schema: cls.schema['title'] = split_camelcase(cls.document_class.__name__) _validator.check_schema(cls.schema) cls.slug = utils.camelcase_slugify(cls.__name__) else: cls.abstract = True
def __call__(self, reference, result, **kwargs): if 'indent' in kwargs: kwargs['indent'] = int(kwargs['indent']) if 'method' in reference.kind: # ordered_schema = natural_order(method_schema(*reference.value)) return 'application/json', json.dumps( method_schema(*reference.value), **kwargs) else: # ordered_schema = natural_order(reference.value.schema) return 'application/json', json.dumps(reference.value.schema, **kwargs)
def schema(self): if self.is_application(): return self.get_application().schema elif self.is_collection(): return self.get_collection().schema elif self.is_document(): return self.get_document().collection.schema elif self.is_subdocument(): return self.get_document().collection.schema elif self.is_application_method_call(): return method_schema(self.get_application(), self.get_application_method()) elif self.is_collection_method_call(): return method_schema(self.get_collection(), self.get_collection_method()) elif self.is_document_method_call(): return method_schema(self.get_document(), self.get_document_method()) else: raise EndpointError( "Endpoint {0} cannot be dereferenced. This is likely a bug.". format(self.url))
def schema(self): ret = { "id": self.url + ";schema", "title": self.title, "type": "object", "description": self.__doc__ or "*No description provided.*", "definitions": self.definitions, "collections": {name: coll.url for name, coll in self._collections.items() if not coll.private}, "methods": {m.slug: method_schema(None, m) for m in self.exposed_methods.values()} } ret = mapjson(lambda x: x(context=self.suite) if callable(x) else x, ret) return ret
def validate(self): target = self.reference.value if self.reference.kind.endswith('method'): schema = method_schema(*target)['definitions']['method_request'] elif self.reference.kind in ['collection', 'application']: schema = target.schema else: schema = target.collection.schema if self.request_method != 'PATCH': for object in self.objects: if not isinstance(object, str): jsonschema.validate(object, schema) else: pass # TODO validate individual keys
def validate(self): target = self.reference.value if self.reference.kind.endswith("method"): schema = method_schema(*target)["definitions"]["method_request"] elif self.reference.kind in ["collection", "application"]: schema = target.schema else: schema = target.collection.schema if self.request_method != "PATCH": for object in self.objects: if not isinstance(object, str): jsonschema.validate(object, schema) else: pass # TODO validate individual keys
def help(self, out=None, initial_heading_level=0): """Return full reStructuredText help for this class""" builder = help.SchemaHelpBuilder(self.schema, self.url, out=out, initial_heading_level=initial_heading_level) builder.begin_subheading(self.name) builder.begin_list() builder.define("Collection", self.collection.url + ';help') builder.define("Schema URL", self.schema_url) builder.define("JSON URL", self.url) builder.end_list() builder.end_subheading() builder.build() if self.exposed_methods: builder.begin_subheading("Methods") for name, method in self.exposed_methods.items(): new_builder = help.SchemaHelpBuilder(method_schema(self, method), initial_heading_level=builder._heading_level) new_builder.build() builder.line(new_builder.rst) return builder.rst
def help(self, out=None, initial_heading_level=0): """Return full reStructuredText help for this class. Args: out (io): An output, usually io.StringIO initial_heading_level (int): 0-5, default 0. The heading level to start at, in case this is being included as part of a broader help scheme. Returns: (str): reStructuredText help. """ builder = help.SchemaHelpBuilder(self.schema, self.url, out=out, initial_heading_level=initial_heading_level) builder.begin_subheading(self.name) builder.begin_list() builder.define("Suite", self.suite.url + '/help') builder.define("Schema URL", self.schema_url) builder.define("Anonymous Reads", "yes" if self.anonymous_reads else "no") builder.end_list() builder.build() builder.line() if self.exposed_methods: builder.begin_subheading("Methods") for name, method in sorted(self.exposed_methods.items(), key=lambda x: x[0]): new_builder = help.SchemaHelpBuilder(method_schema(self, method), initial_heading_level=builder._heading_level) new_builder.build() builder.line(new_builder.rst) builder.end_subheading() builder.begin_subheading("Collections") builder.begin_list() for name, coll in sorted(self._collections.items(), key=lambda x: x[0]): builder.define(name, coll.url + ';help') builder.end_list() builder.end_subheading() return builder.rst