|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import networkx as nx |
| 6 | +import collections |
| 7 | + |
| 8 | +AddressType = collections.namedtuple("AddressType", ("prefix", "type")) |
| 9 | + |
| 10 | + |
| 11 | +def _type_prefix_match(address_types, address): |
| 12 | + """For a given address, find the type matching the address. |
| 13 | +
|
| 14 | + Takes an object containing an array of {prefix, type} pairs, and |
| 15 | + an address. Returns the first type whose corresponding prefix |
| 16 | + was a prefix of the given address. |
| 17 | + """ |
| 18 | + for address_type in address_types: |
| 19 | + prefix = address_type.prefix |
| 20 | + if address[: len(prefix)] == prefix: |
| 21 | + return address_type.type |
| 22 | + raise ValueError("No matching prefix for {}".format(address)) |
| 23 | + |
| 24 | + |
| 25 | +def node_type(address): |
| 26 | + """Return a string that identifies the "type" of a SourceCred node. |
| 27 | +
|
| 28 | + For any anticipated SourceCred node (i.e., it was supplied by one of the two |
| 29 | + standard SourceCred plugins, i.e., sourcecred/git and sourcecred/github), |
| 30 | + this method returns a string which identifies the most specific declared |
| 31 | + node_type that matches the given node. |
| 32 | +
|
| 33 | + The SourceCred type system is still pretty ad-hoc, |
| 34 | + (see: https://github.com/sourcecred/sourcecred/issues/710), so this system is |
| 35 | + likely to change in the future. |
| 36 | + """ |
| 37 | + NODE_PREFIX_TO_TYPE = [ |
| 38 | + AddressType(prefix=["sourcecred", "github", "REPO"], type="github/repo"), |
| 39 | + AddressType( |
| 40 | + prefix=["sourcecred", "github", "USERLIKE", "USER"], type="github/user" |
| 41 | + ), |
| 42 | + AddressType( |
| 43 | + prefix=["sourcecred", "github", "USERLIKE", "BOT"], type="github/bot" |
| 44 | + ), |
| 45 | + AddressType(prefix=["sourcecred", "github", "PULL"], type="github/pull"), |
| 46 | + AddressType(prefix=["sourcecred", "github", "ISSUE"], type="github/issue"), |
| 47 | + AddressType(prefix=["sourcecred", "github", "REVIEW"], type="github/review"), |
| 48 | + AddressType(prefix=["sourcecred", "github", "COMMENT"], type="github/comment"), |
| 49 | + AddressType(prefix=["sourcecred", "git", "COMMIT"], type="git/commit"), |
| 50 | + ] |
| 51 | + return _type_prefix_match(NODE_PREFIX_TO_TYPE, address) |
| 52 | + |
| 53 | + |
| 54 | +def edge_type(address): |
| 55 | + """Return a string that identifies the "type" of a SourceCred edge. |
| 56 | +
|
| 57 | + For any anticipated SourceCred edge (i.e., it was supplied by one of the two |
| 58 | + standard SourceCred plugins, i.e., sourcecred/git and sourcecred/github), |
| 59 | + this method returns a string which identifies the most specific declared |
| 60 | + edge_type that matches the given node. |
| 61 | +
|
| 62 | + The SourceCred type system is still pretty ad-hoc, |
| 63 | + (see: https://github.com/sourcecred/sourcecred/issues/710), so this system is |
| 64 | + likely to change in the future. |
| 65 | + """ |
| 66 | + EDGE_PREFIX_TO_TYPE = [ |
| 67 | + AddressType( |
| 68 | + prefix=["sourcecred", "github", "HAS_PARENT"], type="github/hasParent" |
| 69 | + ), |
| 70 | + AddressType( |
| 71 | + prefix=["sourcecred", "github", "REFERENCES"], type="github/references" |
| 72 | + ), |
| 73 | + AddressType( |
| 74 | + prefix=["sourcecred", "github", "MENTIONS_AUTHOR"], |
| 75 | + type="github/mentionsAuthor", |
| 76 | + ), |
| 77 | + AddressType(prefix=["sourcecred", "github", "AUTHORS"], type="github/authors"), |
| 78 | + AddressType(prefix=["sourcecred", "github", "PULL"], type="github/pull"), |
| 79 | + AddressType(prefix=["sourcecred", "github", "ISSUE"], type="github/issue"), |
| 80 | + AddressType(prefix=["sourcecred", "github", "REVIEW"], type="github/review"), |
| 81 | + AddressType(prefix=["sourcecred", "github", "COMMENT"], type="github/comment"), |
| 82 | + AddressType( |
| 83 | + prefix=["sourcecred", "github", "MERGED_AS"], type="github/mergedAs" |
| 84 | + ), |
| 85 | + AddressType( |
| 86 | + prefix=["sourcecred", "github", "REACTS", "HOORAY"], |
| 87 | + type="github/reactsHooray", |
| 88 | + ), |
| 89 | + AddressType( |
| 90 | + prefix=["sourcecred", "github", "REACTS", "THUMBS_UP"], |
| 91 | + type="github/reactsThumbsUp", |
| 92 | + ), |
| 93 | + AddressType( |
| 94 | + prefix=["sourcecred", "github", "REACTS", "HEART"], |
| 95 | + type="github/reactsHeart", |
| 96 | + ), |
| 97 | + AddressType( |
| 98 | + prefix=["sourcecred", "github", "REACTS", "ROCKET"], |
| 99 | + type="github/reactsRocket", |
| 100 | + ), |
| 101 | + AddressType(prefix=["sourcecred", "git", "HAS_PARENT"], type="git/hasParent"), |
| 102 | + ] |
| 103 | + return _type_prefix_match(EDGE_PREFIX_TO_TYPE, address) |
| 104 | + |
| 105 | + |
| 106 | +def json_to_graph(json): |
| 107 | + """Convert a serialized SourceCred graph to a MultiDiGraph. |
| 108 | +
|
| 109 | + Takes in a Python dict representing a SourceCred graph json. |
| 110 | + Returns a networkx MultiDiGraph, with node and edge type identifiers |
| 111 | + added as an additional property. |
| 112 | + """ |
| 113 | + [compat, data] = json |
| 114 | + assert compat["type"] == "sourcecred/graph", compat |
| 115 | + assert compat["version"] == "0.4.0", compat |
| 116 | + |
| 117 | + def nodePropertyDict(address): |
| 118 | + return {"address": tuple(address), "type": node_type(address)} |
| 119 | + |
| 120 | + def edgePropertyDict(address): |
| 121 | + return {"address": tuple(address), "type": edge_type(address)} |
| 122 | + |
| 123 | + nodes = data["nodes"] |
| 124 | + edges = data["edges"] |
| 125 | + g = nx.MultiDiGraph() |
| 126 | + for (i, n) in enumerate(nodes): |
| 127 | + g.add_node(i, **nodePropertyDict(n)) |
| 128 | + for e in edges: |
| 129 | + g.add_edge(e["srcIndex"], e["dstIndex"], **edgePropertyDict(e["address"])) |
| 130 | + return g |
0 commit comments