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

import os
from functools import lru_cache

from irods.access import iRODSAccess
from irods.models import User, UserGroup
from irods import exception as iexceptions
from rapydo.exceptions import RestApiException

from rapydo.rest.definition import EndpointResource
from rapydo import decorators as decorate
from rapydo.utils.logs import get_logger
log = get_logger(__name__)


class IrodsException(RestApiException):
    pass


class RPC(EndpointResource):

    @decorate.catch_error(exception=IrodsException, catch_generic=False)
    def get(self, irods_location=None):

        # from irods.models import User
        # results = self.rpc.query(User.name).all()
        # log.pp(self.rpc)
        # print("IRODS", self.rpc)
        home = "/%s/home/%s" % (self.rpc.zone, self.rpc.username)

        dirname = home + "/pippo"
        # dirname2 = home + "/clarabella"
        filename = home + "/pippo/pluto.txt"
        filename2 = home + "/pippo/topolino.txt"
        # filename3 = home + "/pippo/paperino.txt"

        self.create_empty(dirname, directory=True, ignore_existing=True)
        self.create_empty(filename, directory=False, ignore_existing=True)

        self.set_inheritance(dirname, inheritance=True, recursive=False)
        self.set_permissions(dirname, "read", "guest", recursive=True)
        self.set_permissions(filename, "write", "guest", recursive=False)

        self.copy(filename, filename2, recursive=False, force=True)
        # self.copy(dirname, dirname2, recursive=True, force=False)

        # self.move(filename2, filename3)

        self.write_file_content(filename, "pippo pluto e topolino\nE Orazio?")
        self.open(filename, "/tmp/mytext.txt")
        self.save("/tmp/mytext.txt", home + "/prova.txt", force=True)

        out = self.get_file_content(home + "/prova.txt")

        out = self.list(path=home, recursive=True, detailed=True, acl=True)

        out = self.get_user_info("guest")
        out = str(self.user_has_group("guest", "public2"))
        _, out = self.check_user_exists("guest", "public2")

        out, _ = self.get_metadata(filename)
        print(out.get("PID"))
        self.remove(filename)
        self.remove(dirname, recursive=True)
        return out

# ##################################
# ##################################
# Copied as they are
# ##################################
# ##################################

    def get_collection_from_path(self, absolute_path):
        return os.path.dirname(absolute_path)

    def get_absolute_path(self, *args, root=None):
        if len(args) < 1:
            return root
        if root is None and not args[0].startswith('/'):
            root = '/'
        return os.path.join(root, *args)

    # def check_certificate_validity(self, certfile, validity_interval=1):
    #     args = ["x509", "-in", certfile, "-text"]
    #     output = self.execute_command("openssl", args)

    #     pattern = re.compile(
    #         r"Validity.*\n\s*Not Before: (.*)\n" +
    #         r"\s*Not After *: (.*)")
    #     validity = pattern.search(output).groups()<commit_msg>Ready to be tested with b2handle<commit_after># -*- coding: utf-8 -*-

import os
from functools import lru_cache

from irods.access import iRODSAccess
from irods.models import User, UserGroup
from irods import exception as iexceptions
from rapydo.exceptions import RestApiException

from rapydo.rest.definition import EndpointResource
from rapydo import decorators as decorate
from rapydo.utils.logs import get_logger
log = get_logger(__name__)


class IrodsException(RestApiException):
    pass


class RPC(EndpointResource):

    @decorate.catch_error(exception=IrodsException, catch_generic=False)
    def get(self, irods_location=None):

        # from irods.models import User
        # results = self.rpc.query(User.name).all()
        # log.pp(self.rpc)
        home = "/%s/home/%s" % (self.rpc.zone, self.rpc.username)
        print("IRODS", self.rpc, home)

        from b2handle.searcher import Searcher
        print("TEST B2HANDLE", Searcher)

        # dirname = home + "/pippo"
        # # dirname2 = home + "/clarabella"
        # filename = home + "/pippo/pluto.txt"
        # filename2 = home + "/pippo/topolino.txt"
        # # filename3 = home + "/pippo/paperino.txt"

        # self.create_empty(dirname, directory=True, ignore_existing=True)
        # self.create_empty(filename, directory=False, ignore_existing=True)

        # self.set_inheritance(dirname, inheritance=True, recursive=False)
        # self.set_permissions(dirname, "read", "guest", recursive=True)
        # self.set_permissions(filename, "write", "guest", recursive=False)

        # self.copy(filename, filename2, recursive=False, force=True)
        # # self.copy(dirname, dirname2, recursive=True, force=False)

        # # self.move(filename2, filename3)

        # self.write_file_content(filename, "pippo pluto e topolino\nE Orazio?")
        # self.open(filename, "/tmp/mytext.txt")
        # self.save("/tmp/mytext.txt", home + "/prova.txt", force=True)

        # out = self.get_file_content(home + "/prova.txt")

        # out = self.list(path=home, recursive=True, detailed=True, acl=True)

        # out = self.get_user_info("guest")
        # out = str(self.user_has_group("guest", "public2"))
        # _, out = self.check_user_exists("guest", "public2")

        # out, _ = self.get_metadata(filename)
        # print(out.get("PID"))
        # self.remove(filename)
        # self.remove(dirname, recursive=True)
        # return out

        return "Hello"

# ##################################
# ##################################
# Copied as they are
# ##################################
# ##################################

    def get_collection_from_path(self, absolute_path):
        return os.path.dirname(absolute_path)

    def get_absolute_path(self, *args, root=None):
        if len(args) < 1:
            return root
        if root is None and not args[0].startswith('/'):
            root = '/'
        return os.path.join(root, *args)

    # def check_certificate_validity(self, certfile, validity_interval=1):
    #     args = ["x509", "-in", certfile, "-text"]
    #     output = self.execute_command("openssl", args)

    #     pattern = re.compile(
    #         r"Validity.*\n\s*Not Before: (.*)\n" +
    #         r"\s*Not After *: (.*)")
    #     validity = pattern.search(output).groups()
{'fraction_non_alphanumeric': 0.16852540272614622, 'fraction_numerical': 0.005421313506815365, 'mean_word_length': 3.1075063613231553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478535', 'n_tokens_mistral': 2089, 'n_tokens_neox': 2006, 'n_words': 404}