parameters: (self)
docstring: Adds the imports back to the file.
  
          (at the index of the first import) sorted alphabetically and split between groups
docstring_summary: Adds the imports back to the file.
docstring_tokens: Adds the imports back to the file .
function: def _add_formatted_imports(self):
          """Adds the imports back to the file.
  
          (at the index of the first import) sorted alphabetically and split between groups
  
          """
          sort_ignore_case = self.config['force_alphabetical_sort_within_sections']
          sections = itertools.chain(self.sections, self.config['forced_separate'])
  
          if self.config['no_sections']:
              self.imports['no_sections'] = {'straight': [], 'from': {}}
              for section in sections:
                  self.imports['no_sections']['straight'].extend(self.imports[section].get('straight', []))
                  self.imports['no_sections']['from'].update(self.imports[section].get('from', {}))
              sections = ('no_sections', )
  
          output = []
          for section in sections:
              straight_modules = self.imports[section]['straight']
              straight_modules = nsorted(straight_modules, key=lambda key: self._module_key(key, self.config))
              from_modules = self.imports[section]['from']
              from_modules = nsorted(from_modules, key=lambda key: self._module_key(key, self.config))
  
              section_output = []
              if self.config['from_first']:
                  self._add_from_imports(from_modules, section, section_output, sort_ignore_case)
                  if self.config['lines_between_types'] and from_modules and straight_modules:
                      section_output.extend([''] * self.config['lines_between_types'])
                  self._add_straight_imports(straight_modules, section, section_output)
              else:
                  self._add_straight_imports(straight_modules, section, section_output)
                  if self.config['lines_between_types'] and from_modules and straight_modules:
                      section_output.extend([''] * self.config['lines_between_types'])
                  self._add_from_imports(from_modules, section, section_output, sort_ignore_case)
  
              if self.config['force_sort_within_sections']:
                  def by_module(line):
                      section = 'B'
                      if line.startswith('#'):
                          return 'AA'
  
                      line = re.sub('^from ', '', line)
                      line = re.sub('^import ', '', line)
                      if line.split(' ')[0] in self.config['force_to_top']:
                          section = 'A'
                      if not self.config['order_by_type']:
                          line = line.lower()
                      return '{0}{1}'.format(section, line)
                  section_output = nsorted(section_output, key=by_module)
  
              if section_output:
                  section_name = section
                  if section_name in self.place_imports:
                      self.place_imports[section_name] = section_output
                      continue
  
                  section_title = self.config.get('import_heading_' + str(section_name).lower(), '')
                  if section_title:
                      section_comment = "# {0}".format(section_title)
                      if not section_comment in self.out_lines[0:1] and not section_comment in self.in_lines[0:1]:
                          section_output.insert(0, section_comment)
                  output += section_output + ([''] * self.config['lines_between_sections'])
  
          while [character.strip() for character in output[-1:]] == [""]:
              output.pop()
  
          output_at = 0
          if self.import_index < self.original_length:
              output_at = self.import_index
          elif self._first_comment_index_end != -1 and self._first_comment_index_start <= 2:
              output_at = self._first_comment_index_end
          self.out_lines[output_at:0] = output
  
          imports_tail = output_at + len(output)
          while [character.strip() for character in self.out_lines[imports_tail: imports_tail + 1]] == [""]:
              self.out_lines.pop(imports_tail)
  
          if len(self.out_lines) > imports_tail:
              next_construct = ""
              self._in_quote = False
              tail = self.out_lines[imports_tail:]
              for index, line in enumerate(tail):
                  if not self._skip_line(line) and line.strip():
                      if line.strip().startswith("#") and len(tail) > (index + 1) and tail[index + 1].strip():
                          continue
                      next_construct = line
                      break
  
              if self.config['lines_after_imports'] != -1:
                  self.out_lines[imports_tail:0] = ["" for line in range(self.config['lines_after_imports'])]
              elif next_construct.startswith("def") or next_construct.startswith("class") or \
                 next_construct.startswith("@") or next_construct.startswith("async def"):
                  self.out_lines[imports_tail:0] = ["", ""]
              else:
                  self.out_lines[imports_tail:0] = [""]
  
          if self.place_imports:
              new_out_lines = []
              for index, line in enumerate(self.out_lines):
                  new_out_lines.append(line)
                  if line in self.import_placements:
                      new_out_lines.extend(self.place_imports[self.import_placements[line]])
                      if len(self.out_lines) <= index or self.out_lines[index + 1].strip() != "":
                          new_out_lines.append("")
              self.out_lines = new_out_lines
function_tokens: def _add_formatted_imports ( self ) : sort_ignore_case = self . config [ 'force_alphabetical_sort_within_sections' ] sections = itertools . chain ( self . sections , self . config [ 'forced_separate' ] ) if self . config [ 'no_sections' ] : self . imports [ 'no_sections' ] = { 'straight' : [ ] , 'from' : { } } for section in sections : self . imports [ 'no_sections' ] [ 'straight' ] . extend ( self . imports [ section ] . get ( 'straight' , [ ] ) ) self . imports [ 'no_sections' ] [ 'from' ] . update ( self . imports [ section ] . get ( 'from' , { } ) ) sections = ( 'no_sections' , ) output = [ ] for section in sections : straight_modules = self . imports [ section ] [ 'straight' ] straight_modules = nsorted ( straight_modules , key = lambda key : self . _module_key ( key , self . config ) ) from_modules = self . imports [ section ] [ 'from' ] from_modules = nsorted ( from_modules , key = lambda key : self . _module_key ( key , self . config ) ) section_output = [ ] if self . config [ 'from_first' ] : self . _add_from_imports ( from_modules , section , section_output , sort_ignore_case ) if self . config [ 'lines_between_types' ] and from_modules and straight_modules : section_output . extend ( [ '' ] * self . config [ 'lines_between_types' ] ) self . _add_straight_imports ( straight_modules , section , section_output ) else : self . _add_straight_imports ( straight_modules , section , section_output ) if self . config [ 'lines_between_types' ] and from_modules and straight_modules : section_output . extend ( [ '' ] * self . config [ 'lines_between_types' ] ) self . _add_from_imports ( from_modules , section , section_output , sort_ignore_case ) if self . config [ 'force_sort_within_sections' ] : def by_module ( line ) : section = 'B' if line . startswith ( '#' ) : return 'AA' line = re . sub ( '^from ' , '' , line ) line = re . sub ( '^import ' , '' , line ) if line . split ( ' ' ) [ 0 ] in self . config [ 'force_to_top' ] : section = 'A' if not self . config [ 'order_by_type' ] : line = line . lower ( ) return '{0}{1}' . format ( section , line ) section_output = nsorted ( section_output , key = by_module ) if section_output : section_name = section if section_name in self . place_imports : self . place_imports [ section_name ] = section_output continue section_title = self . config . get ( 'import_heading_' + str ( section_name ) . lower ( ) , '' ) if section_title : section_comment = "# {0}" . format ( section_title ) if not section_comment in self . out_lines [ 0 : 1 ] and not section_comment in self . in_lines [ 0 : 1 ] : section_output . insert ( 0 , section_comment ) output += section_output + ( [ '' ] * self . config [ 'lines_between_sections' ] ) while [ character . strip ( ) for character in output [ - 1 : ] ] == [ "" ] : output . pop ( ) output_at = 0 if self . import_index < self . original_length : output_at = self . import_index elif self . _first_comment_index_end != - 1 and self . _first_comment_index_start <= 2 : output_at = self . _first_comment_index_end self . out_lines [ output_at : 0 ] = output imports_tail = output_at + len ( output ) while [ character . strip ( ) for character in self . out_lines [ imports_tail : imports_tail + 1 ] ] == [ "" ] : self . out_lines . pop ( imports_tail ) if len ( self . out_lines ) > imports_tail : next_construct = "" self . _in_quote = False tail = self . out_lines [ imports_tail : ] for index , line in enumerate ( tail ) : if not self . _skip_line ( line ) and line . strip ( ) : if line . strip ( ) . startswith ( "#" ) and len ( tail ) > ( index + 1 ) and tail [ index + 1 ] . strip ( ) : continue next_construct = line break if self . config [ 'lines_after_imports' ] != - 1 : self . out_lines [ imports_tail : 0 ] = [ "" for line in range ( self . config [ 'lines_after_imports' ] ) ] elif next_construct . startswith ( "def" ) or next_construct . startswith ( "class" ) or next_construct . startswith ( "@" ) or next_construct . startswith ( "async def" ) : self . out_lines [ imports_tail : 0 ] = [ "" , "" ] else : self . out_lines [ imports_tail : 0 ] = [ "" ] if self . place_imports : new_out_lines = [ ] for index , line in enumerate ( self . out_lines ) : new_out_lines . append ( line ) if line in self . import_placements : new_out_lines . extend ( self . place_imports [ self . import_placements [ line ] ] ) if len ( self . out_lines ) <= index or self . out_lines [ index + 1 ] . strip ( ) != "" : new_out_lines . append ( "" ) self . out_lines = new_out_lines