def _wrap(self, line: str) -> str:
        """
            Returns an import wrapped to the specified line-length, if possible.
        """
        wrap_mode = self.config['multi_line_output']
        if len(line) > self.config['line_length'] and wrap_mode != WrapModes.NOQA:
            line_without_comment = line
            comment = None
            if '#' in line:
                line_without_comment, comment = line.split('#', 1)
            for splitter in ("import ", ".", "as "):
                exp = r"\b" + re.escape(splitter) + r"\b"
                if re.search(exp, line_without_comment) and not line_without_comment.strip().startswith(splitter):
                    line_parts = re.split(exp, line_without_comment)
                    if comment:
                        line_parts[-1] = '{0}#{1}'.format(line_parts[-1], comment)
                    next_line = []
                    while (len(line) + 2) > (self.config['wrap_length'] or self.config['line_length']) and line_parts:
                        next_line.append(line_parts.pop())
                        line = splitter.join(line_parts)
                    if not line:
                        line = next_line.pop()

                    cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip())
                    if self.config['use_parentheses']:
                        output = "{0}{1}({2}{3}{4}{5})".format(
                            line, splitter, self.line_separator, cont_line,
                            "," if self.config['include_trailing_comma'] else "",
                            self.line_separator if wrap_mode in {WrapModes.VERTICAL_HANGING_INDENT,
                                                                 WrapModes.VERTICAL_GRID_GROUPED}
                            else "")
                        lines = output.split(self.line_separator)
                        if self.config['comment_prefix'] in lines[-1] and lines[-1].endswith(')'):
                            line, comment = lines[-1].split(self.config['comment_prefix'], 1)
                            lines[-1] = line + ')' + self.config['comment_prefix'] + comment[:-1]
                        return self.line_separator.join(lines)
                    return "{0}{1}\\{2}{3}".format(line, splitter, self.line_separator, cont_line)
        elif len(line) > self.config['line_length'] and wrap_mode == settings.WrapModes.NOQA:
            if "# NOQA" not in line:
                return "{0}{1} NOQA".format(line, self.config['comment_prefix'])

        return line
def _wrap ( self , line : str ) -> str : wrap_mode = self . config [ 'multi_line_output' ] if len ( line ) > self . config [ 'line_length' ] and wrap_mode != WrapModes . NOQA : line_without_comment = line comment = None if '#' in line : line_without_comment , comment = line . split ( '#' , 1 ) for splitter in ( "import " , "." , "as " ) : exp = r"\b" + re . escape ( splitter ) + r"\b" if re . search ( exp , line_without_comment ) and not line_without_comment . strip ( ) . startswith ( splitter ) : line_parts = re . split ( exp , line_without_comment ) if comment : line_parts [ - 1 ] = '{0}#{1}' . format ( line_parts [ - 1 ] , comment ) next_line = [ ] while ( len ( line ) + 2 ) > ( self . config [ 'wrap_length' ] or self . config [ 'line_length' ] ) and line_parts : next_line . append ( line_parts . pop ( ) ) line = splitter . join ( line_parts ) if not line : line = next_line . pop ( ) cont_line = self . _wrap ( self . config [ 'indent' ] + splitter . join ( next_line ) . lstrip ( ) ) if self . config [ 'use_parentheses' ] : output = "{0}{1}({2}{3}{4}{5})" . format ( line , splitter , self . line_separator , cont_line , "," if self . config [ 'include_trailing_comma' ] else "" , self . line_separator if wrap_mode in { WrapModes . VERTICAL_HANGING_INDENT , WrapModes . VERTICAL_GRID_GROUPED } else "" ) lines = output . split ( self . line_separator ) if self . config [ 'comment_prefix' ] in lines [ - 1 ] and lines [ - 1 ] . endswith ( ')' ) : line , comment = lines [ - 1 ] . split ( self . config [ 'comment_prefix' ] , 1 ) lines [ - 1 ] = line + ')' + self . config [ 'comment_prefix' ] + comment [ : - 1 ] return self . line_separator . join ( lines ) return "{0}{1}\\{2}{3}" . format ( line , splitter , self . line_separator , cont_line ) elif len ( line ) > self . config [ 'line_length' ] and wrap_mode == settings . WrapModes . NOQA : if "# NOQA" not in line : return "{0}{1} NOQA" . format ( line , self . config [ 'comment_prefix' ] ) return line
Returns an import wrapped to the specified line-length, if possible.