【问题标题】:Using Argparse with Google Admin API将 Argparse 与 Google Admin API 一起使用
【发布时间】:2014-11-25 15:59:47
【问题描述】:

我正在使用 Google 的 Python API 来提取审计信息,但我无法让 argparse 的父组参数(这似乎是 API 访问所必需的)和我自己的参数(例如传递一个日期)工作在一起。

代码:

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

似乎无法识别日期参数。任何帮助将不胜感激!

【问题讨论】:

  • 您是否还应该将 userkey 作为要求参数包含在内? developers.google.com/resources/api-libraries/documentation/… 从 pydocs 看来,您需要的不仅仅是日期...
  • 我稍后在应用程序中添加 userKey,硬编码为“all”,这将为所有用户带回事件。当我不尝试传递外部参数时,该代码有效。谢谢!

标签: python argparse google-admin-sdk


【解决方案1】:

我现在可以使用它了 - 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 的额外参数(传递父级)解决了问题

【讨论】:

  • 所以你可以定义你自己的解析器,但你不要自己运行它。那是sample_tools 工作。您是否通过flags 取回“selected_date”?
  • @hpaulj- 是的,它可以作为 flags.selected_date 访问
【解决方案2】:

在我看来,正在发生以下情况:

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)。

【讨论】:

  • 问题肯定出在这里。根据谷歌的说法,我应该能够通过将 Oauth2 的“父级”(tools.argparser 或 tools.run_parser)传递给我自己的解析器来覆盖它。它几乎可以工作,但似乎链条中的某些东西不知道 -d 参数
猜你喜欢
  • 2014-04-07
  • 2015-03-11
  • 2020-11-05
  • 2015-09-07
  • 1970-01-01
  • 2014-03-04
  • 2012-09-12
  • 2016-11-06
  • 2023-03-07
相关资源
最近更新 更多