Example #1
0
def build_schema_doc(key):
    fn = 'collections/' + key + '.rst'
    with open(fn, 'w') as f:
        s = ''
        s += key.title() + '\n'
        s += '=' * len(key) + '\n'
        s += SCHEMAS[key]['_description']['description'] + '\n\n'
        s += 'Schema\n------\nThe following lists key names mapped to its ' \
             'type and meaning for each entry.\n\n'
        schema = SCHEMAS[key]
        schema_list = list(schema.keys())
        schema_list.sort()
        for k in schema_list:
            if k not in ['_description']:
                s += format_key(schema[k], key=k)
        s += '\n\n'
        s += 'YAML Example\n------------\n\n'
        s += '.. code-block:: yaml\n\n'
        temp = tempfile.NamedTemporaryFile()
        temp2 = tempfile.NamedTemporaryFile()
        with open(temp.name, 'w') as ff:
            json.dump(EXEMPLARS[key], ff, sort_keys=True)
        jd = json.dumps(EXEMPLARS[key],
                        sort_keys=True,
                        indent=4,
                        separators=(',', ': '))
        json_to_yaml(temp.name, temp2.name)
        with open(temp2.name, 'r') as ff:
            s += indent(ff.read(), '\t')
        s += '\n\n'
        s += 'JSON/Mongo Example\n------------------\n\n'
        s += '.. code-block:: json\n\n'
        s += indent(jd, '\t')
        s += '\n'
        f.write(s)
Example #2
0
def json_to_yaml(rc):
    """Converts JSON to YAML"""
    from regolith import fsclient
    for inp in rc.files:
        base, ext = os.path.splitext(inp)
        out = base + '.yaml'
        fsclient.json_to_yaml(inp, out)
Example #3
0
def build_schema_doc(key):
    fn = "collections/" + key + ".rst"
    with open(fn, "w") as f:
        s = ""
        s += key.title() + "\n"
        s += "=" * len(key) + "\n"
        s += SCHEMAS[key]["_description"]["description"] + "\n\n"
        s += "Schema\n------\nThe following lists key names mapped to its " "type and meaning for each entry.\n\n"
        schema = SCHEMAS[key]
        schema_list = list(schema.keys())
        schema_list.sort()
        for k in schema_list:
            if k not in ["_description"]:
                s += format_key(schema[k], key=k)
        s += "\n\n"
        s += "YAML Example\n------------\n\n"
        s += ".. code-block:: yaml\n\n"
        temp = tempfile.NamedTemporaryFile()
        temp2 = tempfile.NamedTemporaryFile()
        documents = EXEMPLARS[key]
        if isinstance(documents, MutableMapping):
            documents = [documents]
        documents = {doc["_id"]: doc for doc in documents}
        dump_json(temp.name, documents)
        docs = sorted(documents.values(), key=_id_key)
        lines = [
            json.dumps(doc, sort_keys=True, indent=4, separators=(",", ": "))
            for doc in docs
        ]
        jd = "\n".join(lines)
        json_to_yaml(temp.name, temp2.name)
        with open(temp2.name, "r") as ff:
            s += indent(ff.read(), "\t")
        s += "\n\n"
        s += "JSON/Mongo Example\n------------------\n\n"
        s += ".. code-block:: json\n\n"
        s += indent(jd, "\t")
        s += "\n"
        f.write(s)