@util.positional(2)
def find_definition(name, relative_to=None, importer=__import__):
 if (not ((relative_to is None) or isinstance(relative_to, types.ModuleType) or (isinstance(relative_to, type) and issubclass(relative_to, Message)))):
  raise TypeError(('relative_to must be None, Message definition or module.  Found: %s' % relative_to))
 name_path = name.split('.')
 if (not name_path[0]):
  relative_to = None
  name_path = name_path[1:]
 def search_path():
  'Performs a single iteration searching the path from relative_to.\n\n    This is the function that searches up the path from a relative object.\n\n      fully.qualified.object . relative.or.nested.Definition\n                               ---------------------------->\n                                                  ^\n                                                  |\n                            this part of search --+\n\n    Returns:\n      Message or Enum at the end of name_path, else None.\n    '
  next = relative_to
  for node in name_path:
   attribute = getattr(next, node, None)
   if (attribute is not None):
    next = attribute
   elif ((next is None) or isinstance(next, types.ModuleType)):
    if (next is None):
     module_name = node
    else:
     module_name = ('%s.%s' % (next.__name__, node))
    try:
     fromitem = module_name.split('.')[(-1)]
     next = importer(module_name, '', '', [str(fromitem)])
    except ImportError:
     return None
   else:
    return None
   if ((not isinstance(next, types.ModuleType)) and (not (isinstance(next, type) and issubclass(next, (Message, Enum))))):
    return None
  return next
 while True:
  found = search_path()
  if (isinstance(found, type) and issubclass(found, (Enum, Message))):
   return found
  elif (relative_to is None):
   raise DefinitionNotFoundError(('Could not find definition for %s' % (name,)))
  elif isinstance(relative_to, types.ModuleType):
   module_path = relative_to.__name__.split('.')[:(-1)]
   if (not module_path):
    relative_to = None
   else:
    relative_to = importer('.'.join(module_path), '', '', [module_path[(-1)]])
  elif (isinstance(relative_to, type) and issubclass(relative_to, Message)):
   parent = relative_to.message_definition()
   if (parent is None):
    last_module_name = relative_to.__module__.split('.')[(-1)]
    relative_to = importer(relative_to.__module__, '', '', [last_module_name])
   else:
    relative_to = parent

@ util . positional ( 2 ) def find_definition ( name , relative_to = None , importer = __import__ ) : if ( not ( ( relative_to is None ) or isinstance ( relative_to , types . ModuleType ) or ( isinstance ( relative_to , type ) and issubclass ( relative_to , Message ) ) ) ) : raise TypeError ( ( 'relative_to must be None, Message definition or module.  Found: %s' % relative_to ) ) name_path = name . split ( '.' ) if ( not name_path [ 0 ] ) : relative_to = None name_path = name_path [ 1 : ] def search_path ( ) : next = relative_to for node in name_path : attribute = getattr ( next , node , None ) if ( attribute is not None ) : next = attribute elif ( ( next is None ) or isinstance ( next , types . ModuleType ) ) : if ( next is None ) : module_name = node else : module_name = ( '%s.%s' % ( next . __name__ , node ) ) try : fromitem = module_name . split ( '.' ) [ ( - 1 ) ] next = importer ( module_name , '' , '' , [ str ( fromitem ) ] ) except ImportError : return None else : return None if ( ( not isinstance ( next , types . ModuleType ) ) and ( not ( isinstance ( next , type ) and issubclass ( next , ( Message , Enum ) ) ) ) ) : return None return next while True : found = search_path ( ) if ( isinstance ( found , type ) and issubclass ( found , ( Enum , Message ) ) ) : return found elif ( relative_to is None ) : raise DefinitionNotFoundError ( ( 'Could not find definition for %s' % ( name , ) ) ) elif isinstance ( relative_to , types . ModuleType ) : module_path = relative_to . __name__ . split ( '.' ) [ : ( - 1 ) ] if ( not module_path ) : relative_to = None else : relative_to = importer ( '.' . join ( module_path ) , '' , '' , [ module_path [ ( - 1 ) ] ] ) elif ( isinstance ( relative_to , type ) and issubclass ( relative_to , Message ) ) : parent = relative_to . message_definition ( ) if ( parent is None ) : last_module_name = relative_to . __module__ . split ( '.' ) [ ( - 1 ) ] relative_to = importer ( relative_to . __module__ , '' , '' , [ last_module_name ] ) else : relative_to = parent
return the definition location of the python name at offset a location object is returned if the definition location can be determined .