Show More
@@ -1,132 +1,132 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """ |
|
2 | """ | |
3 | ================================================================================ |
|
3 | ================================================================================ | |
4 |
|
4 | |||
5 | |,---. | | , .| | |
|
5 | |,---. | | , .| | | |
6 | ||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|--- |
|
6 | ||---', .|--- |---.,---.,---. |\ ||---.,---.,---.,---.. ,,---.,---.|--- | |
7 | || | || | || || | | \ || || | || | \ / |---'| | |
|
7 | || | || | || || | | \ || || | || | \ / |---'| | | |
8 | `` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---' |
|
8 | `` `---|`---'` '`---'` ' ` `'`---'`---'`---'` ' `' `---'` `---' | |
9 | `---' |
|
9 | `---' | |
10 | ================================================================================ |
|
10 | ================================================================================ | |
11 |
|
11 | |||
12 | Highly experimental for now |
|
12 | Highly experimental for now | |
13 |
|
13 | |||
14 | """ |
|
14 | """ | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | from __future__ import print_function |
|
18 | from __future__ import print_function | |
19 | import sys |
|
19 | import sys | |
20 | import io |
|
20 | import io | |
21 | import os |
|
21 | import os | |
22 |
|
22 | |||
23 | from converters.template import * |
|
23 | from converters.template import * | |
24 | from converters.template import ConverterTemplate |
|
24 | from converters.template import ConverterTemplate | |
25 | from converters.html import ConverterHTML |
|
25 | from converters.html import ConverterHTML | |
26 | # From IPython |
|
26 | # From IPython | |
27 |
|
27 | |||
28 | # All the stuff needed for the configurable things |
|
28 | # All the stuff needed for the configurable things | |
29 | from IPython.config.application import Application |
|
29 | from IPython.config.application import Application | |
30 | from IPython.config.loader import ConfigFileNotFound |
|
30 | from IPython.config.loader import ConfigFileNotFound | |
31 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum |
|
31 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | |
32 |
|
32 | |||
33 | from converters.transformers import (ConfigurableTransformers,Foobar,ExtractFigureTransformer) |
|
33 | from converters.transformers import (ConfigurableTransformers,Foobar,ExtractFigureTransformer) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | class NbconvertApp(Application): |
|
36 | class NbconvertApp(Application): | |
37 |
|
37 | |||
38 | stdout = Bool(True, config=True) |
|
38 | stdout = Bool(True, config=True) | |
39 | write = Bool(False, config=True) |
|
39 | write = Bool(False, config=True) | |
40 |
|
40 | |||
41 | aliases = { |
|
41 | aliases = { | |
42 | 'stdout':'NbconvertApp.stdout', |
|
42 | 'stdout':'NbconvertApp.stdout', | |
43 | 'write':'NbconvertApp.write', |
|
43 | 'write':'NbconvertApp.write', | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | flags= {} |
|
46 | flags= {} | |
47 | flags['no-stdout'] = ( |
|
47 | flags['no-stdout'] = ( | |
48 | {'NbconvertApp' : {'stdout' : False}}, |
|
48 | {'NbconvertApp' : {'stdout' : False}}, | |
49 | """the doc for this flag |
|
49 | """the doc for this flag | |
50 |
|
50 | |||
51 | """ |
|
51 | """ | |
52 | ) |
|
52 | ) | |
53 |
|
53 | |||
54 | def __init__(self, **kwargs): |
|
54 | def __init__(self, **kwargs): | |
55 | super(NbconvertApp, self).__init__(**kwargs) |
|
55 | super(NbconvertApp, self).__init__(**kwargs) | |
56 | self.classes.insert(0,ConverterTemplate) |
|
56 | self.classes.insert(0,ConverterTemplate) | |
57 | # register class here to have help with help all |
|
57 | # register class here to have help with help all | |
58 | self.classes.insert(0,ExtractFigureTransformer) |
|
58 | self.classes.insert(0,ExtractFigureTransformer) | |
59 | self.classes.insert(0,Foobar) |
|
59 | self.classes.insert(0,Foobar) | |
60 | # ensure those are registerd |
|
60 | # ensure those are registerd | |
61 |
|
61 | |||
62 | def load_config_file(self, profile_name): |
|
62 | def load_config_file(self, profile_name): | |
63 | try: |
|
63 | try: | |
64 | Application.load_config_file( |
|
64 | Application.load_config_file( | |
65 | self, |
|
65 | self, | |
66 | profile_name+'.nbcv', |
|
66 | profile_name+'.nbcv', | |
67 | path=[os.path.join(os.getcwdu(),'profile')] |
|
67 | path=[os.path.join(os.getcwdu(),'profile')] | |
68 | ) |
|
68 | ) | |
69 | except ConfigFileNotFound: |
|
69 | except ConfigFileNotFound: | |
70 | self.log.warn("Config file for profile '%s' not found, giving up ",profile_name) |
|
70 | self.log.warn("Config file for profile '%s' not found, giving up ",profile_name) | |
71 | exit(1) |
|
71 | exit(1) | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | def initialize(self, argv=None): |
|
74 | def initialize(self, argv=None): | |
75 | self.parse_command_line(argv) |
|
75 | self.parse_command_line(argv) | |
76 | cl_config = self.config |
|
76 | cl_config = self.config | |
77 | profile_file = sys.argv[1] |
|
77 | profile_file = sys.argv[1] | |
78 | self.load_config_file(profile_file) |
|
78 | self.load_config_file(profile_file) | |
79 | self.update_config(cl_config) |
|
79 | self.update_config(cl_config) | |
80 |
|
80 | |||
81 |
|
81 | |||
82 |
|
82 | |||
83 | def run(self): |
|
83 | def run(self): | |
84 | """Convert a notebook to html in one step""" |
|
84 | """Convert a notebook to html in one step""" | |
85 | template_file = (self.extra_args or [None])[0] |
|
85 | template_file = (self.extra_args or [None])[0] | |
86 | ipynb_file = (self.extra_args or [None])[1] |
|
86 | ipynb_file = (self.extra_args or [None])[1] | |
87 |
|
87 | |||
88 | template_file = sys.argv[1] |
|
88 | template_file = sys.argv[1] | |
89 |
|
89 | |||
90 | C = ConverterTemplate(config=self.config) |
|
90 | C = ConverterTemplate(config=self.config) | |
91 | C.read(ipynb_file) |
|
91 | C.read(ipynb_file) | |
92 |
|
92 | |||
93 | output,resources = C.convert() |
|
93 | output,resources = C.convert() | |
94 | if self.stdout : |
|
94 | if self.stdout : | |
95 | print(output.encode('utf-8')) |
|
95 | print(output.encode('utf-8')) | |
96 |
|
96 | |||
97 | out_root = ipynb_file[:-6].replace('.','_') |
|
97 | out_root = ipynb_file[:-6].replace('.','_') | |
98 |
|
98 | |||
99 | keys = resources.keys() |
|
99 | keys = resources.keys() | |
100 | if keys : |
|
100 | if keys : | |
101 | if self.write and not os.path.exists(out_root): |
|
101 | if self.write and not os.path.exists(out_root): | |
102 | os.mkdir(out_root) |
|
102 | os.mkdir(out_root) | |
103 | for key in keys: |
|
103 | for key in keys: | |
104 | if self.write: |
|
104 | if self.write: | |
105 | with io.open(os.path.join(out_root,key),'wb') as f: |
|
105 | with io.open(os.path.join(out_root+'_files',key),'wb') as f: | |
106 | print(' writing to ',os.path.join(out_root,key)) |
|
106 | print(' writing to ',os.path.join(out_root,key)) | |
107 | f.write(resources[key]) |
|
107 | f.write(resources[key]) | |
108 | if self.stdout: |
|
108 | if self.stdout: | |
109 | print(''' |
|
109 | print(''' | |
110 | ====================== Keys in Resources ================================== |
|
110 | ====================== Keys in Resources ================================== | |
111 | ''') |
|
111 | ''') | |
112 | print(resources.keys()) |
|
112 | print(resources.keys()) | |
113 | print(""" |
|
113 | print(""" | |
114 | =========================================================================== |
|
114 | =========================================================================== | |
115 | you are responsible from writing those data do a file in the right place if |
|
115 | you are responsible from writing those data do a file in the right place if | |
116 | they need to be. |
|
116 | they need to be. | |
117 | =========================================================================== |
|
117 | =========================================================================== | |
118 | """) |
|
118 | """) | |
119 |
|
119 | |||
120 | def main(): |
|
120 | def main(): | |
121 | """Convert a notebook to html in one step""" |
|
121 | """Convert a notebook to html in one step""" | |
122 | app = NbconvertApp.instance() |
|
122 | app = NbconvertApp.instance() | |
123 | app.description = __doc__ |
|
123 | app.description = __doc__ | |
124 | app.initialize() |
|
124 | app.initialize() | |
125 | app.start() |
|
125 | app.start() | |
126 | app.run() |
|
126 | app.run() | |
127 | #----------------------------------------------------------------------------- |
|
127 | #----------------------------------------------------------------------------- | |
128 | # Script main |
|
128 | # Script main | |
129 | #----------------------------------------------------------------------------- |
|
129 | #----------------------------------------------------------------------------- | |
130 |
|
130 | |||
131 | if __name__ == '__main__': |
|
131 | if __name__ == '__main__': | |
132 | main() |
|
132 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now