def from_data(data):
        """Return an Location constructed from the json-deserialised
           dictionary
        """
        if data is None or len(data) == 0:
            return Location()

        iden = Location()
        iden._drive_guid = data["drive_guid"]

        try:
            iden._encoded_filename = data["encoded_filename"]
        except:
            return iden

        from Acquire.ObjectStore import encoded_to_string \
            as _encoded_to_string
        iden._filename = _encoded_to_string(iden._encoded_filename)

        try:
            iden._version = data["version"]
        except:
            pass

        return iden
Exemple #2
0
    def list_drives(self, drive_uid=None):
        """Return a list of all of the top-level drives to which this user
           has access, or all of the sub-drives of the drive with
           passed 'drive_uid'
        """
        if self.is_null():
            return []

        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket
        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.ObjectStore import encoded_to_string as _encoded_to_string
        from Acquire.Storage import DriveMeta as _DriveMeta

        bucket = _get_service_account_bucket()

        if drive_uid is None:
            # look for the top-level drives
            names = _ObjectStore.get_all_object_names(
                bucket, "%s/%s" % (_drives_root, self._user_guid))
        else:
            # look for the subdrives
            names = _ObjectStore.get_all_object_names(
                bucket,
                "%s/%s/%s" % (_subdrives_root, self._user_guid, drive_uid))

        drives = []
        for name in names:
            drive_name = _encoded_to_string(name.split("/")[-1])
            drives.append(_DriveMeta(name=drive_name, container=drive_uid))

        return drives
    def list_accounts(self, bucket=None):
        """Return the names of all of the accounts in this group"""
        if bucket is None:
            bucket = _login_to_service_account()

        keys = _ObjectStore.get_all_object_names(bucket, self._root())

        accounts = []

        for key in keys:
            accounts.append(_encoded_to_string(key))

        return accounts
    def from_string(s):
        """Return an Location constructed from the passed string"""
        if s.startswith("acquire_file://"):
            parts = s.split("/")
            from Acquire.ObjectStore import encoded_to_string \
                as _encoded_to_string

            try:
                drive_guid = parts[-2]
                filename = _encoded_to_string(parts[-1])
                version = None
            except:
                drive_guid = parts[-4]
                filename = _encoded_to_string(parts[-3])
                version = "/".join([parts[-2], parts[-1]])

            return Location(drive_guid=drive_guid, filename=filename,
                            version=version)

        elif s.startswith("acquire_drive://"):
            parts = s.split("/")
            return Location(drive_guid=parts[-1])
        else:
            return Location()
    def list_accounts(self, bucket=None):
        """Return the names of all of the accounts in this group

            Args:
                bucket (dict, default=None): Bucket from which to load data

            Returns:
                :obj:`list`: List of names of the accounts in this group

        """
        self._assert_is_readable()

        if bucket is None:
            from Acquire.Service import get_service_account_bucket \
                as _get_service_account_bucket
            bucket = _get_service_account_bucket()

        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.ObjectStore import encoded_to_string \
            as _encoded_to_string

        keys = _ObjectStore.get_all_object_names(bucket, self._root())
        root_len = len(self._root())

        accounts = []

        for key in keys:
            try:
                account_key = key[root_len:]
                while account_key.startswith("/"):
                    account_key = account_key[1:]

                accounts.append(_encoded_to_string(account_key))
            except Exception as e:
                from Acquire.Accounting import AccountError
                raise AccountError(
                    "Unable to identify the account associated with key "
                    "'%s', equals '%s': %s" % (key, account_key, str(e)))

        return accounts
Exemple #6
0
    def list_files(self,
                   authorisation=None,
                   par=None,
                   identifiers=None,
                   include_metadata=False,
                   dir=None,
                   filename=None):
        """Return the list of FileMeta data for the files contained
           in this Drive. The passed authorisation is needed in case
           the list contents of this drive is not public.

           If 'dir' is specified, then only search for files in 'dir'.
           If 'filename' is specified, then only search for the
           file called 'filename'
        """
        (drive_acl,
         identifiers) = self._resolve_acl(authorisation=authorisation,
                                          resource="list_files",
                                          par=par,
                                          identifiers=identifiers)

        if par is not None:
            if par.location().is_file():
                dir = None
                filename = par.location().filename()
            elif not par.location().is_drive():
                raise PermissionError(
                    "You do not have permission to read the Drive")

        if not drive_acl.is_readable():
            raise PermissionError(
                "You don't have permission to read this Drive")

        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.ObjectStore import encoded_to_string as _encoded_to_string
        from Acquire.ObjectStore import string_to_encoded as _string_to_encoded
        from Acquire.Storage import FileMeta as _FileMeta

        metadata_bucket = self._get_metadata_bucket()

        if filename is not None:
            if dir is not None:
                filename = "%s/%s" % (dir, filename)

            key = "%s/%s/%s" % (_fileinfo_root, self._drive_uid,
                                _string_to_encoded(filename))

            names = [key]
        elif dir is not None:
            while dir.endswith("/"):
                dir = dir[0:-1]

            encoded_dir = _string_to_encoded(dir)

            while encoded_dir.endswith("="):
                encoded_dir = encoded_dir[0:-1]

            # remove the last two characters, as sometime uuencoding
            # will change the last characters so they don't match
            if len(encoded_dir) > 2:
                encoded_dir = encoded_dir[0:-2]
            else:
                encoded_dir = ""

            key = "%s/%s/%s" % (_fileinfo_root, self._drive_uid, encoded_dir)

            all_names = _ObjectStore.get_all_object_names(metadata_bucket, key)

            names = []

            dir = "%s/" % dir

            for name in all_names:
                decoded_name = _encoded_to_string(name.split("/")[-1])

                if decoded_name.startswith(dir):
                    names.append(name)
        else:
            key = "%s/%s" % (_fileinfo_root, self._drive_uid)
            names = _ObjectStore.get_all_object_names(metadata_bucket, key)

        files = []

        if include_metadata:
            # we need to load all of the metadata info for this file to
            # return to the user
            from Acquire.Storage import FileInfo as _FileInfo

            for name in names:
                try:
                    data = _ObjectStore.get_object_from_json(
                        metadata_bucket, name)
                    fileinfo = _FileInfo.from_data(data,
                                                   identifiers=identifiers,
                                                   upstream=drive_acl)
                    filemeta = fileinfo.get_filemeta()
                    file_acl = filemeta.acl()

                    if file_acl.is_readable() or file_acl.is_writeable():
                        files.append(filemeta)
                except:
                    pass
        else:
            for name in names:
                filename = _encoded_to_string(name.split("/")[-1])
                files.append(_FileMeta(filename=filename))

        return files