2014-09-30 52 views
2

我正在使用Google的Python API来下拉审核信息,但我无法获得argparse的父组参数(这似乎是API访问所必需的)和我自己的参数(例如,传递在一个日期)一起工作。在谷歌管理API中使用Argparse

代码:

import pprint 
import sys 
import re 
import httplib2 
import json 
import collections 
import argparse 

from oauth2client import client 
from apiclient import sample_tools 
from apiclient import discovery 
from oauth2client.client import AccessTokenRefreshError 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.file import Storage 
from oauth2client.tools import run 
from oauth2client import tools 

def main(argv): 
    # Parser for command-line arguments. 
    parser = argparse.ArgumentParser(
    description=__doc__, 
    formatter_class=argparse.RawDescriptionHelpFormatter, 
    parents=[tools.argparser]) 

    parser.add_argument("-d","--selected_date", help="Date (YYYY-mm-dd) to run user usage report", required=True) 

    args = parser.parse_args(argv[1:]) 
    print args 
    selected_date = args.selected_date 
    print selected_date 

    # Authenticate and construct service. 
    service, flags = sample_tools.init(
    argv, 'admin', 'reports_v1', __doc__, __file__, 
    scope='https://www.googleapis.com/auth/admin.reports.usage.readonly') 

    # If the Credentials don't exist or are invalid run through the native client 
    # flow. The Storage object will ensure that if successful the good 
    # Credentials will get written back to a file. 
    storage = Storage('admin.dat') 
    credentials = storage.get() 
    if not credentials or credentials.invalid: 
    credentials = run(FLOW, storage) 

而且在命令行中运行它...

> python user_report.py 
usage: user_report.py [-h] [--auth_host_name AUTH_HOST_NAME] 
        [--noauth_local_webserver] 
        [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]] 
        [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] -d 
        SELECTED_DATE 
user_report.py: error: argument -d/--selected_date is required 

看起来不错那么远,现在加上参数

> python user_report.py -d "2014-09-14" 
Namespace(auth_host_name='localhost', auth_host_port=[8080, 8090], logging_level='ERROR', noauth_local_webserver=False, selected_date='2014-09-14') 
usage: user_report.py [-h] [--auth_host_name AUTH_HOST_NAME] 
        [--noauth_local_webserver] 
        [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]] 
        [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] 
user_report.py: error: unrecognized arguments: -d 2014-09-14 

看样子日期参数不被识别。任何帮助将非常感激!

+0

你应该还包括用户钥的要求ARG? https://developers.google.com/resources/api-libraries/documentation/admin/reports_v1/python/latest/admin_reports_v1.userUsageReport.html 从pydocs看来,您需要的不仅仅是日期... – Emily 2014-09-30 23:22:46

+0

我稍后在应用程序中将userKey添加为“全部”,这将为所有用户带回事件。当我没有试图传入外部参数时,该代码起作用。谢谢! – Alex 2014-10-01 14:15:04

回答

1

在我看来像下面发生的事情:

args = parser.parse_args(argv[1:]) # runs fine 
print args       # produces the Namespace line 
selected_date = args.selected_date 
print selected_date     # where is this output? 

# Authenticate and construct service. 
service, flags = sample_tools.init(...) # is this producing the error? 

我猜的tools.argparser正在sample_tools.init运行,并产生错误,因为它不知道该-d说法。

(我熟悉argparse,但不是这个API)。

+0

问题肯定在这里。根据谷歌,我应该能够通过将Oauth2的“父”(tools.argparser或tools.run_parser)传递给我自己的解析器来覆盖它。它几乎可行,但似乎稍后链中的某些内容不知道-d参数 – Alex 2014-10-01 14:18:56

3

我现在正在使用它 - sample_tools.init正在实例化(更好或更坏)它自己的argparse实例。 Google API允许您传入父级(我在自己的自定义参数中传入的内容),并且所有内容都是有效的。

https://google-api-python-client.googlecode.com/hg/docs/epy/apiclient.sample_tools-pysrc.html

# Parser for command-line arguments 
parent = argparse.ArgumentParser(add_help=False) 
group = parent.add_argument_group('standard') 
parent.add_argument("-d","--selected_date", help="Date (YYYY-mm-dd) to run user usage report", required=True) 

flags = parser.parse_args(argv[1:]) 
print flags 
selected_date = flags.selected_date 
print selected_date 

# Authenticate and construct service. 
service, flags = sample_tools.init(
    argv, 'admin', 'reports_v1', __doc__, __file__, 
    scope='https://www.googleapis.com/auth/admin.reports.usage.readonly', parents=[parent]) 

额外的参数sample_tools.init(通过父)解决了这个问题

+0

因此,您可以定义自己的解析器,但不要自己运行它。这是'sample_tools'工作。你通过'flags'获得'selected_date'吗? – hpaulj 2014-10-01 17:05:45

+0

@ hpaulj-是的,它可以通过flags.selected_date – Alex 2014-10-09 20:51:50