# coding:utf-8
"""
REST API for main
Copyright (C) 2020 JASRI All Rights Reserved.
"""
import requests
import os
import sys
from os.path import expanduser
from . import config
from . import util
from . import rest_auth
from . import rest_proposal
from . import rest_register
from . import rest_data
from . import rest_download
from . import rest_metadata
from . import rest_delete
requests.packages.urllib3.disable_warnings()
[docs]class Repository():
def __init__(self, access_token=None,
local_storage_path=None,
facility=None,
class_name=None,
disk_name=None):
self.__access_token = access_token
self.__local_storage_path = local_storage_path
self.__facility = facility
self.__class_name = class_name
self.__disk_name = disk_name
self.__benten_agent_uri = util.benten_agent_uri()
self.__benten_uploader_enable = util.benten_uploader_enable()
self.__benten_uploader_uri = util.benten_uploader_uri()
self.__benten_uploader_secure_data = util.benten_uploader_secure_data()
self.auth = rest_auth.Main(self)
self.proposal = rest_proposal.Main(self)
self.register = rest_register.Main(self)
self.data = rest_data.Main(self)
self.download = rest_download.Main(self)
self.metadata = rest_metadata.Main(self)
self.delete = rest_delete.Main(self)
[docs] def init(self):
util.log("### Repository::init() ###")
# check BENTEN_WORKDIR
benten_workdir = os.getenv(
"BENTEN_WORKDIR", "{}/.benten".format(expanduser("~")))
util.log("BENTEN_WORKDIR = {}".format(benten_workdir))
util.makedirs(benten_workdir)
util.log("current benten_agent_uri = {}".format(self.__benten_agent_uri))
value = self.__benten_agent_uri.split("//")[1]
benten_agent_host = value.split(":")[0]
benten_agent_port = value.split(":")[1]
try:
vinput = raw_input(
"benten_agent_host[default={}]: ".format(benten_agent_host))
except:
vinput = input(
"benten_agent_host[default={}]: ".format(benten_agent_host))
if vinput != "":
benten_agent_host = vinput
try:
vinput = raw_input(
"benten_agent_port[default={}]: ".format(benten_agent_port))
except:
vinput = input(
"benten_agent_port[default={}]: ".format(benten_agent_port))
if vinput != "":
try:
benten_agent_port = int(vinput)
except:
message = "illegal value({}) for benten_agent_port (not INT)".format(vinput)
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
if benten_agent_port < 0 or benten_agent_port > 65535:
message = "illegal range({}) for benten_agent_port".format(benten_agent_port)
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
util.log("")
util.log("### configurations parameters ###")
util.log("benten_agent_host = {}".format(benten_agent_host))
util.log("benten_agent_port = {}".format(benten_agent_port))
util.log("")
try:
vinput = raw_input(
">> please type 'y' or 'yes' to set parameters :")
except:
vinput = input(">> please type 'y' or 'yes' to set parameters :")
if vinput not in ["y", "yes"]:
util.log("==> configuration parameters were NOT updated")
return
benten_agent_uri = "https://{}:{}".format(benten_agent_host, benten_agent_port)
fname = util.benten_config_filename()
try:
vdict = util.load_yaml(fname)
except:
vdict = {}
vdict["benten_agent_uri"] = benten_agent_uri
util.out_yaml(vdict, filename=fname)
util.log("==> configuration parameters were updated")
[docs] def init_uploader(self):
util.log("### Repository::init_uploader() ###")
# check BENTEN_WORKDIR
benten_workdir = os.getenv(
"BENTEN_WORKDIR", "{}/.benten".format(expanduser("~")))
util.log("BENTEN_WORKDIR = {}".format(benten_workdir))
util.makedirs(benten_workdir)
util.log("current benten_uploader_uri = {}".format(self.__benten_uploader_uri))
value = self.__benten_uploader_uri.split("//")[1]
benten_uploader_host = value.split(":")[0]
benten_uploader_port = value.split(":")[1]
benten_uploader_enable = self.__benten_uploader_enable
benten_uploader_secure_data = self.__benten_uploader_secure_data
try:
vinput = raw_input(
"benten_uploader_host[default={}]: ".format(benten_uploader_host))
except:
vinput = input(
"benten_uploader_host[default={}]: ".format(benten_uploader_host))
if vinput != "":
benten_uploader_host = vinput
try:
vinput = raw_input(
"benten_uploader_port[default={}]: ".format(benten_uploader_port))
except:
vinput = input(
"benten_uploader_port[default={}]: ".format(benten_uploader_port))
if vinput != "":
try:
benten_uploader_port = int(vinput)
except:
message = "illegal value({}) for benten_uploader_port (not INT)".format(vinput)
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
if benten_uploader_port < 0 or benten_uploader_port > 65535:
message = "illegal range({}) for benten_uploader_port" % benten_uploader_port
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
try:
vinput = raw_input(
"benten_uploader_enable[default={}] (False/True): ".format(benten_uploader_enable))
except:
vinput = input(
"benten_uploader_enable[default={}] (False/True): ".format(benten_uploader_enable))
if vinput != "":
if vinput not in ["True", "False"]:
message = "illegal value({}) for benten_uploader_enable".format(vinput)
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
if vinput == "True":
benten_uploader_enable = True
else:
benten_uploader_enable = False
try:
vinput = raw_input(
"benten_uploader_secure_data[default={}] (False/True): ".format(benten_uploader_secure_data))
except:
vinput = input(
"benten_uploader_secure_data[default={}] (False/True): ".format(benten_uploader_secure_data))
if vinput != "":
if vinput not in ["True", "False"]:
message = "illegal value({}) for benten_uploader_secure_data".format(vinput)
raise util.Error(message, domain=util.error_domain(
__file__, sys._getframe(), self.__class__))
if vinput == "True":
benten_uploader_secure_data = True
else:
benten_uploader_secure_data = False
util.log("")
util.log("### configurations parameters ###")
util.log("benten_uploader_host = {}".format(benten_uploader_host))
util.log("benten_uploader_port = {}".format(benten_uploader_port))
util.log("benten_uploader_enable = {}".format(benten_uploader_enable))
util.log("benten_uploader_secure_data = {}".format(benten_uploader_secure_data))
util.log("")
try:
vinput = raw_input(
">> please type 'y' or 'yes' to set parameters :")
except:
vinput = input(">> please type 'y' or 'yes' to set parameters :")
if vinput not in ["y", "yes"]:
util.log("==> configuration parameters were NOT updated")
return
benten_uploader_uri = "ftps://{}:{}".format(
benten_uploader_host, benten_uploader_port)
fname = util.benten_config_filename()
try:
vdict = util.load_yaml(fname)
except:
vdict = {}
vdict["benten_uploader_uri"] = benten_uploader_uri
vdict["benten_uploader_enable"] = benten_uploader_enable
vdict["benten_uploader_secure_data"] = benten_uploader_secure_data
util.out_yaml(vdict, filename=fname)
util.log("==> configuration parameters were updated")
[docs] def endpoint(self, path):
return self.__benten_agent_uri + path
[docs] def access_token(self):
return self.__access_token
[docs] def local_storage_path(self):
return self.__local_storage_path
[docs] def facility(self):
return self.__facility
[docs] def class_name(self):
return self.__class_name
[docs] def disk_name(self):
return self.__disk_name
[docs] def authorize(self, v, debug=True):
self.set_access_token(v)
if debug:
util.log("[Repository::authorize] access_token = {}".format(v))
[docs] def set_access_token(self, v):
self.__access_token = v
[docs] def set_local_storage_path(self, v):
self.__local_storage_path = v
[docs] def set_facility(self, v):
self.__facility = v
[docs] def set_class_name(self, v):
self.__class_name = v
[docs] def set_disk_name(self, v):
self.__disk_name = v