Skip to content
Snippets Groups Projects
Commit 3adafd10 authored by Hans van den Heuvel's avatar Hans van den Heuvel
Browse files

Python script split into chuncks, like the Powershell equivalents.

parent 76821094
Branches
No related tags found
No related merge requests found
#
import getpass
import WebApiClient
import datetime
#
from argparse import ArgumentParser, SUPPRESS
# Please try to use the keyring
# Do not store passwords (even defaults) in a script
import keyring
# input parameters
default_mcra_url = 'https://mcra.test.wur.nl/Mcra90'
parser = ArgumentParser(
description=
'This is a tool to query the MCRA server',
epilog=
'For example %(prog)s -v -u hansheuvel -k Euromix'
)
parser.add_argument(
'-v', '--verbosity', help="Show verbose output",
action="count", default=0)
parser.add_argument(
'-a', '--apiurl', default=default_mcra_url,
help='The MCRA API URL (default: %(default)s)')
parser.add_argument(
'-u', '--username', default=None,
help='The username to connect to API.')
parser.add_argument(
'-k', '--keyring', default=None,
help='The keyring system to use.')
parser.add_argument(
'-i', '--id', default=None,
help='The ID of the datasource.')
args = parser.parse_args()
if args.verbosity >= 1:
print("URL of EuroMix API application: "+default_mcra_url+".")
if args.username is None:
print("MCRA user name:(%s)" % getpass.getuser())
mcrausername = input().strip() or getpass.getuser()
else:
mcrausername = args.username
if args.keyring is None:
mcrapassword=getpass.getpass("MCRA Password:").strip()
else:
mcrapassword = keyring.get_password(args.keyring, mcrausername)
# This is the call to set-up the api
api = WebApiClient.Mcra(
url=args.apiurl,
username=mcrausername,
password=mcrapassword)
print()
# Create a layout comparable to the Powershell one
print('{:<5} {:>6.6} {:<12.12} {:>20.20} {:>7.7} {:>6.6} {:>6.6} {:>7.7} {:<50.50}'.format(
'Mode','Id','Owner','Created','DataSrc','Id','CurId','Version','Name'))
print('{:<5} {:>6.6} {:<12.12} {:>20.20} {:>7.7} {:>6.6} {:>6.6} {:>7.7} {:<50.50}'.format(
'----','--','-----','-------','-------','--','-----','-------','----'))
# Here the actual api call is executed
result = api.get_datasource(args.id)
if result:
print(result[0])
for x in result:
date=datetime.datetime.strptime(x['createdTimeStamp'], '%Y-%m-%dT%H:%M:%S.%f')
print('{:<5} {:>6.6} {:<12.12} {:>20.20} {:>7.7} {:>6.6} {:>6.6} {:>7.7} {:<50.50}'.format(
str(x['accessLevel'])+' '+str(int(x['isDeleted'])) \
+' '+str(int(x['isLocalFile'])),
str(x['idOwner']), x['owner'], datetime.datetime.strftime(date,'%d-%m-%Y %H:%M'),
str(x['idDataSourceRepository']), str(x['id']), str(x['idCurrentVersion']),
str(x['version']), x['name']))
print()
\ No newline at end of file
# #
import getpass import getpass
import WebApiClient import WebApiClient
# #
from argparse import ArgumentParser, SUPPRESS from argparse import ArgumentParser, SUPPRESS
# Please try to use the keyring # Please try to use the keyring
...@@ -14,9 +13,9 @@ default_mcra_url = 'https://mcra.test.wur.nl/Mcra90' ...@@ -14,9 +13,9 @@ default_mcra_url = 'https://mcra.test.wur.nl/Mcra90'
parser = ArgumentParser( parser = ArgumentParser(
description= description=
'This is a tool to download files from the MCRA server', 'This is a tool to query the MCRA server',
epilog= epilog=
'For example Download-MCRA.py -v -u hansheuvel -k Euromix' 'For example %(prog)s -v -u hansheuvel -k Euromix'
) )
parser.add_argument( parser.add_argument(
...@@ -35,6 +34,14 @@ parser.add_argument( ...@@ -35,6 +34,14 @@ parser.add_argument(
'-k', '--keyring', default=None, '-k', '--keyring', default=None,
help='The keyring system to use.') help='The keyring system to use.')
parser.add_argument(
'-i', '--id', default=None,
help='The ID of the file.')
parser.add_argument(
'-f', '--filename', default=None,
help='The name of the file.')
args = parser.parse_args() args = parser.parse_args()
if args.verbosity >= 1: if args.verbosity >= 1:
...@@ -51,30 +58,13 @@ if args.keyring is None: ...@@ -51,30 +58,13 @@ if args.keyring is None:
else: else:
mcrapassword = keyring.get_password(args.keyring, mcrausername) mcrapassword = keyring.get_password(args.keyring, mcrausername)
# This is the call to set-up the api
api = WebApiClient.Mcra( api = WebApiClient.Mcra(
url=args.apiurl, url=args.apiurl,
username=mcrausername, username=mcrausername,
password=mcrapassword) password=mcrapassword)
# Mooi lijstje maken voor een makkelijk overzicht: if args.verbosity >= 1:
print('+++ REPOSITORIES +++++++++++++++++++++++++++++++++++++++++++++') print('Downloading {file}'.format(file=args.filename))
for x in api.get_repository():
print('{:>5} {:<25.25} {:>3} {:>3} {:>3} {:>3} {:>3} {:<25.25}'.format(
x['id'], x['name'], x['idOwner'], x['idParentRepository'],
x['isUserRootRepository'], x['repositoryType'],
x['userAccessLevel'], x['owner']))
print('\n+++ DATASOURCES ++++++++++++++++++++++++++++++++++++++++++++')
for x in api.get_datasource():
print(
'{:>5} {:<25.25} {:>25.25} {:>3} {:>3} {:>3} {:>3} {:<25.25}'.format(
x['id'], x['name'], x['createdTimeStamp'], x['version'],
x['idCurrentVersion'], x['idDataSourceRepository'], x['idOwner'],
x['owner']))
print('\n+++ WORKSPACES +++++++++++++++++++++++++++++++++++++++++++++')
for x in api.get_workspace(): api.get_file(id=args.id, filename=args.filename)
print('{:>5} {:<25.25} {:>25.25}'.format( \ No newline at end of file
x['id'], x['name'], x['dateModified']))
#
import getpass
import WebApiClient
#
from argparse import ArgumentParser, SUPPRESS
# Please try to use the keyring
# Do not store passwords (even defaults) in a script
import keyring
# input parameters
default_mcra_url = 'https://mcra.test.wur.nl/Mcra90'
parser = ArgumentParser(
description=
'This is a tool to query the MCRA server',
epilog=
'For example %(prog)s -v -u hansheuvel -k Euromix'
)
parser.add_argument(
'-v', '--verbosity', help="Show verbose output",
action="count", default=0)
parser.add_argument(
'-a', '--apiurl', default=default_mcra_url,
help='The MCRA API URL (default: %(default)s)')
parser.add_argument(
'-u', '--username', default=None,
help='The username to connect to API.')
parser.add_argument(
'-k', '--keyring', default=None,
help='The keyring system to use.')
parser.add_argument(
'-i', '--id', default=None,
help='The ID of the repository.')
args = parser.parse_args()
if args.verbosity >= 1:
print("URL of EuroMix API application: "+default_mcra_url+".")
if args.username is None:
print("MCRA user name:(%s)" % getpass.getuser())
mcrausername = input().strip() or getpass.getuser()
else:
mcrausername = args.username
if args.keyring is None:
mcrapassword=getpass.getpass("MCRA Password:").strip()
else:
mcrapassword = keyring.get_password(args.keyring, mcrausername)
# This is the call to set-up the api
api = WebApiClient.Mcra(
url=args.apiurl,
username=mcrausername,
password=mcrapassword)
print()
# Create a layout comparable to the Powershell one
print('{:<5} {:>6.6} {:<12.12} {:>6.6} {:>6.6} {:<50.50}'.format(
'Mode','Id','Owner','Parent','Id','Name'))
print('{:<5} {:>6.6} {:<12.12} {:>6.6} {:>6.6} {:<50.50}'.format(
'----','--','-----','------','--','----'))
# Here the actual api call is executed
result = api.get_repository(args.id)
if result:
for x in result:
print('{:<5} {:>6.6} {:<12.12} {:>6.6} {:>6.6} {:<50.50}'.format(
str(x['userAccessLevel'])+' '+str(int(x['isUserRootRepository'])) \
+' '+str(int(x['repositoryType'])),
str(x['idOwner']), x['owner'],
str(x['idParentRepository']), str(x['id']), x['name']))
print()
\ No newline at end of file
#
import getpass
import WebApiClient
import datetime
#
from argparse import ArgumentParser, SUPPRESS
# Please try to use the keyring
# Do not store passwords (even defaults) in a script
import keyring
# input parameters
default_mcra_url = 'https://mcra.test.wur.nl/Mcra90'
parser = ArgumentParser(
description=
'This is a tool to query the MCRA server',
epilog=
'For example %(prog)s -v -u hansheuvel -k Euromix'
)
parser.add_argument(
'-v', '--verbosity', help="Show verbose output",
action="count", default=0)
parser.add_argument(
'-a', '--apiurl', default=default_mcra_url,
help='The MCRA API URL (default: %(default)s)')
parser.add_argument(
'-u', '--username', default=None,
help='The username to connect to API.')
parser.add_argument(
'-k', '--keyring', default=None,
help='The keyring system to use.')
parser.add_argument(
'-i', '--id', default=None,
help='The ID of the workspace.')
args = parser.parse_args()
if args.verbosity >= 1:
print("URL of EuroMix API application: "+default_mcra_url+".")
if args.username is None:
print("MCRA user name:(%s)" % getpass.getuser())
mcrausername = input().strip() or getpass.getuser()
else:
mcrausername = args.username
if args.keyring is None:
mcrapassword=getpass.getpass("MCRA Password:").strip()
else:
mcrapassword = keyring.get_password(args.keyring, mcrausername)
# This is the call to set-up the api
api = WebApiClient.Mcra(
url=args.apiurl,
username=mcrausername,
password=mcrapassword)
print()
# Create a layout comparable to the Powershell one
print('{:>20.20} {:>6.6} {:<50.50}'.format(
'Modified','Id','Name'))
print('{:>20.20} {:>6.6} {:<50.50}'.format(
'--------','--','----'))
# Here the actual api call is executed
result = api.get_workspace(args.id)
if result:
for x in result:
date=datetime.datetime.strptime(x['dateModified'], '%Y-%m-%dT%H:%M:%S.%f')
print('{:>20.20} {:>6.6} {:<50.50}'.format(
datetime.datetime.strftime(date,'%d-%m-%Y %H:%M'), str(x['id']), x['name']))
print()
...@@ -31,27 +31,59 @@ class Mcra: ...@@ -31,27 +31,59 @@ class Mcra:
def get_repository(self, id=None): def get_repository(self, id=None):
if id is None: if id is None:
return self.__call_api__( result = self.__call_api__(
apiCall=f'Repositories/GetAll').json() apiCall='Repositories/GetAll')
else: else:
return self.__call_api__( result = self.__call_api__(
apiCall=f'Repositories/Get/{id}').json() apiCall=f'Repositories/Get/{id}')
# Making sure we can always iterate.
if result.status_code == 500:
# Something went wrong. No results.
return None
else:
if type(result.json()) is not list:
return [result.json()]
else:
return result.json()
def get_datasource(self, id=None): def get_datasource(self, id=None):
if id is None: if id is None:
return self.__call_api__( result = self.__call_api__(
apiCall=f'DataSources/GetAll').json() apiCall=f'DataSources/GetAll').json()
else: else:
return self.__call_api__( result = self.__call_api__(
apiCall=f'DataSources/Get/{id}').json() apiCall=f'DataSources/Get/{id}').json()
# Making sure we can always iterate.
if type(result) is not list:
return [result]
else:
return result
def get_workspace(self, id=None): def get_workspace(self, id=None):
if id is None: if id is None:
return self.__call_api__( result = self.__call_api__(
apiCall=f'Workspace/GetAll').json() apiCall=f'Workspace/GetAll').json()
else: else:
return self.__call_api__( result = self.__call_api__(
apiCall=f'Workspace/Get/{id}').json() apiCall=f'Workspace/Get/{id}').json()
# Making sure we can always iterate.
if type(result) is not list:
return [result]
else:
return result
def get_file(self, id, filename, csv=False):
if csv:
result = self.__call_api__(
apiCall=f'DataSources/DownloadVersionCsv/{id}')
else:
result = self.__call_api__(
apiCall=f'DataSources/DownloadVersion/{id}')
with open(filename, 'wb') as fd:
for chunk in result.iter_content(chunk_size=128):
fd.write(chunk)
def __get_token__(self): def __get_token__(self):
tokenUrl = self.url + "/jwtauth/token" tokenUrl = self.url + "/jwtauth/token"
...@@ -92,10 +124,15 @@ class Mcra: ...@@ -92,10 +124,15 @@ class Mcra:
r.raise_for_status() r.raise_for_status()
except HTTPError as http_err: except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') if str(http_err).startswith('500'):
pass
else:
print(f'HTTP error occurred: {http_err}')
# In case of error 500 with the result:
# {'message': 'An error has occurred.', 'exceptionMessage': 'Object reference not set to an instance of an object.', 'exceptionType': 'System.NullReferenceException'
# Apparently no objects can be returned
except Exception as err: except Exception as err:
print(f'Other error occurred: {err}') print(f'Other error occurred: {err}')
return r return r
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment