From af92e149151bfd7c637b6691c2b7147c1953953f Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Wed, 23 Jan 2019 11:40:34 +0200 Subject: Read configuration from XDG_CONFIG_HOME In addition to ~/.isort.cfg, isort will try to read a configuration file from $XDG_CONFIG_HOME/isort.cfg --- README.rst | 2 +- isort/settings.py | 24 +++++++++++++++++------- setup.py | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index cd55df31..3f54bfb6 100644 --- a/README.rst +++ b/README.rst @@ -228,7 +228,7 @@ Configuring isort If you find the default isort settings do not work well for your project, isort provides several ways to adjust the behavior. -To configure isort for a single user create a ``~/.isort.cfg`` file: +To configure isort for a single user create a ``~/.isort.cfg`` or ``$XDG_CONFIG_HOME/isort.cfg`` file: .. code-block:: ini diff --git a/isort/settings.py b/isort/settings.py index 79ae6c4c..2a06d76b 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -3,7 +3,7 @@ Defines how the default settings for isort should be loaded (First from the default setting dictionary at the top of the file, then overridden by any settings - in ~/.isort.cfg if there are any) + in ~/.isort.cfg or $XDG_CONFIG_HOME/isort.cfg if there are any) Copyright (C) 2013 Timothy Edmund Crosley @@ -33,6 +33,10 @@ import warnings from collections import namedtuple from distutils.util import strtobool +import appdirs +if appdirs.system == 'darwin': + appdirs.system = 'linux2' + from .pie_slice import lru_cache try: @@ -151,16 +155,22 @@ default = {'force_to_top': [], @lru_cache() def from_path(path): computed_settings = default.copy() - _update_settings_with_config(path, '.editorconfig', '~/.editorconfig', ('*', '*.py', '**.py'), computed_settings) - _update_settings_with_config(path, 'pyproject.toml', None, ('tool.isort', ), computed_settings) - _update_settings_with_config(path, '.isort.cfg', '~/.isort.cfg', ('settings', 'isort'), computed_settings) - _update_settings_with_config(path, 'setup.cfg', None, ('isort', 'tool:isort'), computed_settings) - _update_settings_with_config(path, 'tox.ini', None, ('isort', 'tool:isort'), computed_settings) + _update_settings_with_config(path, '.editorconfig', ['~/.editorconfig'], ('*', '*.py', '**.py'), computed_settings) + _update_settings_with_config(path, 'pyproject.toml', [], ('tool.isort', ), computed_settings) + _update_settings_with_config(path, '.isort.cfg', [appdirs.user_config_dir('isort.cfg'), '~/.isort.cfg'], ('settings', 'isort'), computed_settings) + _update_settings_with_config(path, 'setup.cfg', [], ('isort', 'tool:isort'), computed_settings) + _update_settings_with_config(path, 'tox.ini', [], ('isort', 'tool:isort'), computed_settings) return computed_settings def _update_settings_with_config(path, name, default, sections, computed_settings): - editor_config_file = default and os.path.expanduser(default) + editor_config_file = None + for path in default: + expanded = os.path.expanduser(path) + if os.path.exists(expanded): + editor_config_file = expanded + break + tries = 0 current_directory = path while current_directory and tries < MAX_CONFIG_SEARCH_DEPTH: diff --git a/setup.py b/setup.py index cb501f1a..95908a58 100755 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ setup(name='isort', 'pyproject': ['toml'], 'requirements': ['pip', 'pipreqs'], }, - install_requires=['futures; python_version < "3.2"'], + install_requires=['futures; python_version < "3.2"', 'appdirs'], python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*", keywords='Refactor, Python, Python2, Python3, Refactoring, Imports, Sort, Clean', classifiers=['Development Status :: 6 - Mature', -- cgit v1.2.1