def test_loading_a_json_file_with_tabs_falls_back_to_json_loader(): """YAML is _mostly_ compatible with JSON. However, JSON allows tabs between tokens, whilst YAML does not. """ value = RefDict("tests/schemas/with-tabs.json") assert dict(value) == {"some": {"json": ["with", "tabs"]}}
def read_schema_files(schema_root, item): s_path = path.join(schema_path, schema_root + ".yaml#/" + item) # print(s_path) root_def = RefDict(s_path) exclude_keys = ["format", "examples"] return materialize(root_def, exclude_keys=exclude_keys)
def read_schema_files(schema_name, item, byc): s_f_p = get_schema_file_path(schema_name, byc) # print(schema_name, s_f_p) if not s_f_p is False: s_path = path.join(s_f_p, schema_name + ".yaml#/" + item) root_def = RefDict(s_path) exclude_keys = ["format", "examples"] s = materialize(root_def, exclude_keys=exclude_keys) # print(s) return s return False
def generate(): host = os.environ['UNIFI_HOST'] port = int(os.environ['UNIFI_PORT']) username = os.environ['UNIFI_USER'] password = os.environ['UNIFI_PASS'] site = os.environ['UNIFI_SITE'] jinja = Environment(loader=PackageLoader('generate', 'templates')) template = jinja.get_template('client.py') with open('uniman' + os.path.sep + 'client.py', 'w') as client_py: client_py.write(template.render(endpoints=Endpoints)) payloads = { Endpoints.LOGIN.name: {'username': username, 'password': password} } session = Session() for endpoint in Endpoints: request = endpoint.value.to_request(host, port, site, payload=payloads.get(endpoint.name, {})) response = session.send(session.prepare_request(request), verify=False) builder = SchemaBuilder() builder.add_object(response.json()) builder.add_schema({'title': endpoint.name.title().replace('_', '')}) builder.add_schema({'required': []}) builder.add_schema({'properties': {'data': {'items': {'required': []}}}}) schema = json.loads(builder.to_json()) schema_path = 'uniman' + os.path.sep + 'schema' + os.path.sep + endpoint.name.lower() + '.schema.json' schema_file = open(schema_path, 'w') schema_file.write(json.dumps(schema, indent=4)) schema_file.close() json_schema = json_ref_dict.materialize( RefDict.from_uri(schema_path), context_labeller=statham.titles.title_labeller() ) parsed_schema = statham.schema.parser.parse(json_schema) python_class = serialize_python(*parsed_schema) python_class_file = open('uniman' + os.path.sep + 'model' + os.path.sep + endpoint.name.lower() + '.py', 'w') python_class_file.write(python_class) python_class_file.close()
def read_bycon_configs_by_name(name, byc): """podmd Reading the config from the same wrapper dir: module | |- lib - read_specs.py |- config - __name__.yaml podmd""" o = {} ofp = path.join( byc["pkg_path"], "config", name+".yaml" ) with open( ofp ) as od: o = yaml.load( od , Loader=yaml.FullLoader) byc.update({ name: o }) # TODO Move ... if name == "beacon": byc.update({ "beacon-schema": RefDict(ofp) }) return byc
def schema(request): return RefDict(request.param)
def test_from_uri_immediate_ref_primitive(): ref_primitive = RefDict.from_uri( "base/from-uri.json#/refs/to_primitive") assert ref_primitive == 1
def test_from_uri_immediate_ref_object(): ref_object = RefDict.from_uri("base/from-uri.json#/refs/to_object") assert ref_object == {"foo": "bar"}
def ref_dict(): return RefDict(URI.from_string("base/file1.json#/definitions"))
def test_absolute_references_are_detected(): url = "http://json.schemastore.org/azure-iot-edge-deployment-template-2.0" ref_dict = RefDict(url) assert ref_dict["definitions"]["moduleType"] == RefDict( "http://json.schemastore.org/azure-iot-edge-deployment-2.0#" "/definitions/moduleType")
def test_immediate_references_can_be_bypassed(): value = RefDict.from_uri("tests/schemas/immediate-ref.json#/type") assert value == "integer"
def test_immediately_circular_reference_fails(): with pytest.raises(ReferenceParseError): _ = RefDict("tests/schemas/bad-circular.yaml#/definitions/foo")
def test_from_uri_object(): ref_dict = RefDict.from_uri("base/ref-to-primitive.json#/") assert ref_dict == RefDict("base/ref-to-primitive.json#/")
def test_dict_with_ref_to_primitive(): ref_dict = RefDict("base/ref-to-primitive.json#/") assert ref_dict["top"]["primitive"] == "foo" assert ref_dict["top"]["ref_to_primitive"] == "foo"
def test_json_pointer_on_dict(): ref_dict = RefDict("base/reflist.json#/") pointer = JsonPointer("/definitions/foo/not/0") assert pointer.resolve(ref_dict) == {"type": "object"}
def test_ref_dict_raises_when_target_is_not_object(reference: str): with pytest.raises(TypeError): _ = RefDict(reference)
def test_get_non_ref_ref_key(): assert RefDict("base/nonref.json#/definitions")["$ref"] == { "type": "string" }
def test_direct_reference_retrieval_in_array(): assert RefDict("base/reflist.json#/definitions/foo/not/0") == { "type": "object" }
def test_references_propagate_through_arrays(): ref_dict = RefDict("base/reflist.json#/definitions") assert ref_dict["foo"]["not"][0] == {"type": "object"}
def test_loading_unknown_file_raises_document_parse_error(): with pytest.raises(DocumentParseError): _ = RefDict("tests/schemas/nonexistent.yaml#/definitions")
def test_can_get_json_schema_ref(): assert RefDict("https://json-schema.org/draft-07/schema#/")
def test_from_uri_list(): ref_list = RefDict.from_uri("base/from-uri.json#/array") assert ref_list == [1, 2, 3]
def test_immediate_references_is_detected(): value = RefDict.from_uri("tests/schemas/immediate-ref.json") assert value == {"type": "integer"}
def test_can_materialize_json_schema_ref(): schema = materialize(RefDict("https://json-schema.org/draft-07/schema#/")) assert isinstance(schema, dict)
def test_from_uri_primitive(): ref_primitive = RefDict.from_uri("base/from-uri.json#/primitive") assert ref_primitive == 1
def test_from_uri_immediate_ref_list(): ref_list = RefDict.from_uri("base/from-uri.json#/refs/to_array") assert ref_list == [1, 2, 3]
def test_from_uri_object(): ref_dict = RefDict.from_uri("base/from-uri.json#/object") assert ref_dict == {"foo": "bar"}
def test_external_document_loads_correctly(uri: str): assert RefDict(uri)