##// END OF EJS Templates
DEV: Refactor checkpoint logic from FileContentsManager....
DEV: Refactor checkpoint logic from FileContentsManager. - Add a `CheckpointManager` base class and infrastructure for creating a `checkpoint_manager` instance attribute on `ContentsManager`. - Provide default implementations of `delete` and `rename` in the base `ContentsManager` class. `ContentsManager` subclasses are now required to implement `delete_file` and `rename_file`. These methods no longer need to manage checkpoints. - Move checkpoint-related functionality from `FileContentsManager` to a dedicated `FileCheckpointManager` subclass. - Move shared filesystem interaction logic into `FileManagerMixin` used by both `FileContentsManager` and `FileCheckpointManager`. - Minor tweaks to ContentsManager tests to get methods from the right object. The purpose of this change is to provide an API for users to replace just the checkpoint logic associated with a particular `ContentsManager`. In particular, this change makes it possible to easily support remote storage of checkpoints while otherwise retaining normal filesystem interactions.

File last commit:

r18580:ba8461eb
r19727:974ebd4a
Show More
highlightmagics.py
100 lines | 3.2 KiB | text/x-python | PythonLexer
"""This preprocessor detect cells using a different language through
magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked
so that the appropriate highlighter can be used in the `highlight`
filter.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import print_function, absolute_import
import re
# Our own imports
from .base import Preprocessor
from IPython.utils.traitlets import Dict
class HighlightMagicsPreprocessor(Preprocessor):
"""
Detects and tags code cells that use a different languages than Python.
"""
# list of magic language extensions and their associated pygment lexers
default_languages = Dict(
default_value={
'%%R': 'r',
'%%bash': 'bash',
'%%cython': 'cython',
'%%javascript': 'javascript',
'%%julia': 'julia',
'%%latex': 'latex',
'%%octave': 'octave',
'%%perl': 'perl',
'%%ruby': 'ruby',
'%%sh': 'sh'})
# user defined language extensions
languages = Dict(
config=True,
help=("Syntax highlighting for magic's extension languages. "
"Each item associates a language magic extension such as %%R, "
"with a pygments lexer such as r."))
def __init__(self, config=None, **kw):
"""Public constructor"""
super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw)
# Update the default languages dict with the user configured ones
self.default_languages.update(self.languages)
# build a regular expression to catch language extensions and choose
# an adequate pygments lexer
any_language = "|".join(self.default_languages.keys())
self.re_magic_language = re.compile(
r'^\s*({0})\s+'.format(any_language))
def which_magic_language(self, source):
"""
When a cell uses another language through a magic extension,
the other language is returned.
If no language magic is detected, this function returns None.
Parameters
----------
source: str
Source code of the cell to highlight
"""
m = self.re_magic_language.match(source)
if m:
# By construction of the re, the matched language must be in the
# languages dictionary
return self.default_languages[m.group(1)]
else:
return None
def preprocess_cell(self, cell, resources, cell_index):
"""
Tags cells using a magic extension language
Parameters
----------
cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
preprocessors to pass variables into the Jinja engine.
cell_index : int
Index of the cell being processed (see base.py)
"""
# Only tag code cells
if cell.cell_type == "code":
magic_language = self.which_magic_language(cell.source)
if magic_language:
cell['metadata']['magics_language'] = magic_language
return cell, resources