<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> __author__ = '<STR_LIT>' <EOL> import logging <EOL> import os <EOL> import re <EOL> import googlecl . config . parser <EOL> SUBDIR_NAME = '<STR_LIT>' <EOL> DEFAULT_GOOGLECL_DIR = os . path . expanduser ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> HISTORY_FILENAME = '<STR_LIT>' <EOL> TOKENS_FILENAME_FORMAT = '<STR_LIT>' <EOL> DEVKEY_FILENAME = '<STR_LIT>' <EOL> FILE_EXT_PATTERN = re . compile ( '<STR_LIT>' ) <EOL> LOGGER_NAME = __name__ <EOL> LOG = logging . getLogger ( LOGGER_NAME ) <EOL> def determine_terminal_encoding ( config = None ) : <EOL> import sys <EOL> in_enc = '<STR_LIT>' <EOL> out_enc = '<STR_LIT>' <EOL> if sys . stdin . encoding : <EOL> in_enc = sys . stdin . encoding <EOL> if sys . stdout . encoding : <EOL> out_enc = sys . stdout . encoding <EOL> if in_enc . lower ( ) == out_enc . lower ( ) : <EOL> if not in_enc : <EOL> return_enc = None <EOL> if config is not None : <EOL> return_enc = config . safe_get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if return_enc is None : <EOL> return_enc = sys . getdefaultencoding ( ) <EOL> else : <EOL> return_enc = in_enc <EOL> else : <EOL> if in_enc and out_enc : <EOL> LOG . warning ( <EOL> '<STR_LIT>' ) <EOL> LOG . warning ( '<STR_LIT>' + in_enc ) <EOL> LOG . warning ( '<STR_LIT>' + in_enc ) <EOL> return_enc = out_enc or in_enc <EOL> LOG . debug ( '<STR_LIT>' + return_enc ) <EOL> return return_enc <EOL> TERMINAL_ENCODING = determine_terminal_encoding ( ) <EOL> class SafeEncodeError ( Exception ) : <EOL> pass <EOL> class SafeDecodeError ( Exception ) : <EOL> pass <EOL> def build_titles_list ( title , args ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> titles_list = args <EOL> if title : <EOL> titles_list . insert ( <NUM_LIT:0> , title ) <EOL> else : <EOL> titles_list = [ title ] <EOL> return titles_list <EOL> def get_extension_from_path ( path ) : <EOL> """<STR_LIT>""" <EOL> match = FILE_EXT_PATTERN . match ( path ) <EOL> if match : <EOL> return match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> return None <EOL> def get_data_path ( filename , <EOL> default_directories = None , <EOL> create_missing_dir = False ) : <EOL> """<STR_LIT>""" <EOL> return get_xdg_path ( filename , '<STR_LIT>' , default_directories , <EOL> create_missing_dir ) <EOL> def get_xdg_path ( filename , data_type , default_directories = None , <EOL> create_missing_dir = False ) : <EOL> """<STR_LIT>""" <EOL> data_type = data_type . upper ( ) <EOL> if data_type not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise Exception ( '<STR_LIT>' + data_type ) <EOL> xdg_home_dir = os . environ . get ( '<STR_LIT>' + data_type + '<STR_LIT>' ) <EOL> if not xdg_home_dir : <EOL> home_dir = os . path . expanduser ( '<STR_LIT>' ) <EOL> if data_type == '<STR_LIT>' : <EOL> xdg_home_dir = os . path . join ( home_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif data_type == '<STR_LIT>' : <EOL> xdg_home_dir = os . path . join ( home_dir , '<STR_LIT>' ) <EOL> xdg_home_dir = os . path . join ( xdg_home_dir , SUBDIR_NAME ) <EOL> xdg_dir_list = os . environ . get ( '<STR_LIT>' + data_type + '<STR_LIT>' ) <EOL> if not xdg_dir_list : <EOL> if data_type == '<STR_LIT>' : <EOL> xdg_dir_list = '<STR_LIT>' <EOL> elif data_type == '<STR_LIT>' : <EOL> xdg_dir_list = '<STR_LIT>' <EOL> xdg_dir_list = [ os . path . join ( d , SUBDIR_NAME ) <EOL> for d in xdg_dir_list . split ( '<STR_LIT::>' ) ] <EOL> dir_list = [ os . path . abspath ( '<STR_LIT:.>' ) , xdg_home_dir ] + xdg_dir_list + [ DEFAULT_GOOGLECL_DIR ] <EOL> if default_directories : <EOL> dir_list += default_directories <EOL> for directory in dir_list : <EOL> config_path = os . path . join ( directory , filename ) <EOL> if os . path . isfile ( config_path ) : <EOL> return config_path <EOL> LOG . debug ( '<STR_LIT>' + filename + '<STR_LIT>' + str ( dir_list ) ) <EOL> if os . name == '<STR_LIT>' : <EOL> default_dir = xdg_home_dir <EOL> mode = <NUM_LIT:0> <NUM_LIT> <EOL> else : <EOL> default_dir = DEFAULT_GOOGLECL_DIR <EOL> mode = <NUM_LIT:0> <NUM_LIT> <EOL> if not os . path . isdir ( default_dir ) and create_missing_dir : <EOL> try : <EOL> os . makedirs ( default_dir , mode ) <EOL> except OSError , err : <EOL> LOG . error ( err ) <EOL> return '<STR_LIT>' <EOL> return os . path . join ( default_dir , filename ) <EOL> def read_devkey ( ) : <EOL> """<STR_LIT>""" <EOL> key_path = get_data_path ( DEVKEY_FILENAME ) <EOL> devkey = None <EOL> if os . path . exists ( key_path ) : <EOL> with open ( key_path , '<STR_LIT:r>' ) as key_file : <EOL> devkey = key_file . read ( ) . strip ( ) <EOL> return devkey <EOL> def safe_encode ( string , target_encoding = TERMINAL_ENCODING , <EOL> errors = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( string , unicode ) : <EOL> return string . encode ( target_encoding , errors ) <EOL> elif isinstance ( string , str ) : <EOL> try : <EOL> string . decode ( target_encoding ) <EOL> except UnicodeDecodeError : <EOL> raise SafeEncodeError ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return string <EOL> else : <EOL> return str ( string ) <EOL> def safe_decode ( string , current_encoding = '<STR_LIT:utf-8>' , errors = '<STR_LIT:strict>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( string , str ) : <EOL> try : <EOL> return string . decode ( current_encoding , errors ) <EOL> except UnicodeDecodeError : <EOL> raise SafeDecodeError ( current_encoding + '<STR_LIT>' + <EOL> repr ( string ) ) <EOL> elif isinstance ( string , unicode ) : <EOL> return string <EOL> else : <EOL> return unicode ( string ) <EOL> def write_devkey ( devkey ) : <EOL> """<STR_LIT>""" <EOL> import stat <EOL> key_path = get_data_path ( DEVKEY_FILENAME ) <EOL> with open ( key_path , '<STR_LIT:w>' ) as key_file : <EOL> os . chmod ( key_path , stat . S_IRUSR | stat . S_IWUSR ) <EOL> key_file . write ( devkey ) </s>
