Skip to content

Commit e80e0c4

Browse files
authored
Merge pull request #837 from jcb91/cp_merged
merge upstream code-prettify updates
2 parents 6baf2f9 + ed63dce commit e80e0c4

File tree

12 files changed

+1479
-230
lines changed

12 files changed

+1479
-230
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Jupyter-Contrib Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
// Authors: @EWouters, @jfbercher and @jcb91
4+
// Based on: https://github.com/jfbercher/code_prettify and
5+
// https://gist.github.com/takluyver/c8839593c615bb2f6e80
6+
7+
define(function(require, exports, module) {
8+
'use strict';
9+
10+
var kernel_exec_on_cell = require('./kernel_exec_on_cell');
11+
12+
var mod_name = '2to3';
13+
14+
// gives default settings
15+
var cfg = {
16+
add_toolbar_button: true,
17+
hotkeys: {
18+
process_selected: 'Ctrl-M',
19+
process_all: 'Ctrl-Shift-M',
20+
},
21+
register_hotkey: true,
22+
show_alerts_for_errors: true,
23+
button_icon: 'fa-space-shuttle',
24+
button_label: 'Convert Python 2 to 3',
25+
kbd_shortcut_text: 'Convert Python 2 to 3 in' // ' current cell(s)'
26+
};
27+
28+
cfg.kernel_config_map = { // map of parameters for supported kernels
29+
"python": {
30+
"library": [
31+
"import lib2to3.refactor, json",
32+
"_2to3_refactoring_tool = lib2to3.refactor.RefactoringTool(",
33+
" set(lib2to3.refactor.get_fixers_from_package('lib2to3.fixes')))",
34+
"def _2to3_refactor_cell(src):",
35+
" try:",
36+
" tree = _2to3_refactoring_tool.refactor_string(src+'\\n', '<dummy_name>')",
37+
" except (lib2to3.pgen2.parse.ParseError, lib2to3.pgen2.tokenize.TokenError):",
38+
" return src ",
39+
" else:",
40+
" return str(tree)[:-1]",
41+
].join('\n'),
42+
"prefix": "print(json.dumps(_2to3_refactor_cell(u",
43+
"postfix": ")))"
44+
}
45+
};
46+
47+
var converter = new kernel_exec_on_cell.define_plugin(mod_name, cfg);
48+
converter.load_ipython_extension = converter.initialize_plugin;
49+
return converter;
50+
51+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Type: Jupyter Notebook Extension
2+
Name: 2to3 Converter
3+
Description: Converts python2 code in a notebook's code cell to python3 code
4+
Link: README_2to3.md
5+
Main: 2to3.js
6+
Compatibility: Jupyter (4.x)
7+
Parameters:
8+
9+
- name: 2to3.add_toolbar_button
10+
description: Add a toolbar button to convert the selected cell(s)
11+
input_type: checkbox
12+
default: true
13+
14+
- name: 2to3.button_icon
15+
description: |
16+
Toolbar button icon: afont-awesome class defining the icon used for the
17+
toolbar button. See http://fontawesome.io/icons/ for available icons.
18+
input_type: text
19+
default: 'fa-space-shuttle'
20+
21+
- name: 2to3.button_label
22+
description: Toolbar button label text
23+
input_type: text
24+
default: 'Code converter'
25+
26+
- name: 2to3.register_hotkey
27+
description: Register a hotkey to convert the selected cell(s)
28+
input_type: checkbox
29+
default: true
30+
31+
- name: 2to3.hotkeys.process_selected
32+
description: Hotkey to convert the selected cell(s) from python2 to python3
33+
input_type: hotkey
34+
default: 'Ctrl-M'
35+
36+
- name: 2to3.hotkeys.process_all
37+
description: Hotkey to convert the whole notebook
38+
input_type: hotkey
39+
default: 'Ctrl-Shift-M'
40+
41+
- name: 2to3.show_alerts_for_errors
42+
description: Show alerts for errors in the kernel converting calls
43+
input_type: checkbox
44+
default: true
45+
46+
- name: 2to3.kernel_config_map_json
47+
description: |
48+
kernel_config_map_json:
49+
json object defining library calls required to load the kernel-specific
50+
converting modules, and the prefix & postfix for the json-format string
51+
required to make the converting call.
52+
input_type: json_object
53+
default: |
54+
{
55+
"python": {
56+
"library": "import lib2to3.refactor, json\n_2to3_refactoring_tool = lib2to3.refactor.RefactoringTool(\n set(lib2to3.refactor.get_fixers_from_package('lib2to3.fixes')))\ndef _2to3_refactor_cell(src):\n try:\n tree = _2to3_refactoring_tool.refactor_string(src+'\\n', '<dummy_name>')\n except (lib2to3.pgen2.parse.ParseError, lib2to3.pgen2.tokenize.TokenError):\n return src \n else:\n return str(tree)[:-1]",
57+
"prefix": "print(json.dumps(refactor_cell(u",
58+
"postfix": ")))"
59+
}
60+
}

0 commit comments

Comments
 (0)