def test_create_resource(create_test_ontology_fixture): server = "http://0.0.0.0:3333" sipi = "http://0.0.0.0:1024", user = "******", password = "******" projectcode = "00FE" ontoname = "KPT" con = Knora(server, user, password) graph = con.get_ontology_graph('00FE', 'kpt') # print(graph) # exit(0) schema = con.create_schema(projectcode, ontoname) # pprint(schema) # exit(0) inst1_info = con.create_resource( schema, "object1", "obj1_inst1", { "textprop": "Dies ist ein Text!", "intprop": 7, "listprop": "options:opt2", "dateprop": "1966:CE:1967-05-21", "decimalprop": { 'value': "3.14159", 'comment': "Die Zahl PI" }, "geonameprop": "2661604", "richtextprop": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<text><p><strong>this is</strong> text</p> with standoff</text>", "intervalprop": "13.57:15.88" }) pprint(inst1_info) # first upload image to SIPI sipi = Sipi(sipi, con.get_token()) res = sipi.upload_image('test.tif') pprint(res) fileref = res['uploadedFiles'][0]['internalFilename'] inst2_info = con.create_resource(schema, "object2", "obj2_inst1", { "titleprop": "Stained glass", "linkprop": inst1_info['iri'] }, fileref) pprint(inst2_info)
import csv from pprint import pprint import os import requests from knora import Knora, Sipi server = "http://0.0.0.0:3333" #user = "******" user = "******" password = "******" projectcode = "0805" ontoname = "tdk_onto" con = Knora(server) con.login(user, password) sipi = Sipi("http://0.0.0.0:1024", con.get_token()) graph = con.get_ontology_graph(projectcode, ontoname) schema = con.create_schema(projectcode, ontoname) json = { "lageNr": "1234", "lageGrab": 10, "lageUmgebung": "Umgebung", "lageAreal": "Areal", "lageRaum": "Raum", "lageSchnitt": "Schnitt" } result = con.create_resource(schema, "Lage", "test_resource", json) pprint(result)
def program(args) -> None: parser = argparse.ArgumentParser() parser.add_argument( "inproject", help="Shortname of project the data should be added to") parser.add_argument("-s", "--server", type=str, default="http://0.0.0.0:3333", help="URL of the Knora server") parser.add_argument("-S", "--sipi", type=str, default="http://0.0.0.0:1024", help="URL of SIPI server") parser.add_argument("-u", "--user", type=str, default="*****@*****.**", help="Username for Knora") parser.add_argument("-p", "--password", type=str, default="test", help="The password for login") parser.add_argument("-F", "--folder", default="-", help="Input folder.") parser.add_argument("-i", "--infile", default="-", help="Input file.") parser.add_argument("-a", "--assets", default="-", help="Assets folder.") parser.add_argument("-I", "--images", default="-", help="images folder.") parser.add_argument( "-V", "--validate", action='store_true', help="Do only validation of JSON, no upload of the ontology") args = parser.parse_args(args) current_dir = os.path.dirname(os.path.realpath(__file__)) if args.folder == '-': folder = args.inproject + ".dir" else: folder = args.folder if args.assets == '-': assets_path = os.path.join(folder, 'assets') else: assets_path = args.assets if args.images == '-': images_path = os.path.join(folder, 'images') else: images_path = args.images if args.infile == '-': infile_path = os.path.join(folder, args.inproject) + '.xml' else: infile_path = args.infile xmlschema_doc = etree.parse( os.path.join(current_dir, 'knora-data-schema.xsd')) xmlschema = etree.XMLSchema(xmlschema_doc) doc = etree.parse(infile_path) xmlschema.assertValid(doc) del xmlschema del doc del xmlschema_doc print( "The imput data file is syntactically correct and passed validation!") if args.validate: exit(0) # # read the XML file containing the data, including project shortcode # context: etree.iterparse = etree.iterparse(infile_path, events=("start", "end")) resources: List[KnoraResource] = [] permissions: Dict[str, Permission] = {} while True: event, node = next(context) if event == 'start': if node.tag == 'knora': ontology = node.attrib['ontology'] shortcode = node.attrib['shortcode'] elif event == 'start' and node.tag == 'resource': resources.append(KnoraResource(context, node)) elif event == 'start' and node.tag == 'permissions': permission = Permission(context, node) permissions[permission.id] = permission elif event == 'end': if node.tag == 'knora': break context = None # delete XML tree tto save memory # # sort the resources so that resources which do not likt to others come first # resources = do_sortorder(resources) # # connect to Knora # con = Knora(args.server) con.login(args.user, args.password) sipi = Sipi(args.sipi, con.get_token()) graph = con.get_ontology_graph(shortcode, ontology) schema = con.create_schema(shortcode, ontology) permissions_lookup: StrDict = {} for p in permissions.items(): permissions_lookup[p[0]] = create_permission(con, p[1]) resiri_lookup: StrDict = {} # cnt: int = 0 for resource in resources: if resource.image is not None: print('Uploading ' + resource.image + '...') imgres = sipi.upload_image(resource.image) print('Upload done: ' + imgres['uploadedFiles'][0]['internalFilename']) fileref = imgres['uploadedFiles'][0]['internalFilename'] print('Uploading data...') resinfo = con.create_resource( schema=schema, res_class=resource.restype, label=resource.label, values=resource.get_propvals(resiri_lookup, permissions_lookup), permissions=permissions_lookup.get(resource.permissions), stillimage=fileref) else: resinfo = con.create_resource( schema=schema, res_class=resource.restype, label=resource.label, values=resource.get_propvals(resiri_lookup, permissions_lookup), permissions=permissions_lookup.get(resource.permissions)) resiri_lookup[resource.unique_id] = resinfo['iri']
"listprop": "options:opt2", "dateprop": "1966:CE:1967-05-21", "decimalprop": { 'value': "3.14159", 'comment': "Die Zahl PI" }, "geonameprop": "2661604", "richtextprop": { 'value': KnoraStandoffXml( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<text><p><strong>this is</strong> text</p> with standoff</text>" ), 'comment': "Text mit markup" }, "intervalprop": "13.57:15.88" }) pprint(inst1_info) #first upload image to SIPI sipi = Sipi(args.sipi, con.get_token()) res = sipi.upload_image('example.tif') pprint(res) fileref = res['uploadedFiles'][0]['internalFilename'] inst2_info = con.create_resource(schema, "object2", "obj2_inst1", { "titleprop": "Stained glass", "linkprop": inst1_info['iri'] }, fileref) pprint(inst2_info)
# exit(0) inst1_info = con.create_resource(schema, "MyObject", "obj_inst1", { "mySimpleText": "Dies ist ein Text!", "myRichText": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<text><p><strong>this is</strong> text</p> with standoff</text>", "myColor": "#ff7700", "myDate": "1966:CE:1967-05-21", "myDecimal": {'value': "3.14159", 'comment': "Die Zahl PI"}, "myGeoname": "2661604", "myList": "options:opt2", "myInteger": 7, "myInterval": "13.57:15.88", # "myTime": "2019-10-23T13.45:12Z", "myBoolean": True }) pprint(inst1_info) # first upload image to SIPI sipi = Sipi(sipi, con.get_token()) res = sipi.upload_image('test.tif') pprint(res) fileref = res['uploadedFiles'][0]['internalFilename'] inst2_info = con.create_resource(schema, "MyImage", "image_inst1", { "titleprop": "Stained glass", "linkprop": inst1_info['iri'] }, None, fileref) pprint(inst2_info)
from pprint import pprint import os import requests from knora import Knora, Sipi from knora import KnoraError import urllib.parse server = "https://api.tdk.test.dasch.swiss" #user = "******" user = "******" password = "******" projectcode = "0805" ontoname = "tdk_onto" print('Starting connection') con = Knora(server) con.login(user, password) sipi = Sipi("https://iiif.tdk.test.dasch.swiss", con.get_token()) zeichnung_dir = "tdk_Data/zeichnungen/" #graph = con.get_ontology_graph(projectcode, ontoname) schema = con.create_schema(projectcode, ontoname) subdirectory = "tdk_Data" lage_file = "tdk_Lage.csv" kampagne_file = "tdk_Kampagne.csv" zeichnung_file = "tdk_Zeichnungen.csv" pub_file = "tdk_Publikation.csv" smfund_file = "tdk_SMFUND.csv" bild_file = "tdk_Bilder.csv" print('Set-Up finished') class tdk_create_data: def __init__(self):