Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
davebshow committed May 20, 2015
1 parent 766d2cd commit d8b5f85
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 0 deletions.
Empty file added NEWS.txt
Empty file.
39 changes: 39 additions & 0 deletions README.md
@@ -0,0 +1,39 @@
# ipython-gremlin 0.0.1

###A simple extension for the Gremlin Server based on [aiogremlin](https://pypi.python.org/pypi/aiogremlin/0.0.8). Works in both the IPython interpreter and the jupyter notebook. Requires Python 3.3 with Asyncio or Python 3.4.

[Example notebook](https://github.com/davebshow/ipython-gremlin/blob/master/example.ipynb)


Uses sessions to persist Gremlin across cells.

Example use:

```python
>>> %load_ext gremlin
>>> %gremlin 1 + 1

>>> %%gremlin
... graph = TinkerFactory.createModern()
... g = graph.traversal(standard())
... g.V().has('name','marko').out('knows').values('name')
...
['vadas', 'josh']

# store results in a variable like such:
>>> nodes = %gremlin g.V()
>>> nodes[0]
{'id': 1,
'label': 'person',
'properties': {'age': [{'id': 1, 'properties': {}, 'value': 29}],
'name': [{'id': 0, 'properties': {}, 'value': 'marko'}]},
'type': 'vertex'}

# Change to a new session.
>>> import uuid
>>> new_session = str(uuid.uuid4())
>>> %session $new_session

# g is not defined so now this will throw error.
>>> %gremlin g.V()
```
7 changes: 7 additions & 0 deletions README.txt
@@ -0,0 +1,7 @@
**alpha**


`Official Documentation`_


.. _Official Documentation: https://github.com/davebshow/ipython-gremlin
222 changes: 222 additions & 0 deletions example.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gremlin/__init__.py
@@ -0,0 +1,2 @@
from .magic import *
__version__ = "0.0.1"
53 changes: 53 additions & 0 deletions gremlin/magic.py
@@ -0,0 +1,53 @@
import asyncio
import uuid

import aiogremlin

# from IPython.config.configurable import Configurable
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
needs_local_scope)
# from IPython.utils.traitlets import Int, Float, Unicode, Bool


@magics_class
class GremlinMagic(Magics):
"""Provides the Gremlin magic. This will be configurable etc. (eventually)
"""
def __init__(self, shell):
super().__init__(shell)
self._loop = asyncio.get_event_loop()
self._pool = aiogremlin.WebSocketPool("ws://localhost:8182/",
loop=self._loop)
self._session = str(uuid.uuid4())

@needs_local_scope
@line_magic("gremlin")
@cell_magic("gremlin")
def execute(self, line, cell="", local_ns={}):
query = """{}\n{}""".format(line, cell)
query = query.lstrip("\n")
@asyncio.coroutine
def run():
results = []
with (yield from self._pool) as conn:
client = aiogremlin.GremlinClient(connection=conn,
processor="session",
loop=self._loop)
resp = yield from client.submit(query, session=self._session)
while True:
mssg = yield from resp.stream.read()
if mssg is None:
break
results += mssg.data
return results
return self._loop.run_until_complete(run())

@line_magic("session")
def session(self, line):
self._session = line
print(self._session)


def load_ipython_extension(ip):
"""Load the extension in IPython."""
ip.register_magics(GremlinMagic)
27 changes: 27 additions & 0 deletions setup.py
@@ -0,0 +1,27 @@
from setuptools import setup


setup(
name="ipython-gremlin",
version="0.0.1",
license="MIT",
author="davebshow",
author_email="davebshow@gmail.com",
url='https://github.com/davebshow/ipython-gremlin',
description="Runs scripts agains the Gremlin Server from IPython",
long_description=open("README.txt").read(),
packages=["gremlin"],
install_requires=[
"aiogremlin==0.0.8"
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3 :: Only'
]
)

0 comments on commit d8b5f85

Please sign in to comment.