import itertools


def keep_oneway_edges_only(graph):
    """
    Given a starter graph, remove all edges that are not explicitly marked as 'oneway'.  Also removes nodes that are not
    connected to one-way edges.
    Args:
        graph (networkx graph): base graph
    Returns:
        networkx graph without one-way edges or nodes
    """
    g1 = graph.copy()

    # remove edges that are not oneway
    for e in list(g1.edges(data=True)):
        if ('oneway' not in e[2]) or (e[2]['oneway'] != True):
            g1.remove_edge(e[0], e[1])

    # remove nodes that are not connected to oneway edges
    keepnodes = list(itertools.chain(*list(g1.edges())))
    for node in set(g1.nodes) - set(keepnodes):
        g1.remove_node(node)
    return g1