Skip to content

Instantly share code, notes, and snippets.

@iddan

iddan/codemod.py

Created Dec 27, 2019
Embed
What would you like to do?
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