<s> """<STR_LIT>""" <EOL> __author__ = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> import re <EOL> import atom <EOL> import gdata . service <EOL> import gdata . docs <EOL> import urllib <EOL> DATA_KIND_SCHEME = gdata . GDATA_NAMESPACE + '<STR_LIT>' <EOL> DOCUMENT_LABEL = '<STR_LIT>' <EOL> SPREADSHEET_LABEL = '<STR_LIT>' <EOL> PRESENTATION_LABEL = '<STR_LIT>' <EOL> FOLDER_LABEL = '<STR_LIT>' <EOL> PDF_LABEL = '<STR_LIT>' <EOL> LABEL_SCHEME = gdata . GDATA_NAMESPACE + '<STR_LIT>' <EOL> STARRED_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> TRASHED_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> HIDDEN_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> MINE_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> PRIVATE_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> SHARED_WITH_DOMAIN_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> VIEWED_LABEL_TERM = LABEL_SCHEME + '<STR_LIT>' <EOL> FOLDERS_SCHEME_PREFIX = gdata . docs . DOCUMENTS_NAMESPACE + '<STR_LIT>' <EOL> SUPPORTED_FILETYPES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class DocsService ( gdata . service . GDataService ) : <EOL> """<STR_LIT>""" <EOL> __FILE_EXT_PATTERN = re . compile ( '<STR_LIT>' ) <EOL> __RESOURCE_ID_PATTERN = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , email = None , password = None , source = None , <EOL> server = '<STR_LIT>' , additional_headers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> gdata . service . GDataService . __init__ ( <EOL> self , email = email , password = password , service = '<STR_LIT>' , source = source , <EOL> server = server , additional_headers = additional_headers , ** kwargs ) <EOL> def _MakeKindCategory ( self , label ) : <EOL> if label is None : <EOL> return None <EOL> return atom . Category ( scheme = DATA_KIND_SCHEME , <EOL> term = gdata . docs . DOCUMENTS_NAMESPACE + '<STR_LIT:#>' + label , label = label ) <EOL> def _MakeContentLinkFromId ( self , resource_id ) : <EOL> match = self . __RESOURCE_ID_PATTERN . match ( resource_id ) <EOL> label = match . group ( <NUM_LIT:1> ) <EOL> doc_id = match . group ( <NUM_LIT:3> ) <EOL> if label == DOCUMENT_LABEL : <EOL> return '<STR_LIT>' % doc_id <EOL> if label == PRESENTATION_LABEL : <EOL> return '<STR_LIT>' % doc_id <EOL> if label == SPREADSHEET_LABEL : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' % doc_id ) <EOL> raise ValueError , '<STR_LIT>' % resource_id <EOL> def _UploadFile ( self , media_source , title , category , folder_or_uri = None ) : <EOL> """<STR_LIT>""" <EOL> if folder_or_uri : <EOL> try : <EOL> uri = folder_or_uri . content . src <EOL> except AttributeError : <EOL> uri = folder_or_uri <EOL> else : <EOL> uri = '<STR_LIT>' <EOL> entry = gdata . docs . DocumentListEntry ( ) <EOL> entry . title = atom . Title ( text = title ) <EOL> if category is not None : <EOL> entry . category . append ( category ) <EOL> entry = self . Post ( entry , uri , media_source = media_source , <EOL> extra_headers = { '<STR_LIT>' : media_source . file_name } , <EOL> converter = gdata . docs . DocumentListEntryFromString ) <EOL> return entry <EOL> def _DownloadFile ( self , uri , file_path ) : <EOL> """<STR_LIT>""" <EOL> server_response = self . request ( '<STR_LIT:GET>' , uri ) <EOL> response_body = server_response . read ( ) <EOL> if server_response . status != <NUM_LIT:200> : <EOL> raise gdata . service . RequestError , { '<STR_LIT:status>' : server_response . status , <EOL> '<STR_LIT>' : server_response . reason , <EOL> '<STR_LIT:body>' : response_body } <EOL> f = open ( file_path , '<STR_LIT:wb>' ) <EOL> f . write ( response_body ) <EOL> f . flush ( ) <EOL> f . close ( ) <EOL> def MoveIntoFolder ( self , source_entry , folder_entry ) : <EOL> """<STR_LIT>""" <EOL> entry = gdata . docs . DocumentListEntry ( ) <EOL> entry . id = source_entry . id <EOL> entry = self . Post ( entry , folder_entry . content . src , <EOL> converter = gdata . docs . DocumentListEntryFromString ) <EOL> return entry <EOL> def Query ( self , uri , converter = gdata . docs . DocumentListFeedFromString ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( uri , converter = converter ) <EOL> def QueryDocumentListFeed ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( uri , converter = gdata . docs . DocumentListFeedFromString ) <EOL> def GetDocumentListEntry ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( uri , converter = gdata . docs . DocumentListEntryFromString ) <EOL> def GetDocumentListFeed ( self , uri = None ) : <EOL> """<STR_LIT>""" <EOL> if not uri : <EOL> uri = gdata . docs . service . DocumentQuery ( ) . ToUri ( ) <EOL> return self . QueryDocumentListFeed ( uri ) <EOL> def GetDocumentListAclEntry ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( uri , converter = gdata . docs . DocumentListAclEntryFromString ) <EOL> def GetDocumentListAclFeed ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( uri , converter = gdata . docs . DocumentListAclFeedFromString ) <EOL> def Upload ( self , media_source , title , folder_or_uri = None , label = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _UploadFile ( media_source , title , self . _MakeKindCategory ( label ) , <EOL> folder_or_uri ) <EOL> def Download ( self , entry_or_id_or_url , file_path , export_format = None , <EOL> gid = None , extra_params = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( entry_or_id_or_url , gdata . docs . DocumentListEntry ) : <EOL> url = entry_or_id_or_url . content . src <EOL> else : <EOL> if self . __RESOURCE_ID_PATTERN . match ( entry_or_id_or_url ) : <EOL> url = self . _MakeContentLinkFromId ( entry_or_id_or_url ) <EOL> else : <EOL> url = entry_or_id_or_url <EOL> if export_format is not None : <EOL> if url . find ( '<STR_LIT>' ) == - <NUM_LIT:1> : <EOL> raise gdata . service . Error , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> url += '<STR_LIT>' % export_format <EOL> if gid is not None : <EOL> if url . find ( '<STR_LIT>' ) == - <NUM_LIT:1> : <EOL> raise gdata . service . Error , '<STR_LIT>' <EOL> url += '<STR_LIT>' % gid <EOL> if extra_params : <EOL> url += '<STR_LIT:&>' + urllib . urlencode ( extra_params ) <EOL> self . _DownloadFile ( url , file_path ) <EOL> def Export ( self , entry_or_id_or_url , file_path , gid = None , extra_params = None ) : <EOL> """<STR_LIT>""" <EOL> ext = None <EOL> match = self . __FILE_EXT_PATTERN . match ( file_path ) <EOL> if match : <EOL> ext = match . group ( <NUM_LIT:1> ) <EOL> self . Download ( entry_or_id_or_url , file_path , ext , gid , extra_params ) <EOL> def CreateFolder ( self , title , folder_or_uri = None ) : <EOL> """<STR_LIT>""" <EOL> if folder_or_uri : <EOL> try : <EOL> uri = folder_or_uri . content . src <EOL> except AttributeError : <EOL> uri = folder_or_uri <EOL> else : <EOL> uri = '<STR_LIT>' <EOL> folder_entry = gdata . docs . DocumentListEntry ( ) <EOL> folder_entry . title = atom . Title ( text = title ) <EOL> folder_entry . category . append ( self . _MakeKindCategory ( FOLDER_LABEL ) ) <EOL> folder_entry = self . Post ( folder_entry , uri , <EOL> converter = gdata . docs . DocumentListEntryFromString ) <EOL> return folder_entry <EOL> def MoveOutOfFolder ( self , source_entry ) : <EOL> """<STR_LIT>""" <EOL> return self . Delete ( source_entry . GetEditLink ( ) . href ) <EOL> @ atom . deprecated ( '<STR_LIT>' ) <EOL> def UploadPresentation ( self , media_source , title , folder_or_uri = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _UploadFile ( <EOL> media_source , title , self . _MakeKindCategory ( PRESENTATION_LABEL ) , <EOL> folder_or_uri = folder_or_uri ) <EOL> @ atom . deprecated ( '<STR_LIT>' ) <EOL> def UploadSpreadsheet ( self , media_source , title , folder_or_uri = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _UploadFile ( <EOL> media_source , title , self . _MakeKindCategory ( SPREADSHEET_LABEL ) , <EOL> folder_or_uri = folder_or_uri ) <EOL> @ atom . deprecated ( '<STR_LIT>' ) <EOL> def UploadDocument ( self , media_source , title , folder_or_uri = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _UploadFile ( <EOL> media_source , title , self . _MakeKindCategory ( DOCUMENT_LABEL ) , <EOL> folder_or_uri = folder_or_uri ) <EOL> """<STR_LIT>""" <EOL> DownloadDocument = atom . deprecated ( '<STR_LIT>' ) ( Export ) <EOL> DownloadPresentation = atom . deprecated ( '<STR_LIT>' ) ( Export ) <EOL> DownloadSpreadsheet = atom . deprecated ( '<STR_LIT>' ) ( Export ) <EOL> """<STR_LIT>""" <EOL> MoveDocumentIntoFolder = atom . deprecated ( <EOL> '<STR_LIT>' ) ( MoveIntoFolder ) <EOL> MovePresentationIntoFolder = atom . deprecated ( <EOL> '<STR_LIT>' ) ( MoveIntoFolder ) <EOL> MoveSpreadsheetIntoFolder = atom . deprecated ( <EOL> '<STR_LIT>' ) ( MoveIntoFolder ) <EOL> MoveFolderIntoFolder = atom . deprecated ( <EOL> '<STR_LIT>' ) ( MoveIntoFolder ) <EOL> class DocumentQuery ( gdata . service . Query ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , feed = '<STR_LIT>' , visibility = '<STR_LIT>' , <EOL> projection = '<STR_LIT>' , text_query = None , params = None , <EOL> categories = None ) : <EOL> """<STR_LIT>""" <EOL> self . visibility = visibility <EOL> self . projection = projection <EOL> gdata . service . Query . __init__ ( self , feed , text_query , params , categories ) <EOL> def ToUri ( self ) : <EOL> """<STR_LIT>""" <EOL> old_feed = self . feed <EOL> self . feed = '<STR_LIT:/>' . join ( [ old_feed , self . visibility , self . projection ] ) <EOL> new_feed = gdata . service . Query . ToUri ( self ) <EOL> self . feed = old_feed <EOL> return new_feed <EOL> def AddNamedFolder ( self , email , folder_name ) : <EOL> """<STR_LIT>""" <EOL> category = '<STR_LIT>' % ( FOLDERS_SCHEME_PREFIX , email , folder_name ) <EOL> self . categories . append ( category ) <EOL> return category <EOL> def RemoveNamedFolder ( self , email , folder_name ) : <EOL> """<STR_LIT>""" <EOL> category = '<STR_LIT>' % ( FOLDERS_SCHEME_PREFIX , email , folder_name ) <EOL> self . categories . remove ( category ) <EOL> return category <EOL> class DocumentAclQuery ( gdata . service . Query ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resource_id , feed = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . resource_id = resource_id <EOL> gdata . service . Query . __init__ ( self , feed ) <EOL> def ToUri ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( gdata . service . Query . ToUri ( self ) , self . resource_id ) </s>
