<s> from __future__ import print_function <EOL> from dogapi . common import is_p3k , find_localhost <EOL> get_input = input <EOL> if is_p3k ( ) : <EOL> import configparser <EOL> else : <EOL> get_input = raw_input <EOL> import ConfigParser as configparser <EOL> import os <EOL> import sys <EOL> try : <EOL> from UserDict import IterableUserDict <EOL> except ImportError : <EOL> from collections import UserDict as IterableUserDict <EOL> from dogapi import DogHttpApi <EOL> def print_err ( msg ) : <EOL> if is_p3k ( ) : <EOL> print ( '<STR_LIT>' + msg + '<STR_LIT:\n>' , file = sys . stderr ) <EOL> else : <EOL> sys . stderr . write ( msg + '<STR_LIT:\n>' ) <EOL> def report_errors ( res ) : <EOL> if '<STR_LIT>' in res : <EOL> for e in res [ '<STR_LIT>' ] : <EOL> print_err ( '<STR_LIT>' + e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return False <EOL> def report_warnings ( res ) : <EOL> if '<STR_LIT>' in res : <EOL> for e in res [ '<STR_LIT>' ] : <EOL> print_err ( '<STR_LIT>' + e ) <EOL> return True <EOL> return False <EOL> class CommandLineClient ( object ) : <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> self . _dog = None <EOL> @ property <EOL> def dog ( self ) : <EOL> if not self . _dog : <EOL> self . _dog = DogHttpApi ( self . config [ '<STR_LIT>' ] , self . config [ '<STR_LIT>' ] , swallow = True , json_responses = True ) <EOL> return self . _dog <EOL> class DogshellConfig ( IterableUserDict ) : <EOL> def load ( self , config_file , apikey , appkey ) : <EOL> config = configparser . ConfigParser ( ) <EOL> if apikey is not None and appkey is not None : <EOL> self [ '<STR_LIT>' ] = apikey <EOL> self [ '<STR_LIT>' ] = appkey <EOL> else : <EOL> if os . access ( config_file , os . F_OK ) : <EOL> config . read ( config_file ) <EOL> if not config . has_section ( '<STR_LIT>' ) : <EOL> report_errors ( { '<STR_LIT>' : [ '<STR_LIT>' % config_file ] } ) <EOL> else : <EOL> try : <EOL> response = '<STR_LIT>' <EOL> while response . strip ( ) . lower ( ) not in [ '<STR_LIT:y>' , '<STR_LIT:n>' ] : <EOL> response = get_input ( '<STR_LIT>' % config_file ) <EOL> if response . strip ( ) . lower ( ) in [ '<STR_LIT>' , '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> apikey = get_input ( '<STR_LIT>' ) <EOL> appkey = get_input ( '<STR_LIT>' ) <EOL> config . add_section ( '<STR_LIT>' ) <EOL> config . set ( '<STR_LIT>' , '<STR_LIT>' , apikey ) <EOL> config . set ( '<STR_LIT>' , '<STR_LIT>' , appkey ) <EOL> f = open ( config_file , '<STR_LIT:w>' ) <EOL> config . write ( f ) <EOL> f . close ( ) <EOL> print ( '<STR_LIT>' % config_file ) <EOL> elif response . strip ( ) . lower ( ) == '<STR_LIT:n>' : <EOL> print_err ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except KeyboardInterrupt : <EOL> print_err ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert self [ '<STR_LIT>' ] is not None and self [ '<STR_LIT>' ] is not None </s>
