def _GetFileIdHelper( self, parent_folder_id, name): """Get id by query with parent folder's id and wanted file or folder's name. E.g. If path is b/c, and we pass in b's id and c's name. We will return c'id. Args: parent_folder_id: (e.g. root, some_hash_value). name: a file or folder's name (e.g. folderA, cat.img) Returns: subfolder's id: (e.g. some_hash_value) Raises: errors.PluginError: if the filename is not unique. errors.FileNotFoundError: if no file matched the subfolder's name or no matching file id was found. """ param = {} param['q'] = (_QUERY_ITEM_FORMAT % (parent_folder_id, name)) child_ids, _ = self._GetFileIds(param) if len(child_ids) >= 2: raise errors.PluginError(_DUPLICATED_OBJECT_NAME_ERROR % name) if not child_ids: raise errors.FileNotFoundError(_FILE_NOT_FOUND_ERROR % name) return child_ids[0]
def FindBuildChannel(url): """Find a build channel for a given URL. Args: url: a URL. Returns: (a build channel, a build item path) Raises: errors.PluginError: if a build channel ID is not found. """ # Try to handle a build channel specific URL (mtt:///<build_channel_id>/...) build_locator = BuildLocator.ParseUrl(url) if build_locator: config = ndb_models.BuildChannelConfig.get_by_id( build_locator.build_channel_id) if not config: raise errors.PluginError( 'Cannot find build channel %s' % build_locator.build_channel_id, http_status=404) return BuildChannel(config), build_locator.path # Iterate over all build channels to find one that supports this URL for config in ndb_models.BuildChannelConfig.query().fetch(): build_channel = BuildChannel(config) if not build_channel.is_valid: continue # skip invalid channels path = build_channel.FindBuildItemPath(url) if path: return build_channel, path # No matching build channel found return None, None
def Update(self, name, provider_name, options): """Updates a build channel. Args: name: a build channel name. provider_name: a build provider name. options: a option dict. Returns: an updated ndb_models.BuildChannelConfig object. """ provider_class = GetBuildProviderClass(provider_name) if not provider_class: raise errors.PluginError( 'Unknown provider %s' % provider_name, http_status=400) provider = provider_class() provider.UpdateOptions(**options) self.config.name = name self.config.provider_name = provider_name self.config.options = ndb_models.NameValuePair.FromDict(options) self.config.put() self._provider = provider return self.config
def _GetFileIds(self, param): """Get List of file ids. Args: param: A dictionary of parameters Returns: child_ids: a list of google drive file ids next_page_token: return None if not available """ logging.debug('Getting file IDs with param: %s', param) client = self._GetClient() try: files = client.files().list(**param).execute( num_retries=constant.NUM_RETRIES) logging.debug('response = %s', files) next_page_token = files.get('nextPageToken', None) file_ids = [file_item['id'] for file_item in files.get('files', [])] return file_ids, next_page_token except apiclient.errors.HttpError as e: if e.resp.status == constant.HTTP_NOT_FOUND_ERROR_CODE: raise errors.PluginError(_PARAM_NOT_VALID_ERROR % json.dumps(param)) raise
def AddBuildChannel(name, provider_name, options): """Adds a new build channel. Args: name: a build channel name. provider_name: a build provider name. options: a option dict. Returns: a newly created ndb_models.BuildChannelConfig object. """ provider_class = GetBuildProviderClass(provider_name) if not provider_class: raise errors.PluginError( 'Unknown provider %s' % provider_name, http_status=400) provider = provider_class() # Validate options. provider.UpdateOptions(**options) new_config = ndb_models.BuildChannelConfig( id=str(uuid.uuid4()), name=name, provider_name=provider_name, options=ndb_models.NameValuePair.FromDict(options)) new_config.put() return new_config
def provider(self): if self.is_valid: return getattr(self, '_provider') raise errors.PluginError('Unknown provider %s' % self.config.provider_name)