Skip to content

Instantly share code, notes, and snippets.

@iddan
Created December 27, 2019 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iddan/4caf5b89984407d6e775454001654bef to your computer and use it in GitHub Desktop.
Save iddan/4caf5b89984407d6e775454001654bef to your computer and use it in GitHub Desktop.
Refactor Cayley LinkedQL steps
import re
directory = "query/linkedql/"
with open(directory + "steps.go") as file:
content = file.read()
def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
separator = "var _ IteratorStep = (*"
for part in content.split(separator):
step_name = part.split("\n")[0].replace(")(nil)", "")
if step_name == "package linkedql":
continue
header = """
package linkedql
import (
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/query"
"github.com/cayleygraph/cayley/query/path"
"github.com/cayleygraph/quad"
"github.com/cayleygraph/quad/voc"
)
func init() {
Register(&%s{})
}
""" % (step_name)
step_code = header + separator + part
path = directory + "steps/" + convert(step_name) + ".go"
with open(path, 'w+') as file:
file.write(step_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment