<s> import logging <EOL> import os . path <EOL> import sys <EOL> from argparse import ArgumentParser <EOL> from . utils import json_loads <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class Config ( dict ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def current ( default_path = '<STR_LIT>' , args = sys . argv ) : <EOL> if not hasattr ( Config , '<STR_LIT>' ) : <EOL> Config . _current = Config ( default_path , args ) <EOL> return Config . _current <EOL> def __init__ ( self , default_path = '<STR_LIT>' , args = sys . argv ) : <EOL> super ( Config , self ) . __init__ ( ) <EOL> self . args = args <EOL> self . default_path = default_path <EOL> self . config_opt ( ) <EOL> def __getattr__ ( self , name ) : <EOL> return self [ name ] <EOL> def __setattr__ ( self , name , val ) : <EOL> self [ name ] = val <EOL> def config_opt ( self ) : <EOL> opt = self . parse_cmdline ( ) <EOL> self . server = opt . server <EOL> self . password = opt . password <EOL> self . server_port = opt . server_port <EOL> self . local_port = opt . local_port <EOL> self . method = opt . method <EOL> self . timeout = opt . timeout <EOL> self . debug = opt . debug <EOL> def get_file_opt ( self ) : <EOL> p = ArgumentParser ( add_help = False ) <EOL> p . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , default = self . default_path , <EOL> help = "<STR_LIT>" , metavar = "<STR_LIT>" ) <EOL> opt , _ = p . parse_known_args ( self . args ) <EOL> if os . path . exists ( opt . config ) : <EOL> with open ( opt . config ) as f : <EOL> return json_loads ( f . read ( ) ) <EOL> else : <EOL> logger . warning ( "<STR_LIT>" % opt . config ) <EOL> return { } <EOL> def config_file_opt ( self , path2json ) : <EOL> if os . path . exists ( path2json ) : <EOL> with codecs . open ( opt . config , '<STR_LIT:r>' , encoding = '<STR_LIT:ascii>' ) as f : <EOL> json_con = json_loads ( f . read ( ) ) <EOL> self . server = self . server or json_con . get ( '<STR_LIT>' , None ) <EOL> self . password = self . password or json_con . get ( '<STR_LIT:password>' , None ) <EOL> self . server_port = self . server_port or json_con . get ( '<STR_LIT>' , None ) <EOL> self . local_port = self . local_port or json_con . get ( '<STR_LIT>' , None ) <EOL> self . method = self . method or json_con . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . timeout = self . timeout or json_con . get ( '<STR_LIT>' , None ) <EOL> self . debug = self . debug or json_con . get ( '<STR_LIT>' , False ) <EOL> else : <EOL> logger . warning ( "<STR_LIT>" % path2json ) <EOL> def parse_cmdline ( self ) : <EOL> from fukei import __version__ <EOL> parser = ArgumentParser ( usage = "<STR_LIT>" ) <EOL> _ = parser . add_argument <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = '<STR_LIT:127.0.0.1>' , <EOL> help = "<STR_LIT>" , type = str ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = '<STR_LIT>' , help = <EOL> "<STR_LIT>" , type = str ) <EOL> _ ( "<STR_LIT:-c>" , "<STR_LIT>" , default = self . default_path , <EOL> help = "<STR_LIT>" , metavar = "<STR_LIT>" ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT> , help = "<STR_LIT>" , type = int ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT> , <EOL> help = "<STR_LIT>" , type = int ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" , type = str ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT:10> , <EOL> help = "<STR_LIT>" , type = int ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , action = '<STR_LIT:store_true>' , default = False , <EOL> help = "<STR_LIT>" , ) <EOL> _ ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" % __version__ ) <EOL> c = self . get_file_opt ( ) <EOL> parser . set_defaults ( ** c ) <EOL> opt , _ = parser . parse_known_args ( self . args ) <EOL> return opt <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
