NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

common.py (2932B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\dogshell\common.py
      2 from __future__ import print_function
      3 import os
      4 import sys
      5 from datadog.util.compat import is_p3k, configparser, IterableUserDict, get_input
      6 
      7 def print_err(msg):
      8     if is_p3k():
      9         print('ERROR: ' + msg + '\n', file=sys.stderr)
     10     else:
     11         sys.stderr.write(msg + '\n')
     12 
     13 
     14 def report_errors(res):
     15     if 'errors' in res:
     16         for e in res['errors']:
     17             print_err('ERROR: ' + e)
     18 
     19         sys.exit(1)
     20     return False
     21 
     22 
     23 def report_warnings(res):
     24     if 'warnings' in res:
     25         for e in res['warnings']:
     26             print_err('WARNING: ' + e)
     27 
     28         return True
     29     return False
     30 
     31 
     32 class DogshellConfig(IterableUserDict):
     33 
     34     def load(self, config_file, api_key, app_key):
     35         config = configparser.ConfigParser()
     36         if api_key is not None and app_key is not None:
     37             self['api_key'] = api_key
     38             self['app_key'] = app_key
     39         else:
     40             if os.access(config_file, os.F_OK):
     41                 config.read(config_file)
     42                 if not config.has_section('Connection'):
     43                     report_errors({'errors': ['%s has no [Connection] section' % config_file]})
     44             else:
     45                 try:
     46                     response = ''
     47                     while response.strip().lower() not in ('y', 'n'):
     48                         response = get_input('%s does not exist. Would you like to create it? [Y/n] ' % config_file)
     49                         if response.strip().lower() in ('', 'y', 'yes'):
     50                             api_key = get_input('What is your api key? (Get it here: https://app.datadoghq.com/account/settings#api) ')
     51                             app_key = get_input('What is your application key? (Generate one here: https://app.datadoghq.com/account/settings#api) ')
     52                             config.add_section('Connection')
     53                             config.set('Connection', 'apikey', api_key)
     54                             config.set('Connection', 'appkey', app_key)
     55                             f = open(config_file, 'w')
     56                             config.write(f)
     57                             f.close()
     58                             print('Wrote %s' % config_file)
     59                         elif response.strip().lower() == 'n':
     60                             print_err('Exiting\n')
     61                             sys.exit(1)
     62 
     63                 except KeyboardInterrupt:
     64                     print_err('\nExiting')
     65                     sys.exit(1)
     66 
     67             self['api_key'] = config.get('Connection', 'apikey')
     68             self['app_key'] = config.get('Connection', 'appkey')
     69             if config.has_option('Connection', 'host_name'):
     70                 self['host_name'] = config.get('Connection', 'host_name')
     71             if config.has_option('Connection', 'api_host'):
     72                 self['api_host'] = config.get('Connection', 'api_host')