<s> import os <EOL> import pickle <EOL> import pkg_resources <EOL> import re <EOL> import shelve <EOL> import six <EOL> from . conf import settings <EOL> from contextlib import closing <EOL> from decorator import decorator <EOL> from difflib import get_close_matches <EOL> from functools import wraps <EOL> from inspect import getargspec <EOL> from pathlib import Path <EOL> from warnings import warn <EOL> DEVNULL = open ( os . devnull , '<STR_LIT:w>' ) <EOL> if six . PY2 : <EOL> import anydbm <EOL> shelve_open_error = anydbm . error <EOL> else : <EOL> import dbm <EOL> shelve_open_error = dbm . error <EOL> def memoize ( fn ) : <EOL> """<STR_LIT>""" <EOL> memo = { } <EOL> @ wraps ( fn ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if not memoize . disabled : <EOL> key = pickle . dumps ( ( args , kwargs ) ) <EOL> if key not in memo : <EOL> memo [ key ] = fn ( * args , ** kwargs ) <EOL> value = memo [ key ] <EOL> else : <EOL> value = fn ( * args , ** kwargs ) <EOL> return value <EOL> return wrapper <EOL> memoize . disabled = False <EOL> @ memoize <EOL> def which ( program ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from shutil import which <EOL> return which ( program ) <EOL> except ImportError : <EOL> def is_exe ( fpath ) : <EOL> return os . path . isfile ( fpath ) and os . access ( fpath , os . X_OK ) <EOL> fpath , fname = os . path . split ( program ) <EOL> if fpath : <EOL> if is_exe ( program ) : <EOL> return program <EOL> else : <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( '<STR_LIT:">' ) <EOL> exe_file = os . path . join ( path , program ) <EOL> if is_exe ( exe_file ) : <EOL> return exe_file <EOL> return None <EOL> def default_settings ( params ) : <EOL> """<STR_LIT>""" <EOL> def _default_settings ( fn , command ) : <EOL> for k , w in params . items ( ) : <EOL> settings . setdefault ( k , w ) <EOL> return fn ( command ) <EOL> return decorator ( _default_settings ) <EOL> def get_closest ( word , possibilities , n = <NUM_LIT:3> , cutoff = <NUM_LIT> , fallback_to_first = True ) : <EOL> """<STR_LIT>""" <EOL> possibilities = list ( possibilities ) <EOL> try : <EOL> return get_close_matches ( word , possibilities , n , cutoff ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> if fallback_to_first : <EOL> return possibilities [ <NUM_LIT:0> ] <EOL> @ memoize <EOL> def get_all_executables ( ) : <EOL> from thefuck . shells import shell <EOL> def _safe ( fn , fallback ) : <EOL> try : <EOL> return fn ( ) <EOL> except OSError : <EOL> return fallback <EOL> tf_alias = get_alias ( ) <EOL> tf_entry_points = get_installation_info ( ) . get_entry_map ( ) . get ( '<STR_LIT>' , { } ) . keys ( ) <EOL> bins = [ exe . name <EOL> for path in os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT::>' ) <EOL> for exe in _safe ( lambda : list ( Path ( path ) . iterdir ( ) ) , [ ] ) <EOL> if not _safe ( exe . is_dir , True ) <EOL> and exe . name not in tf_entry_points ] <EOL> aliases = [ alias for alias in shell . get_aliases ( ) if alias != tf_alias ] <EOL> return bins + aliases <EOL> def replace_argument ( script , from_ , to ) : <EOL> """<STR_LIT>""" <EOL> replaced_in_the_end = re . sub ( u'<STR_LIT>' . format ( re . escape ( from_ ) ) , u'<STR_LIT>' . format ( to ) , <EOL> script , count = <NUM_LIT:1> ) <EOL> if replaced_in_the_end != script : <EOL> return replaced_in_the_end <EOL> else : <EOL> return script . replace ( <EOL> u'<STR_LIT>' . format ( from_ ) , u'<STR_LIT>' . format ( to ) , <NUM_LIT:1> ) <EOL> @ decorator <EOL> def eager ( fn , * args , ** kwargs ) : <EOL> return list ( fn ( * args , ** kwargs ) ) <EOL> @ eager <EOL> def get_all_matched_commands ( stderr , separator = '<STR_LIT>' ) : <EOL> should_yield = False <EOL> for line in stderr . split ( '<STR_LIT:\n>' ) : <EOL> if separator in line : <EOL> should_yield = True <EOL> elif should_yield and line : <EOL> yield line . strip ( ) <EOL> def replace_command ( command , broken , matched ) : <EOL> """<STR_LIT>""" <EOL> new_cmds = get_close_matches ( broken , matched , cutoff = <NUM_LIT:0.1> ) <EOL> return [ replace_argument ( command . script , broken , new_cmd . strip ( ) ) <EOL> for new_cmd in new_cmds ] <EOL> @ memoize <EOL> def is_app ( command , * app_names , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> at_least = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" . format ( kwargs . keys ( ) ) ) <EOL> if command . script_parts is not None and len ( command . script_parts ) > at_least : <EOL> return command . script_parts [ <NUM_LIT:0> ] in app_names <EOL> return False <EOL> def for_app ( * app_names , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _for_app ( fn , command ) : <EOL> if is_app ( command , * app_names , ** kwargs ) : <EOL> return fn ( command ) <EOL> else : <EOL> return False <EOL> return decorator ( _for_app ) <EOL> def cache ( * depends_on ) : <EOL> """<STR_LIT>""" <EOL> def _get_mtime ( name ) : <EOL> path = os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , name ) <EOL> try : <EOL> return str ( os . path . getmtime ( path ) ) <EOL> except OSError : <EOL> return '<STR_LIT:0>' <EOL> def _get_cache_path ( ) : <EOL> default_xdg_cache_dir = os . path . expanduser ( "<STR_LIT>" ) <EOL> cache_dir = os . getenv ( "<STR_LIT>" , default_xdg_cache_dir ) <EOL> cache_path = Path ( cache_dir ) . joinpath ( '<STR_LIT>' ) . as_posix ( ) <EOL> try : <EOL> os . makedirs ( cache_dir ) <EOL> except OSError : <EOL> if not os . path . isdir ( cache_dir ) : <EOL> raise <EOL> return cache_path <EOL> @ decorator <EOL> def _cache ( fn , * args , ** kwargs ) : <EOL> if cache . disabled : <EOL> return fn ( * args , ** kwargs ) <EOL> key = '<STR_LIT>' . format ( fn . __module__ , repr ( fn ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> etag = '<STR_LIT:.>' . join ( _get_mtime ( name ) for name in depends_on ) <EOL> cache_path = _get_cache_path ( ) <EOL> try : <EOL> with closing ( shelve . open ( cache_path ) ) as db : <EOL> if db . get ( key , { } ) . get ( '<STR_LIT>' ) == etag : <EOL> return db [ key ] [ '<STR_LIT:value>' ] <EOL> else : <EOL> value = fn ( * args , ** kwargs ) <EOL> db [ key ] = { '<STR_LIT>' : etag , '<STR_LIT:value>' : value } <EOL> return value <EOL> except ( shelve_open_error , ImportError ) : <EOL> warn ( "<STR_LIT>" ) <EOL> os . remove ( cache_path ) <EOL> with closing ( shelve . open ( cache_path ) ) as db : <EOL> value = fn ( * args , ** kwargs ) <EOL> db [ key ] = { '<STR_LIT>' : etag , '<STR_LIT:value>' : value } <EOL> return value <EOL> return _cache <EOL> cache . disabled = False <EOL> def compatibility_call ( fn , * args ) : <EOL> """<STR_LIT>""" <EOL> fn_args_count = len ( getargspec ( fn ) . args ) <EOL> if fn . __name__ in ( '<STR_LIT>' , '<STR_LIT>' ) and fn_args_count == <NUM_LIT:2> : <EOL> warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( fn . __name__ , fn . __module__ ) ) <EOL> args += ( settings , ) <EOL> if fn . __name__ == '<STR_LIT>' and fn_args_count == <NUM_LIT:3> : <EOL> warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( fn . __name__ , fn . __module__ ) ) <EOL> args += ( settings , ) <EOL> return fn ( * args ) <EOL> def get_installation_info ( ) : <EOL> return pkg_resources . require ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> def get_alias ( ) : <EOL> return os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ memoize <EOL> def get_valid_history_without_current ( command ) : <EOL> def _not_corrected ( history , tf_alias ) : <EOL> """<STR_LIT>""" <EOL> previous = None <EOL> for line in history : <EOL> if previous is not None and line != tf_alias : <EOL> yield previous <EOL> previous = line <EOL> if history : <EOL> yield history [ - <NUM_LIT:1> ] <EOL> from thefuck . shells import shell <EOL> history = shell . get_history ( ) <EOL> tf_alias = get_alias ( ) <EOL> executables = get_all_executables ( ) <EOL> return [ line for line in _not_corrected ( history , tf_alias ) <EOL> if not line . startswith ( tf_alias ) and not line == command . script <EOL> and line . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] in executables ] </s>
