示例#1
0
def make_json(filename):
    """Function that parses the input schema file and generates JSON.
    """
    parser = LDAPSchemaParser(filename)
    definitions = parser.parse()
    schema_dict = {}
    objectclasses = []
    attributetypes = []
    for obj in definitions['objectClasses']:
        obcl = {}
        props = ['oid', 'names', 'desc', 'must', 'may', 'sup', 'x_origin']
        for prop in props:
            if hasattr(obj, prop):
                if getattr(obj, prop):
                    obcl[prop] = getattr(obj, prop)
        # obcl['obsolete'] = obj.obsolete
        if obj.kind == 0:
            obcl['kind'] = 'STRUCTURAL'
        elif obj.kind == 1:
            obcl['kind'] = 'ABSTRACT'
        elif obj.kind == 2:
            obcl['kind'] = 'AUXILIARY'
        objectclasses.append(obcl)

    for att in definitions['attributeTypes']:
        attype = {}
        props = [
            'oid', 'names', 'desc', 'equality', 'substr', 'ordering', 'syntax',
            'x_origin'
        ]
        for prop in props:
            if hasattr(att, prop):
                if getattr(att, prop):
                    attype[prop] = getattr(att, prop)
        # attype['no_user_mod'] = att.no_user_mod
        # attype['single_value'] = att.single_value
        # attype['obsolete'] = att.obsolete
        attributetypes.append(attype)

    schema_dict['objectClasses'] = objectclasses
    schema_dict['attributeTypes'] = attributetypes
    schema_dict['oidMacros'] = definitions['oidMacros']
    print(json.dumps(schema_dict, indent=4, sort_keys=True))
def make_json(filename):
    """Function that parses the input schema file and generates JSON.
    """
    parser = LDAPSchemaParser(filename)
    definitions = parser.parse()
    schema_dict = {}
    objectclasses = []
    attributetypes = []
    for obj in definitions['objectClasses']:
        obcl = {}
        props = ['oid', 'names', 'desc', 'must', 'may', 'sup', 'x_origin']
        for prop in props:
            if hasattr(obj, prop):
                if getattr(obj, prop):
                    obcl[prop] = getattr(obj, prop)
        # obcl['obsolete'] = obj.obsolete
        if obj.kind == 0:
            obcl['kind'] = 'STRUCTURAL'
        elif obj.kind == 1:
            obcl['kind'] = 'ABSTRACT'
        elif obj.kind == 2:
            obcl['kind'] = 'AUXILIARY'
        objectclasses.append(obcl)

    for att in definitions['attributeTypes']:
        attype = {}
        props = ['oid', 'names', 'desc', 'equality', 'substr', 'ordering',
                 'syntax', 'x_origin']
        for prop in props:
            if hasattr(att, prop):
                if getattr(att, prop):
                    attype[prop] = getattr(att, prop)
        # attype['no_user_mod'] = att.no_user_mod
        # attype['single_value'] = att.single_value
        # attype['obsolete'] = att.obsolete
        attributetypes.append(attype)

    schema_dict['objectClasses'] = objectclasses
    schema_dict['attributeTypes'] = attributetypes
    schema_dict['oidMacros'] = definitions['oidMacros']
    print json.dumps(schema_dict, indent=4, sort_keys=True)