class GoogleDocs: def __init__(self, client, cache_dir): self.client = client self.config = UserConfig(dir_name=cache_dir, config_file="cache", def_config_callback=lambda x: {}) self.cache_dir = cache_dir if not self.config.read_config(): self.config.initialize_dir(None) def getFolders(self): folders = self.get_list("/feeds/default/private/full/-/folder") if not folders.entry: return None return [Folder(self, x) for x in folders.entry] def getFolder(self, descriptor, etag=None): return Folder(self, self.client.GetResourceById(descriptor[id], etag=etag)) def __cached_entry(self, id): return os.path.join(self.cache_dir, id) def get_list(self, url): feed = self.client.GetResources(uri=url) if not feed.entry: return None if feed.GetNextLink(): feed.entry += self.get_list(feed.GetNextLink().href).entry return feed def get_cache_descriptor(self, id): if self.config.has_key(id): self.config[id] return None def open_cached_file(self, id, **kwargs): return open(self.__cached_entry(id), **kwargs) def download(self, id, extra_params=None): item_etag = None if self.config.has_key(id): item_etag = self.config[id][etag] entry = self.client.GetResourceById(id, etag=item_etag) self.client.DownloadResource(entry, self.__cached_entry(id), extra_params=extra_params) self.config[id] = create_descriptor(entry) self.config.write_config() def create(self, title, folder_entry, mime_type="text/plain"): ms = gdata.data.MediaSource(file_handle=StringIO(" "), content_type=mime_type, content_length=1) entry = gdata.docs.data.Resource(type="file", title=title) return self.client.CreateResource(entry, media=ms, collection=folder_entry) def write(self, entry, stream, length, mime_type="text/plain"): ms = gdata.data.MediaSource(file_handle=stream, content_type=mime_type, content_length=length) self.client.UpdateResource(entry, media=ms)
def __init__(self, client, cache_dir): self.client = client self.config = UserConfig(dir_name=cache_dir, config_file="cache", def_config_callback=lambda x: {}) self.cache_dir = cache_dir if not self.config.read_config(): self.config.initialize_dir(None)