Exemplo n.º 1
0
def xml_upload(input_file: str,
               server: str,
               user: str,
               password: str,
               imgdir: str,
               sipi: str,
               verbose: bool) -> bool:
    current_dir = os.path.dirname(os.path.realpath(__file__))

    xmlschema_doc = etree.parse(os.path.join(current_dir, 'knora-data-schema.xsd'))
    xmlschema = etree.XMLSchema(xmlschema_doc)
    doc = etree.parse(input_file)
    xmlschema.assertValid(doc)

    del xmlschema
    del doc
    del xmlschema_doc

    print("The input data file is syntactically correct and passed validation!")

    #
    # Connect to the DaSCH Service Platform API
    #
    con = Connection(server)
    con.login(user, password)

    proj_context = ProjectContext(con=con)

    #
    # read the XML file containing the data, including project shortcode
    #
    context: etree.iterparse = etree.iterparse(input_file, events=("start", "end"))
    resources: List[KnoraResource] = []
    permissions: Dict[str, XmlPermission] = {}
    shortcode: Union[str, None] = None
    default_ontology = None
    while True:
        event, node = next(context)
        if event == 'start':
            if node.tag == 'knora':
                default_ontology = node.attrib['default-ontology']
                shortcode = node.attrib['shortcode']
                proj_context.shortcode = shortcode
            elif event == 'start' and node.tag == 'resource':
                resources.append(KnoraResource(context, node, default_ontology))
            elif event == 'start' and node.tag == 'permissions':
                permission = XmlPermission(context, node, proj_context)
                permissions[permission.id] = permission
        elif event == 'end':
            if node.tag == 'knora':
                break

    #
    # sort the resources so that resources which do not link to others come first
    #
    resources = do_sortorder(resources)

    sipi = Sipi(sipi, con.get_token())

    factory = ResourceInstanceFactory(con, shortcode)

    permissions_lookup: Dict[str, Permissions] = {}
    for key, perm in permissions.items():
        permissions_lookup[key] = perm.get_permission_instance()

    resclassnames = factory.get_resclass_names()
    resclasses: Dict[str, type] = {}
    for resclassname in resclassnames:
        resclasses[resclassname] = factory.get_resclass(resclassname)
    resiri_lookup: StrDict = {}

    for resource in resources:
        if resource.image:
            img = sipi.upload_image(os.path.join(imgdir, resource.image))
            stillimage = img['uploadedFiles'][0]['internalFilename']
        else:
            stillimage = None
        instance = resclasses[resource.restype](con=con,
                                                label=resource.label,
                                                permissions=permissions_lookup.get(resource.permissions),
                                                stillimage=stillimage,
                                                values=resource.get_propvals(resiri_lookup, permissions_lookup)).create()
        resiri_lookup[resource.id] = instance.iri
        print("Created:", instance.iri)
Exemplo n.º 2
0
                             'anything:hasTimeStamp': "2004-04-12T13:20:00Z",
                             'anything:hasInterval': [make_value(value="0.0:3.0", comment="first interval"),
                                                      make_value(value="3.5:3.7", comment="second interval")],
                             'anything:hasRichtext': KnoraStandoffXml("This is <em>bold</em> text."),
                             'anything:hasText': "Dies ist ein einfacher Text",
                             'anything:hasUri': 'http://gaga.com:65500/gugus'
                         }).create()
print('IRI=', a_blue_thing.iri)
print('ARK=', a_blue_thing.ark)
print('VARK=', a_blue_thing.vark)

new_blue_thing = a_blue_thing.read()
new_blue_thing.print()

ThingPicture = factory.get_resclass('anything:ThingPicture')
sipi = Sipi('http://0.0.0.0:1024', con.get_token())
img = sipi.upload_image('gaga.tif')
fileref = img['uploadedFiles'][0]['internalFilename']
resperm = Permissions({PermissionValue.M: ["knora-admin:UnknownUser", "knora-admin:KnownUser"],
                       PermissionValue.CR: ["knora-admin:Creator", "knora-admin:ProjectAdmin"]})
a_thing_picture = ThingPicture(
    con=con,
    label='ThingPicture',
    stillimage=fileref,
    permissions=resperm,
    values={
        'anything:hasPictureTitle': make_value(value="A Thing Picture named Lena", permissions=resperm)
    }
).create()
print('??????????????????????????????????????????')