Esempio n. 1
0
 def __init__(
         self,
         token=None,
         nexus_endpoint="https://nexus.humanbrainproject.org/v0",
         kg_query_endpoint="https://kg.humanbrainproject.eu/query",
         release_endpoint="https://kg.humanbrainproject.eu/api/releases",
         idm_endpoint="https://services.humanbrainproject.eu/idm/v1/api"):
     if token is None:
         if oauth_token_handler:
             token = oauth_token_handler.get_token()
         else:
             try:
                 token = os.environ["HBP_AUTH_TOKEN"]
             except KeyError:
                 raise AuthenticationError("No token provided.")
     ep = urlparse(nexus_endpoint)
     self.nexus_endpoint = nexus_endpoint
     auth_client = AccessTokenClient(token)
     self._nexus_client = NexusClient(scheme=ep.scheme,
                                      host=ep.netloc,
                                      prefix=ep.path[1:],
                                      alternative_namespace=nexus_endpoint,
                                      auth_client=auth_client)
     self._kg_query_client = HttpClient(kg_query_endpoint,
                                        "",
                                        auth_client=auth_client)
     self._release_client = HttpClient(release_endpoint,
                                       "",
                                       auth_client=auth_client,
                                       raw=True)
     self._idm_client = HttpClient(idm_endpoint,
                                   "",
                                   auth_client=auth_client)
     self._instance_repo = self._nexus_client.instances
     self.cache = {}  # todo: use combined uri and rev as cache keys
    def __init__(self, username=None,
                 password=None,
                 job_service="https://nmpi.hbpneuromorphic.eu/api/v2/",
                 quotas_service="https://quotas.hbpneuromorphic.eu",
                 token=None,
                 verify=True):
        if password is None and token is None:
            if have_collab_token_handler:
                # if are we running in a Jupyter notebook within the Collaboratory
                # the token is already available
                token = oauth_token_handler.get_token()
            elif os.path.exists(TOKENFILE):  # check for a stored token
                with open(TOKENFILE) as fp:
                    token = json.load(fp).get(username, None)["access_token"]    
            else:
                # prompt for password
                password = getpass.getpass()
        
        self.username = username
        self.cert = None
        self.verify = verify
        self.token = token
        self.sleep_interval = 2.0
        (scheme, netloc, path, params, query, fragment) = urlparse(job_service)
        self.job_server = "%s://%s" % (scheme, netloc)
        self.quotas_server = quotas_service
        self.storage_client = None
        self.collab_source_folder = "source_code"  # remote folder into which code may be uploaded
        
        # if a token has been given, no need to authenticate
        if not self.token:
            self._hbp_auth(username, password)
        self.auth = HBPAuth(self.token)
        try:
            self._get_user_info()
        except Exception as err:
            if "invalid_token" in str(err):
                password = getpass.getpass()
                self._hbp_auth(username, password)
                self.auth = HBPAuth(self.token)
                self._get_user_info()
            else:
                raise

        with open(TOKENFILE, "w") as fp:
            json.dump({username: {"access_token": self.token}}, fp)

        # get schema
        req = requests.get(job_service, cert=self.cert, verify=self.verify, auth=self.auth)
        if req.ok:
            self._schema = req.json()
            self.resource_map = {name: entry["list_endpoint"]
                                 for name, entry in req.json().items()}
        else:
            self._handle_error(req)
Esempio n. 3
0
 def __init__(self,
              username=None,
              password=None,
              job_service="https://nmpi.hbpneuromorphic.eu/api/v2/",
              quotas_service="https://quotas.hbpneuromorphic.eu",
              token=None,
              verify=True):
     if password is None and token is None:
         if have_collab_token_handler:
             # if are we running in a Jupyter notebook within the Collaboratory
             # the token is already available
             token = oauth_token_handler.get_token()
         else:
             # prompt for password
             password = getpass.getpass()
     self.username = username
     self.cert = None
     self.verify = verify
     self.token = token
     (scheme, netloc, path, params, query, fragment) = urlparse(job_service)
     self.job_server = "%s://%s" % (scheme, netloc)
     self.quotas_server = quotas_service
     # if a token has been given, no need to authenticate
     if not self.token:
         self._hbp_auth(username, password)
     self.auth = HBPAuth(self.token)
     self._get_user_info()
     # get schema
     req = requests.get(job_service,
                        cert=self.cert,
                        verify=self.verify,
                        auth=self.auth)
     if req.ok:
         self._schema = req.json()
         self.resource_map = {
             name: entry["list_endpoint"]
             for name, entry in req.json().items()
         }
     else:
         self._handle_error(req)
Esempio n. 4
0
import os
from fairgraph.client import KGClient
from fairgraph.base import as_list
from fairgraph.brainsimulation import ModelProject
import numpy as np

try:
    from jupyter_collab_storage import oauth_token_handler
    token = oauth_token_handler.get_token()
except ModuleNotFoundError:
    try:
        token = os.environ["HBP_token"]
    except KeyError:
        print(
            'Not able to load the HBP token (neither through the jupyter_collab_storage module nor locally)'
        )


def check_if_alias_is_already_taken(name):

    print('comparing with KG database [...]')
    client = KGClient(token,
                      nexus_endpoint="https://nexus.humanbrainproject.org/v0")
    MODELS = ModelProject.list(client, size=10000)
    NAMES = [project.name for project in MODELS]
    if name in NAMES:
        i0 = int(np.argwhere(np.array(NAMES, dtype=str) == name))
        if type(MODELS[i0].authors) is list:
            author_list = ''
            for nn in list(MODELS[i0].authors):
                author_list += nn.resolve(client).full_name + '; '