Skip to content

Instantly share code, notes, and snippets.

@persquare
Last active January 11, 2019 07:55
Show Gist options
  • Save persquare/b44626a9522fad5a4f6b90cb07a73f17 to your computer and use it in GitHub Desktop.
Save persquare/b44626a9522fad5a4f6b90cb07a73f17 to your computer and use it in GitHub Desktop.
Update old-style actors docs to new-style in place
import os
from docparser import parse_old_doc
import json
import jsonschema
from jsonschema.exceptions import ValidationError
import yaml
from newstore import Store
def actor_files(root):
for dirpath, dirnames, filenames in os.walk(root):
filenames = [os.path.join(dirpath, f) for f in filenames if f.endswith('.py') and f != '__init__.py']
for f in filenames:
yield f
def read_file(filepath):
with open(filepath, 'r') as f:
src = f.read()
return src
def write_file(filepath, src):
with open(filepath, 'w') as f:
f.write(src)
def split_src(src):
return src.split('"""', 2)
def merge_src(pre, docs, post):
indent = len(pre.split('\n')[-1])
indented_docs = '\n'.join([' '*indent + line for line in docs.split('\n')])
indented_docs = '\n' + indented_docs
return '"""'.join((pre, indented_docs, post))
def convert_json(doc_json):
data = json.loads(doc_json)
try:
jsonschema.validate(data, Store.actor_properties_schema)
except ValidationError:
return None
return data
def main(root):
for f in actor_files(root):
pre, docs, post = split_src(read_file(f))
if docs.lstrip().startswith("documentation:"):
continue
try:
doc_json = parse_old_doc(docs)
except Exception as e:
print "Parse error: {}\n {}".format(f, e)
continue
data = convert_json(doc_json)
if not data:
print "Validation failed: {}".format(f)
continue
yaml_docs = yaml.safe_dump(data, default_flow_style=False)
src = merge_src(pre, yaml_docs, post)
write_file(f, src)
if __name__ == '__main__':
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.join(dir_path, "systemactors")
main(dir_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment