def post(G, username, password, group=None): ''' Post a graph to graphspace. If this graph is new, share it with the group. Inputs: Graph (GSGraph Object) Outputs: the graph ID (int) ''' print('Posting graph...') # connect to GraphSpace with the username and password. gs = GraphSpace(username, password) try: # try updating the graph. If the graph does not exist, # this will throw an error. graph = gs.update_graph(G) except: # catch the error and try posting a new graph. graph = gs.post_graph(G) if group: # share this graph with the group. gs.share_graph(graph_id=graph.id, group_name=group) return graph.id
def post_graph_to_graphspace(G, username, password, graph_name, apply_layout=None, layout_name='layout1', group=None, group_id=None, make_public=None): """ Post a graph to graphspace and perform other layout and sharing tasks *G*: Costructed GSGraph object *username*: GraphSpace username *password*: GraphSpace password *graph_name*: Name to give to graph when posting. If a graph with that name already exists, it will be updated *apply_layout*: Graph name to check for x and y positions of a layout (layout_name) and apply them to nodes of this graph *layout_name*: Name of layout to check for in the apply_layout graph. Default: 'layout1' *group*: Name of group to share graph with *group_id*: Not implemented yet. ID of group to share graph with. Could be useful if two groups have the same name *make_public*: Make the graph public """ # post to graphspace gs = GraphSpace(username, password) print("\nPosting graph '%s' to graphspace\n" % (graph_name)) gs_graph = gs.get_graph(graph_name, owner_email=username) layout = None # I often use the layout 'layout1', so I set that as the default # first check if the x and y coordinates should be set from another graph if apply_layout is not None: # if a layout was created for a different graph name, try to copy that layout here print("checking if layout '%s' exists for graph %s" % (layout_name, apply_layout)) layout = gs.get_graph_layout(graph_name=apply_layout, layout_name=layout_name) # if the graph already exists, see if the layout can be copied if gs_graph is not None: print("checking if layout '%s' exists for this graph (%s)" % (layout_name, graph_name)) layout = gs.get_graph_layout(graph=gs_graph, layout_name=layout_name) # now apply the layout if applicable if layout is not None: # set the x and y position of each node in the updated graph to the x and y positions of the layout you created print( "Setting the x and y coordinates of each node to the positions in %s" % (layout_name)) for node, positions in layout.positions_json.items(): G.set_node_position(node_name=node, x=positions['x'], y=positions['y']) # also check nodes that may have added a little more to the name for node in G.nodes(): for n2, positions in layout.positions_json.items(): # remove the newline from the node name if its there n2 = n2.split('\n')[0] if n2 in node: G.set_node_position(node_name=node, x=positions['x'], y=positions['y']) if gs_graph is None: print("\nPosting graph '%s' to graphspace\n" % (graph_name)) gsgraph = gs.post_graph(G) else: # "re-post" or update the graph print("\nGraph '%s' already exists. Updating it\n" % (graph_name)) gsgraph = gs.update_graph(G, graph_name=graph_name, owner_email=username) if make_public is True: print("Making graph '%s' public." % (graph_name)) gsgraph = gs.publish_graph(graph=G) print(gsgraph.url) # TODO implement the group_id. This will allow a user to share a graph with a group that is not their own if group is not None: # create the group if it doesn't exist #group = gs.post_group(GSGroup(name='icsb2017', description='sample group')) # or get the group you already created it print("sharing graph with group '%s'" % (group)) group = gs.get_group(group_name=group) #print(group.url) gs.share_graph(graph=gs_graph, group=group)
def test_share_graph(graph_id, group_id): graphspace = GraphSpace('*****@*****.**', 'user1') response = graphspace.share_graph(graph_id=graph_id, group_id=group_id) assert 'graph_id' in response and response['graph_id'] == graph_id assert 'group_id' in response and response['group_id'] == str(group_id)