-
Notifications
You must be signed in to change notification settings - Fork 3
/
shovel.py
119 lines (90 loc) · 3 KB
/
shovel.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import csv
import json
import jinja2
import livereload
import os
import ramlfications
import sys
import time
import yaml
from shovel import task
RAML_TEMPLATE = '_layouts/api.md'
RAML_TEMPLATE = 'api.md'
CONFIG_FILE = "config.yml"
''' Reads all files in '_data' directory. '''
def load_data():
stream = open(CONFIG_FILE, "r")
config = yaml.load(stream)
# print config['data_files']
data = {}
for f in config['data_files']:
(root, name) = os.path.split(f)
(name_no_ext, ext) = os.path.splitext(name)
data[name_no_ext] = []
load_functions[ext](f, data, name_no_ext)
return data
def load_data_as_json(path, data_store, name):
# print path
# json_data = open(path).read()
# print json_data
json_data = open(path)
data = json.load(json_data)
data_store[name] = data
def load_data_as_csv(path, data_store, name):
with open(path, 'rb') as csvfile:
datareader = csv.DictReader(csvfile)
for row in datareader:
data_store[name].append(row)
load_functions = {'.csv':load_data_as_csv,'.json':load_data_as_json}
def parse_doc(template_path, data):
# api = ramlfications.parse(RAML_FILE)
env = jinja2.Environment(loader=jinja2.FileSystemLoader('./templates'))
env.filters['jsonify'] = json.dumps
template = env.get_template(template_path)
f = open(os.path.join('docs', template_path), 'w')
f.write(template.render(data=data))
f.close()
def convert_templates():
print 'Scanning files...'
data = load_data()
for root, subdirs, files in os.walk('templates'):
for f in files:
templates_sub_path = root.replace('templates/', '')
template = os.path.join(templates_sub_path, f)
if f == '.DS_Store':
continue
parse_doc(template, data)
''' Turns a RAML file into a Markdown page. '''
def parse_raml(raml_file, data):
print 'Parsing ' + raml_file
api = ramlfications.parse(raml_file)
env = jinja2.Environment(loader=jinja2.FileSystemLoader('./_layouts'))
env.filters['jsonify'] = json.dumps
template = env.get_template(RAML_TEMPLATE)
f = open(os.path.join('docs', raml_file.replace('.raml', '.md')), 'w')
f.write(template.render(api=api, data=data))
f.close()
def build_api_docs():
print 'Building API docs...'
data = load_data()
for root, subdirs, files in os.walk('apis'):
for f in files:
if f == '.DS_Store':
continue
file_sub_path = root.replace('apis/', '')
file_path = os.path.join(file_sub_path, f)
parse_raml(file_path, data)
@task
def raml():
'''Converts RAML to Markdown'''
build_api_docs()
convert_templates()
@task
def watch():
raml()
server = livereload.Server()
server.watch('_data/**/*', convert_templates)
server.watch('templates/**/*', convert_templates)
server.watch('apis/*', build_api_docs)
server.watch('examples/*.json', convert_templates)
server.serve()