Skip to content

Commit

Permalink
Merge pull request #806 from r-darwish/xdg
Browse files Browse the repository at this point in the history
Read configuration from XDG_CONFIG_HOME
  • Loading branch information
timothycrosley authored Feb 23, 2019
2 parents 94ed1dd + 9e07c6d commit 61f3cde
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,6 +34,8 @@
from collections import namedtuple
from distutils.util import strtobool

import appdirs

from .pie_slice import lru_cache
from .utils import difference, union

Expand All @@ -47,6 +49,9 @@
except ImportError:
toml = False

if appdirs.system == 'darwin':
appdirs.system = 'linux2'

MAX_CONFIG_SEARCH_DEPTH = 25 # The number of parent directories isort will look for a config file within
DEFAULT_SECTIONS = ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER')

Expand Down Expand Up @@ -159,16 +164,22 @@
@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 potential_settings_path in default:
expanded = os.path.expanduser(potential_settings_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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
'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',
Expand Down

0 comments on commit 61f3cde

Please sign in to comment.