Commit 6d6eb156 authored by Julia Kaufhold's avatar Julia Kaufhold
Browse files

migration to python3, pep8.

parent 00709e2f
\ No newline at end of file
This diff is collapsed.
#!/usr/bin/env python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
# B2SAFE B2SHARE client Command Line Interface #
################################################################################
###########################################################################
# B2SAFE B2SHARE client Command Line Interface #
###########################################################################
import argparse
import logging.handlers
import os
......@@ -12,7 +12,7 @@ import requests
from b2shareclient import B2shareClient
from configuration import Configuration
from manifest import IRODSUtils
from manifest.irodsUtility import IRODSUtils
logger = logging.getLogger('B2shareClientCLI')
......@@ -24,12 +24,15 @@ def draft(args):
logger.info("Start creating draft ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if accessToken is None:
logger.error("Drafting FAILED. No B2SHARE access token found in users meta data.")
logger.error(
"Drafting FAILED. \
No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
b2shcl = B2shareClient(configuration)
filePIDsList = collectPIDsForCollection(args.collectionPath, configuration)
filePIDsList = collectPIDsForCollection(args.collectionPath,
configuration)
commID = args.communityID
if args.communityID is None:
commID = getCommunityIDByName(configuration, args.communityName)
......@@ -42,6 +45,7 @@ def draft(args):
logger.error("Drafting FAILED.")
return recordId
def getCommunityIDByName(configuration, community_name):
community_id = None
if not configuration:
......@@ -53,16 +57,18 @@ def getCommunityIDByName(configuration, community_name):
acces_part = None
list_communities_url = None
if configuration.access_parameter and configuration.access_token:
acces_part = configuration.access_parameter + "=" + configuration.access_token
acces_part = configuration.access_parameter + "=" + \
configuration.access_token
if acces_part and host and endpoint:
list_communities_url = host + endpoint + acces_part
if list_communities_url:
try:
#TODO: delete 'verify=False', if B2SHARE server has proper certificate
# TODO: delete 'verify=False',
# if B2SHARE server has proper certificate
response = requests.get(url=list_communities_url, verify=False)
print(list_communities_url)
logger.debug("status code: " + str(response.status_code))
if (str(response.status_code) == "200"):
if (str(response.status_code) == "200"):
communities_list = response.json()["hits"]["hits"]
for community_object in communities_list:
name = community_object["name"]
......@@ -74,6 +80,7 @@ def getCommunityIDByName(configuration, community_name):
logger.error(e)
return community_id
def getAllCommunities(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -81,19 +88,22 @@ def getAllCommunities(args):
logger.info("Start get all communities ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if accessToken is None:
logger.error("Drafting FAILED. No B2SHARE access token found in users meta data.")
logger.error("Drafting FAILED." +
" No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
b2shcl = B2shareClient(configuration)
communities = b2shcl.getAllCommunities()
if communities:
logger.info("All available communities: \n "+str(communities)+" END.")
print("List of communities and their id's: \n"+ pprint.pformat(communities))
print("List of communities and their id's: \n" +
pprint.pformat(communities))
else:
logger.error("get all communities FAILED.")
return communities
def collectPIDsForCollection(collectionPath, configuration):
PIDobjectsString = '['
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
......@@ -109,14 +119,17 @@ def collectPIDsForCollection(collectionPath, configuration):
return None
for filePath in filePathsMap.keys():
filePID = irodsu.getMetadata(filePath, "PID")
if filePID :
# filePath[1:] deletes leading / in a path as requested in issue #112 on GitHub
PIDobject = '{"key":"'+filePath[1:]+'",'+' "ePIC_PID":"'+filePID[0] +'"}'
PIDobjectsString = PIDobjectsString + PIDobject +','
forLastElemIndex = len(PIDobjectsString) - 1 #delete last comma
if filePID:
# filePath[1:] deletes leading / in a path
# as requested in issue #112 on GitHub
PIDobject = '{"key":"'+filePath[1:] + \
'",'+' "ePIC_PID":"'+filePID[0] + '"}'
PIDobjectsString = PIDobjectsString + PIDobject + ','
forLastElemIndex = len(PIDobjectsString) - 1 # delete last comma
PIDobjectsString = PIDobjectsString[:forLastElemIndex] + ']'
return PIDobjectsString
def collectFilePathsFromTree(filesTree):
filePaths = {}
for coll in filesTree:
......@@ -125,14 +138,15 @@ def collectFilePathsFromTree(filesTree):
filePaths[coll + os.sep + fp] = fp
if len(filesTree[coll]) > 1:
# there are also subdirs
del filesTree[coll]['__files__']
filemap = collectFilePathsFromTree(filesTree[coll])
# merge the map dictionaries
temp = filemap.copy()
temp.update(filePaths)
filePaths = temp
del filesTree[coll]['__files__']
filemap = collectFilePathsFromTree(filesTree[coll])
# merge the map dictionaries
temp = filemap.copy()
temp.update(filePaths)
filePaths = temp
return filePaths
def addMetadata(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -140,11 +154,12 @@ def addMetadata(args):
logger.info("Adding metadata ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if accessToken is None:
logger.error("Adding metadata FAILED. No B2SHARE access token found in users meta data.")
logger.error("Adding metadata FAILED." +
" No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
irods_env=configuration.irodsenv)
......@@ -153,6 +168,7 @@ def addMetadata(args):
b2shcl.addB2shareMetadata(args.record_id, metadata_file)
logger.info("Added metadata")
def publish(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -160,15 +176,18 @@ def publish(args):
logger.info("Publishing ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if not accessToken:
logger.error("Publishing FAILED. No B2SHARE access token found in users meta data.")
logger.error("Publishing FAILED." +
" No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
b2shcl = B2shareClient(configuration)
b2shcl.publishRecord(args.rec_id)
logger.info("Publishing END.")
#get access_token from users metadata in iRODS
# get access_token from users metadata in iRODS
def getAccessTokenWithConfigs(configuration, args):
irodsu = IRODSUtils(configuration.irods_home_dir, logger,
configuration.irods_debug,
......@@ -182,6 +201,7 @@ def getAccessTokenWithConfigs(configuration, args):
else:
return None
def getCommunitySchema(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -189,20 +209,22 @@ def getCommunitySchema(args):
logger.info("Get Community Schema ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if not accessToken:
logger.error("Get Community Schema FAILED. No B2SHARE access token found in users meta data.")
logger.error("Get Community Schema FAILED." +
" No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
commID = args.commID
if commID is None:
commID = getCommunityIDByName(configuration, args.commName)
b2shcl = B2shareClient(configuration)
schema = b2shcl.getCommunitySchema(commID)
logger.info(str(schema))
logger.info("Get Community Schema END.")
return schema
def getDraftByID(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -210,17 +232,19 @@ def getDraftByID(args):
logger.info("Get draft by ID ...")
accessToken = getAccessTokenWithConfigs(configuration, args)
if not accessToken:
logger.error("Get draft by ID FAILED. No B2SHARE access token found in users meta data.")
logger.error("Get draft by ID FAILED. " +
"No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
b2shcl = B2shareClient(configuration)
draft = b2shcl.getDraftByID(args.draft_id)
if draft:
logger.info("Request for a draft with id " + draft + " SUCCESSFUL.")
logger.info("Request for a draft with id " + draft + " SUCCESSFUL.")
logger.info("Get draft by ID END.")
return draft
def deleteDraft(args):
configuration = Configuration(args.confpath, args.debug, args.dryrun,
logger, irodsenv=args.irodsenv)
......@@ -228,14 +252,16 @@ def deleteDraft(args):
logger.info("DELETING DRAFT: " + args.draft_to_delete_id)
accessToken = getAccessTokenWithConfigs(configuration, args)
if not accessToken:
logger.error("DELETING DRAFT FAILED. No B2SHARE access token found in users meta data.")
logger.error("DELETING DRAFT FAILED. " +
"No B2SHARE access token found in users meta data.")
return None
configuration.access_token = accessToken
b2shcl = B2shareClient(configuration)
b2shcl.deleteDraft(args.draft_to_delete_id)
b2shcl.deleteDraft(args.draft_to_delete_id)
logger.info("DELETING DRAFT END.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='B2SAFE B2SHARE client')
parser.add_argument("--confpath", help="path to the configuration file")
......@@ -246,39 +272,58 @@ if __name__ == "__main__":
parser.add_argument("-u", "--userName", help="iRODS user name")
parser.add_argument("--irodsenv", help="Path to irods configuration")
subparsers = parser.add_subparsers(help='sub-command help', dest='subcmd')
parser_draft = subparsers.add_parser('draft', help='create a draft record in B2Share')
parser_draft = subparsers.add_parser(
'draft', help='create a draft record in B2Share')
comm_group = parser_draft.add_mutually_exclusive_group(required=True)
comm_group.add_argument("-c", "--communityName", help="B2Share community name")
comm_group.add_argument("-i", "--communityID", help="B2Share community id")
comm_group.add_argument("-c", "--communityName",
help="B2Share community name")
comm_group.add_argument("-i", "--communityID",
help="B2Share community id")
parser_draft.add_argument('-ti', '--title', help='title of the record')
parser_draft.add_argument('-cp', '--collectionPath', required=True, help='path to the collection in iRODS with files')
parser_draft.add_argument('-cp', '--collectionPath', required=True,
help='path to the collection in iRODS')
parser_draft.set_defaults(func=draft)
parser_meta = subparsers.add_parser('meta', help='add metadata to the draft')
parser_meta.add_argument('-id', '--record_id', required=True, help='the b2share id of the record')
parser_meta.add_argument('-md', '--metadata', required=True, help='path to the metadata JSON file of the record')
parser_meta = subparsers.add_parser(
'meta', help='add metadata to the draft')
parser_meta.add_argument('-id',
'--record_id', required=True,
help='the b2share id of the record')
parser_meta.add_argument('-md', '--metadata', required=True,
help='path to the metadata file of the record')
parser_meta.set_defaults(func=addMetadata)
parser_pub = subparsers.add_parser('pub', help='publish the draft')
parser_pub.add_argument('-pi', '--rec_id', required=True, help='the b2share id of the record')
parser_pub.add_argument('-pi', '--rec_id', required=True,
help='the b2share id of the record')
parser_pub.set_defaults(func=publish)
parser_list_comm = subparsers.add_parser('listCommunities', help="list all communities with their names and id's")
parser_list_comm = subparsers.add_parser('listCommunities',
help="list all communities " +
"with their names and id's")
parser_list_comm.set_defaults(func=getAllCommunities)
parser_commSchema = subparsers.add_parser('communitySchema', help='get community schema')
parser_commSchema = subparsers.add_parser('communitySchema',
help='get community schema')
input_group = parser_commSchema.add_mutually_exclusive_group(required=True)
input_group.add_argument("-cn", "--commName", help="B2Share community name")
input_group.add_argument("-cn", "--commName",
help="B2Share community name")
input_group.add_argument("-ci", "--commID", help="B2Share community id")
parser_commSchema.set_defaults(func=getCommunitySchema)
parser_getDraft = subparsers.add_parser('getDraft', help='get draft and if there write it to the log file')
parser_getDraft.add_argument('-di', '--draft_id', required=True, help='the b2share id of the record')
parser_getDraft = subparsers.add_parser(
'getDraft', help='get draft and if there write it to the log file')
parser_getDraft.add_argument('-di', '--draft_id',
required=True,
help='the b2share id of the record')
parser_getDraft.set_defaults(func=getDraftByID)
parser_deleteDraft = subparsers.add_parser('deleteDraft', help='delete the draft')
parser_deleteDraft.add_argument('-ddi', '--draft_to_delete_id', required=True, help='the b2share id of the record')
parser_deleteDraft = subparsers.add_parser(
'deleteDraft', help='delete the draft')
parser_deleteDraft.add_argument(
'-ddi', '--draft_to_delete_id', required=True,
help='the b2share id of the record')
parser_deleteDraft.set_defaults(func=deleteDraft)
args = parser.parse_args()
......
#!/usr/bin/env python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
# Configuration Class #
################################################################################
import ConfigParser
#############################################################################
# Configuration Class #
#############################################################################
import configparser
import logging.handlers
import os
class Configuration():
"""
"""
Get properties from filesystem
"""
defaultLogDir = os.getcwd() + os.sep + "logs"
defaultLogFileName = os.sep + "b2share_connection.log"
irods_home_dir = ""
irods_debug = ""
def __init__(self, conffile, debug, dryrun, logger,
irodsenv=None):
self.conffile = conffile
......@@ -25,51 +26,59 @@ class Configuration():
self.logger = logger
self.access_token = ""
self.irodsenv = irodsenv
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG, \
'ERROR': logging.ERROR, 'WARNING': logging.WARNING, \
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG,
'ERROR': logging.ERROR, 'WARNING': logging.WARNING,
'CRITICAL': logging.CRITICAL}
self.config = ConfigParser.RawConfigParser()
# return configuration missing exception if expected config is not in the config file
self.config = configparser.RawConfigParser()
# return configuration
# missing exception
# if expected config is not in the config file
def parseConf(self):
"""Parse the configuration file."""
#self.config = ConfigParser.RawConfigParser()
# self.config = ConfigParser.RawConfigParser()
if not os.path.exists(self.conffile):
self.logger.error('missing configuration file %s:' % (self.conffile))
return # error config file not found
self.logger.error('missing configuration file %s:' %
(self.conffile))
return # error config file not found
with open(self.conffile, "r") as confFile:
try:
self.config.readfp(confFile)
except Exception as e:
self.configuration.logger.error(e)
self.logger.error(e)
return
logfilepath = self._getConfOption('Logging', 'log_file')
if not os.path.exists(logfilepath):
# if user did not specified a path for log file default to current dir + b2share_connection.log
# self.logger.error("no location for log file is specified, so try to default to <currend directory>/logs/b2share_connection.log")
try:
# if user did not specified a path for log file
# default to current dir + b2share_connection.log
# self.logger.error("no location for log file is specified,
# so try to default to
# <currend directory>/logs/b2share_connection.log")
try:
os.mkdir(self.defaultLogDir)
logfilepath = self.defaultLogDir + self.defaultLogFileName
except OSError:
# self.logger.error("no location for log file is specified, so try to default to <currend directory>/logs/b2share_connection.log")
logfilepath = os.getcwd()+ self.defaultLogFileName
#print("Creation of the directory %s failed, so defaulting to <currend directory>/b2share_connection.log" % defaultLogDir)
logfilepath = self.defaultLogDir + self.defaultLogFileName
except OSError:
# self.logger.error("no location for log file is specified,
# so try to default to
# <currend directory>/logs/b2share_connection.log")
logfilepath = os.getcwd() + self.defaultLogFileName
# print("Creation of the directory %s failed,
# so defaulting to <currend directory>/b2share_connection.log"
# % defaultLogDir)
loglevel = self._getConfOption('Logging', 'log_level')
if self.debug:
loglevel = 'DEBUG'
self.logger.setLevel(self.log_level[loglevel])
rfh = logging.handlers.RotatingFileHandler(logfilepath, \
maxBytes=8388608, \
rfh = logging.handlers.RotatingFileHandler(logfilepath,
maxBytes=8388608,
backupCount=9)
formatter = logging.Formatter('%(asctime)s %(levelname)s: '
+ '[%(funcName)s] %(message)s')
formatter = logging.Formatter('%(asctime)s %(levelname)s: ' +
'[%(funcName)s] %(message)s')
rfh.setFormatter(formatter)
self.logger.addHandler(rfh)
......@@ -77,12 +86,17 @@ class Configuration():
self.b2share_addr = self._getConfOption('B2SHARE', 'address')
self.b2share_path = self._getConfOption('B2SHARE', 'path')
self.b2share_host_name = self._getConfOption('B2SHARE_HTTP_API', 'host_name')
self.list_communities_endpoint = self._getConfOption('B2SHARE_HTTP_API', 'list_communities_endpoint')
self.records_endpoint = self._getConfOption('B2SHARE_HTTP_API', 'records_endpoint')
self.access_parameter = self._getConfOption('B2SHARE_HTTP_API', 'access_parameter')
self.get_community_schema_endpoint = self._getConfOption('B2SHARE_HTTP_API', 'get_community_schema_endpoint')
self.b2share_host_name = self._getConfOption(
'B2SHARE_HTTP_API', 'host_name')
self.list_communities_endpoint = self._getConfOption(
'B2SHARE_HTTP_API', 'list_communities_endpoint')
self.records_endpoint = self._getConfOption(
'B2SHARE_HTTP_API', 'records_endpoint')
self.access_parameter = self._getConfOption(
'B2SHARE_HTTP_API', 'access_parameter')
self.get_community_schema_endpoint = self._getConfOption(
'B2SHARE_HTTP_API', 'get_community_schema_endpoint')
self.irods_home_dir = self._getConfOption('iRODS', 'irods_home_dir')
self.irods_debug = self._getConfOption('iRODS', 'irods_debug', True)
......@@ -96,11 +110,13 @@ class Configuration():
if boolean:
if opt in ['True', 'true']:
return True
else:
else:
return False
return opt
else:
self.logger.error('missing parameter %s: in section %s' % (section, option))
else:
self.logger.error('missing section %s: with parameter %s' % (section, option))
self.logger.error(
'missing parameter %s: in section %s' % (section, option))
else:
self.logger.error(
'missing section %s: with parameter %s' % (section, option))
return None
#!/usr/bin/env python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import os
import logging.handlers
import argparse
import ConfigParser
import configparser
import tempfile
from manifest import IRODSUtils
from manifest.irodsUtility import IRODSUtils
logger = logging.getLogger('create_md_schema')
class Configuration():
"""
"""
Get properties from filesystem
"""
def __init__(self, conffile, debug, dryrun, logger):
self.conffile = conffile
self.debug = debug
self.dryrun = dryrun
self.logger = logger
self.access_token = ""
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG, \
'ERROR': logging.ERROR, 'WARNING': logging.WARNING, \
self.log_level = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG,
'ERROR': logging.ERROR, 'WARNING': logging.WARNING,
'CRITICAL': logging.CRITICAL}
def parseConf(self):
"""Parse the configuration file."""
self.config = ConfigParser.RawConfigParser()
self.config = configparser.RawConfigParser()
with open(self.conffile, "r") as confFile:
self.config.readfp(confFile)
logfilepath = self._getConfOption('Logging', 'log_file')
loglevel = self._getConfOption('Logging', 'log_level')
if self.debug:
loglevel = 'DEBUG'
logger.setLevel(self.log_level[loglevel])
rfh = logging.handlers.RotatingFileHandler(logfilepath, \
maxBytes=50000000, \
rfh = logging.handlers.RotatingFileHandler(logfilepath,
maxBytes=50000000,
backupCount=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s: '
+ '[%(funcName)s] %(message)s')
formatter = logging.Formatter('%(asctime)s %(levelname)s: ' +
'[%(funcName)s] %(message)s')
rfh.setFormatter(formatter)
logger.addHandler(rfh)
self.b2share_host_name = self._getConfOption('B2SHARE_HTTP_API', 'host_name')
self.list_communities_endpoint = self._getConfOption('B2SHARE_HTTP_API', 'list_communities_endpoint')
self.access_parameter = self._getConfOption('B2SHARE_HTTP_API', 'access_parameter')
self.get_community_schema_endpoint = self._getConfOption('B2SHARE_HTTP_API', 'get_community_schema_endpoint')
self.b2share_host_name = self._getConfOption(
'B2SHARE_HTTP_API', 'host_name')
self.list_communities_endpoint = self._getConfOption(
'B2SHARE_HTTP_API', 'list_communities_endpoint')
self.access_parameter = self._getConfOption(
'B2SHARE_HTTP_API', 'access_parameter')
self.get_community_schema_endpoint = self._getConfOption(
'B2SHARE_HTTP_API', 'get_community_schema_endpoint')
self.irods_zone_name = self._getConfOption('iRODS', 'zone_name')
self.irods_res = self._getConfOption('iRODS', 'resources')
self.irods_home_dir = self._getConfOption('iRODS', 'irods_home_dir')
self.irods_debug = self._getConfOption('iRODS', 'irods_debug', True)
def _getConfOption(self, section, option, boolean=False):
"""
get the options from the configuration file
......@@ -66,24 +70,27 @@ class Configuration():
if (self.config.has_option(section, option)):
opt = self.config.get(section, option)
if boolean:
if opt in ['True', 'true']: return True
else: return False
if opt in ['True', 'true']:
return True
else:
return False
return opt
else:
self.logger.warning('missing parameter %s:%s' % (section,option))
self.logger.warning('missing parameter %s:%s' % (section, option))
return None
def getAllCommunities(configuration):