How to use the isort.settings function in isort

To help you get started, we’ve selected a few isort examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github timothycrosley / isort / isort / isort.py View on Github external
import_statement = self._wrap(self._add_comments(comments, import_start + from_imports.pop(0)))
                        for from_import in from_imports:
                            import_statement += "\n{0}{1}".format(import_start, from_import)
                            comments = None
                    else:
                        if "*" in from_imports:
                            section_output.append(self._add_comments(comments, "{0}*".format(import_start)))
                            from_imports.remove('*')
                            import_statement = import_start + (", ").join(from_imports)
                        else:
                            import_statement = self._add_comments(comments, import_start + (", ").join(from_imports))
                        if not from_imports:
                            import_statement = ""
                        if len(import_statement) > self.config['line_length']:
                            if len(from_imports) > 1:
                                output_mode = settings.WrapModes._fields[self.config.get('multi_line_output',
                                                                                         0)].lower()
                                formatter = getattr(self, "_output_" + output_mode, self._output_grid)
                                dynamic_indent = " " * (len(import_start) + 1)
                                indent = self.config['indent']
                                line_length = self.config['line_length']
                                import_statement = formatter(import_start, copy.copy(from_imports),
                                                            dynamic_indent, indent, line_length, comments)
                                if self.config['balanced_wrapping']:
                                    lines = import_statement.split("\n")
                                    line_count = len(lines)
                                    minimum_length = min([len(line) for line in lines[:-1]])
                                    new_import_statement = import_statement
                                    while (len(lines[-1]) < minimum_length and
                                           len(lines) == line_count and line_length > 10):
                                        import_statement = new_import_statement
                                        line_length -= 1
github thijsdezoete / sublime-text-isort-plugin / isort / isort.py View on Github external
from_imports.remove(from_import)
                            comments = None

                    if star_import:
                        import_statement = import_start + (", ").join(from_imports)
                    else:
                        import_statement = self._add_comments(comments, import_start + (", ").join(from_imports))
                    if not from_imports:
                        import_statement = ""
                    if len(import_statement) > self.config['line_length']:
                        import_statement = self._wrap(import_statement)
                    if len(from_imports) > 1 and (
                        len(import_statement) > self.config['line_length']
                        or self.config.get('force_grid_wrap')
                    ):
                        output_mode = settings.WrapModes._fields[self.config.get('multi_line_output',
                                                                                 0)].lower()
                        formatter = getattr(self, "_output_" + output_mode, self._output_grid)
                        dynamic_indent = " " * (len(import_start) + 1)
                        indent = self.config['indent']
                        line_length = self.config['wrap_length'] or self.config['line_length']
                        import_statement = formatter(import_start, copy.copy(from_imports),
                                                     dynamic_indent, indent, line_length, comments)
                        if self.config['balanced_wrapping']:
                            lines = import_statement.split("\n")
                            line_count = len(lines)
                            if len(lines) > 1:
                                minimum_length = min([len(line) for line in lines[:-1]])
                            else:
                                minimum_length = 0
                            new_import_statement = import_statement
                            while (len(lines[-1]) < minimum_length and
github thijsdezoete / sublime-text-isort-plugin / isort / isort.py View on Github external
def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, check=False,
                 show_diff=False, settings_path=None, **setting_overrides):

        if not settings_path and file_path:
            settings_path = os.path.dirname(os.path.abspath(file_path))
        settings_path = settings_path or os.getcwd()

        print(settings_path)
        self.config = settings.from_path(settings_path).copy()
        for key, value in itemsview(setting_overrides):
            access_key = key.replace('not_', '').lower()
            if type(self.config.get(access_key)) in (list, tuple):
                if key.startswith('not_'):
                    self.config[access_key] = list(set(self.config[access_key]).difference(value))
                else:
                    self.config[access_key] = list(set(self.config[access_key]).union(value))
            else:
                self.config[key] = value

        from pprint import pprint
        print('-' * 80)
        print('config')
        pprint(self.config)
        print('-' * 80)
github ESSS / esss_fix_format / src / esss_fix_format / cli.py View on Github external
except UnicodeDecodeError as e:
            msg = ': ERROR ({}: {})'.format(type(e).__name__, e)
            error_msg = click.format_filename(filename) + msg
            click.secho(error_msg, fg='red')
            errors.append(error_msg)
            return changed, errors, formatter

    new_contents = original_contents

    eol = _peek_eol(first_line)
    ends_with_eol = new_contents.endswith(eol)
    extension = os.path.normcase(os.path.splitext(filename)[1])

    if extension == '.py':
        settings_path = os.path.abspath(os.path.dirname(filename))
        settings_loaded = isort.settings.from_path(settings_path)
        if settings_loaded['line_length'] < 80:
            # The default isort configuration has 79 chars, so, if the passed
            # does not have more than that, complain that .isort.cfg is not configured.
            msg = ': ERROR .isort.cfg not available in repository (or line_length config < 80).'
            error_msg = click.format_filename(filename) + msg
            click.secho(error_msg, fg='red')
            errors.append(error_msg)

        sorter = isort.SortImports(file_contents=new_contents, settings_path=settings_path)
        # On older versions if the entire file is skipped (eg.: by an "isort:skip_file")
        # instruction in the docstring, SortImports doesn't even contain an "output" attribute.
        # In some recent versions it is `None`.
        new_contents = getattr(sorter, 'output', None)
        if new_contents is None:
            new_contents = original_contents
github timothycrosley / isort / isort / isort.py View on Github external
def __init__(self, file_path=None, file_contents=None, write_to_stdout=False, check=False,
                 show_diff=False, settings_path=None, **setting_overrides):

        if not settings_path and file_path:
            settings_path = os.path.dirname(os.path.abspath(file_path))
        settings_path = settings_path or os.getcwd()

        self.config = settings.from_path(settings_path).copy()
        for key, value in itemsview(setting_overrides):
            access_key = key.replace('not_', '').lower()
            if type(self.config.get(access_key)) in (list, tuple):
                if key.startswith('not_'):
                    self.config[access_key] = list(set(self.config[access_key]).difference(value))
                else:
                    self.config[access_key] = list(set(self.config[access_key]).union(value))
            else:
                self.config[key] = value


        if _importmagic.installed and self.config['magic_add'] or self.config['magic_remove']:
            global _IMPORT_MAGIC_INDEX
            if not _IMPORT_MAGIC_INDEX:
                _IMPORT_MAGIC_INDEX = _importmagic.index.SymbolIndex()
                _IMPORT_MAGIC_INDEX.build_index(PYTHONPATH)