1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15  """Utilities for making samples. 
 16   
 17  Consolidates a lot of code commonly repeated in sample applications. 
 18  """ 
 19  from __future__ import absolute_import 
 20   
 21  __author__ = 'jcgregorio@google.com (Joe Gregorio)' 
 22  __all__ = ['init'] 
 23   
 24   
 25  import argparse 
 26  import os 
 27   
 28  from googleapiclient import discovery 
 29  from googleapiclient.http import build_http 
 30   
 31 -def init(argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None): 
  32    """A common initialization routine for samples. 
 33   
 34    Many of the sample applications do the same initialization, which has now 
 35    been consolidated into this function. This function uses common idioms found 
 36    in almost all the samples, i.e. for an API with name 'apiname', the 
 37    credentials are stored in a file named apiname.dat, and the 
 38    client_secrets.json file is stored in the same directory as the application 
 39    main file. 
 40   
 41    Args: 
 42      argv: list of string, the command-line parameters of the application. 
 43      name: string, name of the API. 
 44      version: string, version of the API. 
 45      doc: string, description of the application. Usually set to __doc__. 
 46      file: string, filename of the application. Usually set to __file__. 
 47      parents: list of argparse.ArgumentParser, additional command-line flags. 
 48      scope: string, The OAuth scope used. 
 49      discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL. 
 50   
 51    Returns: 
 52      A tuple of (service, flags), where service is the service object and flags 
 53      is the parsed command-line flags. 
 54    """ 
 55    try: 
 56        from oauth2client import client 
 57        from oauth2client import file 
 58        from oauth2client import tools 
 59    except ImportError: 
 60        raise ImportError('googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again.') 
 61   
 62    if scope is None: 
 63      scope = 'https://www.googleapis.com/auth/' + name 
 64   
 65     
 66    parent_parsers = [tools.argparser] 
 67    parent_parsers.extend(parents) 
 68    parser = argparse.ArgumentParser( 
 69        description=doc, 
 70        formatter_class=argparse.RawDescriptionHelpFormatter, 
 71        parents=parent_parsers) 
 72    flags = parser.parse_args(argv[1:]) 
 73   
 74     
 75     
 76     
 77     
 78    client_secrets = os.path.join(os.path.dirname(filename), 
 79                                  'client_secrets.json') 
 80   
 81     
 82    flow = client.flow_from_clientsecrets(client_secrets, 
 83        scope=scope, 
 84        message=tools.message_if_missing(client_secrets)) 
 85   
 86     
 87     
 88     
 89     
 90    storage = file.Storage(name + '.dat') 
 91    credentials = storage.get() 
 92    if credentials is None or credentials.invalid: 
 93      credentials = tools.run_flow(flow, storage, flags) 
 94    http = credentials.authorize(http=build_http()) 
 95   
 96    if discovery_filename is None: 
 97       
 98      service = discovery.build(name, version, http=http) 
 99    else: 
100       
101      with open(discovery_filename) as discovery_file: 
102        service = discovery.build_from_document( 
103            discovery_file.read(), 
104            base='https://www.googleapis.com/', 
105            http=http) 
106    return (service, flags) 
 107