2
2
# coding: utf-8
3
3
4
4
import doctest
5
- import imp
6
5
import os
7
6
import sys
8
7
9
8
import jinja2
10
9
import yaml
11
10
11
+ from __init__ import load_parent_package
12
+
12
13
# xxx/_building/build_readme.py
13
14
this_base = os .path .dirname (__file__ )
14
15
15
16
j2vars = {}
16
17
17
18
# let it be able to find indirectly dependent package locally
18
19
# e.g.: `k3fs` depends on `k3confloader`
19
- sys .path .insert (0 , os .path .abspath ('..' ))
20
+ sys .path .insert (0 , os .path .abspath (".." ))
20
21
21
22
# load package name from __init__.py
22
- pkg = imp . load_source ( "_foo" , '__init__.py' )
23
- j2vars ["name" ] = pkg . __name__
23
+ package_name , pkg = load_parent_package ( )
24
+ j2vars ["name" ] = package_name
24
25
25
26
26
27
def get_gh_config ():
27
- with open (' .github/settings.yml' , 'r' ) as f :
28
+ with open (" .github/settings.yml" , "r" ) as f :
28
29
cont = f .read ()
29
30
30
31
cfg = yaml .safe_load (cont )
31
- tags = cfg [' repository' ][ ' topics' ].split (',' )
32
+ tags = cfg [" repository" ][ " topics" ].split ("," )
32
33
tags = [x .strip () for x in tags ]
33
- cfg [' repository' ][ ' topics' ] = tags
34
+ cfg [" repository" ][ " topics" ] = tags
34
35
return cfg
35
36
36
37
37
38
cfg = get_gh_config ()
38
- j2vars [' description' ] = cfg [' repository' ][ ' description' ]
39
+ j2vars [" description" ] = cfg [" repository" ][ " description" ]
39
40
40
41
41
42
def get_examples (pkg ):
@@ -44,41 +45,41 @@ def get_examples(pkg):
44
45
es = parser .get_examples (doc )
45
46
rst = []
46
47
for e in es :
47
- rst .append (' >>> ' + e .source .strip ())
48
+ rst .append (" >>> " + e .source .strip ())
48
49
rst .append (e .want .strip ())
49
50
50
- rst = ' \n ' .join (rst )
51
+ rst = " \n " .join (rst )
51
52
52
- for fn in ("synopsis.txt" ,
53
- "synopsis.py" ,
54
- ):
53
+ for fn in (
54
+ "synopsis.txt" ,
55
+ "synopsis.py" ,
56
+ ):
55
57
try :
56
- with open (fn , 'r' ) as f :
57
- rst += ' \n ' + f .read ()
58
+ with open (fn , "r" ) as f :
59
+ rst += " \n " + f .read ()
58
60
59
61
except FileNotFoundError :
60
62
pass
61
63
62
64
return rst
63
65
64
66
65
- j2vars [' synopsis' ] = get_examples (pkg )
66
- j2vars [' package_doc' ] = pkg .__doc__
67
+ j2vars [" synopsis" ] = get_examples (pkg )
68
+ j2vars [" package_doc" ] = pkg .__doc__
67
69
68
70
69
71
def render_j2 (tmpl_path , tmpl_vars , output_path ):
70
- template_loader = jinja2 .FileSystemLoader (searchpath = './' )
71
- template_env = jinja2 .Environment (loader = template_loader ,
72
- undefined = jinja2 .StrictUndefined )
72
+ template_loader = jinja2 .FileSystemLoader (searchpath = "./" )
73
+ template_env = jinja2 .Environment (
74
+ loader = template_loader , undefined = jinja2 .StrictUndefined
75
+ )
73
76
template = template_env .get_template (tmpl_path )
74
77
75
78
txt = template .render (tmpl_vars )
76
79
77
- with open (output_path , 'w' ) as f :
80
+ with open (output_path , "w" ) as f :
78
81
f .write (txt )
79
82
80
83
81
84
if __name__ == "__main__" :
82
- render_j2 ('_building/README.md.j2' ,
83
- j2vars ,
84
- 'README.md' )
85
+ render_j2 ("_building/README.md.j2" , j2vars , "README.md" )
0 commit comments