<commit_before># -*- coding: utf-8 -*-

"""
B2SAFE HTTP REST API endpoints.

Code to implement the /api/resources endpoint

Note:
Endpoints list and behaviour are available at:
https://github.com/EUDAT-B2STAGE/http-api/blob/metadata_parser/docs/user/endpoints.md

"""

import os
import time
from flask import request, current_app

from eudat.apis.common import PRODUCTION, CURRENT_MAIN_ENDPOINT
from eudat.apis.common.b2stage import EudatEndpoint

from rapydo.services.uploader import Uploader
from flask_ext.flask_irods.client import IrodsException
from rapydo.utils import htmlcodes as hcodes
from rapydo import decorators as decorate
from rapydo.utils.logs import get_logger

log = get_logger(__name__)


###############################
# Classes

class BasicEndpoint(Uploader, EudatEndpoint):

    @decorate.catch_error(exception=IrodsException, exception_label='B2SAFE')
    def get(self, irods_location=None):
        """ Download file from filename """

        data = {}
        checksum = None

        if irods_location is None:
            return self.send_errors(
                'Location: missing filepath inside URI',
                code=hcodes.HTTP_BAD_REQUEST)
        irods_location = self.fix_location(irods_location)

        ###################
        # Init EUDAT endpoint resources
        r = self.init_endpoint()
        if r.errors is not None:
            return self.send_errors(errors=r.errors)

        # get parameters with defaults
        icom = r.icommands
        path, resource, filename, force = \
            self.get_file_parameters(icom, path=irods_location)

        is_collection = icom.is_collection(path)
        # Check if it's not a collection because the object does not exist
        if not is_collection:
            icom.get_dataobject(path)

        ###################
        # DOWNLOAD a specific file
        ###################

        # If download is True, trigger file download
        if hasattr(self._args, 'download'):
            if self._args.download and 'true' in self._args.download.lower():
                if is_collection:
                    return self.send_errors(
                        'Collection: recursive download is not allowed')
                else:
                    return self.download_object(r, path)

        ###################
        # DATA LISTING
        ###################

        EMPTY_RESPONSE = {}

        #####################
        # DIRECTORY
        if is_collection:
            collection = path
            data = icom.list(path=collection)
            if len(data) < 1:
                data = EMPTY_RESPONSE
            # Print content list if it's a collection
        #####################
        # FILE (or not existing)
        else:
            collection = icom.get_collection_from_path(path)
            current_filename = path[len(collection) + 1:]
            filelist = icom.list(path=collection)
            data = EMPTY_RESPONSE
            for filename, metadata in filelist.items():
                if filename == current_filename:
                    data[filename] = metadata
            # log.pp(data)

            # # Print file details/sys metadata if it's a specific file
            # data = icom.meta_sys_list(path)

            # if a path that does not exist
            if len(data) < 1:
                return self.send_errors(
                    "Path does not exists or you don't have privileges",
                    code=hcodes.HTTP_BAD_NOTFOUND)

        # Set the right context to each element
        response = []
        for filename, metadata in data.items():

            # Get iRODS checksum
            file_path = os.path.join(collection, filename)
            try:
                obj = icom.get_dataobject(file_path)
                checksum = obj.checksum
            except IrodsException:
                checksum = None

            # Get PID
            out = {}
            try:
                out, _ = icom.get_metadata(path)
            except IrodsException:
                pass

            metadata['PID'] = out.get("PID")
            metadata['checksum'] = checksum
            metadata.pop('path')
            content = {
                'metadata': metadata,
                metadata['object_type']: filename,<commit_msg>Bug fixing: get objects' PID listing a collection<commit_after># -*- coding: utf-8 -*-

"""
B2SAFE HTTP REST API endpoints.

Code to implement the /api/resources endpoint

Note:
Endpoints list and behaviour are available at:
https://github.com/EUDAT-B2STAGE/http-api/blob/metadata_parser/docs/user/endpoints.md

"""

import os
import time
from flask import request, current_app

from eudat.apis.common import PRODUCTION, CURRENT_MAIN_ENDPOINT
from eudat.apis.common.b2stage import EudatEndpoint

from rapydo.services.uploader import Uploader
from flask_ext.flask_irods.client import IrodsException
from rapydo.utils import htmlcodes as hcodes
from rapydo import decorators as decorate
from rapydo.utils.logs import get_logger

log = get_logger(__name__)


###############################
# Classes

class BasicEndpoint(Uploader, EudatEndpoint):

    @decorate.catch_error(exception=IrodsException, exception_label='B2SAFE')
    def get(self, irods_location=None):
        """ Download file from filename """

        data = {}
        checksum = None

        if irods_location is None:
            return self.send_errors(
                'Location: missing filepath inside URI',
                code=hcodes.HTTP_BAD_REQUEST)
        irods_location = self.fix_location(irods_location)

        ###################
        # Init EUDAT endpoint resources
        r = self.init_endpoint()
        if r.errors is not None:
            return self.send_errors(errors=r.errors)

        # get parameters with defaults
        icom = r.icommands
        path, resource, filename, force = \
            self.get_file_parameters(icom, path=irods_location)

        is_collection = icom.is_collection(path)
        # Check if it's not a collection because the object does not exist
        if not is_collection:
            icom.get_dataobject(path)

        ###################
        # DOWNLOAD a specific file
        ###################

        # If download is True, trigger file download
        if hasattr(self._args, 'download'):
            if self._args.download and 'true' in self._args.download.lower():
                if is_collection:
                    return self.send_errors(
                        'Collection: recursive download is not allowed')
                else:
                    return self.download_object(r, path)

        ###################
        # DATA LISTING
        ###################

        EMPTY_RESPONSE = {}

        #####################
        # DIRECTORY
        if is_collection:
            collection = path
            data = icom.list(path=collection)
            if len(data) < 1:
                data = EMPTY_RESPONSE
            # Print content list if it's a collection
        #####################
        # FILE (or not existing)
        else:
            collection = icom.get_collection_from_path(path)
            current_filename = path[len(collection) + 1:]
            filelist = icom.list(path=collection)
            data = EMPTY_RESPONSE
            for filename, metadata in filelist.items():
                if filename == current_filename:
                    data[filename] = metadata
            # log.pp(data)

            # # Print file details/sys metadata if it's a specific file
            # data = icom.meta_sys_list(path)

            # if a path that does not exist
            if len(data) < 1:
                return self.send_errors(
                    "Path does not exists or you don't have privileges",
                    code=hcodes.HTTP_BAD_NOTFOUND)

        # Set the right context to each element
        response = []
        for filename, metadata in data.items():

            # Get iRODS checksum
            file_path = os.path.join(collection, filename)
            try:
                obj = icom.get_dataobject(file_path)
                checksum = obj.checksum
            except IrodsException:
                checksum = None

            # Get PID
            out = {}
            try:
                out, _ = icom.get_metadata(file_path)
            except IrodsException:
                pass

            metadata['PID'] = out.get("PID")
            metadata['checksum'] = checksum
            metadata.pop('path')
            content = {
                'metadata': metadata,
                metadata['object_type']: filename,
{'fraction_non_alphanumeric': 0.11096953550330128, 'fraction_numerical': 0.0018533534113286227, 'mean_word_length': 1.9447476125511596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7422950', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2234, 'n_words': 608}