code: def get_module_imports_on_top_of_file(source, import_line_index):
      
      def is_string_literal(line):
          if line[0] in 'uUbB':
              line = line[1:]
          if line and line[0] in 'rR':
              line = line[1:]
          return line and (line[0] == '"' or line[0] == "'")
  
      def is_future_import(line):
          nodes = ast.parse(line)
          for n in nodes.body:
              if isinstance(n, ast.ImportFrom) and n.module == '__future__':
                  return True
          return False
  
      allowed_try_keywords = ('try', 'except', 'else', 'finally')
      in_docstring = False
      docstring_kind = '"""'
      for cnt, line in enumerate(source):
          if not in_docstring:
              m = DOCSTRING_START_REGEX.match(line.lstrip())
              if m is not None:
                  in_docstring = True
                  docstring_kind = m.group('kind')
                  remain = line[m.end(): m.endpos].rstrip()
                  if remain[-3:] == docstring_kind:  # one line doc
                      in_docstring = False
                  continue
          if in_docstring:
              if line.rstrip()[-3:] == docstring_kind:
                  in_docstring = False
              continue
  
          if not line.rstrip():
              continue
          elif line.startswith('#'):
              continue
  
          if line.startswith('import ') or line.startswith('from '):
              if cnt == import_line_index or is_future_import(line):
                  continue
              return cnt
          elif pycodestyle.DUNDER_REGEX.match(line):
              continue
          elif any(line.startswith(kw) for kw in allowed_try_keywords):
              continue
          elif is_string_literal(line):
              return cnt
          else:
              return cnt
      return 0
docs: return import or from keyword position
  
      example:
        > 0: import sys
          1: import os
          2:
          3: def function():