Пример #1
0
    def get_children_under_parent_id(self,
                                     parent_id,
                                     query_contains_string=None,
                                     query_is_string=None,
                                     max_results=None):

        _logger.info("Getting client for child-listing.")

        client = self.__auth.get_client()

        if query_contains_string and query_is_string:
            _logger.exception("The query_contains_string and query_is_string "
                              "parameters are mutually exclusive.")
            raise

        if query_is_string:
            query = ("title='%s'" %
                     (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query = ("title contains '%s'" %
                     (escape_filename_for_query(query_contains_string)))
        else:
            query = None

        _logger.info(
            "Listing entries under parent with ID [%s].  QUERY= "
            "[%s]", parent_id, query)

        response = client.children().list(q=query,
                                          folderId=parent_id,
                                          maxResults=max_results).execute()

        self.__assert_response_kind(response, 'drive#childList')

        return [entry[u'id'] for entry in response[u'items']]
Пример #2
0
    def get_children_under_parent_id(self,
                                     parent_id,
                                     query_contains_string=None,
                                     query_is_string=None,
                                     max_results=None):

        _logger.info("Getting client for child-listing.")

        client = self.__auth.get_client()

        if query_contains_string and query_is_string:
            _logger.exception("The query_contains_string and query_is_string "
                              "parameters are mutually exclusive.")
            raise

        if query_is_string:
            query = ("title='%s'" % 
                     (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query = ("title contains '%s'" % 
                     (escape_filename_for_query(query_contains_string)))
        else:
            query = None

        _logger.info("Listing entries under parent with ID [%s].  QUERY= "
                     "[%s]", parent_id, query)

        response = client.children().list(
                    q=query, 
                    folderId=parent_id,
                    maxResults=max_results).execute()

        self.__assert_response_kind(response, 'drive#childList')

        return [ entry[u'id'] for entry in response[u'items'] ]
Пример #3
0
    def get_children_under_parent_id(self, \
                                     parent_id, \
                                     query_contains_string=None, \
                                     query_is_string=None, \
                                     max_results=None):

        self.__log.info("Getting client for child-listing.")

        try:
            client = self.get_client()
        except:
            self.__log.exception(
                "There was an error while acquiring the Google "
                "Drive client (get_children_under_parent_id).")
            raise

        if query_contains_string and query_is_string:
            self.__log.exception(
                "The query_contains_string and query_is_string "
                "parameters are mutually exclusive.")
            raise

        if query_is_string:
            query = ("title='%s'" %
                     (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query = ("title contains '%s'" %
                     (escape_filename_for_query(query_contains_string)))
        else:
            query = None

        self.__log.info("Listing entries under parent with ID [%s].  QUERY= "
                        "[%s]" % (parent_id, query))

        try:
            response = client.children().list(q=query, folderId=parent_id, \
                                              maxResults=max_results). \
                                              execute()
        except:
            self.__log.exception("Problem while listing files.")
            raise

        return [entry[u'id'] for entry in response[u'items']]
Пример #4
0
    def get_children_under_parent_id(self, \
                                     parent_id, \
                                     query_contains_string=None, \
                                     query_is_string=None, \
                                     max_results=None):

        self.__log.info("Getting client for child-listing.")

        try:
            client = self.get_client()
        except:
            self.__log.exception("There was an error while acquiring the Google "
                              "Drive client (get_children_under_parent_id).")
            raise

        if query_contains_string and query_is_string:
            self.__log.exception("The query_contains_string and query_is_string "
                              "parameters are mutually exclusive.")
            raise

        if query_is_string:
            query = ("title='%s'" % 
                     (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query = ("title contains '%s'" % 
                     (escape_filename_for_query(query_contains_string)))
        else:
            query = None

        self.__log.info("Listing entries under parent with ID [%s].  QUERY= "
                     "[%s]" % (parent_id, query))

        try:
            response = client.children().list(q=query, folderId=parent_id, \
                                              maxResults=max_results). \
                                              execute()
        except:
            self.__log.exception("Problem while listing files.")
            raise

        return [ entry[u'id'] for entry in response[u'items'] ]
Пример #5
0
    def list_files(self,
                   query_contains_string=None,
                   query_is_string=None,
                   parent_id=None):

        self.__log.info(
            "Listing all files. CONTAINS=[%s] IS=[%s] "
            "PARENT_ID=[%s]" %
            (query_contains_string if query_contains_string is not None else
             '<none>', query_is_string if query_is_string is not None else
             '<none>', parent_id if parent_id is not None else '<none>'))

        try:
            client = self.get_client()
        except:
            self.__log.exception("There was an error while acquiring the "
                                 "Google Drive client (list_files).")
            raise

        query_components = []

        if parent_id:
            query_components.append("'%s' in parents" % (parent_id))

        if query_is_string:
            query_components.append(
                "title='%s'" % (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query_components.append(
                "title contains '%s'" %
                (escape_filename_for_query(query_contains_string)))

        # Make sure that we don't get any entries that we would have to ignore.

        hidden_flags = Conf.get('hidden_flags_list_remote')
        if hidden_flags:
            for hidden_flag in hidden_flags:
                query_components.append("%s = false" % (hidden_flag))

        query = ' and '.join(query_components) if query_components else None

        page_token = None
        page_num = 0
        entries = []
        while 1:
            self.__log.debug("Doing request for listing of files with page-"
                             "token [%s] and page-number (%d): %s" %
                             (page_token, page_num, query))

            try:
                result = client.files().list(q=query, pageToken=page_token).\
                            execute()
            except:
                self.__log.exception("Could not get the list of files.")
                raise

            self.__log.debug("(%d) entries were presented for page-number "
                             "(%d)." % (len(result[u'items']), page_num))

            for entry_raw in result[u'items']:
                try:
                    entry = NormalEntry('list_files', entry_raw)
                except:
                    self.__log.exception(
                        "Could not normalize raw-data for entry "
                        "with ID [%s]." % (entry_raw[u'id']))
                    raise

                entries.append(entry)

            if u'nextPageToken' not in result:
                self.__log.debug("No more pages in file listing.")
                break

            self.__log.debug("Next page-token in file-listing is [%s]." %
                             (result[u'nextPageToken']))
            page_token = result[u'nextPageToken']
            page_num += 1

        return entries
Пример #6
0
    def list_files(self, query_contains_string=None, query_is_string=None, 
                   parent_id=None):
        
        self.__log.info("Listing all files. CONTAINS=[%s] IS=[%s] "
                        "PARENT_ID=[%s]" % 
                        (query_contains_string 
                            if query_contains_string is not None 
                            else '<none>', 
                         query_is_string 
                            if query_is_string is not None 
                            else '<none>', 
                         parent_id if parent_id is not None 
                                   else '<none>'))

        try:
            client = self.get_client()
        except:
            self.__log.exception("There was an error while acquiring the "
                                 "Google Drive client (list_files).")
            raise

        query_components = []

        if parent_id:
            query_components.append("'%s' in parents" % (parent_id))

        if query_is_string:
            query_components.append("title='%s'" % 
                                    (escape_filename_for_query(query_is_string)))
        elif query_contains_string:
            query_components.append("title contains '%s'" % 
                                    (escape_filename_for_query(query_contains_string)))

        # Make sure that we don't get any entries that we would have to ignore.

        hidden_flags = Conf.get('hidden_flags_list_remote')
        if hidden_flags:
            for hidden_flag in hidden_flags:
                query_components.append("%s = false" % (hidden_flag))

        query = ' and '.join(query_components) if query_components else None

        page_token = None
        page_num = 0
        entries = []
        while 1:
            self.__log.debug("Doing request for listing of files with page-"
                             "token [%s] and page-number (%d): %s" % 
                             (page_token, page_num, query))

            try:
                result = client.files().list(q=query, pageToken=page_token).\
                            execute()
            except:
                self.__log.exception("Could not get the list of files.")
                raise

            self.__log.debug("(%d) entries were presented for page-number "
                             "(%d)." % 
                             (len(result[u'items']), page_num))

            for entry_raw in result[u'items']:
                try:
                    entry = NormalEntry('list_files', entry_raw)
                except:
                    self.__log.exception("Could not normalize raw-data for entry "
                                         "with ID [%s]." % (entry_raw[u'id']))
                    raise

                entries.append(entry)

            if u'nextPageToken' not in result:
                self.__log.debug("No more pages in file listing.")
                break

            self.__log.debug("Next page-token in file-listing is [%s]." % (result[u'nextPageToken']))
            page_token = result[u'nextPageToken']
            page_num += 1

        return entries