Esempio n. 1
0
    def __init__(self, unix_username, cw_session_ids, login, cw_instance_names,
                 cw_repositories, base_dir):
        """ Initialize the CubicWebConchUser class.

        Parameters
        ----------
        unix_username: str (mandatory)
            the sftp server will read file system with the permission
            associated to this user.            
        cw_session_ids: list of str (mandatory)
            the cubicweb sessions identifiers.
        login: str (mandatory)
            the cubicweb user login.
        cw_instance_names: list of str (mandatory)
            the cubicweb instance names.
        cw_repositories: cubicweb.server.repository.Repository (mandatory)
            internal cubicweb connections.
        base_dir: str (mandatory)
            base directory in which file are stored (it acts as
            mask, so every files outside this base_dir will be
            invisible).
        """
        # Inheritance
        UnixConchUser.__init__(self, unix_username)

        # Class parameters
        self.login = login

        # create the session associated to each repository
        self.cw_sessions = []
        self.cw_users = []
        self.instance_names = []
        for cnt, item in enumerate(
                zip(cw_session_ids, cw_repositories, cw_instance_names)):

            # Unpack items
            sessionid, repo, instance_name = item

            # Get the corresponding cw session
            session = repo._get_session(sessionid)  # XXX private method
            session.set_cnxset()
            self.cw_sessions.append(session)

            # Get the user entity eid
            login_eid = session.execute(
                "Any X WHERE X is CWUser, X login '{0}'".format(login))
            self.cw_users.append(login_eid[0][0])

            # Store the instance name: assume the name is unique
            self.instance_names.append(instance_name)

        # Create a Search object that provides tools to filter the CWSearch
        # elements
        search_filter = Search(self.cw_sessions, cwusers=self.cw_users)

        # Create an object to translate the paths contained in the CWSearch
        # elements
        self.path_translator = VirtualPathTranslator(search_filter)
        self.path_translator.BASE_REAL_DIR = base_dir
        self.path_translator.INSTANCE_NAMES = self.instance_names
Esempio n. 2
0
    def get_files(self, virtpath, session_index):
        """ Return a list of file associated to CWSearch named 'search_name'
        including rset file which is a pure virtual file.

        Parameters
        ----------
        virtpath: VirtualPath  (mandatory)
            a virtual path of the form (search name, search relpath,
            search basedir, search instance).
        session_index: int (mandatory)
            an index pointing to the instance of interest.

        Returns
        -------
        filepaths: list of 2-uplet (mandatory)
            a list of files formated in a 2-uplet of the form (path, is_virtual).
        """
        # Get the selected session and user
        session = self.cwsessions[session_index]
        cwuser = self.cwusers[session_index]

        # Get all the user CWSearch entities
        rset = session.execute('Any D WHERE S is CWSearch, S title %(title)s, '
                               'S owned_by %(cwuser)s, '
                               'S result F, F data D',
                               {'title': virtpath.search_name,
                                'cwuser': cwuser})

        # Reorganize the file paths
        filepaths = map(lambda x: (x, False),
                        json.loads(rset[0][0].getvalue())["files"])

        # Add the rset to the build tree, add the appropriate
        # file extension
        rset = session.execute('Any T WHERE S is CWSearch, S title %(title)s, '
                               'S rset_type T',
                               {'title': virtpath.search_name})
        fext = VID_TO_EXT[rset[0][0]]
        filepaths.append((osp.join(virtpath.search_basedir,
                          u"request_result" + fext), True))

        return filepaths
Esempio n. 3
0
    def get_file_data(self, file_eid, rset_file, session_index,
                      search_name=None):
        """ Get the Binary data contain in an entity.

        Parameters
        ----------
        file_eid: int (mandatory)
            the eid of the entity containing a 'data' Binary attribute.
        rset_file: bool (mandatory)
            True if we want the rset virtual file associated to the CWSearch,
            False otherwise.
        session_index: int (mandatory)
            an index pointing to the instance of interest.
        search_name: string (optional, default None)
            

        Returns
        -------
        out: Binary or None
            the desired Binary data object.
        """
        # Get the selected session and user
        session = self.cwsessions[session_index]
        cwuser = self.cwusers[session_index]

        if rset_file:
            rset = session.execute('Any D WHERE F is File, '
                                   'S is CWSearch, S title %(title)s, '
                                   'S owned_by %(cwuser)s, S rset F, '
                                   'F data D',
                                   {'title': search_name,
                                    'cwuser': cwuser})
        else:
            rset = session.execute('Any D WHERE F is File, '
                                   'F eid %(eid)s, F data D',
                                   {'eid': file_eid})
        if rset:
            return rset[0][0]