Skip to content

Commit

Permalink
update to python 3 and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Aug 20, 2021
1 parent 9d87be5 commit a6656ae
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 246 deletions.
46 changes: 23 additions & 23 deletions pyorient/groovy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from .scripts import ScriptFunction
from .utils import u


#
# The scanner code came from the TED project.
#
Expand All @@ -51,23 +52,23 @@ def __init__(self, lexicon, flags=0):
self.lexicon = lexicon
self.group_pattern = self._get_group_pattern(flags)

def _get_group_pattern(self,flags):
def _get_group_pattern(self, flags):
# combine phrases into a compound pattern
patterns = []
sub_pattern = sre_parse.Pattern()
sub_pattern.flags = flags
for phrase, action in self.lexicon:
patterns.append(sre_parse.SubPattern(sub_pattern, [
(SUBPATTERN, (len(patterns) + 1, sre_parse.parse(phrase, flags))),
]))
#sub_pattern.groups = len(patterns) + 1
]))
# sub_pattern.groups = len(patterns) + 1
group_pattern = sre_parse.SubPattern(sub_pattern, [(BRANCH, (None, patterns))])
return sre_compile.compile(group_pattern)

def get_multiline(self,f,m):
def get_multiline(self, f, m):
content = []
next_line = ''
while not re.search("^}",next_line):
while not re.search("^}", next_line):
content.append(next_line)
try:
next_line = next(f)
Expand All @@ -78,30 +79,30 @@ def get_multiline(self,f,m):
content = "".join(content)
return content, next_line

def get_item(self,f,line):
def get_item(self, f, line):
# IMPORTANT: Each item needs to be added sequentially
# to make sure the record data is grouped properly
# so make sure you add content by calling callback()
# before doing any recursive calls
match = self.group_pattern.scanner(line).match()
if not match:
return
callback = self.lexicon[match.lastindex-1][1]
callback = self.lexicon[match.lastindex - 1][1]
if "def" in match.group():
# this is a multi-line get
first_line = match.group()
body, current_line = self.get_multiline(f,match)
body, current_line = self.get_multiline(f, match)
sections = [first_line, body, current_line]
content = "\n".join(sections).strip()
callback(self,content)
callback(self, content)
if current_line:
self.get_item(f,current_line)
self.get_item(f, current_line)
else:
callback(self,match.group(1))
callback(self, match.group(1))

def scan(self, fin):
for line in fin:
self.get_item(fin,line)
self.get_item(fin, line)


class GroovyScripts(object):
Expand All @@ -124,21 +125,21 @@ def from_string(cls, groovy_str):

def include(self, groovy_path):
# handler format: (pattern, callback)
handlers = [ ("^def( .*)", self.add_function), ]
handlers = [("^def( .*)", self.add_function), ]

with io.open(groovy_path, 'r', encoding='utf-8') as groovy_file:
Scanner(handlers).scan(groovy_file)

def parse(self, groovy_str):
handlers = [ ("^def( .*)", self.add_function), ]
handlers = [("^def( .*)", self.add_function), ]

scanner = Scanner(handlers).scan(io.StringIO(u(groovy_str)))
scanner = Scanner(handlers).scan(io.StringIO(groovy_str))

def get_functions(self):
return self.functions

# Scanner Callback
def add_function(self,scanner,token):
def add_function(self, scanner, token):
function_definition = token
function_signature = self._get_function_signature(function_definition)
function_name = self._get_function_name(function_signature)
Expand All @@ -154,25 +155,24 @@ def add_function(self,scanner,token):
, function_body, sha1)
self.functions[function_name] = function

def _get_function_signature(self,function_definition):
def _get_function_signature(self, function_definition):
pattern = '^def(.*){'
return re.search(pattern,function_definition).group(1).strip()
return re.search(pattern, function_definition).group(1).strip()

def _get_function_name(self,function_signature):
def _get_function_name(self, function_signature):
pattern = '^(.*)\('
return re.search(pattern,function_signature).group(1).strip()
return re.search(pattern, function_signature).group(1).strip()

def _get_function_body(self,function_definition):
def _get_function_body(self, function_definition):
# remove the first and last lines, and return just the function body
lines = function_definition.split('\n')
body_lines = lines[+1:-1]
function_body = "\n".join(body_lines).strip()
return function_body

def _get_sha1(self,function_definition):
def _get_sha1(self, function_definition):
# this is used to detect version changes
function_definition_bytes = function_definition.encode('utf-8')
sha1 = hashlib.sha1()
sha1.update(function_definition_bytes)
return sha1.hexdigest()

Loading

0 comments on commit a6656ae

Please sign in to comment.