Skip to content

Commit

Permalink
Added configurtion option and functionality for Bulbs to execute serv…
Browse files Browse the repository at this point in the history
…er-side Gremlin scripts.
  • Loading branch information
espeed committed Oct 26, 2013
1 parent 1e17391 commit 9bd62a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions bulbs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Config(object):
:ivar vertex_index: Name of the vertex index. Defaults to "vertex".
:ivar edge_index: Name of the edge index. Defaults to "edge".
:ivar autoindex: Enable auto indexing. Defaults to True.
:ivar server_scripts: Scripts are defined server side. Defaults to False.
Example:
Expand All @@ -59,6 +60,7 @@ def __init__(self, root_uri, username=None, password=None):
self.vertex_index = "vertex"
self.edge_index = "edge"
self.autoindex = True
self.server_scripts = False

# Set the default log level and log handler
self.set_logger(self.log_level, self.log_handler)
Expand Down
37 changes: 31 additions & 6 deletions bulbs/groovy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sre_parse
import sre_compile
from collections import namedtuple
from sre_constants import BRANCH, SUBPATTERN
import hashlib
from . import utils
Expand All @@ -15,13 +16,21 @@
# TODO: Simplify this. You don't need group pattern detection.



Method = namedtuple('Method', ['definition', 'signature', 'body', 'sha1'])

class GroovyScripts(object):
"""
Store and manage an index of Gremlin-Groovy scripts.
:parm config: Config object.
:type config: bulbs.Config
:param file_path: Path to the base Groovy scripts file.
:type file_path: str
:ivar config: Config object.
:ivar source_files: List containing the absolute paths to the script files,
in the order they were added.
:ivar methods: Dict mapping Groovy method names to the actual scripts.
Expand All @@ -34,7 +43,8 @@ class GroovyScripts(object):
#: Relative path to the default script file
default_file = "gremlin.groovy"

def __init__(self, file_path=None):
def __init__(self, config, file_path=None):
self.config = config
self.source_files = list() # an ordered set might be better

# methods format: methods[method_name] = method_body
Expand All @@ -44,6 +54,19 @@ def __init__(self, file_path=None):
file_path = self._get_default_file()
self.update(file_path)

def get_method(self, method_name):
"""
Returns a Python namedtuple for the Groovy script with the method name.
:param method_name: Name of a Groovy method.
:type method_name: str
:rtype: bulbs.groovy.Method
"""
return self.methods[method_name]


def get(self, method_name):
"""
Returns the Groovy script with the method name.
Expand All @@ -54,10 +77,11 @@ def get(self, method_name):
:rtype: str
"""
return self.methods[method_name]
#script = self._build_script(method_definition, method_signature)
#return script

method = self.methods[method_name]
script = method.signature if self.config.server_scripts is True else method.body
return script

def update(self, file_path):
"""
Updates the script index with the Groovy methods in the script file.
Expand Down Expand Up @@ -197,9 +221,10 @@ def add_method(self,scanner,token):
# control by just using the method_body, which the GSE compiles,
# creates a class out of, and stores in a classMap for reuse.
# You can't do imports inside Groovy methods so just using the func body
#sha1 = self._get_sha1(method_definition)
sha1 = self._get_sha1(method_definition)
#self.methods[method_name] = (method_signature, method_definition, sha1)
self.methods[method_name] = method_body
method = Method(method_definition, method_signature, method_body, sha1)
self.methods[method_name] = method

def _get_method_signature(self,method_definition):
pattern = '^def(.*){'
Expand Down
2 changes: 1 addition & 1 deletion bulbs/neo4jserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def __init__(self, config=None):
self.request = self.request_class(self.config, self.type_system.content_type)

# Neo4j supports Gremlin so include the Gremlin-Groovy script library
self.scripts = GroovyScripts()
self.scripts = GroovyScripts(self.config)

# Also include the Neo4j Server-specific Gremlin-Groovy scripts
scripts_file = get_file_path(__file__, "gremlin.groovy")
Expand Down
2 changes: 1 addition & 1 deletion bulbs/rexster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def __init__(self, config=None, db_name=None):
self.request = self.request_class(self.config, self.type_system.content_type)

# Rexster supports Gremlin so include the Gremlin-Groovy script library
self.scripts = GroovyScripts()
self.scripts = GroovyScripts(self.config)

# Also include the Rexster-specific Gremlin-Groovy scripts
scripts_file = get_file_path(__file__, "gremlin.groovy")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def run_tests():

setup (
name = 'bulbs',
version = '0.3.15',
version = '0.3.16',
url = 'https://github.com/espeed/bulbs',
license = 'BSD',
author = 'James Thornton',
Expand Down

0 comments on commit 9bd62a7

Please sign in to comment.