Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

# -*- coding: utf-8 -*- 

""" 

@file 

@brief Magic command to handle files 

""" 

from __future__ import print_function 

import warnings 

with warnings.catch_warnings(): 

warnings.simplefilter("ignore") 

from IPython.core.magic import Magics, magics_class 

 

 

@magics_class 

class MagicClassWithHelpers(Magics): 

 

""" 

Provides some functions reused in others classes inherited from *Magics*. 

The class should not be registered as it is but should be 

used as an ancestor for another class. 

It can be registered this way:: 

 

def register_file_magics(): 

from IPython import get_ipython 

ip = get_ipython() 

ip.register_magics(MagicFile) 

""" 

 

_parser_store = {} 

 

@property 

def Context(self): 

""" 

return the context or None 

""" 

if self.shell is None: 

return None 

return self.shell.user_ns 

 

def add_context(self, context): 

""" 

add context to the class, mostly for debug purpose 

 

@param context dictionary 

""" 

if self.shell is None: 

class EmptyClass: 

 

def __init__(self): 

self.user_ns = {} 

self.shell = EmptyClass() 

for k, v in context.items(): 

self.shell.user_ns[k] = v 

 

def get_parser(self, parser_class, name): 

""" 

returns a parser for a magic command, initialize it 

if it does not exists, it creates. The parsers are stored 

in static member *_parser_store*. 

 

@param parser_class the parser to use for this magic command 

@param name name of the static variable which will contain the parser 

 

See method @see me get_args 

""" 

res = MagicClassWithHelpers._parser_store.get(name, None) 

if res is None: 

MagicClassWithHelpers._parser_store[name] = parser_class() 

return MagicClassWithHelpers._parser_store[name] 

else: 

return res 

 

def get_args(self, line, parser, print_function=print): 

""" 

parser a command line with a given parser 

 

@param line string (command line) 

@param parser parser which has to be used to parse *line* 

@param print_function function to use to display the help 

@return results 

 

If the line cannot be parsed, the function displays the help 

using function print. 

 

Example:: 

 

@line_magic 

def custom_magic_command(self, line): 

parser = self.get_parser(MagicClass.custom_magic_command_parser, "custom_magic_command") 

args = self.get_args(line, parser) 

if args is not None: 

param = args.param 

 

# .... 

""" 

try: 

args = parser.parse_cmd(line, context=self.Context) 

except SystemExit: 

print_function(parser.format_usage()) 

args = None 

 

return args