-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_readme.py
More file actions
90 lines (74 loc) · 3.01 KB
/
make_readme.py
File metadata and controls
90 lines (74 loc) · 3.01 KB
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
# Copyright 2013-2018 The Salish Sea MEOPAR contributors
# and The University of British Columbia
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Salish Sea NEMO Jupyter Notebook collection README generator
"""
import datetime
import glob
import json
import os
import re
NBVIEWER = 'https://nbviewer.jupyter.org/urls'
REPO = 'bitbucket.org/midoss/analysis-ashutosh/raw/default'
REPO_DIR = './'
TITLE_PATTERN = re.compile('#{1,6} ?')
def main():
url = os.path.join(NBVIEWER, REPO, REPO_DIR)
readme = """\
This analysis folder organizes scripts, data and products used to of model output towards understanding Salish Sea circulation, influences and impacts.
The links below are to static renderings of the notebooks via
[nbviewer.jupyter.org](https://nbviewer.jupyter.org/).
Descriptions under the links below are from the first cell of the notebooks
(if that cell contains Markdown or raw text).
"""
for fn in glob.glob('*.ipynb'):
readme += '* ##[{fn}]({url}/{fn}) \n \n'.format(fn=fn, url=url)
readme += notebook_description(fn)
license = """
##License
These notebooks and files are copyright 2013-{this_year}
by the Salish Sea MEOPAR Project Contributors
and The University of British Columbia.
They are licensed under the Apache License, Version 2.0.
https://www.apache.org/licenses/LICENSE-2.0
Please see the LICENSE file for details of the license.
""".format(this_year=datetime.date.today().year)
with open('README.md', 'wt') as f:
f.writelines(readme)
f.writelines(license)
def notebook_description(fn):
description = ''
with open(fn, 'rt') as notebook:
contents = json.load(notebook)
try:
first_cell = contents['worksheets'][0]['cells'][0]
except KeyError:
first_cell = contents['cells'][0]
first_cell_type = first_cell['cell_type']
if first_cell_type not in 'markdown raw'.split():
return description
desc_lines = first_cell['source']
for line in desc_lines:
suffix = ''
if TITLE_PATTERN.match(line):
line = TITLE_PATTERN.sub('**', line)
suffix = '**'
if line.endswith('\n'):
description += (
' {line}{suffix} \n'
.format(line=line[:-1], suffix=suffix))
else:
description += (
' {line}{suffix} '.format(line=line, suffix=suffix))
description += '\n' * 2
return description
if __name__ == '__main__':
main()