Last active
August 29, 2015 14:08
-
-
Save alienrobotwizard/b7d24cf57a3ae414b4ed to your computer and use it in GitHub Desktop.
Demonstrate Ease of Lipstick 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import copy | |
import json | |
from urllib3 import PoolManager | |
from apiclient import APIClient | |
class Lipstick(APIClient): | |
def __init__(self, base_url, rate_limit_lock=None): | |
self.BASE_URL = base_url | |
self.pool = PoolManager() | |
APIClient.__init__(self, rate_limit_lock) | |
def post(self, path, data, **params): | |
return self.pool.urlopen('POST', | |
self._compose_url(path, params), | |
headers={'Content-Type':'application/json'}, | |
body=data) | |
def get(self, path, **params): | |
return self._request('GET', path, params=params) | |
def put(self, path, **params): | |
return self._request('PUT', path, params=params) | |
class Node: | |
def __init__(self, id, type='Plain'): | |
self.id = id; | |
self.properties = {} | |
self.type = type | |
def setProperty(self, name, value): | |
self.properties[name] = value | |
return self | |
class Edge: | |
def __init__(self, u, v, type='Plain'): | |
self.u = u | |
self.v = v | |
self.properties = {} | |
class Graph: | |
def __init__(self, id, name): | |
self.id = id | |
self.name = name | |
self.properties = {} | |
self.nodes = {} | |
self.edges = {} | |
self.node_groups = {} | |
def addNodes(self, nodes): | |
[self.addNode(node) for node in nodes] | |
return self | |
def addNode(self, node): | |
self.nodes[node.id] = node | |
return self | |
def addEdges(self, edges): | |
[self.addEdge(edge) for edge in edges] | |
return self | |
def addEdge(self, edge): | |
self.edges[edge.u+edge.v] = edge | |
return self | |
def to_json(self): | |
data = copy.copy(self.__dict__) | |
data['nodes'] = [node.__dict__ for node in self.nodes.values()] | |
data['edges'] = [edge.__dict__ for edge in self.edges.values()] | |
data['node_groups'] = [] | |
return json.dumps(data) | |
class Template: | |
def __init__(self, name, view=None, template=None): | |
self.name = name | |
self.view = view | |
self.template = template | |
def loadView(self, viewFile): | |
self.view = open(viewFile).read() | |
return self | |
def loadTemplate(self, templateFile): | |
self.template = open(templateFile).read() | |
return self | |
def to_json(self): | |
return json.dumps(self.__dict__) | |
if __name__ == '__main__': | |
g = Graph('1', 'mygraph') | |
g.addNode( | |
Node('a').setProperty('name', 'node A') | |
).addNode( | |
Node('b').setProperty('name', 'node B') | |
) | |
g.addEdge(Edge('a','b')).addEdge(Edge('b','a')) | |
t = Template('Plain').loadView('Plain.js').loadTemplate('Plain.mustache') | |
lipstick = Lipstick('http://localhost:9292/') | |
r = lipstick.post('template/Plain', t.to_json()) | |
print r.data | |
r = lipstick.post('v1/job', g.to_json()) | |
print r.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Plain(properties) { | |
var self = this; | |
self.name = properties.name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div style="border-radius: 50%; background-color: #e6d8b1"> | |
<h2 style="padding: 10px;">{{name}}</h2> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment