nodes: 0, module; 1, function_definition; 2, function_name:order_by_refs; 3, parameters; 4, block; 5, identifier:envs; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, return_statement; 10, comment:"""
      Return topologicaly sorted list of environments.
      I.e. all referenced environments are placed before their references.
      """; 11, assignment; 12, assignment; 13, list_comprehension; 14, identifier:topology; 15, dictionary_comprehension; 16, identifier:by_name; 17, dictionary_comprehension; 18, subscript; 19, for_in_clause; 20, pair; 21, for_in_clause; 22, pair; 23, for_in_clause; 24, identifier:by_name; 25, identifier:name; 26, identifier:name; 27, call; 28, subscript; 29, call; 30, identifier:env; 31, identifier:envs; 32, subscript; 33, identifier:env; 34, identifier:env; 35, identifier:envs; 36, identifier:toposort_flatten; 37, argument_list; 38, identifier:env; 39, string; 40, identifier:set; 41, argument_list; 42, identifier:env; 43, string; 44, identifier:topology; 45, string_content:name; 46, subscript; 47, string_content:name; 48, identifier:env; 49, string; 50, string_content:refs
code: def order_by_refs(envs):
      """
      Return topologicaly sorted list of environments.
      I.e. all referenced environments are placed before their references.
      """
      topology = {
          env['name']: set(env['refs'])
          for env in envs
      }
      by_name = {
          env['name']: env
          for env in envs
      }
      return [
          by_name[name]
          for name in toposort_flatten(topology)
      ]