mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-11-04 09:02:08 +00:00 
			
		
		
		
	#385 Little refactoring
This commit is contained in:
		@@ -71,16 +71,20 @@ class Settings(dict):
 | 
				
			|||||||
                for setting in DEFAULT_SETTINGS.items():
 | 
					                for setting in DEFAULT_SETTINGS.items():
 | 
				
			||||||
                    settings_file.write(u'# {} = {}\n'.format(*setting))
 | 
					                    settings_file.write(u'# {} = {}\n'.format(*setting))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _setup_user_dir(self):
 | 
					    def _get_user_dir_path(self):
 | 
				
			||||||
        """Returns user config dir, create it when it doesn't exist."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # for backward compatibility, use `~/.thefuck` if it exists
 | 
					        # for backward compatibility, use `~/.thefuck` if it exists
 | 
				
			||||||
        user_dir = Path(os.path.expanduser('~/.thefuck'))
 | 
					        legacy_user_dir = Path(os.path.expanduser('~/.thefuck'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not user_dir.is_dir():
 | 
					        if legacy_user_dir.is_dir():
 | 
				
			||||||
 | 
					            return legacy_user_dir
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
            default_xdg_config_dir = os.path.expanduser("~/.config")
 | 
					            default_xdg_config_dir = os.path.expanduser("~/.config")
 | 
				
			||||||
            xdg_config_dir = os.getenv("XDG_CONFIG_HOME", default_xdg_config_dir)
 | 
					            xdg_config_dir = os.getenv("XDG_CONFIG_HOME", default_xdg_config_dir)
 | 
				
			||||||
            user_dir = Path(os.path.join(xdg_config_dir, 'thefuck'))
 | 
					            return Path(os.path.join(xdg_config_dir, 'thefuck'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _setup_user_dir(self):
 | 
				
			||||||
 | 
					        """Returns user config dir, create it when it doesn't exist."""
 | 
				
			||||||
 | 
					        user_dir = self._get_user_dir_path()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        rules_dir = user_dir.joinpath('rules')
 | 
					        rules_dir = user_dir.joinpath('rules')
 | 
				
			||||||
        if not rules_dir.is_dir():
 | 
					        if not rules_dir.is_dir():
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -180,11 +180,7 @@ def cache(*depends_on):
 | 
				
			|||||||
        except OSError:
 | 
					        except OSError:
 | 
				
			||||||
            return '0'
 | 
					            return '0'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @decorator
 | 
					    def _get_cache_path():
 | 
				
			||||||
    def _cache(fn, *args, **kwargs):
 | 
					 | 
				
			||||||
        if cache.disabled:
 | 
					 | 
				
			||||||
            return fn(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        default_xdg_cache_dir = os.path.expanduser("~/.cache")
 | 
					        default_xdg_cache_dir = os.path.expanduser("~/.cache")
 | 
				
			||||||
        cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir)
 | 
					        cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir)
 | 
				
			||||||
        cache_path = Path(cache_dir).joinpath('thefuck').as_posix()
 | 
					        cache_path = Path(cache_dir).joinpath('thefuck').as_posix()
 | 
				
			||||||
@@ -197,11 +193,19 @@ def cache(*depends_on):
 | 
				
			|||||||
            if not os.path.isdir(cache_dir):
 | 
					            if not os.path.isdir(cache_dir):
 | 
				
			||||||
                raise
 | 
					                raise
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return cache_path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @decorator
 | 
				
			||||||
 | 
					    def _cache(fn, *args, **kwargs):
 | 
				
			||||||
 | 
					        if cache.disabled:
 | 
				
			||||||
 | 
					            return fn(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # A bit obscure, but simplest way to generate unique key for
 | 
					        # A bit obscure, but simplest way to generate unique key for
 | 
				
			||||||
        # functions and methods in python 2 and 3:
 | 
					        # functions and methods in python 2 and 3:
 | 
				
			||||||
        key = '{}.{}'.format(fn.__module__, repr(fn).split('at')[0])
 | 
					        key = '{}.{}'.format(fn.__module__, repr(fn).split('at')[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        etag = '.'.join(_get_mtime(name) for name in depends_on)
 | 
					        etag = '.'.join(_get_mtime(name) for name in depends_on)
 | 
				
			||||||
 | 
					        cache_path = _get_cache_path()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        with closing(shelve.open(cache_path)) as db:
 | 
					        with closing(shelve.open(cache_path)) as db:
 | 
				
			||||||
            if db.get(key, {}).get('etag') == etag:
 | 
					            if db.get(key, {}).get('etag') == etag:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user