Skip to content
Permalink
Browse files
feat(verbose xml upload): use v option to print verbose output in XML…
… upload (DSP-1797) (#70)

* Use v option to print imported resources

* Add comments and reformat code
  • Loading branch information
irinaschubert committed Jul 26, 2021
1 parent c255845 commit b1f56a1efe8ff32376c8e4f8bf8f292d6061e016
Showing with 340 additions and 291 deletions.
  1. +44 −67 knora/dsp_tools.py
  2. +36 −23 knora/dsplib/models/sipi.py
  3. +257 −195 knora/dsplib/utils/xml_upload.py
  4. +3 −6 test/test_tools.py
@@ -1,38 +1,47 @@
"""
The code in this file handles the arguments passed by the user from the command line and calls the requested actions.
"""
import argparse
import sys
import os
import pkg_resources # part of setuptools
import sys

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import pkg_resources # part of setuptools

from dsplib.utils.onto_validate import validate_list, validate_ontology
from dsplib.utils.onto_create_lists import create_lists
from dsplib.utils.onto_create_ontology import create_ontology
from dsplib.utils.onto_get import get_ontology
from dsplib.utils.xml_upload import xml_upload
from dsplib.utils.onto_process_excel import list_excel2json
from dsplib.utils.onto_validate import validate_list, validate_ontology
from dsplib.utils.xml_upload import xml_upload

sys.path.append(os.path.dirname(os.path.realpath(__file__)))


def program(args: list) -> None:
"""
The program parses the command line arguments and calls the requested action
def program(args):
Args:
args: list of arguments passed by the user from the command line
Returns:
None
"""
version = pkg_resources.require("dsp-tools")[0].version

#
# parse the arguments of the command line
#
parser = argparse.ArgumentParser(
description=f"dsp-tools (Version {version}) DaSCH Service Platform data modelling tools (© 2021 by DaSCH)."
)
description=f"dsp-tools (Version {version}) DaSCH Service Platform data modelling tools (© 2021 by DaSCH).")

subparsers = parser.add_subparsers(title="Subcommands",
description='Valid subcommands are',
help='sub-command help')
subparsers = parser.add_subparsers(title="Subcommands", description='Valid subcommands are', help='sub-command help')

parser_create = subparsers.add_parser('create', help='Create ontologies, lists etc.')
parser_create.set_defaults(action="create")
parser_create.add_argument("-s", "--server", type=str, default="http://0.0.0.0:3333", help="URL of the DSP server")
parser_create.add_argument("-u", "--user", default="root@example.com", help="Username for DSP server")
parser_create.add_argument("-p", "--password", default="test", help="The password for login")
parser_create.add_argument("-V", "--validate", action='store_true', help="Do only validation of JSON, no upload of the ontology")
parser_create.add_argument("-V", "--validate", action='store_true',
help="Do only validation of JSON, no upload of the ontology")
parser_create.add_argument("-L", "--listfile", type=str, default="lists.json", help="Name of list node informationfile")
parser_create.add_argument("-l", "--lists", action='store_true', help="Only create the lists")
parser_create.add_argument("-v", "--verbose", action="store_true", help="Verbose feedback")
@@ -53,22 +62,22 @@ def program(args):
parser_upload.add_argument("-s", "--server", type=str, default="http://0.0.0.0:3333", help="URL of the DSP server")
parser_upload.add_argument("-u", "--user", type=str, default="root@example.com", help="Username for DSP server")
parser_upload.add_argument("-p", "--password", type=str, default="test", help="The password for login")
parser_upload.add_argument("-V", "--validate", action='store_true', help="Do only validation of JSON, no upload of the ontology")
parser_upload.add_argument("-V", "--validate", action='store_true', help="Do only validation of XML, no upload of the data")
parser_upload.add_argument("-i", "--imgdir", type=str, default=".", help="Path to folder containing the images")
parser_upload.add_argument("-S", "--sipi", type=str, default="http://0.0.0.0:1024", help="URL of SIPI server")
parser_upload.add_argument("-v", "--verbose", action="store_true", help="Verbose feedback")
parser_upload.add_argument("xmlfile", help="path to xml file containing the data", default="data.xml")

parser_excellists = subparsers.add_parser('excel', help='Create lists JSON from excel files')
parser_excellists.set_defaults(action="excel")
parser_excellists.add_argument("-S", "--sheet", type=str, help="Name of excel sheet to be used", default="Tabelle1")
parser_excellists.add_argument("-s", "--shortcode", type=str, help="Shortcode of project", default="4123")
parser_excellists.add_argument("-l", "--listname", type=str, help="Name of list to be created", default="my_list")
parser_excellists.add_argument("-L", "--label", type=str, help="Label of list to be created", default="MyList")
parser_excellists.add_argument("-x", "--lang", type=str, help="Language for label", default="en")
parser_excellists.add_argument("-v", "--verbose", action="store_true", help="Verbose feedback")
parser_excellists.add_argument("excelfile", help="Path to the excel file containing the list data", default="lists.xlsx")
parser_excellists.add_argument("outfile", help="Path to the output JSON file containing the list data", default="list.json")
parser_excel_lists = subparsers.add_parser('excel', help='Create lists JSON from excel files')
parser_excel_lists.set_defaults(action="excel")
parser_excel_lists.add_argument("-S", "--sheet", type=str, help="Name of excel sheet to be used", default="Tabelle1")
parser_excel_lists.add_argument("-s", "--shortcode", type=str, help="Shortcode of project", default="4123")
parser_excel_lists.add_argument("-l", "--listname", type=str, help="Name of list to be created", default="my_list")
parser_excel_lists.add_argument("-L", "--label", type=str, help="Label of list to be created", default="MyList")
parser_excel_lists.add_argument("-x", "--lang", type=str, help="Language for label", default="en")
parser_excel_lists.add_argument("-v", "--verbose", action="store_true", help="Verbose feedback")
parser_excel_lists.add_argument("excelfile", help="Path to the excel file containing the list data", default="lists.xlsx")
parser_excel_lists.add_argument("outfile", help="Path to the output JSON file containing the list data", default="list.json")

args = parser.parse_args(args)

@@ -81,56 +90,24 @@ def program(args):
if args.validate:
validate_list(args.datamodelfile)
else:
create_lists(input_file=args.datamodelfile,
lists_file=args.listfile,
server=args.server,
user=args.user,
password=args.password,
verbose=args.verbose,
dump=args.dump)
create_lists(input_file=args.datamodelfile, lists_file=args.listfile, server=args.server, user=args.user,
password=args.password, verbose=args.verbose, dump=args.dump)
else:
if args.validate:
validate_ontology(args.datamodelfile)
else:
create_ontology(input_file=args.datamodelfile,
lists_file=args.listfile,
server=args.server,
user=args.user,
password=args.password,
verbose=args.verbose,
dump=args.dump if args.dump else False)
create_ontology(input_file=args.datamodelfile, lists_file=args.listfile, server=args.server, user=args.user,
password=args.password, verbose=args.verbose, dump=args.dump if args.dump else False)
elif args.action == "get":
get_ontology(projident=args.project,
outfile=args.datamodelfile,
server=args.server,
user=args.user,
password=args.password,
verbose=args.verbose)
get_ontology(projident=args.project, outfile=args.datamodelfile, server=args.server, user=args.user,
password=args.password, verbose=args.verbose)
elif args.action == "xmlupload":
xml_upload(input_file=args.xmlfile,
server=args.server,
user=args.user,
password=args.password,
imgdir=args.imgdir,
sipi=args.sipi,
verbose=args.verbose,
validate=args.validate)
xml_upload(input_file=args.xmlfile, server=args.server, user=args.user, password=args.password, imgdir=args.imgdir,
sipi=args.sipi, verbose=args.verbose, validate_only=args.validate)
elif args.action == "excel":
list_excel2json(excelpath=args.excelfile,
sheetname=args.sheet,
shortcode=args.shortcode,
listname=args.listname,
label=args.label,
lang=args.lang,
outfile=args.outfile,
verbose=args.verbose)



def main():
program(sys.argv[1:])
list_excel2json(excelpath=args.excelfile, sheetname=args.sheet, shortcode=args.shortcode, listname=args.listname,
label=args.label, lang=args.lang, outfile=args.outfile, verbose=args.verbose)


if __name__ == '__main__':
program(sys.argv[1:])

@@ -1,36 +1,49 @@
import os

import requests

from .helpers import BaseError

import os

class Sipi:
def on_api_error(res):
"""
Checks for any API errors
Args:
res: the response from the API which is checked, usually in JSON format
def __init__(self, sipiserver: str, token: str):
self.sipiserver = sipiserver
self.token = token
Returns:
Knora Error that is being raised
"""

def on_api_error(self, res):
"""
Method to check for any API errors
:param res: The input to check, usually JSON format
:return: Possible KnoraError that is being raised
"""
if res.status_code != 200:
raise BaseError("SIPI-ERROR: status code=" + str(res.status_code) + "\nMessage:" + res.text)

if res.status_code != 200:
raise BaseError("SIPI-ERROR: status code=" + str(res.status_code) + "\nMessage:" + res.text)
if 'error' in res:
raise BaseError("SIPI-ERROR: API error: " + res.error)

if 'error' in res:
raise BaseError("SIPI-ERROR: API error: " + res.error)

class Sipi:
"""Represents the Sipi instance"""

def __init__(self, sipi_server: str, token: str):
self.sipi_server = sipi_server
self.token = token

def upload_bitstream(self, filepath):
print(f"filepath=${os.path.basename(filepath)} (${filepath})")
with open(filepath, 'rb') as bitstreamfile:
files = {
'file': (os.path.basename(filepath), bitstreamfile),
}
req = requests.post(self.sipiserver + "/upload?token=" + self.token,
files=files)
self.on_api_error(req)
"""
Uploads a bitstream to the Sipi server
Args:
filepath: path to the file, could be either absolute or relative
Returns:
API response
"""
with open(filepath, 'rb') as bitstream_file:
files = {'file': (os.path.basename(filepath), bitstream_file), }
req = requests.post(self.sipi_server + "/upload?token=" + self.token, files=files)
on_api_error(req)
print(f'Uploaded file {filepath}')
res = req.json()
return res
Loading

0 comments on commit b1f56a1

Please sign in to comment.