-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipytools.py
86 lines (78 loc) · 2.87 KB
/
ipytools.py
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
# This code can be put in any Python module, it does not require IPython
# itself to be running already. It only creates the magics subclass but
# doesn't instantiate it yet.
from __future__ import print_function
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import traceback as tb
import logging
import os
import sys
# The class MUST call this class decorator at creation time
@magics_class
class LoggerMagic(Magics):
def __init__(self, shell):
super(LoggerMagic, self).__init__(shell)
@line_magic
def lmagic(self, line):
"my line magic"
print("Full access to the main IPython object:", self.shell)
print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
return line
@cell_magic
def logexc(self, line, cell):
"my cell magic for logging"
args = line.split()
if len(args) == 0:
logger_varname = 'logger'
else:
logger_varname = args[0]
logger = self.shell.user_ns.get(logger_varname)
if not isinstance(logger, logging.Logger):
sys.stderr("Cannot find Logger variable '%s'" % logger_varname)
return line, cell
try:
self.shell.ex(cell)
except Exception as e:
logger.info("caugh exception:\n%s" % tb.format_exc())
raise e
@line_cell_magic
def lcmagic(self, line, cell=None):
"Magic that works both as %lcmagic and as %%lcmagic"
if cell is None:
print("Called as line magic")
return line
else:
print("Called as cell magic")
return line, cell
def get_logger(path=None, samedir=None, subdir=None, fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s'):
formatter = logging.Formatter(fmt)
logger = logging.getLogger()
if path is not None:
if samedir is not None:
if isinstance(subdir, list):
subdir.append(path)
else:
subdir = list(os.path.split(subdir)) + [path]
path = get_samedir(samedir, *subdir)
else:
if isinstance(subdir, list):
subdir.append(path)
else:
subdir = os.path.split(subdir) + [path]
path = os.path.join(subdir)
file_handler = logging.FileHandler(path)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
return logger
def get_samedir(path, *filename):
parts = [os.path.dirname(path)] + list(filename)
ret = os.path.join(*parts)
outdir = os.path.dirname(ret)
if not os.path.exists(outdir):
os.makedirs(outdir)
return ret