File: copy_notebook.py

package info (click to toggle)
caffe 1.0.0%2Bgit20180821.99bd997-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,288 kB
  • sloc: cpp: 61,586; python: 5,783; makefile: 599; sh: 559
file content (32 lines) | stat: -rwxr-xr-x 1,089 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
"""
Takes as arguments:
1. the path to a JSON file (such as an IPython notebook).
2. the path to output file

If 'metadata' dict in the JSON file contains 'include_in_docs': true,
then copies the file to output file, appending the 'metadata' property
as YAML front-matter, adding the field 'category' with value 'notebook'.
"""
import os
import sys
import json

filename = sys.argv[1]
output_filename = sys.argv[2]
content = json.load(open(filename))

if 'include_in_docs' in content['metadata'] and content['metadata']['include_in_docs']:
    yaml_frontmatter = ['---']
    for key, val in content['metadata'].iteritems():
        if key == 'example_name':
            key = 'title'
            if val == '':
                val = os.path.basename(filename)
        yaml_frontmatter.append('{}: {}'.format(key, val))
    yaml_frontmatter += ['category: notebook']
    yaml_frontmatter += ['original_path: ' + filename]

    with open(output_filename, 'w') as fo:
        fo.write('\n'.join(yaml_frontmatter + ['---']) + '\n')
        fo.write(open(filename).read())