| 196 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
| 197 | |
| 198 | class Demo(object): |
| 199 | |
| 200 | re_stop = re_mark(r'-*\s?stop\s?-*') |
| 201 | re_silent = re_mark('silent') |
| 202 | re_auto = re_mark('auto') |
| 203 | re_auto_all = re_mark('auto_all') |
| 204 | |
| 205 | def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False, |
| 206 | formatter='terminal', style='default'): |
| 207 | """Make a new demo object. To run the demo, simply call the object. |
| 208 | |
| 209 | See the module docstring for full details and an example (you can use |
| 210 | IPython.Demo? in IPython to see it). |
| 211 | |
| 212 | Inputs: |
| 213 | |
| 214 | - src is either a file, or file-like object, or a |
| 215 | string that can be resolved to a filename. |
| 216 | |
| 217 | Optional inputs: |
| 218 | |
| 219 | - title: a string to use as the demo name. Of most use when the demo |
| 220 | you are making comes from an object that has no filename, or if you |
| 221 | want an alternate denotation distinct from the filename. |
| 222 | |
| 223 | - arg_str(''): a string of arguments, internally converted to a list |
| 224 | just like sys.argv, so the demo script can see a similar |
| 225 | environment. |
| 226 | |
| 227 | - auto_all(None): global flag to run all blocks automatically without |
| 228 | confirmation. This attribute overrides the block-level tags and |
| 229 | applies to the whole demo. It is an attribute of the object, and |
| 230 | can be changed at runtime simply by reassigning it to a boolean |
| 231 | value. |
| 232 | |
| 233 | - format_rst(False): a bool to enable comments and doc strings |
| 234 | formatting with pygments rst lexer |
| 235 | |
| 236 | - formatter('terminal'): a string of pygments formatter name to be |
| 237 | used. Useful values for terminals: terminal, terminal256, |
| 238 | terminal16m |
| 239 | |
| 240 | - style('default'): a string of pygments style name to be used. |
| 241 | """ |
| 242 | if hasattr(src, "read"): |
| 243 | # It seems to be a file or a file-like object |
| 244 | self.fname = "from a file-like object" |
| 245 | if title == '': |
| 246 | self.title = "from a file-like object" |
| 247 | else: |
| 248 | self.title = title |
| 249 | else: |
| 250 | # Assume it's a string or something that can be converted to one |
| 251 | self.fname = src |
| 252 | if title == '': |
| 253 | (filepath, filename) = os.path.split(src) |
| 254 | self.title = filename |
| 255 | else: |