将 Argparse 与 Google Admin API 结合使用 [英] Using Argparse with Google Admin API

查看:24
本文介绍了将 Argparse 与 Google Admin API 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Google's Python API to pull down auditing information, but I can't get the parent group arguments for argparse (which appear to be required for API access) and my own arguments (e.g. passing in a date) to work together.

代码:

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)

并从命令行运行它...

And running it from the command line...

> 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

目前看起来不错...现在添加一个参数

Looks good so far... now add an argument

> 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

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

It appears that the date argument is not recognized. Any help would be much appreciated!

推荐答案

我现在可以使用了 - sample_tools.init 正在实例化(无论好坏)它自己的 argparse 实例.Google API 允许您传入父级(我在其中传入了我自己的自定义参数)并且一切正常.

I have it working now- sample_tools.init is instantiating (for better or worse) its own argparse instance. The Google API allows you to pass in a parent (where I passed in my own custom arguments) and everything is works.

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 的额外参数(传递父级)修复了问题

The extra argument to sample_tools.init (passing the parent) fixes the problem

这篇关于将 Argparse 与 Google Admin API 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆