<s> from __future__ import unicode_literals <EOL> import os <EOL> import codecs <EOL> import shutil <EOL> import argparse <EOL> from functools import partial <EOL> import six <EOL> from six . moves import configparser <EOL> from . import docs , __version__ <EOL> from . objects import KeyMap <EOL> PACKAGE = os . path . dirname ( __file__ ) <EOL> HOME = os . path . expanduser ( '<STR_LIT>' ) <EOL> TEMPLATE = os . path . join ( PACKAGE , '<STR_LIT>' ) <EOL> DEFAULT_CONFIG = os . path . join ( PACKAGE , '<STR_LIT>' ) <EOL> XDG_HOME = os . getenv ( '<STR_LIT>' , os . path . join ( HOME , '<STR_LIT>' ) ) <EOL> CONFIG = os . path . join ( XDG_HOME , '<STR_LIT>' , '<STR_LIT>' ) <EOL> TOKEN = os . path . join ( XDG_HOME , '<STR_LIT>' , '<STR_LIT>' ) <EOL> HISTORY = os . path . join ( XDG_HOME , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def build_parser ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> prog = '<STR_LIT>' , description = docs . SUMMARY , <EOL> epilog = docs . CONTROLS , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:version>' , version = '<STR_LIT>' + __version__ ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , const = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , const = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> const = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , const = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , const = True , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def copy_default_config ( filename = CONFIG ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( filename ) : <EOL> try : <EOL> ch = six . moves . input ( <EOL> '<STR_LIT>' % filename ) <EOL> if ch not in ( '<STR_LIT:Y>' , '<STR_LIT:y>' ) : <EOL> return <EOL> except KeyboardInterrupt : <EOL> return <EOL> filepath = os . path . dirname ( filename ) <EOL> if not os . path . exists ( filepath ) : <EOL> os . makedirs ( filepath ) <EOL> print ( '<STR_LIT>' % filename ) <EOL> shutil . copy ( DEFAULT_CONFIG , filename ) <EOL> os . chmod ( filename , <NUM_LIT> ) <EOL> class OrderedSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , elements = None ) : <EOL> elements = elements or [ ] <EOL> self . _set = set ( elements ) <EOL> self . _list = elements <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _set <EOL> def __len__ ( self ) : <EOL> return len ( self . _list ) <EOL> def __getitem__ ( self , item ) : <EOL> return self . _list [ item ] <EOL> def add ( self , item ) : <EOL> self . _set . add ( item ) <EOL> self . _list . append ( item ) <EOL> class Config ( object ) : <EOL> def __init__ ( self , history_file = HISTORY , token_file = TOKEN , ** kwargs ) : <EOL> self . history_file = history_file <EOL> self . token_file = token_file <EOL> self . config = kwargs <EOL> default , bindings = self . get_file ( DEFAULT_CONFIG ) <EOL> self . default = default <EOL> self . keymap = KeyMap ( bindings ) <EOL> self . refresh_token = None <EOL> self . history = OrderedSet ( ) <EOL> def __getitem__ ( self , item ) : <EOL> if item in self . config : <EOL> return self . config [ item ] <EOL> else : <EOL> return self . default . get ( item , None ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . config [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> self . config . pop ( key , None ) <EOL> def update ( self , ** kwargs ) : <EOL> self . config . update ( kwargs ) <EOL> def load_refresh_token ( self ) : <EOL> if os . path . exists ( self . token_file ) : <EOL> with open ( self . token_file ) as fp : <EOL> self . refresh_token = fp . read ( ) . strip ( ) <EOL> else : <EOL> self . refresh_token = None <EOL> def save_refresh_token ( self ) : <EOL> self . _ensure_filepath ( self . token_file ) <EOL> with open ( self . token_file , '<STR_LIT>' ) as fp : <EOL> fp . write ( self . refresh_token ) <EOL> def delete_refresh_token ( self ) : <EOL> if os . path . exists ( self . token_file ) : <EOL> os . remove ( self . token_file ) <EOL> self . refresh_token = None <EOL> def load_history ( self ) : <EOL> if os . path . exists ( self . history_file ) : <EOL> with codecs . open ( self . history_file , encoding = '<STR_LIT:utf-8>' ) as fp : <EOL> self . history = OrderedSet ( [ line . strip ( ) for line in fp ] ) <EOL> else : <EOL> self . history = OrderedSet ( ) <EOL> def save_history ( self ) : <EOL> self . _ensure_filepath ( self . history_file ) <EOL> with codecs . open ( self . history_file , '<STR_LIT>' , encoding = '<STR_LIT:utf-8>' ) as fp : <EOL> fp . writelines ( '<STR_LIT:\n>' . join ( self . history [ - self [ '<STR_LIT>' ] : ] ) ) <EOL> def delete_history ( self ) : <EOL> if os . path . exists ( self . history_file ) : <EOL> os . remove ( self . history_file ) <EOL> self . history = OrderedSet ( ) <EOL> @ staticmethod <EOL> def get_args ( ) : <EOL> """<STR_LIT>""" <EOL> parser = build_parser ( ) <EOL> args = vars ( parser . parse_args ( ) ) <EOL> return { key : val for key , val in args . items ( ) if val is not None } <EOL> @ classmethod <EOL> def get_file ( cls , filename = None ) : <EOL> """<STR_LIT>""" <EOL> if filename is None : <EOL> filename = CONFIG <EOL> config = configparser . ConfigParser ( ) <EOL> if os . path . exists ( filename ) : <EOL> with codecs . open ( filename , encoding = '<STR_LIT:utf-8>' ) as fp : <EOL> config . readfp ( fp ) <EOL> return cls . _parse_rtv_file ( config ) <EOL> @ staticmethod <EOL> def _parse_rtv_file ( config ) : <EOL> rtv = { } <EOL> if config . has_section ( '<STR_LIT>' ) : <EOL> rtv = dict ( config . items ( '<STR_LIT>' ) ) <EOL> params = { <EOL> '<STR_LIT:ascii>' : partial ( config . getboolean , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : partial ( config . getboolean , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : partial ( config . getboolean , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : partial ( config . getboolean , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : partial ( config . getint , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : partial ( config . getint , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : lambda x : rtv [ x ] . split ( '<STR_LIT:U+002C>' ) <EOL> } <EOL> for key , func in params . items ( ) : <EOL> if key in rtv : <EOL> rtv [ key ] = func ( key ) <EOL> bindings = { } <EOL> if config . has_section ( '<STR_LIT>' ) : <EOL> bindings = dict ( config . items ( '<STR_LIT>' ) ) <EOL> for name , keys in bindings . items ( ) : <EOL> bindings [ name ] = [ key . strip ( ) for key in keys . split ( '<STR_LIT:U+002C>' ) ] <EOL> return rtv , bindings <EOL> @ staticmethod <EOL> def _ensure_filepath ( filename ) : <EOL> """<STR_LIT>""" <EOL> filepath = os . path . dirname ( filename ) <EOL> if not os . path . exists ( filepath ) : <EOL> os . makedirs ( filepath ) </s>
