Show More
@@ -27,89 +27,14 b' from IPython.utils.process import find_cmd, FindCmdError' | |||||
27 |
|
27 | |||
28 | from .exceptions import ConversionException |
|
28 | from .exceptions import ConversionException | |
29 |
|
29 | |||
30 |
|
||||
31 | #---------------------------------------------------------------------------- |
|
|||
32 | # Preliminary checks. |
|
|||
33 | # Not finding Pandoc is not always fatal so only a warning is issued at the |
|
|||
34 | # module root level so that the import of this module is not fatal. |
|
|||
35 | #---------------------------------------------------------------------------- |
|
|||
36 |
|
||||
37 | # command line to make pandoc print it's version. It is also the |
|
|||
38 | # easiest way to make pandoc return at all. |
|
|||
39 | __pandoc_version_call = ['pandoc', '-v'] |
|
|||
40 |
|
||||
41 | class PandocMissing(ConversionException): |
|
|||
42 | """Exception raised when Pandoc is missing. """ |
|
|||
43 | def __init__(self, cmd, exc, *args, **kwargs): |
|
|||
44 | super(PandocMissing, self).__init__( "The command '%s' returned an error: %s.\n" %(" ".join(cmd), exc) + |
|
|||
45 | "Please check that pandoc is installed:\n" + |
|
|||
46 | "http://johnmacfarlane.net/pandoc/installing.html" ) |
|
|||
47 | self.exc = exc |
|
|||
48 |
|
||||
49 | def __bool__(self): |
|
|||
50 | return False |
|
|||
51 |
|
||||
52 | __nonzero__ = __bool__ |
|
|||
53 |
|
||||
54 |
|
||||
55 | def pandoc_available(failmode="return", warn=False): |
|
|||
56 | """Is pandoc available. Only tries to call Pandoc |
|
|||
57 | and inform you that it succeeded or failed. |
|
|||
58 |
|
||||
59 | Parameters |
|
|||
60 | ---------- |
|
|||
61 | - failmode : string |
|
|||
62 | either "return" or "raise". If "return" and pandoc |
|
|||
63 | is not available, will return (False, e) where e is |
|
|||
64 | the exception returned by subprocess.check_call. |
|
|||
65 | - warn : bool |
|
|||
66 | issue a user warning if pandoc is not available. |
|
|||
67 |
|
||||
68 | Return |
|
|||
69 | ------ |
|
|||
70 | out : (Bool, Exception) |
|
|||
71 | On success will return True. On failure and failmode=="return" |
|
|||
72 | will return False-valued PandocMissing instance |
|
|||
73 | """ |
|
|||
74 |
|
||||
75 | try: |
|
|||
76 | find_cmd("pandoc") |
|
|||
77 | return True |
|
|||
78 | except FindCmdError as e: |
|
|||
79 | if warn: |
|
|||
80 | warnings.warn( |
|
|||
81 | "Pandoc cannot be found (calling %s failed).\n" % " ".join(alt or __pandoc_version_call) + |
|
|||
82 | "Please check that pandoc is installed:\n" + |
|
|||
83 | "http://johnmacfarlane.net/pandoc/installing.html" |
|
|||
84 | ) |
|
|||
85 |
|
||||
86 | exc = PandocMissing("pandoc", e) |
|
|||
87 | if failmode == "return": |
|
|||
88 | return exc |
|
|||
89 | else: |
|
|||
90 | raise exc |
|
|||
91 |
|
||||
92 |
|
||||
93 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
94 | # Classes and functions |
|
31 | # Classes and functions | |
95 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
96 | minimal_version = "1.12.1" |
|
33 | minimal_version = "1.12.1" | |
97 |
|
34 | |||
98 | # The following holds cached values about the pandoc executable. |
|
35 | # command line to make pandoc print it's version. It is also the | |
99 | def clean_cache(new=False): |
|
36 | # easiest way to make pandoc return at all. | |
100 | if new: |
|
37 | __pandoc_version_call = ['pandoc', '-v'] | |
101 | global __cache |
|
|||
102 | cache = {} |
|
|||
103 | __cache = cache |
|
|||
104 | else: |
|
|||
105 | cache = __cache |
|
|||
106 | cache.clear() |
|
|||
107 |
|
||||
108 | cache['version_ok'] = False |
|
|||
109 | cache['version'] = None |
|
|||
110 | return cache |
|
|||
111 |
|
||||
112 | __cache = clean_cache(new=True) |
|
|||
113 |
|
38 | |||
114 |
|
39 | |||
115 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
40 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): | |
@@ -149,6 +74,42 b" def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):" | |||||
149 | return out.rstrip('\n') |
|
74 | return out.rstrip('\n') | |
150 |
|
75 | |||
151 |
|
76 | |||
|
77 | def pandoc_available(failmode="return", warn=False): | |||
|
78 | """Is pandoc available. Relies on `IPython.utils.process.find_cmd`. | |||
|
79 | ||||
|
80 | Parameters | |||
|
81 | ---------- | |||
|
82 | - failmode : string | |||
|
83 | either "return" or "raise". See below. | |||
|
84 | - warn : bool | |||
|
85 | issue a user warning if pandoc is not available. | |||
|
86 | ||||
|
87 | Return | |||
|
88 | ------ | |||
|
89 | out : (Bool, Exception) | |||
|
90 | On success will return True. On failure and failmode=="return" | |||
|
91 | will return False-valued PandocMissing instance. If failmode is | |||
|
92 | anything else, the function will not return but raise PandocMissing. | |||
|
93 | """ | |||
|
94 | ||||
|
95 | try: | |||
|
96 | find_cmd("pandoc") | |||
|
97 | return True | |||
|
98 | except FindCmdError as e: | |||
|
99 | if warn: | |||
|
100 | warnings.warn( | |||
|
101 | "Pandoc cannot be found (find_cmd('pandoc') failed).\n"+ | |||
|
102 | "Please check that pandoc is installed:\n" + | |||
|
103 | "http://johnmacfarlane.net/pandoc/installing.html" | |||
|
104 | ) | |||
|
105 | ||||
|
106 | exc = PandocMissing("pandoc", e) | |||
|
107 | if failmode == "return": | |||
|
108 | return exc | |||
|
109 | else: | |||
|
110 | raise exc | |||
|
111 | ||||
|
112 | ||||
152 | def get_pandoc_version(): |
|
113 | def get_pandoc_version(): | |
153 | """Gets the Pandoc version if Pandoc is installed. |
|
114 | """Gets the Pandoc version if Pandoc is installed. | |
154 |
|
115 | |||
@@ -190,4 +151,42 b' def check_pandoc_version():' | |||||
190 | RuntimeWarning, stacklevel=2) |
|
151 | RuntimeWarning, stacklevel=2) | |
191 | return __cache['version_ok'] |
|
152 | return __cache['version_ok'] | |
192 |
|
153 | |||
|
154 | #----------------------------------------------------------------------------- | |||
|
155 | # Exception handling | |||
|
156 | #----------------------------------------------------------------------------- | |||
|
157 | class PandocMissing(ConversionException): | |||
|
158 | """Exception raised when Pandoc is missing. """ | |||
|
159 | def __init__(self, cmd, exc, *args, **kwargs): | |||
|
160 | super(PandocMissing, self).__init__( "The command '%s' returned an error: %s.\n" %(" ".join(cmd), exc) + | |||
|
161 | "Please check that pandoc is installed:\n" + | |||
|
162 | "http://johnmacfarlane.net/pandoc/installing.html" ) | |||
|
163 | self.exc = exc | |||
|
164 | ||||
|
165 | def __bool__(self): | |||
|
166 | return False | |||
|
167 | ||||
|
168 | __nonzero__ = __bool__ | |||
|
169 | ||||
|
170 | ||||
|
171 | #----------------------------------------------------------------------------- | |||
|
172 | # Internal state management | |||
|
173 | #----------------------------------------------------------------------------- | |||
|
174 | def clean_cache(new=False): | |||
|
175 | if new: | |||
|
176 | global __cache | |||
|
177 | cache = {} | |||
|
178 | __cache = cache | |||
|
179 | else: | |||
|
180 | cache = __cache | |||
|
181 | cache.clear() | |||
|
182 | ||||
|
183 | cache['version_ok'] = False | |||
|
184 | cache['version'] = None | |||
|
185 | return cache | |||
|
186 | ||||
|
187 | # The following holds cache values about the pandoc executable. | |||
|
188 | __cache = clean_cache(new=True) | |||
|
189 | ||||
|
190 | ||||
|
191 | ||||
193 |
|
192 |
General Comments 0
You need to be logged in to leave comments.
Login now