Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(verbose xml upload): use v option to print verbose output in XML upload (DSP-1797) #70

Merged
merged 9 commits into from Jul 26, 2021
67 changes: 39 additions & 28 deletions knora/dsp_tools.py
@@ -1,24 +1,35 @@
"""
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)."
)
Expand All @@ -32,8 +43,10 @@ def program(args):
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("-L", "--listfile", type=str, default="lists.json", help="Name of list node informationfile")
parser_create.add_argument("-V", "--validate", action='store_true',
help="Do only validation of JSON, no upload of the ontology")
irinaschubert marked this conversation as resolved.
Show resolved Hide resolved
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")
parser_create.add_argument("-d", "--dump", action="store_true", help="dump test files for DSP-API requests")
Expand All @@ -53,22 +66,26 @@ 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)

Expand Down Expand Up @@ -114,7 +131,7 @@ def program(args):
imgdir=args.imgdir,
sipi=args.sipi,
verbose=args.verbose,
validate=args.validate)
validate_only=args.validate)
elif args.action == "excel":
list_excel2json(excelpath=args.excelfile,
sheetname=args.sheet,
Expand All @@ -126,11 +143,5 @@ def program(args):
verbose=args.verbose)



def main():
program(sys.argv[1:])


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

54 changes: 34 additions & 20 deletions knora/dsplib/models/sipi.py
Expand Up @@ -3,34 +3,48 @@

import os

class Sipi:

def on_api_error(res):
"""
Checks for any API errors

def __init__(self, sipiserver: str, token: str):
self.sipiserver = sipiserver
self.token = token
Args:
res: the response from the API which is checked, usually in JSON format

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
"""
Returns:
Knora Error 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:
"""
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), bitstreamfile),
'file': (os.path.basename(filepath), bitstream_file),
}
req = requests.post(self.sipiserver + "/upload?token=" + self.token,
files=files)
self.on_api_error(req)
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