forked from sagemath/sagecell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
172 lines (143 loc) · 5.25 KB
/
misc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Misc functions / classes
"""
from contextlib import contextmanager
from datetime import datetime
import os
import shutil
import stat
import sys
class Config(object):
"""
Config file wrapper / handler class
This is designed to make loading and working with an
importable configuration file with options relevant to
multiple classes more convenient.
Rather than re-importing a configuration module whenever
a specific is attribute is needed, a Config object can
be instantiated by some base application and the relevant
attributes can be passed to whatever classes / functions
are needed.
This class tracks both the default and user-specified
configuration files
"""
def __init__(self):
import config_default
self.config = None
self.config_default = config_default
try:
import config
self.config = config
except ImportError:
pass
def get(self, attr):
"""
Get a config attribute. If the attribute is defined
in the user-specified file, that is used, otherwise
the default config file attribute is used if
possible. If the attribute is a dictionary, the items
in config and default_config will be merged.
:arg attr str: the name of the attribute to get
:returns: the value of the named attribute, or
None if the attribute does not exist.
"""
result = self.get_default(attr)
if self.config is not None:
try:
val = getattr(self.config, attr)
if isinstance(val, dict):
result.update(val)
else:
result = val
except AttributeError:
pass
return result
def get_default(self, attr):
"""
Get a config attribute from the default config file.
:arg attr str: the name of the attribute toget
:returns: the value of the named attribute, or
None if the attribute does not exist.
"""
config_val = None
try:
config_val = getattr(self.config_default, attr)
except AttributeError:
pass
return config_val
def set(self, attr, value):
"""
Set a config attribute
:arg attr str: the name of the attribute to set
:arg value: an arbitrary value to set the named
attribute to
"""
setattr(self.config, attr, value)
def get_attrs(self):
"""
Get a list of all the config object's attributes
This isn't very useful right now, since it includes
__<attr>__ attributes and the like.
:returns: a list of all attributes belonging to
the imported config module.
:rtype: list
"""
return dir(self.config)
@contextmanager
def session_metadata(metadata):
# flush any messages waiting in buffers
sys.stdout.flush()
sys.stderr.flush()
session = sys.stdout.session
old_metadata = session.metadata
new_metadata = old_metadata.copy()
new_metadata.update(metadata)
session.metadata = new_metadata
yield
sys.stdout.flush()
sys.stderr.flush()
session.metadata = old_metadata
def display_file(path, mimetype=None):
path = os.path.relpath(path)
if path.startswith("../"):
shutil.copy(path, ".")
path = os.path.basename(path)
os.chmod(path, stat.S_IMODE(os.stat(path).st_mode) | stat.S_IRGRP)
if mimetype is None:
mimetype = 'application/x-file'
mt = os.path.getmtime(path)
display_message({
'text/plain': '%s file' % mimetype,
mimetype: path + '?m=%s' % mt})
sys._sage_.sent_files[path] = mt
def display_html(s):
display_message({'text/plain': 'html', 'text/html': s})
def display_message(data, metadata=None):
sys.stdout.session.send(sys.stdout.pub_thread,
'display_data',
content={'data': data, 'source': 'sagecell'},
parent=sys.stdout.parent_header,
metadata=metadata)
def stream_message(stream, data, metadata=None):
sys.stdout.session.send(sys.stdout.pub_thread,
'stream',
content={'name': stream, 'data': data},
parent=sys.stdout.parent_header,
metadata=metadata)
def reset_kernel_timeout(timeout):
sys.stdout.session.send(sys.stdout.pub_thread,
'kernel_timeout',
content={'timeout': float(timeout)},
parent=sys.stdout.parent_header)
def javascript(code):
sys._sage_.display_message({'application/javascript': code, 'text/plain': 'javascript code'})
def sage_json(obj):
import sage.all
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, sage.rings.integer.Integer):
return int(obj)
elif isinstance(obj, (sage.rings.real_mpfr.RealLiteral, sage.rings.real_mpfr.RealNumber, sage.rings.real_double.RealDoubleElement)):
return float(obj)
else:
raise TypeError("Object of type %s with value of %s is not JSON serializable" % (type(obj), repr(obj)))